ChibiOS/HAL 9.0.0
hal_can.h
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.h
19 * @brief CAN Driver macros and structures.
20 *
21 * @addtogroup CAN
22 * @{
23 */
24
25#ifndef HAL_CAN_H
26#define HAL_CAN_H
27
28#if (HAL_USE_CAN == TRUE) || defined(__DOXYGEN__)
29
30/*===========================================================================*/
31/* Driver constants. */
32/*===========================================================================*/
33
34/**
35 * @name CAN status flags
36 * @{
37 */
38/**
39 * @brief Errors rate warning.
40 */
41#define CAN_LIMIT_WARNING 1U
42/**
43 * @brief Errors rate error.
44 */
45#define CAN_LIMIT_ERROR 2U
46/**
47 * @brief Bus off condition reached.
48 */
49#define CAN_BUS_OFF_ERROR 4U
50/**
51 * @brief Framing error of some kind on the CAN bus.
52 */
53#define CAN_FRAMING_ERROR 8U
54/**
55 * @brief Overflow in receive queue.
56 */
57#define CAN_OVERFLOW_ERROR 16U
58/** @} */
59
60/**
61 * @brief Special mailbox identifier.
62 */
63#define CAN_ANY_MAILBOX 0U
64
65/*===========================================================================*/
66/* Driver pre-compile time settings. */
67/*===========================================================================*/
68
69/**
70 * @name CAN configuration options
71 * @{
72 */
73/**
74 * @brief Sleep mode related APIs inclusion switch.
75 * @details This option can only be enabled if the CAN implementation supports
76 * the sleep mode, see the macro @p CAN_SUPPORTS_SLEEP exported by
77 * the underlying implementation.
78 */
79#if !defined(CAN_USE_SLEEP_MODE) || defined(__DOXYGEN__)
80#define CAN_USE_SLEEP_MODE TRUE
81#endif
82
83/**
84 * @brief Enforces the driver to use direct callbacks rather than OSAL events.
85 */
86#if !defined(CAN_ENFORCE_USE_CALLBACKS) || defined(__DOXYGEN__)
87#define CAN_ENFORCE_USE_CALLBACKS FALSE
88#endif
89/** @} */
90
91/*===========================================================================*/
92/* Derived constants and error checks. */
93/*===========================================================================*/
94
95/*===========================================================================*/
96/* Driver data structures and types. */
97/*===========================================================================*/
98
99/**
100 * @brief Driver state machine possible states.
101 */
102typedef enum {
103 CAN_UNINIT = 0, /**< Not initialized. */
104 CAN_STOP = 1, /**< Stopped. */
105 CAN_STARTING = 2, /**< Starting. */
106 CAN_STOPPING = 3, /**< Stopping. */
107 CAN_READY = 4, /**< Ready. */
108 CAN_SLEEP = 5 /**< Sleep state. */
109} canstate_t;
110
111#include "hal_can_lld.h"
112
113/*===========================================================================*/
114/* Driver macros. */
115/*===========================================================================*/
116
117/**
118 * @name Macro Functions
119 * @{
120 */
121/**
122 * @brief Converts a mailbox index to a bit mask.
123 */
124#define CAN_MAILBOX_TO_MASK(mbx) (1U << ((mbx) - 1U))
125
126/**
127 * @brief Legacy name for @p canTransmitTimeout().
128 *
129 * @deprecated
130 */
131#define canTransmit(canp, mailbox, ctfp, timeout) \
132 canTransmitTimeout(canp, mailbox, ctfp, timeout)
133
134/**
135 * @brief Legacy name for @p canReceiveTimeout().
136 *
137 * @deprecated
138 */
139#define canReceive(canp, mailbox, crfp, timeout) \
140 canReceiveTimeout(canp, mailbox, crfp, timeout)
141/** @} */
142
143/**
144 * @name Low level driver helper macros
145 * @{
146 */
147#if (CAN_ENFORCE_USE_CALLBACKS == FALSE) || defined(__DOXYGEN__)
148/**
149 * @brief TX mailbox empty event.
150 */
151#define _can_tx_empty_isr(canp, flags) { \
152 osalSysLockFromISR(); \
153 osalThreadDequeueAllI(&(canp)->txqueue, MSG_OK); \
154 osalEventBroadcastFlagsI(&(canp)->txempty_event, flags); \
155 osalSysUnlockFromISR(); \
156}
157
158/**
159 * @brief RX mailbox empty full event.
160 */
161#define _can_rx_full_isr(canp, flags) { \
162 osalSysLockFromISR(); \
163 osalThreadDequeueAllI(&(canp)->rxqueue, MSG_OK); \
164 osalEventBroadcastFlagsI(&(canp)->rxfull_event, flags); \
165 osalSysUnlockFromISR(); \
166}
167
168/**
169 * @brief Wakeup event.
170 */
171#define _can_wakeup_isr(canp) { \
172 osalSysLockFromISR(); \
173 osalEventBroadcastFlagsI(&(canp)->wakeup_event, 0U); \
174 osalSysUnlockFromISR(); \
175}
176
177/**
178 * @brief Error event.
179 */
180#define _can_error_isr(canp, flags) { \
181 osalSysLockFromISR(); \
182 osalEventBroadcastFlagsI(&(canp)->error_event, flags); \
183 osalSysUnlockFromISR(); \
184}
185#else /* CAN_ENFORCE_USE_CALLBACKS == TRUE */
186#define _can_tx_empty_isr(canp, flags) { \
187 if ((canp)->txempty_cb != NULL) { \
188 (canp)->txempty_cb(canp, flags); \
189 } \
190 osalSysLockFromISR(); \
191 osalThreadDequeueAllI(&(canp)->txqueue, MSG_OK); \
192 osalSysUnlockFromISR(); \
193}
194
195#define _can_rx_full_isr(canp, flags) { \
196 if ((canp)->rxfull_cb != NULL) { \
197 (canp)->rxfull_cb(canp, flags); \
198 } \
199 osalSysLockFromISR(); \
200 osalThreadDequeueAllI(&(canp)->rxqueue, MSG_OK); \
201 osalSysUnlockFromISR(); \
202}
203
204#define _can_wakeup_isr(canp) { \
205 if ((canp)->wakeup_cb != NULL) { \
206 (canp)->wakeup_cb(canp, 0U); \
207 } \
208}
209
210#define _can_error_isr(canp, flags) { \
211 if ((canp)->error_cb != NULL) { \
212 (canp)->error_cb(canp, flags); \
213 } \
214}
215#endif /* CAN_ENFORCE_USE_CALLBACKS == TRUE */
216/** @} */
217
218/*===========================================================================*/
219/* External declarations. */
220/*===========================================================================*/
221
222#ifdef __cplusplus
223extern "C" {
224#endif
225 void canInit(void);
226 void canObjectInit(CANDriver *canp);
227 msg_t canStart(CANDriver *canp, const CANConfig *config);
228 void canStop(CANDriver *canp);
229 bool canTryTransmitI(CANDriver *canp,
230 canmbx_t mailbox,
231 const CANTxFrame *ctfp);
232 bool canTryReceiveI(CANDriver *canp,
233 canmbx_t mailbox,
234 CANRxFrame *crfp);
235 void canTryAbortX(CANDriver *canp,
236 canmbx_t mailbox);
238 canmbx_t mailbox,
239 const CANTxFrame *ctfp,
240 sysinterval_t timeout);
242 canmbx_t mailbox,
243 CANRxFrame *crfp,
244 sysinterval_t timeout);
245#if CAN_USE_SLEEP_MODE
246 void canSleep(CANDriver *canp);
247 void canWakeup(CANDriver *canp);
248#endif
249#ifdef __cplusplus
250}
251#endif
252
253#endif /* HAL_USE_CAN == TRUE */
254
255#endif /* HAL_CAN_H */
256
257/** @} */
void canWakeup(CANDriver *canp)
Enforces leaving the sleep mode.
Definition hal_can.c:385
uint32_t canmbx_t
Type of a transmission mailbox index.
Definition hal_can_lld.h:78
void canInit(void)
CAN Driver initialization.
Definition hal_can.c:56
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
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 canTryAbortX(CANDriver *canp, canmbx_t mailbox)
Tries to abort an ongoing transmission.
Definition hal_can.c:244
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
canstate_t
Driver state machine possible states.
Definition hal_can.h:102
msg_t canStart(CANDriver *canp, const CANConfig *config)
Configures and activates the CAN peripheral.
Definition hal_can.c:105
void canObjectInit(CANDriver *canp)
Initializes the standard part of a CANDriver structure.
Definition hal_can.c:68
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_UNINIT
Definition hal_can.h:103
@ CAN_STARTING
Definition hal_can.h:105
@ CAN_READY
Definition hal_can.h:107
int32_t msg_t
Type of a message.
Definition osal.h:159
uint32_t sysinterval_t
Type of system time interval.
Definition osal.h:169
PLATFORM CAN subsystem low level driver header.
CAN received frame.
CAN transmission frame.
Definition hal_can_lld.h:96