blob: 72fa39b9d4d1db1591800b6a0ed1de6e12370d57 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
/*
******************************************************************************
* @file HAL_Can.h
* @version V1.0.0
* @date 2020
* @brief Header file of CAN HAL module.
******************************************************************************
*/
#ifndef __HAL_CAN_H__
#define __HAL_CAN_H__
#include "ACM32Fxx_HAL.h"
/**
* @}
*/
/******************************************************************************/
/* Peripheral Registers Bits Definition */
/******************************************************************************/
/******************************************************************************/
/* (CAN) */
/******************************************************************************/
/**************** Bit definition for CAN MOD register ***********************/
#define CAN_MOD_RM BIT0
#define CAN_MOD_LOM BIT1
#define CAN_MOD_STM BIT2
#define CAN_MOD_AFM BIT3
#define CAN_MOD_SM BIT4
/**************** Bit definition for CAN CMR register ***********************/
#define CAN_CMR_TR BIT0
#define CAN_CMR_AT BIT1
#define CAN_CMR_RRB BIT2
#define CAN_CMR_CDO BIT3
#define CAN_CMR_SRR BIT4
/**************** Bit definition for CAN SR register ***********************/
#define CAN_SR_RBS BIT0
#define CAN_SR_DOS BIT1
#define CAN_SR_TBS BIT2
#define CAN_SR_TCS BIT3
#define CAN_SR_RS BIT4
#define CAN_SR_TS BIT5
#define CAN_SR_ES BIT6
#define CAN_SR_BS BIT7
/**************** Bit definition for CAN IR register ***********************/
#define CAN_IR_RI BIT0
#define CAN_IR_TI BIT1
#define CAN_IR_EI BIT2
#define CAN_IR_DOI BIT3
#define CAN_IR_WUI BIT4
#define CAN_IR_EPI BIT5
#define CAN_IR_ALI BIT6
#define CAN_IR_BEI BIT7
/**************** Bit definition for CAN IER register ***********************/
#define CAN_IER_RIE BIT0
#define CAN_IER_TIE BIT1
#define CAN_IER_EIE BIT2
#define CAN_IER_DOIE BIT3
#define CAN_IER_WUIE BIT4
#define CAN_IER_EPIE BIT5
#define CAN_IER_ALIE BIT6
#define CAN_IER_BEIE BIT7
/**
* @brief CAN init structure definition
*/
typedef struct
{
uint32_t CAN_Mode; /*!< Specifies the CAN operating mode.
This parameter can be a value of
@ref CAN_mode e.g:CAN_Mode_Normal CAN_Mode_Normal*/
uint32_t CAN_SJW; /*!< Specifies the maximum number of time quanta
the CAN hardware is allowed to lengthen or
shorten a bit to perform resynchronization.
This parameter can be a value of
@ref CAN_SJW e.g:CAN_SJW_1tq--CAN_SJW_4tq*/
uint32_t CAN_BRP ; /*!< Specifies the number of time quanta in Bit
Segment 1. This parameter can be a value between 0 and 63 */
uint32_t CAN_TSEG1; /*!< Specifies the number of time quanta in Bit
Segment 1. This parameter can be a value of
@ref CAN_TSEG1 e.g: CAN_TSEG1_1tq-CAN_TSEG1_16tq*/
uint32_t CAN_TSEG2; /*!< Specifies the number of time quanta in Bit
Segment 2.This parameter can be a value of
@ref CAN_TSEG2 e.g:CAN_TSEG1_1tq-CAN_TSEG16_tq*/
uint32_t CAN_SAM ; /*!< Specifies the CAN operating mode.
This parameter can be a value of
@ref CAN_SAM e.g:CAN_SAM_1time CAN_SAM_3time*/
} CAN_InitTypeDef;
/**
* @brief CAN filter init structure definition
*/
typedef struct
{
uint32_t CAN_FilterMode; /*!< Specifies the filter mode to be initialized.
This parameter can be a value of
@ref CAN_filter_mode e.g:CAN_FilterMode_Dual CAN_FilterMode_Single*/
uint32_t CAN_FilterId1; /*!< Specifies the filter identification number
This parameter can be a value between 0x0000 and 0x1FFFFFFF */
uint32_t CAN_FilterId2; /*!< Specifies the filter identification number
only CAN_FilterMode=CAN_FilterMode_Dual Enable
This parameter can be a value between 0x0000 and 0x1FFFFFFF */
uint32_t CAN_FilterMaskId1; /*!< Specifies the filter identification mask number
This parameter can be a value between 0x0000 and 0x1FFFFFFF */
uint32_t CAN_FilterMaskId2; /*!< Specifies the filter identification mask number
only CAN_FilterMode=CAN_FilterMode_Dual Enable
This parameter can be a value between 0x0000 and 0x1FFFFFFF */
} CAN_FilterInitTypeDef;
/**
* @brief CAN RxTxMessege structure definition
*/
typedef struct
{
uint32_t StdId; /*!< Specifies the standard identifier.
This parameter can be a value between 0 to 0x7FF. */
uint32_t ExtId; /*!< Specifies the extended identifier.
This parameter can be a value between 0 to 0x1FFFFFFF. */
uint32_t IDE; /*!< Specifies the type of identifier for the message that
will be transmitted. This parameter can be a value
of @ref CAN_identifier_type e.g: CAN_Id_Standard CAN_Id_Extended*/
uint32_t RTR; /*!< Specifies the type of frame for the message that will
be transmitted. This parameter can be a value of
@ref CAN_remote_transmission e.g: CAN_RTR_Data CAN_RTR_Remote */
uint32_t DLC; /*!< Specifies the length of the frame that will be
transmitted. This parameter can be a value between 0 to 8 */
uint8_t Data[8]; /*!< Contains the data to be transmitted. It ranges from 0 to 0xFF. */
} CanTxRxMsg;
/**
* @brief CAN handle Structure definition
*/
typedef struct __CAN_HandleTypeDef
{
CAN_TypeDef *Instance; /*!< Register base address */
CAN_InitTypeDef Init; /*!< CAN required parameters */
CanTxRxMsg *RxMessage; /*!< CAN RxMessage */
void (*CAN_ReceiveIT_Callback)(struct __CAN_HandleTypeDef *hcan); /* CAN ReceiveIT complete callback */
void (*CAN_TransmitIT_Callback)(struct __CAN_HandleTypeDef *hcan); /* CAN TransmitIT complete callback */
} CAN_HandleTypeDef;
typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
#define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE))
#define IS_CAN_ALL_PERIPH(PERIPH) (((PERIPH) == CAN1))
/** @defgroup CAN_identifier_type
* @{
*/
#define CAN_Id_Standard ((uint32_t)0x00000000) /*!< Standard Id */
#define CAN_Id_Extended ((uint32_t)0x00000001) /*!< Extended Id */
#define IS_CAN_IDTYPE(IDTYPE) (((IDTYPE) == CAN_Id_Standard) || \
((IDTYPE) == CAN_Id_Extended))
/**
* @}
*/
/** @defgroup CAN_remote_transmission
* @{
*/
#define CAN_RTR_Data ((uint32_t)0x00000000) /*!< Data frame */
#define CAN_RTR_Remote ((uint32_t)0x00000001) /*!< Remote frame */
#define IS_CAN_RTR(RTR) (((RTR) == CAN_RTR_Data) || ((RTR) == CAN_RTR_Remote))
/**
* @}
*/
/** @defgroup CAN_TxRxMessege
* @{
*/
#define IS_CAN_STDID(STDID) ((STDID) <= ((uint32_t)0x7FF))
#define IS_CAN_EXTID(EXTID) ((EXTID) <= ((uint32_t)0x1FFFFFFF))
#define IS_CAN_DLC(DLC) ((DLC) <= ((uint8_t)0x08))
#define IS_CAN_GET_FLAG(FLAG) (((FLAG) == CAN_SR_RBS) || ((FLAG) == CAN_SR_DOS) || \
((FLAG) == CAN_SR_TBS) || ((FLAG) == CAN_SR_TCS) || \
((FLAG) == CAN_SR_RS) || ((FLAG) == CAN_SR_TS) || \
((FLAG) == CAN_SR_ES) || ((FLAG) == CAN_SR_BS))
#define IS_CAN_BRP(BRP) (((BRP) <= 63))
/**
* @defgroup CAN_Mode
* @{
*/
#define CAN_Mode_Normal ((uint8_t)0x00) /*!< Normal mode */
#define CAN_Mode_SlefTest ((uint8_t)0x01) /*!< SlefTest mode */
#define IS_CAN_MODE(MODE) (((MODE) == CAN_Mode_Normal) ||\
((MODE) == CAN_Mode_SlefTest))
/**
* @}
*/
/**
* @defgroup CAN_Operating_Mode
* @{
*/
#define CAN_OperatingMode_Normal ((uint8_t)0x00) /*!< Initialization mode */
#define CAN_OperatingMode_Initialization ((uint8_t)0x01) /*!< Normal mode */
#define CAN_OperatingMode_Listen ((uint8_t)0x02) /*!< Listen mode */
#define CAN_OperatingMode_SelfTest ((uint8_t)0x04) /*!< Listen mode */
#define CAN_OperatingMode_Sleep ((uint8_t)0x10) /*!< sleep mode */
#define IS_CAN_OPERATING_MODE(MODE) (((MODE) == CAN_OperatingMode_Initialization) ||\
((MODE) == CAN_OperatingMode_Normal)|| \
((MODE) == CAN_OperatingMode_Sleep)|| \
((MODE) == CAN_OperatingMode_SelfTest)|| \
((MODE) == CAN_OperatingMode_Listen))
/**
* @}
*/
/** @defgroup CAN_SAM
* @{
*/
#define CAN_SAM_1time ((uint8_t)0x00) /*!< 1 time quantum */
#define CAN_SAM_3time ((uint8_t)0x01) /*!< 2 time quantum */
#define IS_CAN_SAM(SAM) (((SAM) == CAN_SAM_1time) || ((SAM) == CAN_SAM_3time))
/**
* @}
*/
/** @defgroup CAN_synchronisation_jump_width
* @{
*/
#define CAN_SJW_1tq ((uint8_t)0x00) /*!< 1 time quantum */
#define CAN_SJW_2tq ((uint8_t)0x01) /*!< 2 time quantum */
#define CAN_SJW_3tq ((uint8_t)0x02) /*!< 3 time quantum */
#define CAN_SJW_4tq ((uint8_t)0x03) /*!< 4 time quantum */
#define IS_CAN_SJW(SJW) (((SJW) == CAN_SJW_1tq) || ((SJW) == CAN_SJW_2tq)|| \
((SJW) == CAN_SJW_3tq) || ((SJW) == CAN_SJW_4tq))
/**
* @}
*/
/** @defgroup CAN_time_quantum_in_bit_segment_1
* @{
*/
#define CAN_TSEG1_1tq ((uint8_t)0x00) /*!< 1 time quantum */
#define CAN_TSEG1_2tq ((uint8_t)0x01) /*!< 2 time quantum */
#define CAN_TSEG1_3tq ((uint8_t)0x02) /*!< 3 time quantum */
#define CAN_TSEG1_4tq ((uint8_t)0x03) /*!< 4 time quantum */
#define CAN_TSEG1_5tq ((uint8_t)0x04) /*!< 5 time quantum */
#define CAN_TSEG1_6tq ((uint8_t)0x05) /*!< 6 time quantum */
#define CAN_TSEG1_7tq ((uint8_t)0x06) /*!< 7 time quantum */
#define CAN_TSEG1_8tq ((uint8_t)0x07) /*!< 8 time quantum */
#define CAN_TSEG1_9tq ((uint8_t)0x08) /*!< 9 time quantum */
#define CAN_TSEG1_10tq ((uint8_t)0x09) /*!< 10 time quantum */
#define CAN_TSEG1_11tq ((uint8_t)0x0A) /*!< 11 time quantum */
#define CAN_TSEG1_12tq ((uint8_t)0x0B) /*!< 12 time quantum */
#define CAN_TSEG1_13tq ((uint8_t)0x0C) /*!< 13 time quantum */
#define CAN_TSEG1_14tq ((uint8_t)0x0D) /*!< 14 time quantum */
#define CAN_TSEG1_15tq ((uint8_t)0x0E) /*!< 15 time quantum */
#define CAN_TSEG1_16tq ((uint8_t)0x0F) /*!< 16 time quantum */
#define IS_CAN_TSEG1(TSEG1) ((TSEG1) <= CAN_TSEG1_16tq)
/**
* @}
*/
/** @defgroup CAN_time_quantum_in_bit_segment_2
* @{
*/
#define CAN_TSEG2_1tq ((uint8_t)0x00) /*!< 1 time quantum */
#define CAN_TSEG2_2tq ((uint8_t)0x01) /*!< 2 time quantum */
#define CAN_TSEG2_3tq ((uint8_t)0x02) /*!< 3 time quantum */
#define CAN_TSEG2_4tq ((uint8_t)0x03) /*!< 4 time quantum */
#define CAN_TSEG2_5tq ((uint8_t)0x04) /*!< 5 time quantum */
#define CAN_TSEG2_6tq ((uint8_t)0x05) /*!< 6 time quantum */
#define CAN_TSEG2_7tq ((uint8_t)0x06) /*!< 7 time quantum */
#define CAN_TSEG2_8tq ((uint8_t)0x07) /*!< 8 time quantum */
#define IS_CAN_TSEG2(TSEG) ((TSEG) <= CAN_TSEG2_8tq)
/**
* @}
*/
/** @defgroup CAN_filter_mode
* @{
*/
#define CAN_FilterMode_Dual ((uint8_t)0x00) /*!< identifier list mode */
#define CAN_FilterMode_Single ((uint8_t)0x01) /*!< identifier/mask mode */
#define IS_CAN_FILTER_MODE(MODE) (((MODE) == CAN_FilterMode_Dual) || \
((MODE) == CAN_FilterMode_Single))
/**
* @}
*/
/** @defgroup CAN_ErrorCode
* @{
*/
#define CAN_ErrorType_ErrCode ((uint8_t)0xC0) /*!< identifier list mode */
#define CAN_ErrorType_Direction ((uint8_t)0x20) /*!< identifier/mask mode */
#define CAN_ErrorType_SegCode ((uint8_t)0x1F) /*!< identifier/mask mode */
#define IS_CAN_ErrorType(ErrorType) (((ErrorType) == CAN_ErrorType_ErrCode) || \
((ErrorType) == CAN_ErrorType_Direction)|| \
((ErrorType) == CAN_ErrorType_SegCode))
/**
* @}
*/
/* Initialization and Configuration functions *********************************/
void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan);
void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan);
HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef *hcan);
HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef *hcan);
void HAL_CAN_ConfigFilter(CAN_HandleTypeDef *hcan,CAN_FilterInitTypeDef* CAN_FilterInitStruct);
/* Transmit functions *********************************************************/
HAL_StatusTypeDef HAL_CAN_Transmit(CAN_HandleTypeDef *hcan, CanTxRxMsg* TxMessage);
void HAL_CAN_CancelTransmit(CAN_HandleTypeDef *hcan);
/* Receive functions **********************************************************/
HAL_StatusTypeDef HAL_CAN_Receive(CAN_HandleTypeDef *hcan, CanTxRxMsg* RxMessage);
HAL_StatusTypeDef HAL_CAN_Receive_IT(CAN_HandleTypeDef *hcan, CanTxRxMsg* RxMessage);
int8_t HAL_CAN_GetReceiveFiFoCounter(CAN_HandleTypeDef *hcan);
int8_t HAL_CAN_GetReceiveFiFoAddr(CAN_HandleTypeDef *hcan);
void HAL_CAN_ReleaseReceiveFIFO(CAN_HandleTypeDef *hcan);
void HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, CanTxRxMsg* RxMessage);
/* Operation modes functions **************************************************/
HAL_StatusTypeDef HAL_CAN_OperatingModeRequest(CAN_HandleTypeDef *hcan, uint8_t CAN_OperatingMode);
void HAL_CAN_ClearOverload(CAN_HandleTypeDef *hcan);
void HAL_CAN_SelfReceive(CAN_HandleTypeDef *hcan);
HAL_StatusTypeDef HAL_CAN_Sleep(CAN_HandleTypeDef *hcan);
HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan);
/* Error Code management functions **********************************/
int8_t HAL_CAN_GetErrorCode(CAN_HandleTypeDef *hcan,uint32_t Error_Type);
int8_t HAL_CAN_GetErrorAlarmCounter(CAN_HandleTypeDef *hcan);
int8_t HAL_CAN_GetArbitrationErrorPosition(CAN_HandleTypeDef *hcan);
int8_t HAL_CAN_GetReceiveErrorCounter(CAN_HandleTypeDef *hcan);
int8_t HAL_CAN_GetTransmitErrorCounter(CAN_HandleTypeDef *hcan);
void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan);
#endif
|