ChibiOS/HAL  6.1.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  */
102 typedef enum {
103  CAN_UNINIT = 0, /**< Not initialized. */
104  CAN_STOP = 1, /**< Stopped. */
105  CAN_STARTING = 2, /**< Starting. */
106  CAN_READY = 3, /**< Ready. */
107  CAN_SLEEP = 4 /**< Sleep state. */
108 } canstate_t;
109 
110 #include "hal_can_lld.h"
111 
112 /*===========================================================================*/
113 /* Driver macros. */
114 /*===========================================================================*/
115 
116 /**
117  * @name Macro Functions
118  * @{
119  */
120 /**
121  * @brief Converts a mailbox index to a bit mask.
122  */
123 #define CAN_MAILBOX_TO_MASK(mbx) (1U << ((mbx) - 1U))
124 
125 /**
126  * @brief Legacy name for @p canTransmitTimeout().
127  *
128  * @deprecated
129  */
130 #define canTransmit(canp, mailbox, ctfp, timeout) \
131  canTransmitTimeout(canp, mailbox, ctfp, timeout)
132 
133 /**
134  * @brief Legacy name for @p canReceiveTimeout().
135  *
136  * @deprecated
137  */
138 #define canReceive(canp, mailbox, crfp, timeout) \
139  canReceiveTimeout(canp, mailbox, crfp, timeout)
140 /** @} */
141 
142 /**
143  * @name Low level driver helper macros
144  * @{
145  */
146 #if (CAN_ENFORCE_USE_CALLBACKS == FALSE) || defined(__DOXYGEN__)
147 /**
148  * @brief TX mailbox empty event.
149  */
150 #define _can_tx_empty_isr(canp, flags) { \
151  osalSysLockFromISR(); \
152  osalThreadDequeueAllI(&(canp)->txqueue, MSG_OK); \
153  osalEventBroadcastFlagsI(&(canp)->txempty_event, flags); \
154  osalSysUnlockFromISR(); \
155 }
156 
157 /**
158  * @brief RX mailbox empty full event.
159  */
160 #define _can_rx_full_isr(canp, flags) { \
161  osalSysLockFromISR(); \
162  osalThreadDequeueAllI(&(canp)->rxqueue, MSG_OK); \
163  osalEventBroadcastFlagsI(&(canp)->rxfull_event, flags); \
164  osalSysUnlockFromISR(); \
165 }
166 
167 /**
168  * @brief Error event.
169  */
170 #define _can_wakeup_isr(canp) { \
171  osalSysLockFromISR(); \
172  osalEventBroadcastFlagsI(&(canp)->wakeup_event, 0U); \
173  osalSysUnlockFromISR(); \
174 }
175 
176 /**
177  * @brief Error event.
178  */
179 #define _can_error_isr(canp, flags) { \
180  osalSysLockFromISR(); \
181  osalEventBroadcastFlagsI(&(canp)->error_event, flags); \
182  osalSysUnlockFromISR(); \
183 }
184 #else /* CAN_ENFORCE_USE_CALLBACKS == TRUE */
185 #define _can_tx_empty_isr(canp, flags) { \
186  if ((canp)->txempty_cb != NULL) { \
187  (canp)->txempty_cb(canp, flags); \
188  } \
189  osalSysLockFromISR(); \
190  osalThreadDequeueAllI(&(canp)->txqueue, MSG_OK); \
191  osalSysUnlockFromISR(); \
192 }
193 
194 #define _can_rx_full_isr(canp, flags) { \
195  if ((canp)->rxfull_cb != NULL) { \
196  (canp)->rxfull_cb(canp, flags); \
197  } \
198  osalSysLockFromISR(); \
199  osalThreadDequeueAllI(&(canp)->rxqueue, MSG_OK); \
200  osalSysUnlockFromISR(); \
201 }
202 
203 #define _can_wakeup_isr(canp) { \
204  if ((canp)->wakeup_cb != NULL) { \
205  (canp)->wakeup_cb(canp, 0U); \
206  } \
207 }
208 
209 #define _can_error_isr(canp, flags) { \
210  if ((canp)->error_cb != NULL) { \
211  (canp)->error_cb(canp, flags); \
212  } \
213 }
214 #endif /* CAN_ENFORCE_USE_CALLBACKS == TRUE */
215 /** @} */
216 
217 /*===========================================================================*/
218 /* External declarations. */
219 /*===========================================================================*/
220 
221 #ifdef __cplusplus
222 extern "C" {
223 #endif
224  void canInit(void);
225  void canObjectInit(CANDriver *canp);
226  void canStart(CANDriver *canp, const CANConfig *config);
227  void canStop(CANDriver *canp);
228  bool canTryTransmitI(CANDriver *canp,
229  canmbx_t mailbox,
230  const CANTxFrame *ctfp);
231  bool canTryReceiveI(CANDriver *canp,
232  canmbx_t mailbox,
233  CANRxFrame *crfp);
235  canmbx_t mailbox,
236  const CANTxFrame *ctfp,
237  sysinterval_t timeout);
239  canmbx_t mailbox,
240  CANRxFrame *crfp,
241  sysinterval_t timeout);
242 #if CAN_USE_SLEEP_MODE
243  void canSleep(CANDriver *canp);
244  void canWakeup(CANDriver *canp);
245 #endif
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 #endif /* HAL_USE_CAN == TRUE */
251 
252 #endif /* HAL_CAN_H */
253 
254 /** @} */
uint32_t canmbx_t
Type of a transmission mailbox index.
Definition: hal_can_lld.h:78
CAN transmission frame.
Definition: hal_can_lld.h:96
void canStart(CANDriver *canp, const CANConfig *config)
Configures and activates the CAN peripheral.
Definition: hal_can.c:104
CAN received frame.
Definition: hal_can_lld.h:119
Structure representing an CAN driver.
Definition: hal_can_lld.h:150
msg_t canTransmitTimeout(CANDriver *canp, canmbx_t mailbox, const CANTxFrame *ctfp, sysinterval_t timeout)
Can frame transmission.
Definition: hal_can.c:242
int32_t msg_t
Type of a message.
Definition: osal.h:160
void canObjectInit(CANDriver *canp)
Initializes the standard part of a CANDriver structure.
Definition: hal_can.c:68
PLATFORM CAN subsystem low level driver header.
bool canTryReceiveI(CANDriver *canp, canmbx_t mailbox, CANRxFrame *crfp)
Can frame receive attempt.
Definition: hal_can.c:200
void canSleep(CANDriver *canp)
Enters the sleep mode.
Definition: hal_can.c:329
void canInit(void)
CAN Driver initialization.
Definition: hal_can.c:56
void canWakeup(CANDriver *canp)
Enforces leaving the sleep mode.
Definition: hal_can.c:354
uint32_t sysinterval_t
Type of system time interval.
Definition: osal.h:170
void canStop(CANDriver *canp)
Deactivates the CAN peripheral.
Definition: hal_can.c:131
Driver configuration structure.
Definition: hal_can_lld.h:142
msg_t canReceiveTimeout(CANDriver *canp, canmbx_t mailbox, CANRxFrame *crfp, sysinterval_t timeout)
Can frame receive.
Definition: hal_can.c:290
canstate_t
Driver state machine possible states.
Definition: hal_can.h:102
bool canTryTransmitI(CANDriver *canp, canmbx_t mailbox, const CANTxFrame *ctfp)
Can frame transmission attempt.
Definition: hal_can.c:166