ChibiOS/HAL  6.1.0
hal_can_lld.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_lld.h
19  * @brief PLATFORM CAN subsystem low level driver header.
20  *
21  * @addtogroup CAN
22  * @{
23  */
24 
25 #ifndef HAL_CAN_LLD_H
26 #define HAL_CAN_LLD_H
27 
28 #if (HAL_USE_CAN == TRUE) || defined(__DOXYGEN__)
29 
30 /*===========================================================================*/
31 /* Driver constants. */
32 /*===========================================================================*/
33 
34 /**
35  * @brief Number of transmit mailboxes.
36  */
37 #define CAN_TX_MAILBOXES 1
38 
39 /**
40  * @brief Number of receive mailboxes.
41  */
42 #define CAN_RX_MAILBOXES 1
43 
44 /*===========================================================================*/
45 /* Driver pre-compile time settings. */
46 /*===========================================================================*/
47 
48 /**
49  * @name PLATFORM configuration options
50  * @{
51  */
52 /**
53  * @brief CAN1 driver enable switch.
54  * @details If set to @p TRUE the support for CAN1 is included.
55  * @note The default is @p FALSE.
56  */
57 #if !defined(PLATFORM_CAN_USE_CAN1) || defined(__DOXYGEN__)
58 #define PLATFORM_CAN_USE_CAN1 FALSE
59 #endif
60 /** @} */
61 
62 /*===========================================================================*/
63 /* Derived constants and error checks. */
64 /*===========================================================================*/
65 
66 /*===========================================================================*/
67 /* Driver data structures and types. */
68 /*===========================================================================*/
69 
70 /**
71  * @brief Type of a structure representing an CAN driver.
72  */
73 typedef struct CANDriver CANDriver;
74 
75 /**
76  * @brief Type of a transmission mailbox index.
77  */
78 typedef uint32_t canmbx_t;
79 
80 #if defined(CAN_ENFORCE_USE_CALLBACKS) || defined(__DOXYGEN__)
81 /**
82  * @brief Type of a CAN notification callback.
83  *
84  * @param[in] canp pointer to the @p CANDriver object triggering the
85  * callback
86  * @param[in] flags flags associated to the mailbox callback
87  */
88 typedef void (*can_callback_t)(CANDriver *canp, uint32_t flags);
89 #endif
90 
91 /**
92  * @brief CAN transmission frame.
93  * @note Accessing the frame data as word16 or word32 is not portable because
94  * machine data endianness, it can be still useful for a quick filling.
95  */
96 typedef struct {
97  /*lint -save -e46 [6.1] Standard types are fine too.*/
98  uint8_t DLC:4; /**< @brief Data length. */
99  uint8_t RTR:1; /**< @brief Frame type. */
100  uint8_t IDE:1; /**< @brief Identifier type. */
101  union {
102  uint32_t SID:11; /**< @brief Standard identifier.*/
103  uint32_t EID:29; /**< @brief Extended identifier.*/
104  uint32_t _align1;
105  };
106  /*lint -restore*/
107  union {
108  uint8_t data8[8]; /**< @brief Frame data. */
109  uint16_t data16[4]; /**< @brief Frame data. */
110  uint32_t data32[2]; /**< @brief Frame data. */
111  };
112 } CANTxFrame;
113 
114 /**
115  * @brief CAN received frame.
116  * @note Accessing the frame data as word16 or word32 is not portable because
117  * machine data endianness, it can be still useful for a quick filling.
118  */
119 typedef struct {
120  /*lint -save -e46 [6.1] Standard types are fine too.*/
121  uint8_t FMI; /**< @brief Filter id. */
122  uint16_t TIME; /**< @brief Time stamp. */
123  uint8_t DLC:4; /**< @brief Data length. */
124  uint8_t RTR:1; /**< @brief Frame type. */
125  uint8_t IDE:1; /**< @brief Identifier type. */
126  union {
127  uint32_t SID:11; /**< @brief Standard identifier.*/
128  uint32_t EID:29; /**< @brief Extended identifier.*/
129  uint32_t _align1;
130  };
131  /*lint -restore*/
132  union {
133  uint8_t data8[8]; /**< @brief Frame data. */
134  uint16_t data16[4]; /**< @brief Frame data. */
135  uint32_t data32[2]; /**< @brief Frame data. */
136  };
137 } CANRxFrame;
138 
139 /**
140  * @brief Driver configuration structure.
141  */
142 typedef struct {
143  /* End of the mandatory fields.*/
144  uint32_t dummy;
145 } CANConfig;
146 
147 /**
148  * @brief Structure representing an CAN driver.
149  */
150 struct CANDriver {
151  /**
152  * @brief Driver state.
153  */
155  /**
156  * @brief Current configuration data.
157  */
159  /**
160  * @brief Transmission threads queue.
161  */
163  /**
164  * @brief Receive threads queue.
165  */
167 #if (CAN_ENFORCE_USE_CALLBACKS == FALSE) || defined (__DOXYGEN__)
168  /**
169  * @brief One or more frames become available.
170  * @note After broadcasting this event it will not be broadcasted again
171  * until the received frames queue has been completely emptied. It
172  * is <b>not</b> broadcasted for each received frame. It is
173  * responsibility of the application to empty the queue by
174  * repeatedly invoking @p chReceive() when listening to this event.
175  * This behavior minimizes the interrupt served by the system
176  * because CAN traffic.
177  * @note The flags associated to the listeners will indicate which
178  * receive mailboxes become non-empty.
179  */
181  /**
182  * @brief One or more transmission mailbox become available.
183  * @note The flags associated to the listeners will indicate which
184  * transmit mailboxes become empty.
185  */
187  /**
188  * @brief A CAN bus error happened.
189  * @note The flags associated to the listeners will indicate the
190  * error(s) that have occurred.
191  */
193 #if (CAN_USE_SLEEP_MODE == TRUE) || defined (__DOXYGEN__)
194  /**
195  * @brief Entering sleep state event.
196  */
198  /**
199  * @brief Exiting sleep state event.
200  */
202 #endif
203 #else /* CAN_ENFORCE_USE_CALLBACKS == TRUE */
204  /**
205  * @brief One or more frames become available.
206  * @note After calling this function it will not be called again
207  * until the received frames queue has been completely emptied. It
208  * is <b>not</b> called for each received frame. It is
209  * responsibility of the application to empty the queue by
210  * repeatedly invoking @p chTryReceiveI().
211  * This behavior minimizes the interrupt served by the system
212  * because CAN traffic.
213  */
214  can_callback_t rxfull_cb;
215  /**
216  * @brief One or more transmission mailbox become available.
217  * @note The flags associated to the callback will indicate which
218  * transmit mailboxes become empty.
219  */
220  can_callback_t txempty_cb;
221  /**
222  * @brief A CAN bus error happened.
223  */
224  can_callback_t error_cb;
225 #if (CAN_USE_SLEEP_MODE == TRUE) || defined (__DOXYGEN__)
226  /**
227  * @brief Exiting sleep state.
228  */
229  can_callback_t wakeup_cb;
230 #endif
231 #endif
232  /* End of the mandatory fields.*/
233 };
234 
235 /*===========================================================================*/
236 /* Driver macros. */
237 /*===========================================================================*/
238 
239 /*===========================================================================*/
240 /* External declarations. */
241 /*===========================================================================*/
242 
243 #if (PLATFORM_CAN_USE_CAN1 == TRUE) && !defined(__DOXYGEN__)
244 extern CANDriver CAND1;
245 #endif
246 
247 #ifdef __cplusplus
248 extern "C" {
249 #endif
250  void can_lld_init(void);
251  void can_lld_start(CANDriver *canp);
252  void can_lld_stop(CANDriver *canp);
253  bool can_lld_is_tx_empty(CANDriver *canp, canmbx_t mailbox);
254  void can_lld_transmit(CANDriver *canp,
255  canmbx_t mailbox,
256  const CANTxFrame *ctfp);
257  bool can_lld_is_rx_nonempty(CANDriver *canp, canmbx_t mailbox);
258  void can_lld_receive(CANDriver *canp,
259  canmbx_t mailbox,
260  CANRxFrame *crfp);
261 #if CAN_USE_SLEEP_MODE == TRUE
262  void can_lld_sleep(CANDriver *canp);
263  void can_lld_wakeup(CANDriver *canp);
264 #endif
265 #ifdef __cplusplus
266 }
267 #endif
268 
269 #endif /* HAL_USE_CAN == TRUE */
270 
271 #endif /* HAL_CAN_LLD_H */
272 
273 /** @} */
event_source_t wakeup_event
Exiting sleep state event.
Definition: hal_can_lld.h:201
uint32_t canmbx_t
Type of a transmission mailbox index.
Definition: hal_can_lld.h:78
void can_lld_transmit(CANDriver *canp, canmbx_t mailbox, const CANTxFrame *ctfp)
Inserts a frame into the transmit queue.
Definition: hal_can_lld.c:154
Events source object.
Definition: osal.h:212
CAN transmission frame.
Definition: hal_can_lld.h:96
event_source_t txempty_event
One or more transmission mailbox become available.
Definition: hal_can_lld.h:186
CAN received frame.
Definition: hal_can_lld.h:119
event_source_t error_event
A CAN bus error happened.
Definition: hal_can_lld.h:192
void can_lld_stop(CANDriver *canp)
Deactivates the CAN peripheral.
Definition: hal_can_lld.c:101
canstate_t state
Driver state.
Definition: hal_can_lld.h:154
Structure representing an CAN driver.
Definition: hal_can_lld.h:150
void can_lld_wakeup(CANDriver *canp)
Enforces leaving the sleep mode.
Definition: hal_can_lld.c:233
CANDriver CAND1
CAN1 driver identifier.
Definition: hal_can_lld.c:41
Type of a thread queue.
Definition: osal.h:232
void can_lld_receive(CANDriver *canp, canmbx_t mailbox, CANRxFrame *crfp)
Receives a frame from the input queue.
Definition: hal_can_lld.c:202
const CANConfig * config
Current configuration data.
Definition: hal_can_lld.h:158
uint16_t TIME
Time stamp.
Definition: hal_can_lld.h:122
void can_lld_sleep(CANDriver *canp)
Enters the sleep mode.
Definition: hal_can_lld.c:220
bool can_lld_is_rx_nonempty(CANDriver *canp, canmbx_t mailbox)
Determines whether a frame has been received.
Definition: hal_can_lld.c:176
void can_lld_start(CANDriver *canp)
Configures and activates the CAN peripheral.
Definition: hal_can_lld.c:80
bool can_lld_is_tx_empty(CANDriver *canp, canmbx_t mailbox)
Determines whether a frame can be transmitted.
Definition: hal_can_lld.c:127
void can_lld_init(void)
Low level CAN driver initialization.
Definition: hal_can_lld.c:65
Driver configuration structure.
Definition: hal_can_lld.h:142
threads_queue_t rxqueue
Receive threads queue.
Definition: hal_can_lld.h:166
void(* can_callback_t)(CANDriver *canp, uint32_t flags)
Type of a CAN notification callback.
Definition: hal_can_lld.h:88
canstate_t
Driver state machine possible states.
Definition: hal_can.h:102
event_source_t rxfull_event
One or more frames become available.
Definition: hal_can_lld.h:180
threads_queue_t txqueue
Transmission threads queue.
Definition: hal_can_lld.h:162
event_source_t sleep_event
Entering sleep state event.
Definition: hal_can_lld.h:197
uint8_t FMI
Filter id.
Definition: hal_can_lld.h:121