ChibiOS/HAL 9.0.0
|
Generic CAN Driver. More...
Generic CAN Driver.
This module implements a generic CAN (Controller Area Network) driver allowing the exchange of information at frame level.
HAL_USE_CAN
option must be enabled in halconf.h
.The driver implements a state machine internally, not all the driver functionalities can be used in any moment, any transition not explicitly shown in the following diagram has to be considered an error and shall be captured by an assertion (if enabled).
CAN status flags | |
#define | CAN_LIMIT_WARNING 1U |
Errors rate warning. | |
#define | CAN_LIMIT_ERROR 2U |
Errors rate error. | |
#define | CAN_BUS_OFF_ERROR 4U |
Bus off condition reached. | |
#define | CAN_FRAMING_ERROR 8U |
Framing error of some kind on the CAN bus. | |
#define | CAN_OVERFLOW_ERROR 16U |
Overflow in receive queue. |
CAN configuration options | |
#define | CAN_USE_SLEEP_MODE TRUE |
Sleep mode related APIs inclusion switch. | |
#define | CAN_ENFORCE_USE_CALLBACKS FALSE |
Enforces the driver to use direct callbacks rather than OSAL events. |
Macro Functions | |
#define | CAN_MAILBOX_TO_MASK(mbx) |
Converts a mailbox index to a bit mask. | |
#define | canTransmit(canp, mailbox, ctfp, timeout) |
Legacy name for canTransmitTimeout() . | |
#define | canReceive(canp, mailbox, crfp, timeout) |
Legacy name for canReceiveTimeout() . |
Low level driver helper macros | |
#define | _can_tx_empty_isr(canp, flags) |
TX mailbox empty event. | |
#define | _can_rx_full_isr(canp, flags) |
RX mailbox empty full event. | |
#define | _can_wakeup_isr(canp) |
Wakeup event. | |
#define | _can_error_isr(canp, flags) |
Error event. |
PLATFORM configuration options | |
#define | PLATFORM_CAN_USE_CAN1 FALSE |
CAN1 driver enable switch. |
Data Structures | |
struct | CANTxFrame |
CAN transmission frame. More... | |
struct | CANRxFrame |
CAN received frame. More... | |
struct | hal_can_config |
Type of a CAN configuration structure. More... | |
struct | hal_can_driver |
Structure representing an CAN driver. More... |
Macros | |
#define | CAN_ANY_MAILBOX 0U |
Special mailbox identifier. | |
#define | CAN_TX_MAILBOXES 1 |
Number of transmit mailboxes. | |
#define | CAN_RX_MAILBOXES 1 |
Number of receive mailboxes. |
Typedefs | |
typedef struct hal_can_driver | CANDriver |
Type of a structure representing an CAN driver. | |
typedef uint32_t | canmbx_t |
Type of a transmission mailbox index. | |
typedef void(* | can_callback_t) (CANDriver *canp, uint32_t flags) |
Type of a CAN notification callback. | |
typedef struct hal_can_config | CANConfig |
Type of a CAN configuration structure. |
Enumerations | |
enum | canstate_t { CAN_UNINIT = 0 , CAN_STOP = 1 , CAN_STARTING = 2 , CAN_STOPPING = 3 , CAN_READY = 4 , CAN_SLEEP = 5 } |
Driver state machine possible states. More... |
Functions | |
void | canInit (void) |
CAN Driver initialization. | |
void | canObjectInit (CANDriver *canp) |
Initializes the standard part of a CANDriver structure. | |
msg_t | canStart (CANDriver *canp, const CANConfig *config) |
Configures and activates the CAN peripheral. | |
void | canStop (CANDriver *canp) |
Deactivates the CAN peripheral. | |
bool | canTryTransmitI (CANDriver *canp, canmbx_t mailbox, const CANTxFrame *ctfp) |
Can frame transmission attempt. | |
bool | canTryReceiveI (CANDriver *canp, canmbx_t mailbox, CANRxFrame *crfp) |
Can frame receive attempt. | |
void | canTryAbortX (CANDriver *canp, canmbx_t mailbox) |
Tries to abort an ongoing transmission. | |
msg_t | canTransmitTimeout (CANDriver *canp, canmbx_t mailbox, const CANTxFrame *ctfp, sysinterval_t timeout) |
Can frame transmission. | |
msg_t | canReceiveTimeout (CANDriver *canp, canmbx_t mailbox, CANRxFrame *crfp, sysinterval_t timeout) |
Can frame receive. | |
void | canSleep (CANDriver *canp) |
Enters the sleep mode. | |
void | canWakeup (CANDriver *canp) |
Enforces leaving the sleep mode. | |
void | can_lld_init (void) |
Low level CAN driver initialization. | |
void | can_lld_start (CANDriver *canp) |
Configures and activates the CAN peripheral. | |
void | can_lld_stop (CANDriver *canp) |
Deactivates the CAN peripheral. | |
bool | can_lld_is_tx_empty (CANDriver *canp, canmbx_t mailbox) |
Determines whether a frame can be transmitted. | |
void | can_lld_transmit (CANDriver *canp, canmbx_t mailbox, const CANTxFrame *ctfp) |
Inserts a frame into the transmit queue. | |
bool | can_lld_is_rx_nonempty (CANDriver *canp, canmbx_t mailbox) |
Determines whether a frame has been received. | |
void | can_lld_receive (CANDriver *canp, canmbx_t mailbox, CANRxFrame *crfp) |
Receives a frame from the input queue. | |
void | can_lld_abort (CANDriver *canp, canmbx_t mailbox) |
Tries to abort an ongoing transmission. | |
void | can_lld_sleep (CANDriver *canp) |
Enters the sleep mode. | |
void | can_lld_wakeup (CANDriver *canp) |
Enforces leaving the sleep mode. |
Variables | |
CANDriver | CAND1 |
CAN1 driver identifier. |
#define CAN_FRAMING_ERROR 8U |
#define CAN_ANY_MAILBOX 0U |
Special mailbox identifier.
Definition at line 63 of file hal_can.h.
Referenced by can_lld_is_rx_nonempty(), can_lld_is_tx_empty(), and canTryAbortX().
#define CAN_USE_SLEEP_MODE TRUE |
#define CAN_ENFORCE_USE_CALLBACKS FALSE |
#define CAN_MAILBOX_TO_MASK | ( | mbx | ) |
#define canTransmit | ( | canp, | |
mailbox, | |||
ctfp, | |||
timeout ) |
Legacy name for canTransmitTimeout()
.
#define canReceive | ( | canp, | |
mailbox, | |||
crfp, | |||
timeout ) |
Legacy name for canReceiveTimeout()
.
#define _can_tx_empty_isr | ( | canp, | |
flags ) |
#define _can_rx_full_isr | ( | canp, | |
flags ) |
#define _can_wakeup_isr | ( | canp | ) |
#define _can_error_isr | ( | canp, | |
flags ) |
#define CAN_TX_MAILBOXES 1 |
Number of transmit mailboxes.
Definition at line 37 of file hal_can_lld.h.
Referenced by canTransmitTimeout(), canTryAbortX(), and canTryTransmitI().
#define CAN_RX_MAILBOXES 1 |
Number of receive mailboxes.
Definition at line 42 of file hal_can_lld.h.
Referenced by canReceiveTimeout(), and canTryReceiveI().
#define PLATFORM_CAN_USE_CAN1 FALSE |
CAN1 driver enable switch.
If set to TRUE
the support for CAN1 is included.
FALSE
. Definition at line 58 of file hal_can_lld.h.
typedef struct hal_can_driver CANDriver |
Type of a structure representing an CAN driver.
Definition at line 73 of file hal_can_lld.h.
typedef uint32_t canmbx_t |
Type of a transmission mailbox index.
Definition at line 78 of file hal_can_lld.h.
typedef void(* can_callback_t) (CANDriver *canp, uint32_t flags) |
Type of a CAN notification callback.
[in] | canp | pointer to the CANDriver object triggering the callback |
[in] | flags | flags associated to the mailbox callback |
Definition at line 88 of file hal_can_lld.h.
typedef struct hal_can_config CANConfig |
Type of a CAN configuration structure.
enum canstate_t |
void canInit | ( | void | ) |
CAN Driver initialization.
halInit()
, there is no need to explicitly initialize the driver.Definition at line 56 of file hal_can.c.
References can_lld_init().
Referenced by halInit().
void canObjectInit | ( | CANDriver * | canp | ) |
Initializes the standard part of a CANDriver
structure.
[out] | canp | pointer to the CANDriver object |
Definition at line 68 of file hal_can.c.
References CAN_STOP, hal_can_driver::config, hal_can_driver::error_event, osalEventObjectInit(), osalThreadQueueObjectInit(), hal_can_driver::rxfull_event, hal_can_driver::rxqueue, hal_can_driver::sleep_event, hal_can_driver::state, hal_can_driver::txempty_event, hal_can_driver::txqueue, and hal_can_driver::wakeup_event.
Referenced by can_lld_init().
Configures and activates the CAN peripheral.
[in] | canp | pointer to the CANDriver object |
[in] | config | pointer to the CANConfig object. Depending on the implementation the value can be NULL . |
Definition at line 105 of file hal_can.c.
References can_lld_start(), CAN_READY, CAN_STARTING, CAN_STOP, hal_can_driver::config, HAL_RET_SUCCESS, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and hal_can_driver::state.
void canStop | ( | CANDriver * | canp | ) |
Deactivates the CAN peripheral.
[in] | canp | pointer to the CANDriver object |
Definition at line 145 of file hal_can.c.
References can_lld_stop(), CAN_READY, CAN_STOP, CAN_STOPPING, hal_can_driver::config, MSG_RESET, osalDbgAssert, osalDbgCheck, osalOsRescheduleS(), osalSysLock(), osalSysUnlock(), osalThreadDequeueAllI(), hal_can_driver::rxqueue, hal_can_driver::state, and hal_can_driver::txqueue.
bool canTryTransmitI | ( | CANDriver * | canp, |
canmbx_t | mailbox, | ||
const CANTxFrame * | ctfp ) |
Can frame transmission attempt.
The specified frame is queued for transmission, if the hardware queue is full then the function fails.
[in] | canp | pointer to the CANDriver object |
[in] | mailbox | mailbox number, CAN_ANY_MAILBOX for any mailbox |
[in] | ctfp | pointer to the CAN frame to be transmitted |
false | Frame transmitted. |
true | Mailbox full. |
Definition at line 181 of file hal_can.c.
References can_lld_is_tx_empty(), can_lld_transmit(), CAN_READY, CAN_SLEEP, CAN_TX_MAILBOXES, osalDbgAssert, osalDbgCheck, osalDbgCheckClassI, and hal_can_driver::state.
bool canTryReceiveI | ( | CANDriver * | canp, |
canmbx_t | mailbox, | ||
CANRxFrame * | crfp ) |
Can frame receive attempt.
The function tries to fetch a frame from a mailbox.
[in] | canp | pointer to the CANDriver object |
[in] | mailbox | mailbox number, CAN_ANY_MAILBOX for any mailbox |
[out] | crfp | pointer to the buffer where the CAN frame is copied |
false | Frame fetched. |
true | Mailbox empty. |
Definition at line 215 of file hal_can.c.
References can_lld_is_rx_nonempty(), can_lld_receive(), CAN_READY, CAN_RX_MAILBOXES, CAN_SLEEP, osalDbgAssert, osalDbgCheck, osalDbgCheckClassI, and hal_can_driver::state.
Tries to abort an ongoing transmission.
[in] | canp | pointer to the CANDriver object |
[in] | mailbox | mailbox number |
Definition at line 244 of file hal_can.c.
References CAN_ANY_MAILBOX, can_lld_abort(), CAN_TX_MAILBOXES, and osalDbgCheck.
msg_t canTransmitTimeout | ( | CANDriver * | canp, |
canmbx_t | mailbox, | ||
const CANTxFrame * | ctfp, | ||
sysinterval_t | timeout ) |
Can frame transmission.
The specified frame is queued for transmission, if the hardware queue is full then the invoking thread is queued.
[in] | canp | pointer to the CANDriver object |
[in] | mailbox | mailbox number, CAN_ANY_MAILBOX for any mailbox |
[in] | ctfp | pointer to the CAN frame to be transmitted |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | the frame has been queued for transmission. |
MSG_TIMEOUT | The operation has timed out. |
MSG_RESET | The driver has been stopped while waiting. |
Definition at line 274 of file hal_can.c.
References can_lld_is_tx_empty(), can_lld_transmit(), CAN_READY, CAN_SLEEP, CAN_TX_MAILBOXES, MSG_OK, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), osalThreadEnqueueTimeoutS(), hal_can_driver::state, and hal_can_driver::txqueue.
msg_t canReceiveTimeout | ( | CANDriver * | canp, |
canmbx_t | mailbox, | ||
CANRxFrame * | crfp, | ||
sysinterval_t | timeout ) |
Can frame receive.
The function waits until a frame is received.
[in] | canp | pointer to the CANDriver object |
[in] | mailbox | mailbox number, CAN_ANY_MAILBOX for any mailbox |
[out] | crfp | pointer to the buffer where the CAN frame is copied |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | a frame has been received and placed in the buffer. |
MSG_TIMEOUT | The operation has timed out. |
MSG_RESET | The driver has been stopped while waiting. |
Definition at line 321 of file hal_can.c.
References can_lld_is_rx_nonempty(), can_lld_receive(), CAN_READY, CAN_RX_MAILBOXES, CAN_SLEEP, MSG_OK, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), osalThreadEnqueueTimeoutS(), hal_can_driver::rxqueue, and hal_can_driver::state.
void canSleep | ( | CANDriver * | canp | ) |
Enters the sleep mode.
This function puts the CAN driver in sleep mode and broadcasts the sleep_event
event source.
CAN_USE_SLEEP_MODE
must be enabled and the CAN_SUPPORTS_SLEEP
mode must be supported by the low level driver.[in] | canp | pointer to the CANDriver object |
Definition at line 360 of file hal_can.c.
References can_lld_sleep(), CAN_READY, CAN_SLEEP, osalDbgAssert, osalDbgCheck, osalEventBroadcastFlagsI(), osalOsRescheduleS(), osalSysLock(), osalSysUnlock(), hal_can_driver::sleep_event, and hal_can_driver::state.
void canWakeup | ( | CANDriver * | canp | ) |
Enforces leaving the sleep mode.
[in] | canp | pointer to the CANDriver object |
Definition at line 385 of file hal_can.c.
References can_lld_wakeup(), CAN_READY, CAN_SLEEP, osalDbgAssert, osalDbgCheck, osalEventBroadcastFlagsI(), osalOsRescheduleS(), osalSysLock(), osalSysUnlock(), hal_can_driver::state, and hal_can_driver::wakeup_event.
void can_lld_init | ( | void | ) |
Low level CAN driver initialization.
Definition at line 65 of file hal_can_lld.c.
References CAND1, and canObjectInit().
Referenced by canInit().
void can_lld_start | ( | CANDriver * | canp | ) |
Configures and activates the CAN peripheral.
[in] | canp | pointer to the CANDriver object |
Definition at line 80 of file hal_can_lld.c.
References CAN_STOP, CAND1, and hal_can_driver::state.
Referenced by canStart().
void can_lld_stop | ( | CANDriver * | canp | ) |
Deactivates the CAN peripheral.
[in] | canp | pointer to the CANDriver object |
Definition at line 101 of file hal_can_lld.c.
References CAN_READY, CAND1, and hal_can_driver::state.
Referenced by canStop().
Determines whether a frame can be transmitted.
[in] | canp | pointer to the CANDriver object |
[in] | mailbox | mailbox number, CAN_ANY_MAILBOX for any mailbox |
false | no space in the transmit queue. |
true | transmit slot available. |
Definition at line 127 of file hal_can_lld.c.
References CAN_ANY_MAILBOX.
Referenced by canTransmitTimeout(), and canTryTransmitI().
void can_lld_transmit | ( | CANDriver * | canp, |
canmbx_t | mailbox, | ||
const CANTxFrame * | ctfp ) |
Inserts a frame into the transmit queue.
[in] | canp | pointer to the CANDriver object |
[in] | ctfp | pointer to the CAN frame to be transmitted |
[in] | mailbox | mailbox number, CAN_ANY_MAILBOX for any mailbox |
Definition at line 154 of file hal_can_lld.c.
Referenced by canTransmitTimeout(), and canTryTransmitI().
Determines whether a frame has been received.
[in] | canp | pointer to the CANDriver object |
[in] | mailbox | mailbox number, CAN_ANY_MAILBOX for any mailbox |
false | no space in the transmit queue. |
true | transmit slot available. |
Definition at line 176 of file hal_can_lld.c.
References CAN_ANY_MAILBOX.
Referenced by canReceiveTimeout(), and canTryReceiveI().
void can_lld_receive | ( | CANDriver * | canp, |
canmbx_t | mailbox, | ||
CANRxFrame * | crfp ) |
Receives a frame from the input queue.
[in] | canp | pointer to the CANDriver object |
[in] | mailbox | mailbox number, CAN_ANY_MAILBOX for any mailbox |
[out] | crfp | pointer to the buffer where the CAN frame is copied |
Definition at line 202 of file hal_can_lld.c.
Referenced by canReceiveTimeout(), and canTryReceiveI().
Tries to abort an ongoing transmission.
[in] | canp | pointer to the CANDriver object |
[in] | mailbox | mailbox number |
Definition at line 220 of file hal_can_lld.c.
Referenced by canTryAbortX().
void can_lld_sleep | ( | CANDriver * | canp | ) |
Enters the sleep mode.
[in] | canp | pointer to the CANDriver object |
Definition at line 235 of file hal_can_lld.c.
Referenced by canSleep().
void can_lld_wakeup | ( | CANDriver * | canp | ) |
Enforces leaving the sleep mode.
[in] | canp | pointer to the CANDriver object |
Definition at line 248 of file hal_can_lld.c.
Referenced by canWakeup().
CANDriver CAND1 |
CAN1 driver identifier.
Definition at line 41 of file hal_can_lld.c.
Referenced by can_lld_init(), can_lld_start(), and can_lld_stop().