can.h
Go to the documentation of this file.
1 /*
2  * SocialLedge.com - Copyright (C) 2013
3  *
4  * This file is part of free software framework for embedded processors.
5  * You can use it and/or distribute it as long as this copyright header
6  * remains unmodified. The code is free for personal use and requires
7  * permission to use in a commercial product.
8  *
9  * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
10  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
12  * I SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
13  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
14  *
15  * You can reach the author of this software at :
16  * p r e e t . w i k i @ g m a i l . c o m
17  */
18 
44 #ifndef CAN_H__
45 #define CAN_H__
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 #include <stdint.h>
50 #include <stdbool.h>
51 
52 
53 
58 typedef union {
59  uint64_t qword;
60  uint32_t dwords[2];
61  uint16_t words [4];
62  uint8_t bytes [8];
63 } can_data_t;
64 
69 typedef struct {
70  union {
71  uint32_t frame;
72  struct {
73  uint32_t : 16;
74  uint32_t data_len : 4;
75  uint32_t : 10;
76  uint32_t is_rtr : 1;
77  uint32_t is_29bit : 1;
78  } frame_fields;
79  };
80 
81  uint32_t msg_id;
83 } __attribute__((__packed__)) can_msg_t;
84 
89 typedef struct {
90  struct {
91  uint32_t msg_id : 11;
92  uint32_t : 5;
93  uint32_t data_len : 4;
94  uint32_t : 4;
95  uint32_t semphr : 2;
96  uint32_t : 4;
97  uint32_t rtr : 1;
98  uint32_t : 1;
99  };
100 
101  can_data_t data;
102 } __attribute__((__packed__)) can_fullcan_msg_t;
103 
105 typedef enum {
109 } can_t;
110 
115 typedef void (*can_void_func_t)(uint32_t);
116 
142 bool CAN_init(can_t can, uint32_t baudrate_kbps, uint16_t rxq_size, uint16_t txq_size,
143  can_void_func_t bus_off_cb, can_void_func_t data_ovr_cb);
144 
153 bool CAN_rx(can_t can, can_msg_t *msg, uint32_t timeout_ms);
154 
179 bool CAN_tx(can_t can, can_msg_t *msg, uint32_t timeout_ms);
180 
185 bool CAN_is_bus_off(can_t can);
186 void CAN_reset_bus(can_t can);
190 uint16_t CAN_get_rx_watermark(can_t can);
191 uint16_t CAN_get_tx_watermark(can_t can);
192 uint16_t CAN_get_tx_count(can_t can);
193 uint16_t CAN_get_rx_count(can_t can);
194 
201 uint16_t CAN_get_rx_dropped_count(can_t can);
202 
211 
212 /* ---------------------------------------------------------------------------------------
213  * Rest of the API is for filtering specific messages.
214  * Any messages not defined in the acceptance filter are ignored, and will not be
215  * acknowledged by the CAN BUS.
216  * ---------------------------------------------------------------------------------------
217  */
218 
225 typedef union {
226  struct {
227  uint16_t id : 11;
228  uint16_t fc_intr : 1;
229  uint16_t disable : 1;
230  uint16_t can_num : 3;
231  };
232  uint16_t raw;
233 } __attribute__((__packed__)) can_std_id_t;
234 
239 typedef struct {
240  can_std_id_t low;
241  can_std_id_t high;
243 
248 typedef struct {
249  uint32_t id : 29;
250  uint32_t can_num : 3;
251 } __attribute__((__packed__)) can_ext_id_t;
252 
257 typedef struct {
258  can_ext_id_t low;
259  can_ext_id_t high;
261 
271 can_std_id_t CAN_gen_sid(can_t can, uint16_t id);
272 can_ext_id_t CAN_gen_eid(can_t can, uint32_t id);
289 bool CAN_fullcan_add_entry(can_t can, can_std_id_t id1, can_std_id_t id2);
290 
296 can_fullcan_msg_t* CAN_fullcan_get_entry_ptr(can_std_id_t fc_id);
297 
306 bool CAN_fullcan_read_msg_copy(can_fullcan_msg_t *fc_msg_ptr, can_fullcan_msg_t *msg_copy_ptr);
307 
311 uint8_t CAN_fullcan_get_num_entries(void);
312 
358 bool CAN_setup_filter(const can_std_id_t *std_id_list, uint16_t sid_cnt,
359  const can_std_grp_id_t *std_group_id_list, uint16_t sgp_cnt,
360  const can_ext_id_t *ext_id_list, uint16_t eid_cnt,
361  const can_ext_grp_id_t *ext_group_id_list, uint16_t egp_cnt);
362 
363 
364 
365 #ifdef __cplusplus
366 }
367 #endif
368 #endif /* CAN_H__ */
uint16_t CAN_get_tx_count(can_t can)
Number of messages written to the CAN HW.
Definition: can.c:511
void CAN_reset_bus(can_t can)
Definition: can.c:488
can_std_id_t low
< CAN standard ID group
Definition: can.h:240
Definition: can.h:257
can_fullcan_msg_t * CAN_fullcan_get_entry_ptr(can_std_id_t fc_id)
Definition: can.c:598
uint16_t CAN_get_rx_count(can_t can)
Number of messages successfully queued from CAN interrupt (not including dropped) ...
Definition: can.c:516
CAN #1.
Definition: can.h:106
bool CAN_setup_filter(const can_std_id_t *std_id_list, uint16_t sid_cnt, const can_std_grp_id_t *std_group_id_list, uint16_t sgp_cnt, const can_ext_id_t *ext_id_list, uint16_t eid_cnt, const can_ext_grp_id_t *ext_group_id_list, uint16_t egp_cnt)
Definition: can.c:668
__attribute__
Definition: rtc.h:56
Do not use or change.
Definition: can.h:108
bool CAN_tx(can_t can, can_msg_t *msg, uint32_t timeout_ms)
Definition: can.c:414
bool CAN_is_bus_off(can_t can)
Definition: can.c:482
void(* can_void_func_t)(uint32_t)
Definition: can.h:115
can_ext_id_t CAN_gen_eid(can_t can, uint32_t id)
Definition: can.c:545
bool CAN_fullcan_read_msg_copy(can_fullcan_msg_t *fc_msg_ptr, can_fullcan_msg_t *msg_copy_ptr)
Definition: can.c:636
uint16_t CAN_get_rx_dropped_count(can_t can)
Definition: can.c:521
uint16_t CAN_get_tx_watermark(can_t can)
TX FreeRTOS Queue watermark.
Definition: can.c:506
can_ext_id_t low
< CAN extended ID group
Definition: can.h:258
Definition: can.h:58
can_std_id_t CAN_gen_sid(can_t can, uint16_t id)
Definition: can.c:531
Definition: can.h:239
can_std_id_t high
High range.
Definition: can.h:241
uint32_t msg_id
CAN Message ID (11-bit or 29-bit)
Definition: can.h:81
CAN #2.
Definition: can.h:107
uint64_t qword
All 64-bits of data of a CAN message.
Definition: can.h:59
bool CAN_rx(can_t can, can_msg_t *msg, uint32_t timeout_ms)
Definition: can.c:460
uint16_t CAN_get_rx_watermark(can_t can)
RX FreeRTOS Queue watermark.
Definition: can.c:501
bool CAN_init(can_t can, uint32_t baudrate_kbps, uint16_t rxq_size, uint16_t txq_size, can_void_func_t bus_off_cb, can_void_func_t data_ovr_cb)
Definition: can.c:285
can_t
The CAN BUS type.
Definition: can.h:105
uint16_t raw
Definition: can.h:232
uint32_t frame
32-bit CAN frame aligned with frame_fields (bit members)
Definition: can.h:71
bool CAN_fullcan_add_entry(can_t can, can_std_id_t id1, can_std_id_t id2)
Definition: can.c:557
can_data_t data
CAN data.
Definition: can.h:82
void CAN_bypass_filter_accept_all_msgs(void)
Definition: can.c:526
uint8_t CAN_fullcan_get_num_entries(void)
Definition: can.c:663
can_ext_id_t high
High range.
Definition: can.h:259