1 /**
2  * @file can.h
3  * @copyright Copyright (C) 2015-2018 Alibaba Group Holding Limited
4  */
5 
6 #ifndef HAL_CAN_H
7 #define HAL_CAN_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #include <stdint.h>
14 
15 /** @addtogroup hal_can CAN
16  *  CAN hal API.
17  *
18  *  @{
19  */
20 
21 /*
22  * CAN handle configuration
23  */
24 typedef struct {
25     uint32_t baud_rate;           /**< baud rate of can */
26     uint8_t  ide;                 /**< 0:normal can, 1:extend can */
27     uint8_t  auto_bus_off;        /**< 1:enable auto bus off, 0:disable */
28     uint8_t  auto_retry_transmit; /**< 1:enable retry transmit, 0:disable */
29 } can_config_t;
30 
31 /*
32  * CAN device description
33  */
34 typedef struct {
35     uint8_t      port;   /**< can port */
36     can_config_t config; /**< can config */
37     void        *priv;   /**< priv data */
38 } can_dev_t;
39 
40 /*
41  * CAN frameheader config
42  */
43 typedef struct {
44     uint32_t id;  /**< id of can */
45     uint8_t  rtr; /**< 0:data frame, 1:remote frame */
46     uint8_t  dlc; /**< must <=8 */
47 } can_frameheader_t;
48 
49 /*
50  * CAN filter_item config
51  */
52 typedef struct{
53     uint8_t  rtr;         /**< 0:data frame, 1:remote frame */
54     uint32_t check_id;    /**< the filter identification number */
55     uint32_t filter_mask; /**< the filter mask number or identification number */
56 } can_filter_item_t;
57 
58 /**
59  * Initialises a CAN interface
60  *
61  * @param[in]  can  the interface which should be initialised
62  *
63  * @return  0 : on success, EINVAL : if an error occurred with any step
64  */
65 int32_t hal_can_init(can_dev_t *can);
66 
67 /**
68  * config a CAN fliter
69  *
70  * @param[in]  can             the interface which should be initialised
71  * @param[in]  filter_grp_cnt  0 will make all id pass. This value must be <=10
72  * @param[in]  filter_config   point to a filter config
73  *
74  * @return  0 : on success, EIO : if an error occurred with any step
75  */
76 int32_t hal_can_filter_init(can_dev_t *can, const uint8_t filter_grp_cnt, can_filter_item_t *filter_config);
77 
78 /**
79  * Transmit data by CAN
80  *
81  * @param[in]  can        the can interface
82  * @param[in]  tx_header  frame head
83  * @param[in]  data       pointer to the start of data
84  * @param[in]  timeout    timeout in milisecond, set this value to HAL_WAIT_FOREVER
85  *                        if you want to wait forever
86  *
87  * @return  0 : on success, EIO : if an error occurred with any step
88  */
89 int32_t hal_can_send(can_dev_t *can, can_frameheader_t *tx_header, const void *data, const uint32_t timeout);
90 
91 /**
92  * Receive data by CAN
93  *
94  * @param[in]   can        the can interface
95  * @param[out]  rx_header  frame head
96  * @param[out]  data       pointer to the start of data
97  * @param[in]   timeout    timeout in milisecond, set this value to HAL_WAIT_FOREVER
98  *                         if you want to wait forever
99  *
100  * @return  0 : on success, EIO : if an error occurred with any step
101  */
102 int32_t hal_can_recv(can_dev_t *can, can_frameheader_t *rx_header, void *data, const uint32_t timeout);
103 
104 /**
105  * Deinitialises a CAN interface
106  *
107  * @param[in]  can  the interface which should be deinitialised
108  *
109  * @return  0 : on success, EIO : if an error occurred with any step
110  */
111 int32_t hal_can_finalize(can_dev_t *can);
112 
113 /** @} */
114 
115 #ifdef __cplusplus
116 }
117 #endif
118 
119 #endif /* HAL_CAN_H */
120 
121