@@ -33,6 +33,7 @@ extern "C" {
3333#define CAN_STD_ID_MASK CAN_MAX_STD_ID
3434#define CAN_EXT_ID_MASK (0x1FFFFFFF)
3535#define CAN_MAX_DLC (8)
36+ #define CAN_MAX_DLEN 8
3637
3738/* CAN_TX_* are the error flags from tx_callback and send.*/
3839/** send successfully */
@@ -96,8 +97,49 @@ enum can_mode {
9697 CAN_SILENT_LOOPBACK_MODE
9798};
9899
100+ /*
101+ * Controller Area Network Identifier structure for Linux compatibility.
102+ *
103+ * The fields in this type are:
104+ *
105+ * bit 0-28 : CAN identifier (11/29 bit)
106+ * bit 29 : error message frame flag (0 = data frame, 1 = error message)
107+ * bit 30 : remote transmission request flag (1 = rtr frame)
108+ * bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
109+ */
110+ typedef u32_t canid_t ;
111+
112+ /**
113+ * @brief CAN frame structure that is compatible with Linux. This is mainly
114+ * used by Socket CAN code.
115+ *
116+ * @details Used to pass CAN messages from userspace to the socket CAN and vice
117+ * versa.
118+ */
119+ struct can_frame {
120+ /** 32 bit CAN_ID + EFF/RTR/ERR flags */
121+ canid_t can_id ;
122+
123+ /** The length of the message */
124+ u8_t can_dlc ;
125+
126+ /** The message data */
127+ u8_t data [CAN_MAX_DLEN ];
128+ };
129+
99130/**
100- * @brief can message structure
131+ * @brief CAN filter that is compatible with Linux. This is mainly used by
132+ * Socket CAN code.
133+ *
134+ * @details A filter matches, when "received_can_id & mask == can_id & mask"
135+ */
136+ struct can_filter {
137+ canid_t can_id ;
138+ canid_t can_mask ;
139+ };
140+
141+ /**
142+ * @brief CAN message structure
101143 *
102144 * Used to pass can messages from userspace to the driver and
103145 * from driver to userspace
@@ -127,15 +169,15 @@ struct can_msg {
127169} __packed ;
128170
129171/**
130- * @brief can filter structure
172+ * @brief CAN filter structure
131173 *
132174 * Used to pass can identifier filter information to the driver.
133175 * rtr_mask and *_id_mask are used to mask bits of the rtr and id fields.
134176 * If the mask bit is 0, the value of the corresponding bit in the id or rtr
135177 * field don't care for the filter matching.
136178 *
137179 */
138- struct can_filter {
180+ struct can_msg_filter {
139181 /** Indicates the identifier type (standard or extended)
140182 * use can_ide enum for assignment
141183 */
@@ -218,13 +260,14 @@ typedef int (*can_send_t)(struct device *dev, struct can_msg *msg,
218260 * *
219261 * @param dev Pointer to the device structure for the driver instance.
220262 * @param msgq Pointer to the already initialized message queue.
221- * @param filter Pointer to a can_filter structure defining the id filtering.
263+ * @param filter Pointer to a can_msg_filter structure defining the id
264+ * filtering.
222265 *
223266 * @retval filter id on success.
224267 * @retval CAN_NO_FREE_FILTER if there is no filter left.
225268 */
226269typedef int (* can_attach_msgq_t )(struct device * dev , struct k_msgq * msg_q ,
227- const struct can_filter * filter );
270+ const struct can_msg_filter * filter );
228271
229272/**
230273 * @brief Attach an isr callback function to a single or group of identifiers.
@@ -238,13 +281,14 @@ typedef int (*can_attach_msgq_t)(struct device *dev, struct k_msgq *msg_q,
238281 * *
239282 * @param dev Pointer to the device structure for the driver instance.
240283 * @param isr Callback function pointer.
241- * @param filter Pointer to a can_filter structure defining the id filtering.
284+ * @param filter Pointer to a can_msg_filter structure defining the id
285+ * filtering.
242286 *
243287 * @retval filter id on success.
244288 * @retval CAN_NO_FREE_FILTER if there is no filter left.
245289 */
246290typedef int (* can_attach_isr_t )(struct device * dev , can_rx_callback_t isr ,
247- const struct can_filter * filter );
291+ const struct can_msg_filter * filter );
248292
249293/**
250294 * @brief Detach an isr or message queue from the identifier filtering.
@@ -325,11 +369,11 @@ static inline int can_write(struct device *dev, u8_t *data, u8_t length,
325369
326370
327371__syscall int can_attach_msgq (struct device * dev , struct k_msgq * msg_q ,
328- const struct can_filter * filter );
372+ const struct can_msg_filter * filter );
329373
330374static inline int _impl_can_attach_msgq (struct device * dev ,
331375 struct k_msgq * msg_q ,
332- const struct can_filter * filter )
376+ const struct can_msg_filter * filter )
333377{
334378 const struct can_driver_api * api = dev -> driver_api ;
335379
@@ -338,10 +382,10 @@ static inline int _impl_can_attach_msgq(struct device *dev,
338382
339383
340384__syscall int can_attach_isr (struct device * dev , can_rx_callback_t isr ,
341- const struct can_filter * filter );
385+ const struct can_msg_filter * filter );
342386static inline int _impl_can_attach_isr (struct device * dev ,
343387 can_rx_callback_t isr ,
344- const struct can_filter * filter )
388+ const struct can_msg_filter * filter )
345389{
346390 const struct can_driver_api * api = dev -> driver_api ;
347391
@@ -370,6 +414,71 @@ static inline int _impl_can_configure(struct device *dev, enum can_mode mode,
370414 return api -> configure (dev , mode , bitrate );
371415}
372416
417+ /**
418+ * @brief Converter that translates betwen can_frame and can_msg structs.
419+ *
420+ * @param frame Pointer to can_frame struct.
421+ * @param msg Pointer to can_msg struct.
422+ */
423+ static inline void can_copy_frame_to_msg (struct can_frame * frame ,
424+ struct can_msg * msg )
425+ {
426+ msg -> id_type = (frame -> can_id & BIT (31 )) >> 31 ;
427+ msg -> rtr = (frame -> can_id & BIT (30 )) >> 30 ;
428+ msg -> ext_id = frame -> can_id & BIT_MASK (29 );
429+ msg -> dlc = frame -> can_dlc ;
430+ memcpy (msg -> data , frame -> data , sizeof (msg -> data ));
431+ }
432+
433+ /**
434+ * @brief Converter that translates betwen can_msg and can_frame structs.
435+ *
436+ * @param msg Pointer to can_msg struct.
437+ * @param frame Pointer to can_frame struct.
438+ */
439+ static inline void can_copy_msg_to_frame (struct can_msg * msg ,
440+ struct can_frame * frame )
441+ {
442+ frame -> can_id = (msg -> id_type << 31 ) | (msg -> rtr << 30 ) | msg -> ext_id ;
443+ frame -> can_dlc = msg -> dlc ;
444+ memcpy (frame -> data , msg -> data , sizeof (frame -> data ));
445+ }
446+
447+ /**
448+ * @brief Converter that translates betwen can_filter and can_msg_filter
449+ * structs.
450+ *
451+ * @param filter Pointer to can_filter struct.
452+ * @param msg_filter Pointer to can_msg_filter struct.
453+ */
454+ static inline
455+ void can_copy_filter_to_msg_filter (struct can_filter * filter ,
456+ struct can_msg_filter * msg_filter )
457+ {
458+ msg_filter -> id_type = (filter -> can_id & BIT (31 )) >> 31 ;
459+ msg_filter -> rtr = (filter -> can_id & BIT (30 )) >> 30 ;
460+ msg_filter -> ext_id = filter -> can_id & BIT_MASK (29 );
461+ msg_filter -> rtr_mask = (filter -> can_mask & BIT (30 )) >> 30 ;
462+ msg_filter -> ext_id_mask = filter -> can_mask & BIT_MASK (29 );
463+ }
464+
465+ /**
466+ * @brief Converter that translates betwen can_msg_filter and can_filter
467+ * structs.
468+ *
469+ * @param msg_filter Pointer to can_msg_filter struct.
470+ * @param filter Pointer to can_filter struct.
471+ */
472+ static inline
473+ void can_copy_msg_filter_to_filter (struct can_msg_filter * msg_filter ,
474+ struct can_filter * filter )
475+ {
476+ filter -> can_id = (msg_filter -> id_type << 31 ) |
477+ (msg_filter -> rtr << 30 ) | msg_filter -> ext_id ;
478+ filter -> can_mask = (msg_filter -> rtr_mask << 30 ) |
479+ (msg_filter -> id_type << 31 ) | msg_filter -> ext_id_mask ;
480+ }
481+
373482#ifdef __cplusplus
374483}
375484#endif
0 commit comments