summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriabdalkader <i.abdalkader@gmail.com>2022-01-17 00:12:01 +0200
committerDamien George <damien@micropython.org>2022-04-02 22:37:27 +1100
commitff287d085f95ab0ca3fdf67ee9d8cce6f046b01e (patch)
tree618272c7c9c40f080c19b04d9099fe2727164949
parent8baf05af8c8011d586ce767da70b4c6f47f55d6c (diff)
stm32/pyb_can: Enable CAN FD frame support and BRS.
- Enable CAN FD frame support and BRS. - Optimize the message RAM usage per FDCAN instance. - Document the usage and different sections of the Message RAM.
-rw-r--r--ports/stm32/fdcan.c98
-rw-r--r--ports/stm32/pyb_can.c133
2 files changed, 151 insertions, 80 deletions
diff --git a/ports/stm32/fdcan.c b/ports/stm32/fdcan.c
index 8465b806a..63c6c3123 100644
--- a/ports/stm32/fdcan.c
+++ b/ports/stm32/fdcan.c
@@ -56,14 +56,19 @@
#define FDCAN_IT_GROUP_RX_FIFO1 (FDCAN_ILS_RF1NL | FDCAN_ILS_RF1FL | FDCAN_ILS_RF1LL)
#endif
+// The dedicated Message RAM should be 2560 words, but the way it's defined in stm32h7xx_hal_fdcan.c
+// as (SRAMCAN_BASE + FDCAN_MESSAGE_RAM_SIZE - 0x4U) limits the usable number of words to 2559 words.
+#define FDCAN_MESSAGE_RAM_SIZE (2560 - 1)
+
// also defined in <PROC>_hal_fdcan.c, but not able to declare extern and reach the variable
-static const uint8_t DLCtoBytes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64};
+const uint8_t DLCtoBytes[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64};
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) {
(void)auto_restart;
FDCAN_InitTypeDef *init = &can_obj->can.Init;
- init->FrameFormat = FDCAN_FRAME_CLASSIC;
+ // Configure FDCAN with FD frame and BRS support.
+ init->FrameFormat = FDCAN_FRAME_FD_BRS;
init->Mode = mode;
init->NominalPrescaler = prescaler; // tq = NominalPrescaler x (1/fdcan_ker_ck)
@@ -81,46 +86,58 @@ bool can_init(pyb_can_obj_t *can_obj, uint32_t mode, uint32_t prescaler, uint32_
init->DataSyncJumpWidth = 1;
init->DataTimeSeg1 = 1;
init->DataTimeSeg2 = 1;
- #endif
-
- #if defined(STM32H7)
- // variable used to specify RAM address in HAL, only for H7, G4 uses defined offset address in HAL
- // The Message RAM is shared between CAN1 and CAN2. Setting the offset to half
- // the Message RAM for the second CAN and using half the resources for each CAN.
+ init->StdFiltersNbr = 28; // /2 ? if FDCAN2 is used !!?
+ init->ExtFiltersNbr = 0; // Not used
+ init->TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
+ #elif defined(STM32H7)
+ // The dedicated FDCAN RAM is 2560 32-bit words and shared between the FDCAN instances.
+ // To support 2 FDCAN instances simultaneously, the Message RAM is divided in half by
+ // setting the second FDCAN memory offset to half the RAM size. With this configuration,
+ // the maximum words per FDCAN instance is 1280 32-bit words.
if (can_obj->can_id == PYB_CAN_1) {
init->MessageRAMOffset = 0;
} else {
- init->MessageRAMOffset = 2560 / 2;
+ init->MessageRAMOffset = FDCAN_MESSAGE_RAM_SIZE / 2;
}
- #endif
-
- #if defined(STM32G4)
-
- init->StdFiltersNbr = 28; // /2 ? if FDCAN2 is used !!?
- init->ExtFiltersNbr = 0; // Not used
-
- #elif defined(STM32H7)
-
- init->StdFiltersNbr = 64; // 128 / 2
- init->ExtFiltersNbr = 0; // Not used
-
- init->TxEventsNbr = 16; // 32 / 2
- init->RxBuffersNbr = 32; // 64 / 2
- init->TxBuffersNbr = 16; // 32 / 2
-
- init->RxFifo0ElmtsNbr = 64; // 128 / 2
- init->RxFifo0ElmtSize = FDCAN_DATA_BYTES_8;
-
- init->RxFifo1ElmtsNbr = 64; // 128 / 2
- init->RxFifo1ElmtSize = FDCAN_DATA_BYTES_8;
-
- init->TxFifoQueueElmtsNbr = 16; // Tx fifo elements
- init->TxElmtSize = FDCAN_DATA_BYTES_8;
+ // An element stored in the Message RAM contains an identifier, DLC, control bits, the
+ // data field and the specific transmission or reception bits field for control.
+ // The following code configures the different Message RAM sections per FDCAN instance.
+
+ // The RAM filtering section is configured for 64 x 1 word elements for 11-bit standard
+ // identifiers, and 31 x 2 words elements for 29-bit extended identifiers.
+ // The total number of words reserved for the filtering per FDCAN instance is 126 words.
+ init->StdFiltersNbr = 64;
+ // Note extended identifiers are Not used in pyb_can.c and Not handled correctly.
+ // Disable the extended identifiers filters for now until this is fixed properly.
+ init->ExtFiltersNbr = 0 /*31*/;
+
+ // The Tx event FIFO is used to store the message ID and the timestamp of successfully
+ // transmitted elements. The Tx event FIFO can store a maximum of 32 (2 words) elements.
+ // NOTE: Events are stored in Tx event FIFO only if tx_msg.TxEventFifoControl is enabled.
+ init->TxEventsNbr = 0;
+
+ // Transmission section is configured in FIFO mode operation, with no dedicated Tx buffers.
+ // The Tx FIFO can store a maximum of 32 elements (or 576 words), each element is 18 words
+ // long (to support a maximum of 64 bytes data field):
+ // 2 words header + 16 words data field (to support up to 64 bytes of data).
+ // The total number of words reserved for the Tx FIFO per FDCAN instance is 288 words.
+ init->TxBuffersNbr = 0;
+ init->TxFifoQueueElmtsNbr = 16;
+ init->TxElmtSize = FDCAN_DATA_BYTES_64;
+ init->TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
+ // Reception section is configured to use Rx FIFO 0 and Rx FIFO1, with no dedicated Rx buffers.
+ // Each Rx FIFO can store a maximum of 64 elements (1152 words), each element is 18 words
+ // long (to support a maximum of 64 bytes data field):
+ // 2 words header + 16 words data field (to support up to 64 bytes of data).
+ // The total number of words reserved for the Rx FIFOs per FDCAN instance is 864 words.
+ init->RxBuffersNbr = 0;
+ init->RxFifo0ElmtsNbr = 24;
+ init->RxFifo0ElmtSize = FDCAN_DATA_BYTES_64;
+ init->RxFifo1ElmtsNbr = 24;
+ init->RxFifo1ElmtSize = FDCAN_DATA_BYTES_64;
#endif
- init->TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
-
FDCAN_GlobalTypeDef *CANx = NULL;
const pin_obj_t *pins[2];
@@ -159,7 +176,10 @@ bool can_init(pyb_can_obj_t *can_obj, uint32_t mode, uint32_t prescaler, uint32_
// init CANx
can_obj->can.Instance = CANx;
- HAL_FDCAN_Init(&can_obj->can);
+ // catch bad configuration errors.
+ if (HAL_FDCAN_Init(&can_obj->can) != HAL_OK) {
+ return false;
+ }
// Disable acceptance of non-matching frames (enabled by default)
HAL_FDCAN_ConfigGlobalFilter(&can_obj->can, FDCAN_REJECT, FDCAN_REJECT, DISABLE, DISABLE);
@@ -168,7 +188,7 @@ bool can_init(pyb_can_obj_t *can_obj, uint32_t mode, uint32_t prescaler, uint32_
HAL_FDCAN_Start(&can_obj->can);
// Reset all filters
- for (int f = 0; f < 64; ++f) {
+ for (int f = 0; f < init->StdFiltersNbr; ++f) {
can_clearfilter(can_obj, f, 0);
}
@@ -299,10 +319,12 @@ int can_receive(FDCAN_HandleTypeDef *can, int fifo, FDCAN_RxHeaderTypeDef *hdr,
hdr->FDFormat = *address & FDCAN_ELEMENT_MASK_FDF;
hdr->FilterIndex = (*address & FDCAN_ELEMENT_MASK_FIDX) >> 24;
hdr->IsFilterMatchingFrame = (*address++ & FDCAN_ELEMENT_MASK_ANMF) >> 31;
+ // Convert DLC to Bytes.
+ hdr->DataLength = DLCtoBytes[hdr->DataLength];
// Copy data
uint8_t *pdata = (uint8_t *)address;
- for (uint32_t i = 0; i < DLCtoBytes[hdr->DataLength]; ++i) {
+ for (uint32_t i = 0; i < hdr->DataLength; ++i) {
*data++ = *pdata++;
}
diff --git a/ports/stm32/pyb_can.c b/ports/stm32/pyb_can.c
index 8007fd9e3..0c5c60255 100644
--- a/ports/stm32/pyb_can.c
+++ b/ports/stm32/pyb_can.c
@@ -42,6 +42,7 @@
#if MICROPY_HW_ENABLE_FDCAN
#define CAN_MAX_FILTER (64)
+#define CAN_MAX_DATA_FRAME (64)
#define CAN_FIFO0 FDCAN_RX_FIFO0
#define CAN_FIFO1 FDCAN_RX_FIFO1
@@ -89,10 +90,11 @@
// Both banks start at 0
STATIC uint8_t can2_start_bank = 0;
-
+extern const uint8_t DLCtoBytes[16];
#else
#define CAN_MAX_FILTER (28)
+#define CAN_MAX_DATA_FRAME (8)
#define CAN_DEFAULT_PRESCALER (100)
#define CAN_DEFAULT_SJW (1)
@@ -180,19 +182,50 @@ STATIC uint32_t pyb_can_get_source_freq() {
return can_kern_clk;
}
+STATIC void pyb_can_get_bit_timing(mp_uint_t baudrate, mp_uint_t sample_point,
+ mp_int_t *bs1_out, mp_int_t *bs2_out, mp_int_t *prescaler_out) {
+ uint32_t can_kern_clk = pyb_can_get_source_freq();
+
+ // The following max values work on all MCUs for classical CAN.
+ for (int brp = 1; brp < 512; brp++) {
+ for (int bs1 = 1; bs1 < 16; bs1++) {
+ for (int bs2 = 1; bs2 < 8; bs2++) {
+ if ((baudrate == (can_kern_clk / (brp * (1 + bs1 + bs2)))) &&
+ ((sample_point * 10) == (((1 + bs1) * 1000) / (1 + bs1 + bs2)))) {
+ *bs1_out = bs1;
+ *bs2_out = bs2;
+ *prescaler_out = brp;
+ return;
+ }
+ }
+ }
+ }
+
+ mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("couldn't match baudrate and sample point"));
+}
+
// init(mode, extframe=False, prescaler=100, *, sjw=1, bs1=6, bs2=8)
STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_mode, ARG_extframe, ARG_prescaler, ARG_sjw, ARG_bs1, ARG_bs2, ARG_auto_restart, ARG_baudrate, ARG_sample_point };
+ enum { ARG_mode, ARG_extframe, ARG_prescaler, ARG_sjw, ARG_bs1, ARG_bs2, ARG_auto_restart, ARG_baudrate, ARG_sample_point,
+ ARG_brs_prescaler, ARG_brs_sjw, ARG_brs_bs1, ARG_brs_bs2, ARG_brs_baudrate, ARG_brs_sample_point };
static const mp_arg_t allowed_args[] = {
- { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = CAN_MODE_NORMAL} },
- { MP_QSTR_extframe, MP_ARG_BOOL, {.u_bool = false} },
- { MP_QSTR_prescaler, MP_ARG_INT, {.u_int = CAN_DEFAULT_PRESCALER} },
- { MP_QSTR_sjw, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_SJW} },
- { MP_QSTR_bs1, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_BS1} },
- { MP_QSTR_bs2, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_BS2} },
- { MP_QSTR_auto_restart, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
- { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
- { MP_QSTR_sample_point, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 75} }, // 75% sampling point
+ { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = CAN_MODE_NORMAL} },
+ { MP_QSTR_extframe, MP_ARG_BOOL, {.u_bool = false} },
+ { MP_QSTR_prescaler, MP_ARG_INT, {.u_int = CAN_DEFAULT_PRESCALER} },
+ { MP_QSTR_sjw, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_SJW} },
+ { MP_QSTR_bs1, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_BS1} },
+ { MP_QSTR_bs2, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_BS2} },
+ { MP_QSTR_auto_restart, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_sample_point, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 75} }, // 75% sampling point
+ #if MICROPY_HW_ENABLE_FDCAN
+ { MP_QSTR_brs_prescaler, MP_ARG_INT, {.u_int = CAN_DEFAULT_PRESCALER} },
+ { MP_QSTR_brs_sjw, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_SJW} },
+ { MP_QSTR_brs_bs1, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_BS1} },
+ { MP_QSTR_brs_bs2, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_BS2} },
+ { MP_QSTR_brs_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_brs_sample_point, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }
+ #endif
};
// parse args
@@ -206,34 +239,30 @@ STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp
// Calculate CAN bit timing from baudrate if provided
if (args[ARG_baudrate].u_int != 0) {
- uint32_t baudrate = args[ARG_baudrate].u_int;
- uint32_t sampoint = args[ARG_sample_point].u_int;
- uint32_t can_kern_clk = pyb_can_get_source_freq();
- bool timing_found = false;
-
- // The following max values work on all MCUs for classical CAN.
- for (int brp = 1; brp < 512 && !timing_found; brp++) {
- for (int bs1 = 1; bs1 < 16 && !timing_found; bs1++) {
- for (int bs2 = 1; bs2 < 8 && !timing_found; bs2++) {
- if ((baudrate == (can_kern_clk / (brp * (1 + bs1 + bs2)))) &&
- ((sampoint * 10) == (((1 + bs1) * 1000) / (1 + bs1 + bs2)))) {
- args[ARG_bs1].u_int = bs1;
- args[ARG_bs2].u_int = bs2;
- args[ARG_prescaler].u_int = brp;
- timing_found = true;
- }
- }
- }
- }
- if (!timing_found) {
- mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("couldn't match baudrate and sample point"));
- }
+ pyb_can_get_bit_timing(args[ARG_baudrate].u_int, args[ARG_sample_point].u_int,
+ &args[ARG_bs1].u_int, &args[ARG_bs2].u_int, &args[ARG_prescaler].u_int);
}
- // init CAN (if it fails, it's because the port doesn't exist)
+ #if MICROPY_HW_ENABLE_FDCAN
+ // If no sample point is provided for data bit timing, use the nominal sample point.
+ if (args[ARG_brs_sample_point].u_int == 0) {
+ args[ARG_brs_sample_point].u_int = args[ARG_sample_point].u_int;
+ }
+ // Calculate BRS CAN bit timing from baudrate if provided
+ if (args[ARG_brs_baudrate].u_int != 0) {
+ pyb_can_get_bit_timing(args[ARG_brs_baudrate].u_int, args[ARG_brs_sample_point].u_int,
+ &args[ARG_brs_bs1].u_int, &args[ARG_brs_bs2].u_int, &args[ARG_brs_prescaler].u_int);
+ }
+ // Set BRS bit timings.
+ self->can.Init.DataPrescaler = args[ARG_brs_prescaler].u_int;
+ self->can.Init.DataSyncJumpWidth = args[ARG_brs_sjw].u_int;
+ self->can.Init.DataTimeSeg1 = args[ARG_bs1].u_int; // DataTimeSeg1 = Propagation_segment + Phase_segment_1
+ self->can.Init.DataTimeSeg2 = args[ARG_bs2].u_int;
+ #endif
+
if (!can_init(self, args[ARG_mode].u_int, args[ARG_prescaler].u_int, args[ARG_sjw].u_int,
args[ARG_bs1].u_int, args[ARG_bs2].u_int, args[ARG_auto_restart].u_bool)) {
- mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("CAN(%d) doesn't exist"), self->can_id);
+ mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("CAN(%d) init failure"), self->can_id);
}
return mp_const_none;
@@ -450,12 +479,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_can_any_obj, pyb_can_any);
// send(send, addr, *, timeout=5000)
STATIC mp_obj_t pyb_can_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_data, ARG_id, ARG_timeout, ARG_rtr };
+ enum { ARG_data, ARG_id, ARG_timeout, ARG_rtr, ARG_fdf, ARG_brs };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_rtr, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ #if MICROPY_HW_ENABLE_FDCAN
+ { MP_QSTR_fdf, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ { MP_QSTR_brs, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ #endif
};
// parse args
@@ -468,7 +501,7 @@ STATIC mp_obj_t pyb_can_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
uint8_t data[1];
pyb_buf_get_for_send(args[ARG_data].u_obj, &bufinfo, data);
- if (bufinfo.len > 8) {
+ if (bufinfo.len > CAN_MAX_DATA_FRAME) {
mp_raise_ValueError(MP_ERROR_TEXT("CAN data field too long"));
}
@@ -476,13 +509,12 @@ STATIC mp_obj_t pyb_can_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
CanTxMsgTypeDef tx_msg;
#if MICROPY_HW_ENABLE_FDCAN
- uint8_t tx_data[8];
+ uint8_t tx_data[CAN_MAX_DATA_FRAME];
+ memset(tx_data, 0, sizeof(tx_data));
+
tx_msg.MessageMarker = 0;
tx_msg.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
- tx_msg.BitRateSwitch = FDCAN_BRS_OFF;
- tx_msg.FDFormat = FDCAN_CLASSIC_CAN;
tx_msg.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
- tx_msg.DataLength = (bufinfo.len << 16); // TODO DLC for len > 8
if (self->extframe) {
tx_msg.Identifier = args[ARG_id].u_int & 0x1FFFFFFF;
@@ -496,6 +528,23 @@ STATIC mp_obj_t pyb_can_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
} else {
tx_msg.TxFrameType = FDCAN_REMOTE_FRAME;
}
+ if (args[ARG_fdf].u_bool == false) {
+ tx_msg.FDFormat = FDCAN_CLASSIC_CAN;
+ } else {
+ tx_msg.FDFormat = FDCAN_FD_CAN;
+ }
+ if (args[ARG_brs].u_bool == false) {
+ tx_msg.BitRateSwitch = FDCAN_BRS_OFF;
+ } else {
+ tx_msg.BitRateSwitch = FDCAN_BRS_ON;
+ }
+ // Roundup DataLength to next DLC size and encode to DLC.
+ for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(DLCtoBytes); i++) {
+ if (bufinfo.len <= DLCtoBytes[i]) {
+ tx_msg.DataLength = (i << 16);
+ break;
+ }
+ }
#else
tx_msg.DLC = bufinfo.len;
uint8_t *tx_data = tx_msg.Data; // Data is uint32_t but holds only 1 byte
@@ -565,7 +614,7 @@ STATIC mp_obj_t pyb_can_recv(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
// receive the data
CanRxMsgTypeDef rx_msg;
#if MICROPY_HW_ENABLE_FDCAN
- uint8_t rx_data[8];
+ uint8_t rx_data[CAN_MAX_DATA_FRAME];
#else
uint8_t *rx_data = rx_msg.Data;
#endif