diff options
Diffstat (limited to 'lib/chibios/os/hal/templates/hal_can_lld.h')
-rw-r--r-- | lib/chibios/os/hal/templates/hal_can_lld.h | 275 |
1 files changed, 275 insertions, 0 deletions
diff --git a/lib/chibios/os/hal/templates/hal_can_lld.h b/lib/chibios/os/hal/templates/hal_can_lld.h new file mode 100644 index 000000000..624c7bf24 --- /dev/null +++ b/lib/chibios/os/hal/templates/hal_can_lld.h | |||
@@ -0,0 +1,275 @@ | |||
1 | /* | ||
2 | ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio | ||
3 | |||
4 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | you may not use this file except in compliance with the License. | ||
6 | You may obtain a copy of the License at | ||
7 | |||
8 | http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | |||
10 | Unless required by applicable law or agreed to in writing, software | ||
11 | distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | See the License for the specific language governing permissions and | ||
14 | limitations under the License. | ||
15 | */ | ||
16 | |||
17 | /** | ||
18 | * @file hal_can_lld.h | ||
19 | * @brief PLATFORM CAN subsystem low level driver header. | ||
20 | * | ||
21 | * @addtogroup CAN | ||
22 | * @{ | ||
23 | */ | ||
24 | |||
25 | #ifndef HAL_CAN_LLD_H | ||
26 | #define HAL_CAN_LLD_H | ||
27 | |||
28 | #if (HAL_USE_CAN == TRUE) || defined(__DOXYGEN__) | ||
29 | |||
30 | /*===========================================================================*/ | ||
31 | /* Driver constants. */ | ||
32 | /*===========================================================================*/ | ||
33 | |||
34 | /** | ||
35 | * @brief Number of transmit mailboxes. | ||
36 | */ | ||
37 | #define CAN_TX_MAILBOXES 1 | ||
38 | |||
39 | /** | ||
40 | * @brief Number of receive mailboxes. | ||
41 | */ | ||
42 | #define CAN_RX_MAILBOXES 1 | ||
43 | |||
44 | /*===========================================================================*/ | ||
45 | /* Driver pre-compile time settings. */ | ||
46 | /*===========================================================================*/ | ||
47 | |||
48 | /** | ||
49 | * @name PLATFORM configuration options | ||
50 | * @{ | ||
51 | */ | ||
52 | /** | ||
53 | * @brief CAN1 driver enable switch. | ||
54 | * @details If set to @p TRUE the support for CAN1 is included. | ||
55 | * @note The default is @p FALSE. | ||
56 | */ | ||
57 | #if !defined(PLATFORM_CAN_USE_CAN1) || defined(__DOXYGEN__) | ||
58 | #define PLATFORM_CAN_USE_CAN1 FALSE | ||
59 | #endif | ||
60 | /** @} */ | ||
61 | |||
62 | /*===========================================================================*/ | ||
63 | /* Derived constants and error checks. */ | ||
64 | /*===========================================================================*/ | ||
65 | |||
66 | /*===========================================================================*/ | ||
67 | /* Driver data structures and types. */ | ||
68 | /*===========================================================================*/ | ||
69 | |||
70 | /** | ||
71 | * @brief Type of a structure representing an CAN driver. | ||
72 | */ | ||
73 | typedef struct CANDriver CANDriver; | ||
74 | |||
75 | /** | ||
76 | * @brief Type of a transmission mailbox index. | ||
77 | */ | ||
78 | typedef uint32_t canmbx_t; | ||
79 | |||
80 | #if defined(CAN_ENFORCE_USE_CALLBACKS) || defined(__DOXYGEN__) | ||
81 | /** | ||
82 | * @brief Type of a CAN notification callback. | ||
83 | * | ||
84 | * @param[in] canp pointer to the @p CANDriver object triggering the | ||
85 | * callback | ||
86 | * @param[in] flags flags associated to the mailbox callback | ||
87 | */ | ||
88 | typedef void (*can_callback_t)(CANDriver *canp, uint32_t flags); | ||
89 | #endif | ||
90 | |||
91 | /** | ||
92 | * @brief CAN transmission frame. | ||
93 | * @note Accessing the frame data as word16 or word32 is not portable because | ||
94 | * machine data endianness, it can be still useful for a quick filling. | ||
95 | */ | ||
96 | typedef struct { | ||
97 | /*lint -save -e46 [6.1] Standard types are fine too.*/ | ||
98 | uint8_t DLC:4; /**< @brief Data length. */ | ||
99 | uint8_t RTR:1; /**< @brief Frame type. */ | ||
100 | uint8_t IDE:1; /**< @brief Identifier type. */ | ||
101 | union { | ||
102 | uint32_t SID:11; /**< @brief Standard identifier.*/ | ||
103 | uint32_t EID:29; /**< @brief Extended identifier.*/ | ||
104 | uint32_t _align1; | ||
105 | }; | ||
106 | /*lint -restore*/ | ||
107 | union { | ||
108 | uint8_t data8[8]; /**< @brief Frame data. */ | ||
109 | uint16_t data16[4]; /**< @brief Frame data. */ | ||
110 | uint32_t data32[2]; /**< @brief Frame data. */ | ||
111 | }; | ||
112 | } CANTxFrame; | ||
113 | |||
114 | /** | ||
115 | * @brief CAN received frame. | ||
116 | * @note Accessing the frame data as word16 or word32 is not portable because | ||
117 | * machine data endianness, it can be still useful for a quick filling. | ||
118 | */ | ||
119 | typedef struct { | ||
120 | /*lint -save -e46 [6.1] Standard types are fine too.*/ | ||
121 | uint8_t FMI; /**< @brief Filter id. */ | ||
122 | uint16_t TIME; /**< @brief Time stamp. */ | ||
123 | uint8_t DLC:4; /**< @brief Data length. */ | ||
124 | uint8_t RTR:1; /**< @brief Frame type. */ | ||
125 | uint8_t IDE:1; /**< @brief Identifier type. */ | ||
126 | union { | ||
127 | uint32_t SID:11; /**< @brief Standard identifier.*/ | ||
128 | uint32_t EID:29; /**< @brief Extended identifier.*/ | ||
129 | uint32_t _align1; | ||
130 | }; | ||
131 | /*lint -restore*/ | ||
132 | union { | ||
133 | uint8_t data8[8]; /**< @brief Frame data. */ | ||
134 | uint16_t data16[4]; /**< @brief Frame data. */ | ||
135 | uint32_t data32[2]; /**< @brief Frame data. */ | ||
136 | }; | ||
137 | } CANRxFrame; | ||
138 | |||
139 | /** | ||
140 | * @brief Driver configuration structure. | ||
141 | */ | ||
142 | typedef struct { | ||
143 | /* End of the mandatory fields.*/ | ||
144 | uint32_t dummy; | ||
145 | } CANConfig; | ||
146 | |||
147 | /** | ||
148 | * @brief Structure representing an CAN driver. | ||
149 | */ | ||
150 | struct CANDriver { | ||
151 | /** | ||
152 | * @brief Driver state. | ||
153 | */ | ||
154 | canstate_t state; | ||
155 | /** | ||
156 | * @brief Current configuration data. | ||
157 | */ | ||
158 | const CANConfig *config; | ||
159 | /** | ||
160 | * @brief Transmission threads queue. | ||
161 | */ | ||
162 | threads_queue_t txqueue; | ||
163 | /** | ||
164 | * @brief Receive threads queue. | ||
165 | */ | ||
166 | threads_queue_t rxqueue; | ||
167 | #if (CAN_ENFORCE_USE_CALLBACKS == FALSE) || defined (__DOXYGEN__) | ||
168 | /** | ||
169 | * @brief One or more frames become available. | ||
170 | * @note After broadcasting this event it will not be broadcasted again | ||
171 | * until the received frames queue has been completely emptied. It | ||
172 | * is <b>not</b> broadcasted for each received frame. It is | ||
173 | * responsibility of the application to empty the queue by | ||
174 | * repeatedly invoking @p chReceive() when listening to this event. | ||
175 | * This behavior minimizes the interrupt served by the system | ||
176 | * because CAN traffic. | ||
177 | * @note The flags associated to the listeners will indicate which | ||
178 | * receive mailboxes become non-empty. | ||
179 | */ | ||
180 | event_source_t rxfull_event; | ||
181 | /** | ||
182 | * @brief One or more transmission mailbox become available. | ||
183 | * @note The flags associated to the listeners will indicate which | ||
184 | * transmit mailboxes become empty. | ||
185 | */ | ||
186 | event_source_t txempty_event; | ||
187 | /** | ||
188 | * @brief A CAN bus error happened. | ||
189 | * @note The flags associated to the listeners will indicate the | ||
190 | * error(s) that have occurred. | ||
191 | */ | ||
192 | event_source_t error_event; | ||
193 | #if (CAN_USE_SLEEP_MODE == TRUE) || defined (__DOXYGEN__) | ||
194 | /** | ||
195 | * @brief Entering sleep state event. | ||
196 | */ | ||
197 | event_source_t sleep_event; | ||
198 | /** | ||
199 | * @brief Exiting sleep state event. | ||
200 | */ | ||
201 | event_source_t wakeup_event; | ||
202 | #endif | ||
203 | #else /* CAN_ENFORCE_USE_CALLBACKS == TRUE */ | ||
204 | /** | ||
205 | * @brief One or more frames become available. | ||
206 | * @note After calling this function it will not be called again | ||
207 | * until the received frames queue has been completely emptied. It | ||
208 | * is <b>not</b> called for each received frame. It is | ||
209 | * responsibility of the application to empty the queue by | ||
210 | * repeatedly invoking @p chTryReceiveI(). | ||
211 | * This behavior minimizes the interrupt served by the system | ||
212 | * because CAN traffic. | ||
213 | */ | ||
214 | can_callback_t rxfull_cb; | ||
215 | /** | ||
216 | * @brief One or more transmission mailbox become available. | ||
217 | * @note The flags associated to the callback will indicate which | ||
218 | * transmit mailboxes become empty. | ||
219 | */ | ||
220 | can_callback_t txempty_cb; | ||
221 | /** | ||
222 | * @brief A CAN bus error happened. | ||
223 | */ | ||
224 | can_callback_t error_cb; | ||
225 | #if (CAN_USE_SLEEP_MODE == TRUE) || defined (__DOXYGEN__) | ||
226 | /** | ||
227 | * @brief Exiting sleep state. | ||
228 | */ | ||
229 | can_callback_t wakeup_cb; | ||
230 | #endif | ||
231 | #endif | ||
232 | /* End of the mandatory fields.*/ | ||
233 | }; | ||
234 | |||
235 | /*===========================================================================*/ | ||
236 | /* Driver macros. */ | ||
237 | /*===========================================================================*/ | ||
238 | |||
239 | /*===========================================================================*/ | ||
240 | /* External declarations. */ | ||
241 | /*===========================================================================*/ | ||
242 | |||
243 | #if (PLATFORM_CAN_USE_CAN1 == TRUE) && !defined(__DOXYGEN__) | ||
244 | extern CANDriver CAND1; | ||
245 | #endif | ||
246 | |||
247 | #ifdef __cplusplus | ||
248 | extern "C" { | ||
249 | #endif | ||
250 | void can_lld_init(void); | ||
251 | void can_lld_start(CANDriver *canp); | ||
252 | void can_lld_stop(CANDriver *canp); | ||
253 | bool can_lld_is_tx_empty(CANDriver *canp, canmbx_t mailbox); | ||
254 | void can_lld_transmit(CANDriver *canp, | ||
255 | canmbx_t mailbox, | ||
256 | const CANTxFrame *ctfp); | ||
257 | bool can_lld_is_rx_nonempty(CANDriver *canp, canmbx_t mailbox); | ||
258 | void can_lld_receive(CANDriver *canp, | ||
259 | canmbx_t mailbox, | ||
260 | CANRxFrame *crfp); | ||
261 | void can_lld_abort(CANDriver *canp, | ||
262 | canmbx_t mailbox); | ||
263 | #if CAN_USE_SLEEP_MODE == TRUE | ||
264 | void can_lld_sleep(CANDriver *canp); | ||
265 | void can_lld_wakeup(CANDriver *canp); | ||
266 | #endif | ||
267 | #ifdef __cplusplus | ||
268 | } | ||
269 | #endif | ||
270 | |||
271 | #endif /* HAL_USE_CAN == TRUE */ | ||
272 | |||
273 | #endif /* HAL_CAN_LLD_H */ | ||
274 | |||
275 | /** @} */ | ||