1 /********************************** (C) COPYRIGHT  *******************************
2 * File Name          : ch32f20x_can.h
3 * Author             : WCH
4 * Version            : V1.0.0
5 * Date               : 2021/08/08
6 * Description        : This file contains all the functions prototypes for the
7 *                      CAN firmware library.
8 *******************************************************************************/
9 #ifndef __CH32F20x_CAN_H
10 #define __CH32F20x_CAN_H
11 
12 #ifdef __cplusplus
13  extern "C" {
14 #endif
15 
16 #include "ch32f20x.h"
17 
18 /* CAN init structure definition */
19 typedef struct
20 {
21   uint16_t CAN_Prescaler;   /* Specifies the length of a time quantum.
22                                It ranges from 1 to 1024. */
23 
24   uint8_t CAN_Mode;         /* Specifies the CAN operating mode.
25                                This parameter can be a value of
26                               @ref CAN_operating_mode */
27 
28   uint8_t CAN_SJW;          /* Specifies the maximum number of time quanta
29                                the CAN hardware is allowed to lengthen or
30                                shorten a bit to perform resynchronization.
31                                This parameter can be a value of
32                                @ref CAN_synchronisation_jump_width */
33 
34   uint8_t CAN_BS1;          /* Specifies the number of time quanta in Bit
35                                Segment 1. This parameter can be a value of
36                                @ref CAN_time_quantum_in_bit_segment_1 */
37 
38   uint8_t CAN_BS2;          /* Specifies the number of time quanta in Bit
39                                Segment 2.
40                                This parameter can be a value of
41                                @ref CAN_time_quantum_in_bit_segment_2 */
42 
43   FunctionalState CAN_TTCM; /* Enable or disable the time triggered
44                                communication mode. This parameter can be set
45                                either to ENABLE or DISABLE. */
46 
47   FunctionalState CAN_ABOM;  /* Enable or disable the automatic bus-off
48                                 management. This parameter can be set either
49                                 to ENABLE or DISABLE. */
50 
51   FunctionalState CAN_AWUM;  /* Enable or disable the automatic wake-up mode.
52                                 This parameter can be set either to ENABLE or
53                                 DISABLE. */
54 
55   FunctionalState CAN_NART;  /* Enable or disable the no-automatic
56                                 retransmission mode. This parameter can be
57                                 set either to ENABLE or DISABLE. */
58 
59   FunctionalState CAN_RFLM;  /* Enable or disable the Receive FIFO Locked mode.
60                                 This parameter can be set either to ENABLE
61                                 or DISABLE. */
62 
63   FunctionalState CAN_TXFP;  /* Enable or disable the transmit FIFO priority.
64                                 This parameter can be set either to ENABLE
65                                 or DISABLE. */
66 } CAN_InitTypeDef;
67 
68 /* CAN filter init structure definition */
69 typedef struct
70 {
71   uint16_t CAN_FilterIdHigh;         /* Specifies the filter identification number (MSBs for a 32-bit
72                                             configuration, first one for a 16-bit configuration).
73                                             This parameter can be a value between 0x0000 and 0xFFFF */
74 
75   uint16_t CAN_FilterIdLow;          /* Specifies the filter identification number (LSBs for a 32-bit
76                                             configuration, second one for a 16-bit configuration).
77                                             This parameter can be a value between 0x0000 and 0xFFFF */
78 
79   uint16_t CAN_FilterMaskIdHigh;     /* Specifies the filter mask number or identification number,
80                                             according to the mode (MSBs for a 32-bit configuration,
81                                             first one for a 16-bit configuration).
82                                             This parameter can be a value between 0x0000 and 0xFFFF */
83 
84   uint16_t CAN_FilterMaskIdLow;      /* Specifies the filter mask number or identification number,
85                                             according to the mode (LSBs for a 32-bit configuration,
86                                             second one for a 16-bit configuration).
87                                             This parameter can be a value between 0x0000 and 0xFFFF */
88 
89   uint16_t CAN_FilterFIFOAssignment; /* Specifies the FIFO (0 or 1) which will be assigned to the filter.
90                                             This parameter can be a value of @ref CAN_filter_FIFO */
91 
92   uint8_t CAN_FilterNumber;          /* Specifies the filter which will be initialized. It ranges from 0 to 13. */
93 
94   uint8_t CAN_FilterMode;            /* Specifies the filter mode to be initialized.
95                                             This parameter can be a value of @ref CAN_filter_mode */
96 
97   uint8_t CAN_FilterScale;           /* Specifies the filter scale.
98                                             This parameter can be a value of @ref CAN_filter_scale */
99 
100   FunctionalState CAN_FilterActivation; /* Enable or disable the filter.
101                                             This parameter can be set either to ENABLE or DISABLE. */
102 } CAN_FilterInitTypeDef;
103 
104 /* CAN Tx message structure definition */
105 typedef struct
106 {
107   uint32_t StdId;  /* Specifies the standard identifier.
108                       This parameter can be a value between 0 to 0x7FF. */
109 
110   uint32_t ExtId;  /* Specifies the extended identifier.
111                       This parameter can be a value between 0 to 0x1FFFFFFF. */
112 
113   uint8_t IDE;     /* Specifies the type of identifier for the message that
114                       will be transmitted. This parameter can be a value
115                       of @ref CAN_identifier_type */
116 
117   uint8_t RTR;     /* Specifies the type of frame for the message that will
118                       be transmitted. This parameter can be a value of
119                       @ref CAN_remote_transmission_request */
120 
121   uint8_t DLC;     /* Specifies the length of the frame that will be
122                       transmitted. This parameter can be a value between
123                       0 to 8 */
124 
125   uint8_t Data[8]; /* Contains the data to be transmitted. It ranges from 0
126                        to 0xFF. */
127 } CanTxMsg;
128 
129 /* CAN Rx message structure definition  */
130 typedef struct
131 {
132   uint32_t StdId;  /* Specifies the standard identifier.
133                       This parameter can be a value between 0 to 0x7FF. */
134 
135   uint32_t ExtId;  /* Specifies the extended identifier.
136                       This parameter can be a value between 0 to 0x1FFFFFFF. */
137 
138   uint8_t IDE;     /* Specifies the type of identifier for the message that
139                       will be received. This parameter can be a value of
140                       @ref CAN_identifier_type */
141 
142   uint8_t RTR;     /* Specifies the type of frame for the received message.
143                       This parameter can be a value of
144                       @ref CAN_remote_transmission_request */
145 
146   uint8_t DLC;     /* Specifies the length of the frame that will be received.
147                       This parameter can be a value between 0 to 8 */
148 
149   uint8_t Data[8]; /* Contains the data to be received. It ranges from 0 to
150                       0xFF. */
151 
152   uint8_t FMI;     /* Specifies the index of the filter the message stored in
153                       the mailbox passes through. This parameter can be a
154                       value between 0 to 0xFF */
155 } CanRxMsg;
156 
157 /* CAN_sleep_constants */
158 #define CAN_InitStatus_Failed              ((uint8_t)0x00) /* CAN initialization failed */
159 #define CAN_InitStatus_Success             ((uint8_t)0x01) /* CAN initialization OK */
160 
161 /* CAN_Mode */
162 #define CAN_Mode_Normal                    ((uint8_t)0x00)  /* normal mode */
163 #define CAN_Mode_LoopBack                  ((uint8_t)0x01)  /* loopback mode */
164 #define CAN_Mode_Silent                    ((uint8_t)0x02)  /* silent mode */
165 #define CAN_Mode_Silent_LoopBack           ((uint8_t)0x03)  /* loopback combined with silent mode */
166 
167 /* CAN_Operating_Mode */
168 #define CAN_OperatingMode_Initialization   ((uint8_t)0x00) /* Initialization mode */
169 #define CAN_OperatingMode_Normal           ((uint8_t)0x01) /* Normal mode */
170 #define CAN_OperatingMode_Sleep            ((uint8_t)0x02) /* sleep mode */
171 
172 /* CAN_Mode_Status */
173 #define CAN_ModeStatus_Failed              ((uint8_t)0x00)                /* CAN entering the specific mode failed */
174 #define CAN_ModeStatus_Success             ((uint8_t)!CAN_ModeStatus_Failed)   /* CAN entering the specific mode Succeed */
175 
176 /* CAN_synchronisation_jump_width */
177 #define CAN_SJW_1tq                        ((uint8_t)0x00)  /* 1 time quantum */
178 #define CAN_SJW_2tq                        ((uint8_t)0x01)  /* 2 time quantum */
179 #define CAN_SJW_3tq                        ((uint8_t)0x02)  /* 3 time quantum */
180 #define CAN_SJW_4tq                        ((uint8_t)0x03)  /* 4 time quantum */
181 
182 /* CAN_time_quantum_in_bit_segment_1 */
183 #define CAN_BS1_1tq                        ((uint8_t)0x00)  /* 1 time quantum */
184 #define CAN_BS1_2tq                        ((uint8_t)0x01)  /* 2 time quantum */
185 #define CAN_BS1_3tq                        ((uint8_t)0x02)  /* 3 time quantum */
186 #define CAN_BS1_4tq                        ((uint8_t)0x03)  /* 4 time quantum */
187 #define CAN_BS1_5tq                        ((uint8_t)0x04)  /* 5 time quantum */
188 #define CAN_BS1_6tq                        ((uint8_t)0x05)  /* 6 time quantum */
189 #define CAN_BS1_7tq                        ((uint8_t)0x06)  /* 7 time quantum */
190 #define CAN_BS1_8tq                        ((uint8_t)0x07)  /* 8 time quantum */
191 #define CAN_BS1_9tq                        ((uint8_t)0x08)  /* 9 time quantum */
192 #define CAN_BS1_10tq                       ((uint8_t)0x09)  /* 10 time quantum */
193 #define CAN_BS1_11tq                       ((uint8_t)0x0A)  /* 11 time quantum */
194 #define CAN_BS1_12tq                       ((uint8_t)0x0B)  /* 12 time quantum */
195 #define CAN_BS1_13tq                       ((uint8_t)0x0C)  /* 13 time quantum */
196 #define CAN_BS1_14tq                       ((uint8_t)0x0D)  /* 14 time quantum */
197 #define CAN_BS1_15tq                       ((uint8_t)0x0E)  /* 15 time quantum */
198 #define CAN_BS1_16tq                       ((uint8_t)0x0F)  /* 16 time quantum */
199 
200 /* CAN_time_quantum_in_bit_segment_2 */
201 #define CAN_BS2_1tq                        ((uint8_t)0x00)  /* 1 time quantum */
202 #define CAN_BS2_2tq                        ((uint8_t)0x01)  /* 2 time quantum */
203 #define CAN_BS2_3tq                        ((uint8_t)0x02)  /* 3 time quantum */
204 #define CAN_BS2_4tq                        ((uint8_t)0x03)  /* 4 time quantum */
205 #define CAN_BS2_5tq                        ((uint8_t)0x04)  /* 5 time quantum */
206 #define CAN_BS2_6tq                        ((uint8_t)0x05)  /* 6 time quantum */
207 #define CAN_BS2_7tq                        ((uint8_t)0x06)  /* 7 time quantum */
208 #define CAN_BS2_8tq                        ((uint8_t)0x07)  /* 8 time quantum */
209 
210 /* CAN_filter_mode */
211 #define CAN_FilterMode_IdMask              ((uint8_t)0x00)  /* identifier/mask mode */
212 #define CAN_FilterMode_IdList              ((uint8_t)0x01)  /* identifier list mode */
213 
214 /* CAN_filter_scale */
215 #define CAN_FilterScale_16bit              ((uint8_t)0x00) /* Two 16-bit filters */
216 #define CAN_FilterScale_32bit              ((uint8_t)0x01) /* One 32-bit filter */
217 
218 /* CAN_filter_FIFO */
219 #define CAN_Filter_FIFO0                   ((uint8_t)0x00)  /* Filter FIFO 0 assignment for filter x */
220 #define CAN_Filter_FIFO1                   ((uint8_t)0x01)  /* Filter FIFO 1 assignment for filter x */
221 
222 /* CAN_identifier_type */
223 #define CAN_Id_Standard                    ((uint32_t)0x00000000)  /* Standard Id */
224 #define CAN_Id_Extended                    ((uint32_t)0x00000004)  /* Extended Id */
225 
226 /* CAN_remote_transmission_request */
227 #define CAN_RTR_Data                       ((uint32_t)0x00000000)  /* Data frame */
228 #define CAN_RTR_Remote                     ((uint32_t)0x00000002)  /* Remote frame */
229 
230 /* CAN_transmit_constants */
231 #define CAN_TxStatus_Failed                ((uint8_t)0x00)/* CAN transmission failed */
232 #define CAN_TxStatus_Ok                    ((uint8_t)0x01) /* CAN transmission succeeded */
233 #define CAN_TxStatus_Pending               ((uint8_t)0x02) /* CAN transmission pending */
234 #define CAN_TxStatus_NoMailBox             ((uint8_t)0x04) /* CAN cell did not provide an empty mailbox */
235 
236 /* CAN_receive_FIFO_number_constants */
237 #define CAN_FIFO0                          ((uint8_t)0x00) /* CAN FIFO 0 used to receive */
238 #define CAN_FIFO1                          ((uint8_t)0x01) /* CAN FIFO 1 used to receive */
239 
240 /* CAN_sleep_constants */
241 #define CAN_Sleep_Failed                   ((uint8_t)0x00) /* CAN did not enter the sleep mode */
242 #define CAN_Sleep_Ok                       ((uint8_t)0x01) /* CAN entered the sleep mode */
243 
244 /* CAN_wake_up_constants */
245 #define CAN_WakeUp_Failed                  ((uint8_t)0x00) /* CAN did not leave the sleep mode */
246 #define CAN_WakeUp_Ok                      ((uint8_t)0x01) /* CAN leaved the sleep mode */
247 
248 /* CAN_Error_Code_constants */
249 #define CAN_ErrorCode_NoErr                ((uint8_t)0x00) /* No Error */
250 #define	CAN_ErrorCode_StuffErr             ((uint8_t)0x10) /* Stuff Error */
251 #define	CAN_ErrorCode_FormErr              ((uint8_t)0x20) /* Form Error */
252 #define	CAN_ErrorCode_ACKErr               ((uint8_t)0x30) /* Acknowledgment Error */
253 #define	CAN_ErrorCode_BitRecessiveErr      ((uint8_t)0x40) /* Bit Recessive Error */
254 #define	CAN_ErrorCode_BitDominantErr       ((uint8_t)0x50) /* Bit Dominant Error */
255 #define	CAN_ErrorCode_CRCErr               ((uint8_t)0x60) /* CRC Error  */
256 #define	CAN_ErrorCode_SoftwareSetErr       ((uint8_t)0x70) /* Software Set Error */
257 
258 
259 /* CAN_flags */
260 /* Transmit Flags */
261 #define CAN_FLAG_RQCP0                     ((uint32_t)0x38000001) /* Request MailBox0 Flag */
262 #define CAN_FLAG_RQCP1                     ((uint32_t)0x38000100) /* Request MailBox1 Flag */
263 #define CAN_FLAG_RQCP2                     ((uint32_t)0x38010000) /* Request MailBox2 Flag */
264 
265 /* Receive Flags */
266 #define CAN_FLAG_FMP0                      ((uint32_t)0x12000003) /* FIFO 0 Message Pending Flag */
267 #define CAN_FLAG_FF0                       ((uint32_t)0x32000008) /* FIFO 0 Full Flag            */
268 #define CAN_FLAG_FOV0                      ((uint32_t)0x32000010) /* FIFO 0 Overrun Flag         */
269 #define CAN_FLAG_FMP1                      ((uint32_t)0x14000003) /* FIFO 1 Message Pending Flag */
270 #define CAN_FLAG_FF1                       ((uint32_t)0x34000008) /* FIFO 1 Full Flag            */
271 #define CAN_FLAG_FOV1                      ((uint32_t)0x34000010) /* FIFO 1 Overrun Flag         */
272 
273 /* Operating Mode Flags */
274 #define CAN_FLAG_WKU                       ((uint32_t)0x31000008) /* Wake up Flag */
275 #define CAN_FLAG_SLAK                      ((uint32_t)0x31000012) /* Sleep acknowledge Flag */
276 
277 /* Error Flags */
278 #define CAN_FLAG_EWG                       ((uint32_t)0x10F00001) /* Error Warning Flag   */
279 #define CAN_FLAG_EPV                       ((uint32_t)0x10F00002) /* Error Passive Flag   */
280 #define CAN_FLAG_BOF                       ((uint32_t)0x10F00004) /* Bus-Off Flag         */
281 #define CAN_FLAG_LEC                       ((uint32_t)0x30F00070) /* Last error code Flag */
282 
283 
284 /* CAN_interrupts */
285 #define CAN_IT_TME                         ((uint32_t)0x00000001) /* Transmit mailbox empty Interrupt*/
286 
287 /* Receive Interrupts */
288 #define CAN_IT_FMP0                        ((uint32_t)0x00000002) /* FIFO 0 message pending Interrupt*/
289 #define CAN_IT_FF0                         ((uint32_t)0x00000004) /* FIFO 0 full Interrupt*/
290 #define CAN_IT_FOV0                        ((uint32_t)0x00000008) /* FIFO 0 overrun Interrupt*/
291 #define CAN_IT_FMP1                        ((uint32_t)0x00000010) /* FIFO 1 message pending Interrupt*/
292 #define CAN_IT_FF1                         ((uint32_t)0x00000020) /* FIFO 1 full Interrupt*/
293 #define CAN_IT_FOV1                        ((uint32_t)0x00000040) /* FIFO 1 overrun Interrupt*/
294 
295 /* Operating Mode Interrupts */
296 #define CAN_IT_WKU                         ((uint32_t)0x00010000) /* Wake-up Interrupt*/
297 #define CAN_IT_SLK                         ((uint32_t)0x00020000) /* Sleep acknowledge Interrupt*/
298 
299 /* Error Interrupts */
300 #define CAN_IT_EWG                         ((uint32_t)0x00000100) /* Error warning Interrupt*/
301 #define CAN_IT_EPV                         ((uint32_t)0x00000200) /* Error passive Interrupt*/
302 #define CAN_IT_BOF                         ((uint32_t)0x00000400) /* Bus-off Interrupt*/
303 #define CAN_IT_LEC                         ((uint32_t)0x00000800) /* Last error code Interrupt*/
304 #define CAN_IT_ERR                         ((uint32_t)0x00008000) /* Error Interrupt*/
305 
306 /* Flags named as Interrupts : kept only for FW compatibility */
307 #define CAN_IT_RQCP0    CAN_IT_TME
308 #define CAN_IT_RQCP1    CAN_IT_TME
309 #define CAN_IT_RQCP2    CAN_IT_TME
310 
311 /* CAN_Legacy */
312 #define CANINITFAILED               CAN_InitStatus_Failed
313 #define CANINITOK                   CAN_InitStatus_Success
314 #define CAN_FilterFIFO0             CAN_Filter_FIFO0
315 #define CAN_FilterFIFO1             CAN_Filter_FIFO1
316 #define CAN_ID_STD                  CAN_Id_Standard
317 #define CAN_ID_EXT                  CAN_Id_Extended
318 #define CAN_RTR_DATA                CAN_RTR_Data
319 #define CAN_RTR_REMOTE              CAN_RTR_Remote
320 #define CANTXFAILE                  CAN_TxStatus_Failed
321 #define CANTXOK                     CAN_TxStatus_Ok
322 #define CANTXPENDING                CAN_TxStatus_Pending
323 #define CAN_NO_MB                   CAN_TxStatus_NoMailBox
324 #define CANSLEEPFAILED              CAN_Sleep_Failed
325 #define CANSLEEPOK                  CAN_Sleep_Ok
326 #define CANWAKEUPFAILED             CAN_WakeUp_Failed
327 #define CANWAKEUPOK                 CAN_WakeUp_Ok
328 
329 
330 void CAN_DeInit(CAN_TypeDef* CANx);
331 uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct);
332 void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct);
333 void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct);
334 void CAN_SlaveStartBank(uint8_t CAN_BankNumber);
335 void CAN_DBGFreeze(CAN_TypeDef* CANx, FunctionalState NewState);
336 void CAN_TTComModeCmd(CAN_TypeDef* CANx, FunctionalState NewState);
337 uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage);
338 uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox);
339 void CAN_CancelTransmit(CAN_TypeDef* CANx, uint8_t Mailbox);
340 void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage);
341 void CAN_FIFORelease(CAN_TypeDef* CANx, uint8_t FIFONumber);
342 uint8_t CAN_MessagePending(CAN_TypeDef* CANx, uint8_t FIFONumber);
343 uint8_t CAN_OperatingModeRequest(CAN_TypeDef* CANx, uint8_t CAN_OperatingMode);
344 uint8_t CAN_Sleep(CAN_TypeDef* CANx);
345 uint8_t CAN_WakeUp(CAN_TypeDef* CANx);
346 uint8_t CAN_GetLastErrorCode(CAN_TypeDef* CANx);
347 uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef* CANx);
348 uint8_t CAN_GetLSBTransmitErrorCounter(CAN_TypeDef* CANx);
349 void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState);
350 FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG);
351 void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG);
352 ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT);
353 void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT);
354 
355 #ifdef __cplusplus
356 }
357 #endif
358 
359 #endif
360 
361 
362 
363 
364 
365