1 /**
2  * @file can.h
3  * @copyright Copyright (C) 2015-2018 Alibaba Group Holding Limited
4  */
5 
6 #ifndef AOS_HAL_CAN_H
7 #define AOS_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 #define CAN_BAUD_1M   1000000
22 #define CAN_BAUD_500K 500000
23 #define CAN_BAUD_250K 250000
24 #define CAN_BAUD_125K 125000
25 
26 #define CAN_IDE_NORMAL  0
27 #define CAN_IDE_EXTEND  1
28 
29 #define CAN_AUTO_BUS_OFF_DISABLE 0
30 #define CAN_AUTO_BUS_OFF_ENABLE  1
31 
32 #define CAN_AUTO_RETRY_TRANSMIT_DISABLE 0
33 #define CAN_AUTO_RETRY_TRANSMIT_ENABLE  1
34 
35 #if !defined(HAL_CAN_H)
36 /*
37  * CAN handle configuration
38  */
39 typedef struct {
40     uint32_t baud_rate;           /**< baud rate of can */
41     uint8_t  ide;                 /**< 0:normal can, 1:extend can */
42     uint8_t  auto_bus_off;        /**< 1:enable auto bus off, 0:disable */
43     uint8_t  auto_retry_transmit; /**< 1:enable retry transmit, 0:disable */
44 } can_config_t;
45 
46 /*
47  * CAN device description
48  */
49 typedef struct {
50     uint8_t      port;   /**< can port */
51     can_config_t config; /**< can config */
52     void        *priv;   /**< priv data */
53 } can_dev_t;
54 
55 /*
56  * CAN frameheader config
57  */
58 typedef struct {
59     uint32_t id;  /**< id of can */
60     uint8_t  rtr; /**< 0:data frame, 1:remote frame */
61     uint8_t  dlc; /**< must <=8 */
62 } can_frameheader_t;
63 
64 /*
65  * CAN filter_item config
66  */
67 typedef struct{
68     uint8_t  rtr;         /**< 0:data frame, 1:remote frame */
69     uint32_t check_id;    /**< the filter identification number */
70     uint32_t filter_mask; /**< the filter mask number or identification number */
71 } can_filter_item_t;
72 #endif
73 
74 /**
75  * Initialises a CAN interface
76  *
77  * @param[in]  can  the interface which should be initialised
78  *
79  * @return  0 : on success, EINVAL : if an error occurred with any step
80  */
81 int32_t aos_hal_can_init(can_dev_t *can);
82 
83 /**
84  * config a CAN fliter
85  *
86  * @param[in]  can             the interface which should be initialised
87  * @param[in]  filter_grp_cnt  0 will make all id pass. This value must be <=10
88  * @param[in]  filter_config   point to a filter config
89  *
90  * @return  0 : on success, EIO : if an error occurred with any step
91  */
92 int32_t aos_hal_can_filter_init(can_dev_t *can, const uint8_t filter_grp_cnt, can_filter_item_t *filter_config);
93 
94 /**
95  * Transmit data by CAN
96  *
97  * @param[in]  can        the can interface
98  * @param[in]  tx_header  frame head
99  * @param[in]  data       pointer to the start of data
100  * @param[in]  timeout    timeout in milisecond, set this value to HAL_WAIT_FOREVER
101  *                        if you want to wait forever
102  *
103  * @return  0 : on success, EIO : if an error occurred with any step
104  */
105 int32_t aos_hal_can_send(can_dev_t *can, can_frameheader_t *tx_header, const void *data, const uint32_t timeout);
106 
107 /**
108  * Receive data by CAN
109  *
110  * @param[in]   can        the can interface
111  * @param[out]  rx_header  frame head
112  * @param[out]  data       pointer to the start of data
113  * @param[in]   timeout    timeout in milisecond, set this value to HAL_WAIT_FOREVER
114  *                         if you want to wait forever
115  *
116  * @return  0 : on success, EIO : if an error occurred with any step
117  */
118 int32_t aos_hal_can_recv(can_dev_t *can, can_frameheader_t *rx_header, void *data, const uint32_t timeout);
119 
120 /**
121  * Deinitialises a CAN interface
122  *
123  * @param[in]  can  the interface which should be deinitialised
124  *
125  * @return  0 : on success, EIO : if an error occurred with any step
126  */
127 int32_t aos_hal_can_finalize(can_dev_t *can);
128 
129 /** @} */
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif /* AOS_HAL_CAN_H */
136 
137