ChibiOS 21.11.4
hal_can.c
Go to the documentation of this file.
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.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 canp->state = CAN_STOPPING;
155 can_lld_stop(canp);
156 canp->config = NULL;
157 canp->state = CAN_STOP;
158
159 /* Threads waiting on CAN APIs are notified that the driver has been
160 stopped in order to not have stuck threads.*/
165}
166
167/**
168 * @brief Can frame transmission attempt.
169 * @details The specified frame is queued for transmission, if the hardware
170 * queue is full then the function fails.
171 *
172 * @param[in] canp pointer to the @p CANDriver object
173 * @param[in] mailbox mailbox number, @p CAN_ANY_MAILBOX for any mailbox
174 * @param[in] ctfp pointer to the CAN frame to be transmitted
175 * @return The operation result.
176 * @retval false Frame transmitted.
177 * @retval true Mailbox full.
178 *
179 * @iclass
180 */
182 canmbx_t mailbox,
183 const CANTxFrame *ctfp) {
184
186 osalDbgCheck((canp != NULL) && (ctfp != NULL) &&
187 (mailbox <= (canmbx_t)CAN_TX_MAILBOXES));
188 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
189 "invalid state");
190
191 /* If the RX mailbox is full then the function fails.*/
192 if (!can_lld_is_tx_empty(canp, mailbox)) {
193 return true;
194 }
195
196 /* Transmitting frame.*/
197 can_lld_transmit(canp, mailbox, ctfp);
198
199 return false;
200}
201
202/**
203 * @brief Can frame receive attempt.
204 * @details The function tries to fetch a frame from a mailbox.
205 *
206 * @param[in] canp pointer to the @p CANDriver object
207 * @param[in] mailbox mailbox number, @p CAN_ANY_MAILBOX for any mailbox
208 * @param[out] crfp pointer to the buffer where the CAN frame is copied
209 * @return The operation result.
210 * @retval false Frame fetched.
211 * @retval true Mailbox empty.
212 *
213 * @iclass
214 */
216 canmbx_t mailbox,
217 CANRxFrame *crfp) {
218
220 osalDbgCheck((canp != NULL) && (crfp != NULL) &&
221 (mailbox <= (canmbx_t)CAN_RX_MAILBOXES));
222 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
223 "invalid state");
224
225 /* If the RX mailbox is empty then the function fails.*/
226 if (!can_lld_is_rx_nonempty(canp, mailbox)) {
227 return true;
228 }
229
230 /* Fetching the frame.*/
231 can_lld_receive(canp, mailbox, crfp);
232
233 return false;
234}
235
236/**
237 * @brief Tries to abort an ongoing transmission.
238 *
239 * @param[in] canp pointer to the @p CANDriver object
240 * @param[in] mailbox mailbox number
241 *
242 * @xclass
243 */
245 canmbx_t mailbox) {
246
247 osalDbgCheck((canp != NULL) &&
248 (mailbox != CAN_ANY_MAILBOX) &&
249 (mailbox <= (canmbx_t)CAN_TX_MAILBOXES));
250
251 can_lld_abort(canp, mailbox);
252}
253
254/**
255 * @brief Can frame transmission.
256 * @details The specified frame is queued for transmission, if the hardware
257 * queue is full then the invoking thread is queued.
258 * @note Trying to transmit while in sleep mode simply enqueues the thread.
259 *
260 * @param[in] canp pointer to the @p CANDriver object
261 * @param[in] mailbox mailbox number, @p CAN_ANY_MAILBOX for any mailbox
262 * @param[in] ctfp pointer to the CAN frame to be transmitted
263 * @param[in] timeout the number of ticks before the operation timeouts,
264 * the following special values are allowed:
265 * - @a TIME_IMMEDIATE immediate timeout.
266 * - @a TIME_INFINITE no timeout.
267 * @return The operation result.
268 * @retval MSG_OK the frame has been queued for transmission.
269 * @retval MSG_TIMEOUT The operation has timed out.
270 * @retval MSG_RESET The driver has been stopped while waiting.
271 *
272 * @api
273 */
275 canmbx_t mailbox,
276 const CANTxFrame *ctfp,
277 sysinterval_t timeout) {
278
279 osalDbgCheck((canp != NULL) && (ctfp != NULL) &&
280 (mailbox <= (canmbx_t)CAN_TX_MAILBOXES));
281
282 osalSysLock();
283 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
284 "invalid state");
285
286 /*lint -save -e9007 [13.5] Right side is supposed to be pure.*/
287 while ((canp->state == CAN_SLEEP) || !can_lld_is_tx_empty(canp, mailbox)) {
288 /*lint -restore*/
289 msg_t msg = osalThreadEnqueueTimeoutS(&canp->txqueue, timeout);
290 if (msg != MSG_OK) {
292 return msg;
293 }
294 }
295 can_lld_transmit(canp, mailbox, ctfp);
297 return MSG_OK;
298}
299
300/**
301 * @brief Can frame receive.
302 * @details The function waits until a frame is received.
303 * @note Trying to receive while in sleep mode simply enqueues the thread.
304 *
305 * @param[in] canp pointer to the @p CANDriver object
306 * @param[in] mailbox mailbox number, @p CAN_ANY_MAILBOX for any mailbox
307 * @param[out] crfp pointer to the buffer where the CAN frame is copied
308 * @param[in] timeout the number of ticks before the operation timeouts,
309 * the following special values are allowed:
310 * - @a TIME_IMMEDIATE immediate timeout (useful in an
311 * event driven scenario where a thread never blocks
312 * for I/O).
313 * - @a TIME_INFINITE no timeout.
314 * @return The operation result.
315 * @retval MSG_OK a frame has been received and placed in the buffer.
316 * @retval MSG_TIMEOUT The operation has timed out.
317 * @retval MSG_RESET The driver has been stopped while waiting.
318 *
319 * @api
320 */
322 canmbx_t mailbox,
323 CANRxFrame *crfp,
324 sysinterval_t timeout) {
325
326 osalDbgCheck((canp != NULL) && (crfp != NULL) &&
327 (mailbox <= (canmbx_t)CAN_RX_MAILBOXES));
328
329 osalSysLock();
330 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
331 "invalid state");
332
333 /*lint -save -e9007 [13.5] Right side is supposed to be pure.*/
334 while ((canp->state == CAN_SLEEP) || !can_lld_is_rx_nonempty(canp, mailbox)) {
335 /*lint -restore*/
336 msg_t msg = osalThreadEnqueueTimeoutS(&canp->rxqueue, timeout);
337 if (msg != MSG_OK) {
339 return msg;
340 }
341 }
342 can_lld_receive(canp, mailbox, crfp);
344 return MSG_OK;
345}
346
347#if (CAN_USE_SLEEP_MODE == TRUE) || defined(__DOXYGEN__)
348/**
349 * @brief Enters the sleep mode.
350 * @details This function puts the CAN driver in sleep mode and broadcasts
351 * the @p sleep_event event source.
352 * @pre In order to use this function the option @p CAN_USE_SLEEP_MODE must
353 * be enabled and the @p CAN_SUPPORTS_SLEEP mode must be supported
354 * by the low level driver.
355 *
356 * @param[in] canp pointer to the @p CANDriver object
357 *
358 * @api
359 */
360void canSleep(CANDriver *canp) {
361
362 osalDbgCheck(canp != NULL);
363
364 osalSysLock();
365 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
366 "invalid state");
367 if (canp->state == CAN_READY) {
368 can_lld_sleep(canp);
369 canp->state = CAN_SLEEP;
370#if CAN_ENFORCE_USE_CALLBACKS == FALSE
373#endif
374 }
376}
377
378/**
379 * @brief Enforces leaving the sleep mode.
380 * @note The sleep mode is supposed to be usually exited automatically by
381 * an hardware event.
382 *
383 * @param[in] canp pointer to the @p CANDriver object
384 */
385void canWakeup(CANDriver *canp) {
386
387 osalDbgCheck(canp != NULL);
388
389 osalSysLock();
390 osalDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP),
391 "invalid state");
392 if (canp->state == CAN_SLEEP) {
393 can_lld_wakeup(canp);
394 canp->state = CAN_READY;
395#if CAN_ENFORCE_USE_CALLBACKS == FALSE
398#endif
399 }
401}
402#endif /* CAN_USE_SLEEP_MODE == TRUE */
403
404#endif /* HAL_USE_CAN == TRUE */
405
406/** @} */
void canWakeup(CANDriver *canp)
Enforces leaving the sleep mode.
Definition hal_can.c:385
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:181
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:274
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:360
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:244
#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:321
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:215
@ CAN_STOPPING
Definition hal_can.h:106
@ CAN_STOP
Definition hal_can.h:104
@ CAN_SLEEP
Definition hal_can.h:108
@ CAN_STARTING
Definition hal_can.h:105
@ CAN_READY
Definition hal_can.h:107
#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:91
int32_t msg_t
Definition chearly.h:88
#define MSG_OK
Normal wakeup message.
Definition chschd.h:39
#define MSG_RESET
Wakeup caused by a reset condition.
Definition chschd.h:42
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:119
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.