Skip to content

Commit f7a07b3

Browse files
iabdalkaderdpgeorge
authored andcommitted
stm32: Add support for FDCAN peripheral, exposed as pyb.CAN.
The new fdcan.c file provides the low-level C interface to the FDCAN peripheral, and pyb_can.c is updated to support both traditional CAN and FDCAN, depending on the MCU being compiled for.
1 parent d06fd38 commit f7a07b3

File tree

7 files changed

+604
-52
lines changed

7 files changed

+604
-52
lines changed

ports/stm32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ SRC_C = \
256256
qspi.c \
257257
uart.c \
258258
can.c \
259+
fdcan.c \
259260
pyb_can.c \
260261
usb.c \
261262
wdt.c \

ports/stm32/can.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ void can_deinit_all(void) {
4747
}
4848
}
4949

50+
#if !MICROPY_HW_ENABLE_FDCAN
51+
5052
bool can_init(pyb_can_obj_t *can_obj, uint32_t mode, uint32_t prescaler, uint32_t sjw, uint32_t bs1, uint32_t bs2, bool auto_restart) {
5153
CAN_InitTypeDef *init = &can_obj->can.Init;
5254
init->Mode = mode << 4; // shift-left so modes fit in a small-int
@@ -158,7 +160,7 @@ void can_deinit(pyb_can_obj_t *self) {
158160
}
159161
}
160162

161-
void can_clearfilter(uint32_t f, uint8_t bank) {
163+
void can_clearfilter(pyb_can_obj_t *self, uint32_t f, uint8_t bank) {
162164
CAN_FilterConfTypeDef filter;
163165

164166
filter.FilterIdHigh = 0;
@@ -175,12 +177,12 @@ void can_clearfilter(uint32_t f, uint8_t bank) {
175177
HAL_CAN_ConfigFilter(NULL, &filter);
176178
}
177179

178-
int can_receive(CAN_TypeDef *can, int fifo, CanRxMsgTypeDef *msg, uint32_t timeout_ms) {
180+
int can_receive(CAN_HandleTypeDef *can, int fifo, CanRxMsgTypeDef *msg, uint8_t *data, uint32_t timeout_ms) {
179181
volatile uint32_t *rfr;
180182
if (fifo == CAN_FIFO0) {
181-
rfr = &can->RF0R;
183+
rfr = &can->Instance->RF0R;
182184
} else {
183-
rfr = &can->RF1R;
185+
rfr = &can->Instance->RF1R;
184186
}
185187

186188
// Wait for a message to become available, with timeout
@@ -193,7 +195,7 @@ int can_receive(CAN_TypeDef *can, int fifo, CanRxMsgTypeDef *msg, uint32_t timeo
193195
}
194196

195197
// Read message data
196-
CAN_FIFOMailBox_TypeDef *box = &can->sFIFOMailBox[fifo];
198+
CAN_FIFOMailBox_TypeDef *box = &can->Instance->sFIFOMailBox[fifo];
197199
msg->IDE = box->RIR & 4;
198200
if (msg->IDE == CAN_ID_STD) {
199201
msg->StdId = box->RIR >> 21;
@@ -204,15 +206,15 @@ int can_receive(CAN_TypeDef *can, int fifo, CanRxMsgTypeDef *msg, uint32_t timeo
204206
msg->DLC = box->RDTR & 0xf;
205207
msg->FMI = box->RDTR >> 8 & 0xff;
206208
uint32_t rdlr = box->RDLR;
207-
msg->Data[0] = rdlr;
208-
msg->Data[1] = rdlr >> 8;
209-
msg->Data[2] = rdlr >> 16;
210-
msg->Data[3] = rdlr >> 24;
209+
data[0] = rdlr;
210+
data[1] = rdlr >> 8;
211+
data[2] = rdlr >> 16;
212+
data[3] = rdlr >> 24;
211213
uint32_t rdhr = box->RDHR;
212-
msg->Data[4] = rdhr;
213-
msg->Data[5] = rdhr >> 8;
214-
msg->Data[6] = rdhr >> 16;
215-
msg->Data[7] = rdhr >> 24;
214+
data[4] = rdhr;
215+
data[5] = rdhr >> 8;
216+
data[6] = rdhr >> 16;
217+
data[7] = rdhr >> 24;
216218

217219
// Release (free) message from FIFO
218220
*rfr |= CAN_RF0R_RFOM0;
@@ -427,4 +429,6 @@ void CAN3_SCE_IRQHandler(void) {
427429
}
428430
#endif
429431

432+
#endif // !MICROPY_HW_ENABLE_FDCAN
433+
430434
#endif // MICROPY_HW_ENABLE_CAN

ports/stm32/can.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
#define MASK32 (2)
3838
#define LIST32 (3)
3939

40+
#if MICROPY_HW_ENABLE_FDCAN
41+
#define CAN_TypeDef FDCAN_GlobalTypeDef
42+
#define CAN_HandleTypeDef FDCAN_HandleTypeDef
43+
#define CanTxMsgTypeDef FDCAN_TxHeaderTypeDef
44+
#define CanRxMsgTypeDef FDCAN_RxHeaderTypeDef
45+
#endif
46+
4047
enum {
4148
CAN_STATE_STOPPED,
4249
CAN_STATE_ERROR_ACTIVE,
@@ -74,10 +81,9 @@ void can_deinit_all(void);
7481
bool can_init(pyb_can_obj_t *can_obj, uint32_t mode, uint32_t prescaler, uint32_t sjw, uint32_t bs1, uint32_t bs2, bool auto_restart);
7582
void can_deinit(pyb_can_obj_t *self);
7683

77-
void can_clearfilter(uint32_t f, uint8_t bank);
78-
int can_receive(CAN_TypeDef *can, int fifo, CanRxMsgTypeDef *msg, uint32_t timeout_ms);
84+
void can_clearfilter(pyb_can_obj_t *self, uint32_t f, uint8_t bank);
85+
int can_receive(CAN_HandleTypeDef *can, int fifo, CanRxMsgTypeDef *msg, uint8_t *data, uint32_t timeout_ms);
7986
HAL_StatusTypeDef CAN_Transmit(CAN_HandleTypeDef *hcan, uint32_t Timeout);
80-
8187
void pyb_can_handle_callback(pyb_can_obj_t *self, uint fifo_id, mp_obj_t callback, mp_obj_t irq_reason);
8288

8389
#endif // MICROPY_INCLUDED_STM32_CAN_H

0 commit comments

Comments
 (0)