ChibiOS 21.11.5
hal_can.c
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 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.c
19 * @brief CAN Driver code.
20 *
21 * @addtogroup CAN
22 * @{
23 */
24
25#include "hal.h"
26
27#if (HAL_USE_CAN == TRUE) || defined(__DOXYGEN__)
28
29/*===========================================================================*/
30/* Driver local definitions. */
31/*===========================================================================*/
32
33/*===========================================================================*/
34/* Driver exported variables. */
35/*===========================================================================*/
36
37/*===========================================================================*/
38/* Driver local variables and types. */
39/*===========================================================================*/
40
41/*===========================================================================*/
42/* Driver local functions. */
43/*===========================================================================*/
44
45/*===========================================================================*/
46/* Driver exported functions. */
47/*===========================================================================*/
48
49/**
50 * @brief CAN Driver initialization.
51 * @note This function is implicitly invoked by @p halInit(), there is
52 * no need to explicitly initialize the driver.
53 *
54 * @init
55 */
56void canInit(void) {
57
59}
60
61/**
62 * @brief Initializes the standard part of a @p CANDriver structure.
63 *
64 * @param[out] canp pointer to the @p CANDriver object
65 *
66 * @init
67 */
69
70 canp->state = CAN_STOP;
71 canp->config = NULL;
74#if CAN_ENFORCE_USE_CALLBACKS == FALSE
78#if CAN_USE_SLEEP_MODE == TRUE
81#endif
82#else /* CAN_ENFORCE_USE_CALLBACKS == TRUE */
83 canp->rxfull_cb = NULL;
84 canp->txempty_cb = NULL;
85 canp->error_cb = NULL;
86#if CAN_USE_SLEEP_MODE == TRUE
87 canp->wakeup_cb = NULL;
88#endif
89#endif /* CAN_ENFORCE_USE_CALLBACKS == TRUE */
90}
91
92/**
93 * @brief Configures and activates the CAN peripheral.
94 * @note Activating the CAN bus can be a slow operation.
95 * @note Unlike other drivers it is not possible to restart the CAN
96 * driver without first stopping it using canStop().
97 *
98 * @param[in] canp pointer to the @p CANDriver object
99 * @param[in] config pointer to the @p CANConfig object. Depending on
100 * the implementation the value can be @p NULL.
101 * @return The operation status.
102 *
103 * @api
104 */
105msg_t canStart(CANDriver *canp, const CANConfig *config) {
106 msg_t msg;
107
108 osalDbgCheck(canp != NULL);
109
110 osalSysLock();
111 osalDbgAssert(canp->state == CAN_STOP, "invalid state");
112
113 /* Entering initialization mode. */
114 canp->state = CAN_STARTING;
115 canp->config = config;
116
117 /* Low level initialization, could be a slow process and sleeps could
118 be performed inside.*/
119#if defined(CAN_LLD_ENHANCED_API)
120 msg = can_lld_start(canp);
121 if (msg == HAL_RET_SUCCESS) {
122 canp->state = CAN_READY;
123 }
124 else {
125 canp->state = CAN_STOP;
126 }
127#else
128 can_lld_start(canp);
129 canp->state = CAN_READY;
130 msg = HAL_RET_SUCCESS;
131#endif
132
134
135 return msg;
136}
137
138/**
139 * @brief Deactivates the CAN peripheral.
140 *
141 * @param[in] canp pointer to the @p CANDriver object
142 *
143 * @api
144 */
145void canStop(CANDriver *canp) {
146
147 osalDbgCheck(canp != NULL);
148
149 osalSysLock();
150 osalDbgAssert((canp->state == CAN_STOP) || (canp->state == CAN_READY),
151 "invalid state");
152
153 /* The low level driver is stopped.*/
154 can_lld_stop(canp);
155 canp->config = NULL;
156 canp->state = CAN_STOP;
157
158 /* Threads waiting on CAN APIs are notified that the driver has been
159 stopped in order to not have stuck threads.*/
164}
165
166/**
167 * @brief Can frame transmission attempt.
168 * @details The specified frame is queued for transmission, if the hardware
169 * queue is full then the function fails.
170 *
171 * @param[in] canp pointer to the @p CANDriver object
172 * @param[in] mailbox mailbox number, @p CAN_ANY_MAILBOX for any mailbox
173 * @param[in] ctfp pointer to the CAN frame to be transmitted
174 * @return The operation result.
175 * @retval false Frame transmitted.
176 * @retval true Mailbox full.
177 *
178 * @iclass
179 */
181 canmbx_t mailbox,
182 const CANTxFrame *ctfp) {
183
185 osalDbgCheck((canp != NULL) && (ctfp != NULL) &&
186 (mailbox <= (canmbx_t)CAN_TX_MAILBOXES));
187 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
188 "invalid state");
189
190 /* If the RX mailbox is full then the function fails.*/
191 if (!can_lld_is_tx_empty(canp, mailbox)) {
192 return true;
193 }
194
195 /* Transmitting frame.*/
196 can_lld_transmit(canp, mailbox, ctfp);
197
198 return false;
199}
200
201/**
202 * @brief Can frame receive attempt.
203 * @details The function tries to fetch a frame from a mailbox.
204 *
205 * @param[in] canp pointer to the @p CANDriver object
206 * @param[in] mailbox mailbox number, @p CAN_ANY_MAILBOX for any mailbox
207 * @param[out] crfp pointer to the buffer where the CAN frame is copied
208 * @return The operation result.
209 * @retval false Frame fetched.
210 * @retval true Mailbox empty.
211 *
212 * @iclass
213 */
215 canmbx_t mailbox,
216 CANRxFrame *crfp) {
217
219 osalDbgCheck((canp != NULL) && (crfp != NULL) &&
220 (mailbox <= (canmbx_t)CAN_RX_MAILBOXES));
221 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
222 "invalid state");
223
224 /* If the RX mailbox is empty then the function fails.*/
225 if (!can_lld_is_rx_nonempty(canp, mailbox)) {
226 return true;
227 }
228
229 /* Fetching the frame.*/
230 can_lld_receive(canp, mailbox, crfp);
231
232 return false;
233}
234
235/**
236 * @brief Tries to abort an ongoing transmission.
237 *
238 * @param[in] canp pointer to the @p CANDriver object
239 * @param[in] mailbox mailbox number
240 *
241 * @xclass
242 */
244 canmbx_t mailbox) {
245
246 osalDbgCheck((canp != NULL) &&
247 (mailbox != CAN_ANY_MAILBOX) &&
248 (mailbox <= (canmbx_t)CAN_TX_MAILBOXES));
249
250 can_lld_abort(canp, mailbox);
251}
252
253/**
254 * @brief Can frame transmission.
255 * @details The specified frame is queued for transmission, if the hardware
256 * queue is full then the invoking thread is queued.
257 * @note Trying to transmit while in sleep mode simply enqueues the thread.
258 *
259 * @param[in] canp pointer to the @p CANDriver object
260 * @param[in] mailbox mailbox number, @p CAN_ANY_MAILBOX for any mailbox
261 * @param[in] ctfp pointer to the CAN frame to be transmitted
262 * @param[in] timeout the number of ticks before the operation timeouts,
263 * the following special values are allowed:
264 * - @a TIME_IMMEDIATE immediate timeout.
265 * - @a TIME_INFINITE no timeout.
266 * @return The operation result.
267 * @retval MSG_OK the frame has been queued for transmission.
268 * @retval MSG_TIMEOUT The operation has timed out.
269 * @retval MSG_RESET The driver has been stopped while waiting.
270 *
271 * @api
272 */
274 canmbx_t mailbox,
275 const CANTxFrame *ctfp,
276 sysinterval_t timeout) {
277
278 osalDbgCheck((canp != NULL) && (ctfp != NULL) &&
279 (mailbox <= (canmbx_t)CAN_TX_MAILBOXES));
280
281 osalSysLock();
282 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
283 "invalid state");
284
285 /*lint -save -e9007 [13.5] Right side is supposed to be pure.*/
286 while ((canp->state == CAN_SLEEP) || !can_lld_is_tx_empty(canp, mailbox)) {
287 /*lint -restore*/
288 msg_t msg = osalThreadEnqueueTimeoutS(&canp->txqueue, timeout);
289 if (msg != MSG_OK) {
291 return msg;
292 }
293 }
294 can_lld_transmit(canp, mailbox, ctfp);
296 return MSG_OK;
297}
298
299/**
300 * @brief Can frame receive.
301 * @details The function waits until a frame is received.
302 * @note Trying to receive while in sleep mode simply enqueues the thread.
303 *
304 * @param[in] canp pointer to the @p CANDriver object
305 * @param[in] mailbox mailbox number, @p CAN_ANY_MAILBOX for any mailbox
306 * @param[out] crfp pointer to the buffer where the CAN frame is copied
307 * @param[in] timeout the number of ticks before the operation timeouts,
308 * the following special values are allowed:
309 * - @a TIME_IMMEDIATE immediate timeout (useful in an
310 * event driven scenario where a thread never blocks
311 * for I/O).
312 * - @a TIME_INFINITE no timeout.
313 * @return The operation result.
314 * @retval MSG_OK a frame has been received and placed in the buffer.
315 * @retval MSG_TIMEOUT The operation has timed out.
316 * @retval MSG_RESET The driver has been stopped while waiting.
317 *
318 * @api
319 */
321 canmbx_t mailbox,
322 CANRxFrame *crfp,
323 sysinterval_t timeout) {
324
325 osalDbgCheck((canp != NULL) && (crfp != NULL) &&
326 (mailbox <= (canmbx_t)CAN_RX_MAILBOXES));
327
328 osalSysLock();
329 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
330 "invalid state");
331
332 /*lint -save -e9007 [13.5] Right side is supposed to be pure.*/
333 while ((canp->state == CAN_SLEEP) || !can_lld_is_rx_nonempty(canp, mailbox)) {
334 /*lint -restore*/
335 msg_t msg = osalThreadEnqueueTimeoutS(&canp->rxqueue, timeout);
336 if (msg != MSG_OK) {
338 return msg;
339 }
340 }
341 can_lld_receive(canp, mailbox, crfp);
343 return MSG_OK;
344}
345
346#if (CAN_USE_SLEEP_MODE == TRUE) || defined(__DOXYGEN__)
347/**
348 * @brief Enters the sleep mode.
349 * @details This function puts the CAN driver in sleep mode and broadcasts
350 * the @p sleep_event event source.
351 * @pre In order to use this function the option @p CAN_USE_SLEEP_MODE must
352 * be enabled and the @p CAN_SUPPORTS_SLEEP mode must be supported
353 * by the low level driver.
354 *
355 * @param[in] canp pointer to the @p CANDriver object
356 *
357 * @api
358 */
359void canSleep(CANDriver *canp) {
360
361 osalDbgCheck(canp != NULL);
362
363 osalSysLock();
364 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
365 "invalid state");
366 if (canp->state == CAN_READY) {
367 can_lld_sleep(canp);
368 canp->state = CAN_SLEEP;
369#if CAN_ENFORCE_USE_CALLBACKS == FALSE
372#endif
373 }
375}
376
377/**
378 * @brief Enforces leaving the sleep mode.
379 * @note The sleep mode is supposed to be usually exited automatically by
380 * an hardware event.
381 *
382 * @param[in] canp pointer to the @p CANDriver object
383 */
384void canWakeup(CANDriver *canp) {
385
386 osalDbgCheck(canp != NULL);
387
388 osalSysLock();
389 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
390 "invalid state");
391 if (canp->state == CAN_SLEEP) {
392 can_lld_wakeup(canp);
393 canp->state = CAN_READY;
394#if CAN_ENFORCE_USE_CALLBACKS == FALSE
397#endif
398 }
400}
401#endif /* CAN_USE_SLEEP_MODE == TRUE */
402
403#endif /* HAL_USE_CAN == TRUE */
404
405/** @} */
void canWakeup(CANDriver *canp)
Enforces leaving the sleep mode.
Definition hal_can.c:384
void can_lld_receive(CANDriver *canp, canmbx_t mailbox, CANRxFrame *crfp)
Receives a frame from the input queue.
uint32_t canmbx_t
Type of a transmission mailbox index.
Definition hal_can_lld.h:78
#define CAN_ANY_MAILBOX
Special mailbox identifier.
Definition hal_can.h:63
void canInit(void)
CAN Driver initialization.
Definition hal_can.c:56
void can_lld_transmit(CANDriver *canp, canmbx_t mailbox, const CANTxFrame *ctfp)
Inserts a frame into the transmit queue.
bool canTryTransmitI(CANDriver *canp, canmbx_t mailbox, const CANTxFrame *ctfp)
Can frame transmission attempt.
Definition hal_can.c:180
void canStop(CANDriver *canp)
Deactivates the CAN peripheral.
Definition hal_can.c:145
msg_t canTransmitTimeout(CANDriver *canp, canmbx_t mailbox, const CANTxFrame *ctfp, sysinterval_t timeout)
Can frame transmission.
Definition hal_can.c:273
void can_lld_abort(CANDriver *canp, canmbx_t mailbox)
Tries to abort an ongoing transmission.
void can_lld_wakeup(CANDriver *canp)
Enforces leaving the sleep mode.
void can_lld_sleep(CANDriver *canp)
Enters the sleep mode.
struct hal_can_config CANConfig
Type of a CAN configuration structure.
void canSleep(CANDriver *canp)
Enters the sleep mode.
Definition hal_can.c:359
void can_lld_init(void)
Low level CAN driver initialization.
Definition hal_can_lld.c:65
void canTryAbortX(CANDriver *canp, canmbx_t mailbox)
Tries to abort an ongoing transmission.
Definition hal_can.c:243
#define CAN_RX_MAILBOXES
Number of receive mailboxes.
Definition hal_can_lld.h:42
#define CAN_TX_MAILBOXES
Number of transmit mailboxes.
Definition hal_can_lld.h:37
struct hal_can_driver CANDriver
Type of a structure representing an CAN driver.
Definition hal_can_lld.h:73
msg_t canReceiveTimeout(CANDriver *canp, canmbx_t mailbox, CANRxFrame *crfp, sysinterval_t timeout)
Can frame receive.
Definition hal_can.c:320
msg_t canStart(CANDriver *canp, const CANConfig *config)
Configures and activates the CAN peripheral.
Definition hal_can.c:105
void can_lld_stop(CANDriver *canp)
Deactivates the CAN peripheral.
void canObjectInit(CANDriver *canp)
Initializes the standard part of a CANDriver structure.
Definition hal_can.c:68
bool can_lld_is_rx_nonempty(CANDriver *canp, canmbx_t mailbox)
Determines whether a frame has been received.
bool can_lld_is_tx_empty(CANDriver *canp, canmbx_t mailbox)
Determines whether a frame can be transmitted.
void can_lld_start(CANDriver *canp)
Configures and activates the CAN peripheral.
Definition hal_can_lld.c:80
bool canTryReceiveI(CANDriver *canp, canmbx_t mailbox, CANRxFrame *crfp)
Can frame receive attempt.
Definition hal_can.c:214
@ CAN_STOP
Definition hal_can.h:104
@ CAN_SLEEP
Definition hal_can.h:107
@ CAN_STARTING
Definition hal_can.h:105
@ CAN_READY
Definition hal_can.h:106
#define HAL_RET_SUCCESS
Definition hal.h:93
msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp, sysinterval_t timeout)
Enqueues the caller thread.
Definition osal.c:273
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition osal.h:601
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition osal.h:611
void osalThreadDequeueAllI(threads_queue_t *tqp, msg_t msg)
Dequeues and wakes up all threads from the queue.
Definition osal.c:305
static void osalThreadQueueObjectInit(threads_queue_t *tqp)
Initializes a threads queue object.
Definition osal.h:725
void osalOsRescheduleS(void)
Checks if a reschedule is required and performs it.
Definition osal.c:119
static void osalEventObjectInit(event_source_t *esp)
Initializes an event source object.
Definition osal.h:737
#define osalDbgAssert(c, remark)
Condition assertion.
Definition osal.h:264
#define osalDbgCheck(c)
Function parameters check.
Definition osal.h:284
void osalEventBroadcastFlagsI(event_source_t *esp, eventflags_t flags)
Add flags to an event source object.
Definition osal.c:319
#define osalDbgCheckClassI()
I-Class state check.
Definition osal.h:298
uint32_t eventflags_t
Definition chearly.h:90
int32_t msg_t
Definition chearly.h:87
#define MSG_OK
Normal wakeup message.
Definition chschd.h:38
#define MSG_RESET
Wakeup caused by a reset condition.
Definition chschd.h:41
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:118
HAL subsystem header.
CAN received frame.
CAN transmission frame.
Definition hal_can_lld.h:96
const CANConfig * config
Current configuration data.
canstate_t state
Driver state.
event_source_t sleep_event
Entering sleep state event.
event_source_t wakeup_event
Exiting sleep state event.
threads_queue_t txqueue
Transmission threads queue.
event_source_t error_event
A CAN bus error happened.
event_source_t rxfull_event
One or more frames become available.
event_source_t txempty_event
One or more transmission mailbox become available.
threads_queue_t rxqueue
Receive threads queue.