From 274d8aa40ca7be2e5b82b02f8f9718f4859cf9fd Mon Sep 17 00:00:00 2001 From: Mohammed Mubeen Date: Wed, 19 May 2021 11:48:00 +0530 Subject: [PATCH 1/6] The default FIFO for can by hardware is FIFO0 as set by the CAN STM API in configuration. Hence the read api is modified to access FIFO0 only --- targets/TARGET_STM/can_api.c | 40 +++++++++++++++++------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index abfff56496f..cb3313f04d9 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -646,6 +646,8 @@ void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable) #include #include +#define DEFAULT_RXFIFO 0 // default rx fifo for can by hardware is FIFO0 + static uint32_t can_irq_ids[CAN_NUM] = {0}; static can_irq_handler irq_handler; @@ -956,8 +958,10 @@ int can_write(can_t *obj, CAN_Message msg, int cc) int can_read(can_t *obj, CAN_Message *msg, int handle) { + //handle is the FIFO number + int rxfifo_default = DEFAULT_RXFIFO; //FIFO selection cannot be controlled by software for STM32, default FIFO is 0 CAN_TypeDef *can = obj->CanHandle.Instance; // check FPM0 which holds the pending message count in FIFO 0 @@ -967,36 +971,30 @@ int can_read(can_t *obj, CAN_Message *msg, int handle) } /* Get the Id */ - msg->format = (CANFormat)(((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR) >> 2); + msg->format = (CANFormat)(((uint8_t)0x04 & can->sFIFOMailBox[rxfifo_default].RIR) >> 2); if (!msg->format) { - msg->id = (uint32_t)0x000007FF & (can->sFIFOMailBox[handle].RIR >> 21); + msg->id = (uint32_t)0x000007FF & (can->sFIFOMailBox[rxfifo_default].RIR >> 21); } else { - msg->id = (uint32_t)0x1FFFFFFF & (can->sFIFOMailBox[handle].RIR >> 3); + msg->id = (uint32_t)0x1FFFFFFF & (can->sFIFOMailBox[rxfifo_default].RIR >> 3); } - msg->type = (CANType)(((uint8_t)0x02 & can->sFIFOMailBox[handle].RIR) >> 1); + msg->type = (CANType)(((uint8_t)0x02 & can->sFIFOMailBox[rxfifo_default].RIR) >> 1); /* Get the DLC */ - msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR; + msg->len = (uint8_t)0x0F & can->sFIFOMailBox[rxfifo_default].RDTR; /* Get the FMI */ - // msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8); + // msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDTR >> 8); /* Get the data field */ - msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR; - msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8); - msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16); - msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24); - msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR; - msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8); - msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16); - msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24); + msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[rxfifo_default].RDLR; + msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDLR >> 8); + msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDLR >> 16); + msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDLR >> 24); + msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[rxfifo_default].RDHR; + msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDHR >> 8); + msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDHR >> 16); + msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDHR >> 24); /* Release the FIFO */ - if (handle == CAN_FIFO0) { - /* Release FIFO0 */ - can->RF0R |= CAN_RF0R_RFOM0; - } else { /* FIFONumber == CAN_FIFO1 */ - /* Release FIFO1 */ - can->RF1R |= CAN_RF1R_RFOM1; - } + can->RF0R |= CAN_RF0R_RFOM0; return 1; } From 064f94d0a6cb3360927cdbff92635d3b6132b9cf Mon Sep 17 00:00:00 2001 From: Mohammed Mubeen Date: Tue, 25 May 2021 23:16:49 +0530 Subject: [PATCH 2/6] Added to return failure if filter if unsupported format --- targets/TARGET_STM/can_api.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index cb3313f04d9..b3e15353b92 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -329,10 +329,12 @@ int can_frequency(can_t *obj, int f) */ int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) { - UNUSED(handle); // Not supported yet (seems to be a used in read function?) - FDCAN_FilterTypeDef sFilterConfig = {0}; + if(handle != 0) { // message filter handle is not supported for STM controllers + return 0; + } + if (format == CANStandard) { sFilterConfig.IdType = FDCAN_STANDARD_ID; sFilterConfig.FilterIndex = 0; @@ -391,7 +393,7 @@ int can_write(can_t *obj, CAN_Message msg, int cc) int can_read(can_t *obj, CAN_Message *msg, int handle) { - UNUSED(handle); // Not supported yet (seems to be a handle to a filter configuration?) + UNUSED(handle); // Not supported if (HAL_FDCAN_GetRxFifoFillLevel(&obj->CanHandle, FDCAN_RX_FIFO0) == 0) { return 0; // No message arrived @@ -1100,6 +1102,10 @@ int can_mode(can_t *obj, CanMode mode) int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) { int success = 0; + + if(handle != 0) { // message filter handle not supported yet for STM controllers + return 0; + } // filter for CANAny format cannot be configured for STM32 if ((format == CANStandard) || (format == CANExtended)) { @@ -1128,6 +1134,8 @@ int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t { success = 1; } + } else if (format == CANAny) { + success = 0; // filter for CANAny is not supported by STM32, return a failure } return success; From 5049b518fb69e896386d0a13c9617dbc46aa05ab Mon Sep 17 00:00:00 2001 From: Mohammed Mubeen Date: Mon, 21 Jun 2021 08:51:10 +0530 Subject: [PATCH 3/6] Added working functionality to the can_filter api to accept IDs for filtering in both bxCAN and FDCAN --- targets/TARGET_STM/can_api.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index b3e15353b92..1ee85960b90 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -163,12 +163,23 @@ static void _can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz obj->CanHandle.Init.DataSyncJumpWidth = 0x1; // Not used - only in FDCAN obj->CanHandle.Init.DataTimeSeg1 = 0x1; // Not used - only in FDCAN obj->CanHandle.Init.DataTimeSeg2 = 0x1; // Not used - only in FDCAN -#ifndef TARGET_STM32G4 +#ifdef TARGET_STM32H7 + /* Message RAM offset is only supported in STM32H7 platforms of supported FDCAN platforms */ obj->CanHandle.Init.MessageRAMOffset = 0; + + /* The number of Standard and Extended ID filters are initialized to the maximum possile extent + * for STM32H7 platforms + */ + obj->CanHandle.Init.StdFiltersNbr = 128; // to be aligned with the handle parameter in can_filter + obj->CanHandle.Init.ExtFiltersNbr = 128; // to be aligned with the handle parameter in can_filter +#else + /* The number of Standard and Extended ID filters are initialized to the maximum possile extent + * for STM32G0x1, STM32G4 and STM32L5 platforms + */ + obj->CanHandle.Init.StdFiltersNbr = 28; // to be aligned with the handle parameter in can_filter + obj->CanHandle.Init.ExtFiltersNbr = 8; // to be aligned with the handle parameter in can_filter #endif - obj->CanHandle.Init.StdFiltersNbr = 1; // to be aligned with the handle parameter in can_filter - obj->CanHandle.Init.ExtFiltersNbr = 1; // to be aligned with the handle parameter in can_filter -#ifndef TARGET_STM32G4 +#ifdef TARGET_STM32H7 obj->CanHandle.Init.RxFifo0ElmtsNbr = 8; obj->CanHandle.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_8; obj->CanHandle.Init.RxFifo1ElmtsNbr = 0; @@ -180,7 +191,7 @@ static void _can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz obj->CanHandle.Init.TxFifoQueueElmtsNbr = 3; #endif obj->CanHandle.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; -#ifndef TARGET_STM32G4 +#ifdef TARGET_STM32H7 obj->CanHandle.Init.TxElmtSize = FDCAN_DATA_BYTES_8; #endif can_internal_init(obj); @@ -331,20 +342,16 @@ int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t { FDCAN_FilterTypeDef sFilterConfig = {0}; - if(handle != 0) { // message filter handle is not supported for STM controllers - return 0; - } - if (format == CANStandard) { sFilterConfig.IdType = FDCAN_STANDARD_ID; - sFilterConfig.FilterIndex = 0; + sFilterConfig.FilterIndex = handle; sFilterConfig.FilterType = FDCAN_FILTER_MASK; sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0; sFilterConfig.FilterID1 = id; sFilterConfig.FilterID2 = mask; } else if (format == CANExtended) { sFilterConfig.IdType = FDCAN_EXTENDED_ID; - sFilterConfig.FilterIndex = 0; + sFilterConfig.FilterIndex = handle; sFilterConfig.FilterType = FDCAN_FILTER_MASK; sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0; sFilterConfig.FilterID1 = id; From 2cc08cf5a6f5cc736d79a08f9340ac0a9c9d0fc4 Mon Sep 17 00:00:00 2001 From: Mohammed Mubeen Date: Mon, 21 Jun 2021 14:34:48 +0530 Subject: [PATCH 4/6] Rebaseing to current master --- targets/TARGET_STM/can_api.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 1ee85960b90..934a864fd30 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -400,7 +400,7 @@ int can_write(can_t *obj, CAN_Message msg, int cc) int can_read(can_t *obj, CAN_Message *msg, int handle) { - UNUSED(handle); // Not supported + UNUSED(handle); // Not supported, RXFIFO0 is set default by can_filter and cannot be changed. if (HAL_FDCAN_GetRxFifoFillLevel(&obj->CanHandle, FDCAN_RX_FIFO0) == 0) { return 0; // No message arrived @@ -1109,10 +1109,6 @@ int can_mode(can_t *obj, CanMode mode) int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) { int success = 0; - - if(handle != 0) { // message filter handle not supported yet for STM controllers - return 0; - } // filter for CANAny format cannot be configured for STM32 if ((format == CANStandard) || (format == CANExtended)) { From 9732cdcce6d75ccd1cfe6858a64cc7beac91f46e Mon Sep 17 00:00:00 2001 From: Mohammed Mubeen Date: Tue, 22 Jun 2021 14:22:16 +0530 Subject: [PATCH 5/6] Updated comments appropriately --- targets/TARGET_STM/can_api.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 934a864fd30..45ce7cda105 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -967,10 +967,8 @@ int can_write(can_t *obj, CAN_Message msg, int cc) int can_read(can_t *obj, CAN_Message *msg, int handle) { - - //handle is the FIFO number - - int rxfifo_default = DEFAULT_RXFIFO; //FIFO selection cannot be controlled by software for STM32, default FIFO is 0 + //FIFO selection cannot be controlled by software for STM32, default FIFO is 0, hence handle is not used + int rxfifo_default = DEFAULT_RXFIFO; CAN_TypeDef *can = obj->CanHandle.Instance; // check FPM0 which holds the pending message count in FIFO 0 From 6f436dbac3bad85c9d99ed9cb13c8016ad9422c6 Mon Sep 17 00:00:00 2001 From: Mohammed Mubeen Date: Fri, 25 Jun 2021 12:06:44 +0530 Subject: [PATCH 6/6] Adjusted spcae alignment --- targets/TARGET_STM/can_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 45ce7cda105..3262919d676 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -1136,7 +1136,7 @@ int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t success = 1; } } else if (format == CANAny) { - success = 0; // filter for CANAny is not supported by STM32, return a failure + success = 0; // filter for CANAny is not supported by STM32, return a failure } return success;