From cf9f097328b3fcc55852ba4f9aeb52df04cbf87a Mon Sep 17 00:00:00 2001 From: Rod Bayliss III Date: Fri, 15 May 2020 15:09:50 -0700 Subject: [PATCH 01/11] Modifying CANLIB to add the H7 --- src/driver.h | 2 +- src/drivers/inc_template/stm32h7xx.h | 27 +++++++++ src/drivers/src/stm32h7xx.c | 84 ++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 src/drivers/inc_template/stm32h7xx.h create mode 100644 src/drivers/src/stm32h7xx.c diff --git a/src/driver.h b/src/driver.h index ccde3f9..be8c12d 100644 --- a/src/driver.h +++ b/src/driver.h @@ -3,7 +3,7 @@ // INCLUDE THIS AFTER YOUR DRIVER -#include "drivers/inc/stm32f4xx.h" // BAD HACK +#include "drivers/inc/stm32h7xx.h" #include "bus.h" CANlib_Transmit_Error_T CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus); diff --git a/src/drivers/inc_template/stm32h7xx.h b/src/drivers/inc_template/stm32h7xx.h new file mode 100644 index 0000000..1596d18 --- /dev/null +++ b/src/drivers/inc_template/stm32h7xx.h @@ -0,0 +1,27 @@ +#ifndef __STM32H7XX_CAN_H +#define __STM32H7XX_CAN_H + +#include "static.h" +#include "stm32h7xx_hal.h" +#include "bus.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// BUILD CAN_Raw_Bus_T + +typedef uint32_t Time_T; // in ms +typedef HAL_StatusTypeDef CANlib_Transmit_Error_T; +typedef HAL_StatusTypeDef CANlib_Init_Error_T; + +CANlib_Transmit_Error_T CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus); +void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif // __STM32H7XX_CAN_H diff --git a/src/drivers/src/stm32h7xx.c b/src/drivers/src/stm32h7xx.c new file mode 100644 index 0000000..a4624ff --- /dev/null +++ b/src/drivers/src/stm32h7xx.c @@ -0,0 +1,84 @@ +#include "static.h" + +#include "drivers/inc/stm32h7xx.h" +#include "stm32h7xx_hal.h" +#include "bus.h" +#include "driver.h" +#include +#include +#ifdef USING_LOGGING_CALLBACK + #include "log.h" +#endif + +extern CAN_HandleTypeDef hcan1; +extern CAN_HandleTypeDef hcan2; +extern CAN_HandleTypeDef hcan3; + +HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { + CAN_Raw_Bus_T raw_bus = CANlib_GetRawBus(bus); + int bus_num; + CAN_HandleTypeDef* hcan; + switch(raw_bus) { + case CAN_1: + hcan = &hcan1; + bus_num = 1; + break; + case CAN_2: + hcan = &hcan2; + bus_num = 2; + break; + case CAN_3: + hcan = &hcan3; + bus_num = 3; + break; + default: + return HAL_ERROR; + } + + CAN_TxHeaderTypeDef pHeader; + uint32_t pTxMailbox = 0; + + pHeader.DLC = frame->dlc; + pHeader.StdId = frame->id; + pHeader.IDE = frame->extended ? CAN_ID_EXT: CAN_ID_STD; + pHeader.RTR = CAN_RTR_DATA; // Data frame (as opposed to a remote frame) + pHeader.TransmitGlobalTime = DISABLE; // Don't replace last 2 bytes of data with TX time. +#ifdef USING_LOGGING_CALLBACK + log_frame(frame, bus_num); +#else + UNUSED(bus_num); +#endif + return HAL_FDCAN_AddMessageToTxBuffer(hcan, &pHeader, frame->data, &pTxMailbox); +} + +void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { + CAN_Raw_Bus_T raw_bus = CANlib_GetRawBus(bus); + CAN_HandleTypeDef *hcan; + switch(raw_bus) { + case CAN_1: + hcan = &hcan1; + break; + case CAN_2: + hcan = &hcan2; + break; + case CAN_3: + hcan = &hcan3; + break; + default: + return; + } + + uint8_t data[8] = {}; + CAN_RxHeaderTypeDef pHeader; + for (int fifo = 0; fifo < 2; fifo++) { // There are 2 receive FIFOs + if (HAL_FDCAN_GetRxFifoFillLevel(hcan, fifo) > 0) { + HAL_FDCAN_GetRxMessage(hcan, fifo, &pHeader, data); + frame->id = pHeader.IDE == CAN_ID_STD ? pHeader.StdId : pHeader.ExtId; + frame->dlc = pHeader.DLC; + + memcpy(frame->data, data, sizeof(data)); + frame->extended = pHeader.IDE == CAN_ID_EXT; + return; + } + } +} From 82bc84317e201e3ef4836edcc5f4a688a6966dde Mon Sep 17 00:00:00 2001 From: Rod Bayliss III Date: Fri, 15 May 2020 15:57:25 -0700 Subject: [PATCH 02/11] changing the terminology to match the drivers --- src/drivers/src/stm32h7xx.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/drivers/src/stm32h7xx.c b/src/drivers/src/stm32h7xx.c index a4624ff..dccae68 100644 --- a/src/drivers/src/stm32h7xx.c +++ b/src/drivers/src/stm32h7xx.c @@ -10,14 +10,14 @@ #include "log.h" #endif -extern CAN_HandleTypeDef hcan1; -extern CAN_HandleTypeDef hcan2; -extern CAN_HandleTypeDef hcan3; +extern FDCAN_HandleTypeDef hcan1; +extern FDCAN_HandleTypeDef hcan2; +extern FDCAN_HandleTypeDef hcan3; HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = CANlib_GetRawBus(bus); int bus_num; - CAN_HandleTypeDef* hcan; + FDCAN_HandleTypeDef* hcan; switch(raw_bus) { case CAN_1: hcan = &hcan1; @@ -35,14 +35,14 @@ HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { return HAL_ERROR; } - CAN_TxHeaderTypeDef pHeader; + FDCAN_TxHeaderTypeDef pHeader; uint32_t pTxMailbox = 0; - pHeader.DLC = frame->dlc; - pHeader.StdId = frame->id; - pHeader.IDE = frame->extended ? CAN_ID_EXT: CAN_ID_STD; - pHeader.RTR = CAN_RTR_DATA; // Data frame (as opposed to a remote frame) - pHeader.TransmitGlobalTime = DISABLE; // Don't replace last 2 bytes of data with TX time. + pHeader.DataLength = frame->dlc; + pHeader.Identifier= frame->id; + pHeader.IdType = frame->extended ? FDCAN_EXTENDED_ID: FDCAN_STANDARD_ID; + //pHeader.RTR = CAN_RTR_DATA; // Data frame (as opposed to a remote frame) + //pHeader.TransmitGlobalTime = DISABLE; // Don't replace last 2 bytes of data with TX time. #ifdef USING_LOGGING_CALLBACK log_frame(frame, bus_num); #else @@ -51,9 +51,8 @@ HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { return HAL_FDCAN_AddMessageToTxBuffer(hcan, &pHeader, frame->data, &pTxMailbox); } -void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { - CAN_Raw_Bus_T raw_bus = CANlib_GetRawBus(bus); - CAN_HandleTypeDef *hcan; +void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = CANlib_GetRawBus(bus); + FDCAN_HandleTypeDef *hcan; switch(raw_bus) { case CAN_1: hcan = &hcan1; @@ -69,15 +68,15 @@ void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { } uint8_t data[8] = {}; - CAN_RxHeaderTypeDef pHeader; + FDCAN_RxHeaderTypeDef pHeader; for (int fifo = 0; fifo < 2; fifo++) { // There are 2 receive FIFOs if (HAL_FDCAN_GetRxFifoFillLevel(hcan, fifo) > 0) { HAL_FDCAN_GetRxMessage(hcan, fifo, &pHeader, data); - frame->id = pHeader.IDE == CAN_ID_STD ? pHeader.StdId : pHeader.ExtId; - frame->dlc = pHeader.DLC; + frame->id = pHeader.IdType == FDCAN_STANDARD_ID ? pHeader.Identifier: pHeader.Identifier; + frame->dlc = pHeader.DataLength; memcpy(frame->data, data, sizeof(data)); - frame->extended = pHeader.IDE == CAN_ID_EXT; + frame->extended = pHeader.IdType == FDCAN_EXTENDED_ID; return; } } From 987aa4561970ce403dab1f3ba16e4267096914f8 Mon Sep 17 00:00:00 2001 From: Mingfei Date: Sat, 16 May 2020 12:57:36 -0400 Subject: [PATCH 03/11] Correct handle names to match CubeMX generation --- src/drivers/src/stm32h7xx.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/drivers/src/stm32h7xx.c b/src/drivers/src/stm32h7xx.c index dccae68..864ecb8 100644 --- a/src/drivers/src/stm32h7xx.c +++ b/src/drivers/src/stm32h7xx.c @@ -10,25 +10,26 @@ #include "log.h" #endif -extern FDCAN_HandleTypeDef hcan1; -extern FDCAN_HandleTypeDef hcan2; -extern FDCAN_HandleTypeDef hcan3; +extern FDCAN_HandleTypeDef hfdcan1; +// TODO: do some macro crap to automatically deal with these if not used +extern FDCAN_HandleTypeDef hfdcan2; +extern FDCAN_HandleTypeDef hfdcan3; HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = CANlib_GetRawBus(bus); int bus_num; - FDCAN_HandleTypeDef* hcan; + FDCAN_HandleTypeDef* hfdcan; switch(raw_bus) { case CAN_1: - hcan = &hcan1; + hfdcan = &hfdcan1; bus_num = 1; break; case CAN_2: - hcan = &hcan2; + hfdcan = &hfdcan2; bus_num = 2; break; case CAN_3: - hcan = &hcan3; + hfdcan = &hfdcan3; bus_num = 3; break; default: @@ -48,20 +49,20 @@ HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { #else UNUSED(bus_num); #endif - return HAL_FDCAN_AddMessageToTxBuffer(hcan, &pHeader, frame->data, &pTxMailbox); + return HAL_FDCAN_AddMessageToTxBuffer(hfdcan, &pHeader, frame->data, &pTxMailbox); } void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = CANlib_GetRawBus(bus); - FDCAN_HandleTypeDef *hcan; + FDCAN_HandleTypeDef *hfdcan; switch(raw_bus) { case CAN_1: - hcan = &hcan1; + hfdcan = &hfdcan1; break; case CAN_2: - hcan = &hcan2; + hfdcan = &hfdcan2; break; case CAN_3: - hcan = &hcan3; + hfdcan = &hfdcan3; break; default: return; @@ -70,8 +71,8 @@ void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = uint8_t data[8] = {}; FDCAN_RxHeaderTypeDef pHeader; for (int fifo = 0; fifo < 2; fifo++) { // There are 2 receive FIFOs - if (HAL_FDCAN_GetRxFifoFillLevel(hcan, fifo) > 0) { - HAL_FDCAN_GetRxMessage(hcan, fifo, &pHeader, data); + if (HAL_FDCAN_GetRxFifoFillLevel(hfdcan, fifo) > 0) { + HAL_FDCAN_GetRxMessage(hfdcan, fifo, &pHeader, data); frame->id = pHeader.IdType == FDCAN_STANDARD_ID ? pHeader.Identifier: pHeader.Identifier; frame->dlc = pHeader.DataLength; From 5b69a985a93ee9c37d05f160d5d2a978fc3eb23b Mon Sep 17 00:00:00 2001 From: Rod Bayliss III Date: Sat, 16 May 2020 20:35:22 -0700 Subject: [PATCH 04/11] CAN is sending on the H7 :^) --- src/drivers/src/stm32h7xx.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/drivers/src/stm32h7xx.c b/src/drivers/src/stm32h7xx.c index 864ecb8..2696255 100644 --- a/src/drivers/src/stm32h7xx.c +++ b/src/drivers/src/stm32h7xx.c @@ -37,19 +37,21 @@ HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { } FDCAN_TxHeaderTypeDef pHeader; - uint32_t pTxMailbox = 0; - pHeader.DataLength = frame->dlc; - pHeader.Identifier= frame->id; + pHeader.DataLength = FDCAN_DLC_BYTES_8; + //pHeader.DataLength = frame->dlc; + pHeader.Identifier= 0x333; + //pHeader.Identifier= frame->id; pHeader.IdType = frame->extended ? FDCAN_EXTENDED_ID: FDCAN_STANDARD_ID; - //pHeader.RTR = CAN_RTR_DATA; // Data frame (as opposed to a remote frame) + pHeader.FDFormat = FDCAN_CLASSIC_CAN; + pHeader.TxFrameType = FDCAN_DATA_FRAME; //pHeader.TransmitGlobalTime = DISABLE; // Don't replace last 2 bytes of data with TX time. #ifdef USING_LOGGING_CALLBACK log_frame(frame, bus_num); #else UNUSED(bus_num); #endif - return HAL_FDCAN_AddMessageToTxBuffer(hfdcan, &pHeader, frame->data, &pTxMailbox); + return HAL_FDCAN_AddMessageToTxFifoQ(hfdcan, &pHeader, frame->data); } void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = CANlib_GetRawBus(bus); From c9b38d05301499b167af0fcb5fb6b9a2a1ffd070 Mon Sep 17 00:00:00 2001 From: Rod Bayliss III Date: Sun, 17 May 2020 09:15:27 -0700 Subject: [PATCH 05/11] Actually using the right can_id, datalength is hardcoded --- src/drivers/src/stm32h7xx.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/drivers/src/stm32h7xx.c b/src/drivers/src/stm32h7xx.c index 2696255..247226d 100644 --- a/src/drivers/src/stm32h7xx.c +++ b/src/drivers/src/stm32h7xx.c @@ -40,8 +40,7 @@ HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { pHeader.DataLength = FDCAN_DLC_BYTES_8; //pHeader.DataLength = frame->dlc; - pHeader.Identifier= 0x333; - //pHeader.Identifier= frame->id; + pHeader.Identifier= frame->id; pHeader.IdType = frame->extended ? FDCAN_EXTENDED_ID: FDCAN_STANDARD_ID; pHeader.FDFormat = FDCAN_CLASSIC_CAN; pHeader.TxFrameType = FDCAN_DATA_FRAME; From 03b8c6c354a9ba2c79e19791a2d861a80c50c1bf Mon Sep 17 00:00:00 2001 From: fischerm Date: Sat, 20 Feb 2021 16:27:57 -0500 Subject: [PATCH 06/11] empty can messages on inverter fixed, mingfei's a king :crown: --- src/drivers/src/stm32h7xx.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/drivers/src/stm32h7xx.c b/src/drivers/src/stm32h7xx.c index 247226d..19267f9 100644 --- a/src/drivers/src/stm32h7xx.c +++ b/src/drivers/src/stm32h7xx.c @@ -38,6 +38,8 @@ HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { FDCAN_TxHeaderTypeDef pHeader; + pHeader.ErrorStateIndicator = 0; + pHeader.BitRateSwitch = 0; pHeader.DataLength = FDCAN_DLC_BYTES_8; //pHeader.DataLength = frame->dlc; pHeader.Identifier= frame->id; From dd6e920cb96912b9887cd4b6a8d9512196bd785e Mon Sep 17 00:00:00 2001 From: Mingfei Date: Sat, 26 Feb 2022 19:57:53 -0500 Subject: [PATCH 07/11] Fix conversions to and from HAL-defined DLC macros --- src/drivers/src/stm32h7xx.c | 129 ++++++++++++++++++++++++++++++++++-- 1 file changed, 125 insertions(+), 4 deletions(-) diff --git a/src/drivers/src/stm32h7xx.c b/src/drivers/src/stm32h7xx.c index d3d7951..8eb04ea 100644 --- a/src/drivers/src/stm32h7xx.c +++ b/src/drivers/src/stm32h7xx.c @@ -6,6 +6,7 @@ #include "driver.h" #include #include +#include #ifdef USING_LOGGING_CALLBACK #include "log.h" #endif @@ -15,6 +16,10 @@ extern FDCAN_HandleTypeDef hfdcan1; extern FDCAN_HandleTypeDef hfdcan2; extern FDCAN_HandleTypeDef hfdcan3; +// these are needed because STM decided that making FDCAN_DLC_BYTES_3 actually be 3 made too much sense +static bool size_to_FDCAN_def(uint8_t size, uint32_t *def); +static bool FDCAN_def_to_size(uint32_t def, uint8_t *size); + HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = CANlib_GetRawBus(bus); int bus_num; @@ -40,13 +45,15 @@ HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { pHeader.ErrorStateIndicator = 0; pHeader.BitRateSwitch = 0; - pHeader.DataLength = FDCAN_DLC_BYTES_8; - //pHeader.DataLength = frame->dlc; pHeader.Identifier= frame->id; pHeader.IdType = frame->extended ? FDCAN_EXTENDED_ID: FDCAN_STANDARD_ID; pHeader.FDFormat = FDCAN_CLASSIC_CAN; pHeader.TxFrameType = FDCAN_DATA_FRAME; - //pHeader.TransmitGlobalTime = DISABLE; // Don't replace last 2 bytes of data with TX time. + // TODO: figure out if we need to set the MessageMarker field (no idea what that does) + if (!size_to_FDCAN_def(frame->dlc, &pHeader.DataLength)) { + return HAL_ERROR; + } + #ifdef USING_LOGGING_CALLBACK log_frame(frame, bus_num); #else @@ -77,7 +84,7 @@ void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = if (HAL_FDCAN_GetRxFifoFillLevel(hfdcan, fifo) > 0) { HAL_FDCAN_GetRxMessage(hfdcan, fifo, &pHeader, data); frame->id = pHeader.IdType == FDCAN_STANDARD_ID ? pHeader.Identifier: pHeader.Identifier; - frame->dlc = pHeader.DataLength; + FDCAN_def_to_size(pHeader.DataLength, &(frame->dlc)); memcpy(frame->data, data, sizeof(data)); frame->extended = pHeader.IdType == FDCAN_EXTENDED_ID; @@ -89,3 +96,117 @@ void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = Time_T CANlib_GetTick(void) { return HAL_GetTick(); } + +static bool size_to_FDCAN_def(uint8_t size, uint32_t *def) { + switch (size) { + case 0: + *def = FDCAN_DLC_BYTES_0; + break; + case 1: + *def = FDCAN_DLC_BYTES_1; + break; + case 2: + *def = FDCAN_DLC_BYTES_2; + break; + case 3: + *def = FDCAN_DLC_BYTES_3; + break; + case 4: + *def = FDCAN_DLC_BYTES_4; + break; + case 5: + *def = FDCAN_DLC_BYTES_5; + break; + case 6: + *def = FDCAN_DLC_BYTES_6; + break; + case 7: + *def = FDCAN_DLC_BYTES_7; + break; + case 8: + *def = FDCAN_DLC_BYTES_8; + break; + case 12: + *def = FDCAN_DLC_BYTES_12; + break; + case 16: + *def = FDCAN_DLC_BYTES_16; + break; + case 20: + *def = FDCAN_DLC_BYTES_20; + break; + case 24: + *def = FDCAN_DLC_BYTES_24; + break; + case 32: + *def = FDCAN_DLC_BYTES_32; + break; + case 48: + *def = FDCAN_DLC_BYTES_48; + break; + case 64: + *def = FDCAN_DLC_BYTES_64; + break; + default: + return false; + break; + } + return true; +} + +static bool FDCAN_def_to_size(uint32_t def, uint8_t *size) { + switch (def) { + case FDCAN_DLC_BYTES_0: + *size = 0; + break; + case FDCAN_DLC_BYTES_1: + *size = 1; + break; + case FDCAN_DLC_BYTES_2: + *size = 2; + break; + case FDCAN_DLC_BYTES_3: + *size = 3; + break; + case FDCAN_DLC_BYTES_4: + *size = 4; + break; + case FDCAN_DLC_BYTES_5: + *size = 5; + break; + case FDCAN_DLC_BYTES_6: + *size = 6; + break; + case FDCAN_DLC_BYTES_7: + *size = 7; + break; + case FDCAN_DLC_BYTES_8: + *size = 8; + break; + case FDCAN_DLC_BYTES_12: + *size = 12; + break; + case FDCAN_DLC_BYTES_16: + *size = 16; + break; + case FDCAN_DLC_BYTES_20: + *size = 20; + break; + case FDCAN_DLC_BYTES_24: + *size = 24; + break; + case FDCAN_DLC_BYTES_32: + *size = 32; + break; + case FDCAN_DLC_BYTES_48: + *size = 48; + break; + case FDCAN_DLC_BYTES_64: + *size = 64; + break; + default: + return false; + break; + } + return true; +} From 5375f1035de57e292fd487dc36145fd36619cbe6 Mon Sep 17 00:00:00 2001 From: Mingfei Date: Sat, 26 Feb 2022 20:02:03 -0500 Subject: [PATCH 08/11] Fix H7 driver to check the FIFOs correctly Note that receiving messages does not work without this change --- src/drivers/src/stm32h7xx.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/drivers/src/stm32h7xx.c b/src/drivers/src/stm32h7xx.c index 8eb04ea..0d48af5 100644 --- a/src/drivers/src/stm32h7xx.c +++ b/src/drivers/src/stm32h7xx.c @@ -78,18 +78,22 @@ void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = return; } + // There are 2 receive FIFOs + // note that the macros are not equal to 0 and 1 uint8_t data[8] = {}; FDCAN_RxHeaderTypeDef pHeader; - for (int fifo = 0; fifo < 2; fifo++) { // There are 2 receive FIFOs - if (HAL_FDCAN_GetRxFifoFillLevel(hfdcan, fifo) > 0) { - HAL_FDCAN_GetRxMessage(hfdcan, fifo, &pHeader, data); - frame->id = pHeader.IdType == FDCAN_STANDARD_ID ? pHeader.Identifier: pHeader.Identifier; - FDCAN_def_to_size(pHeader.DataLength, &(frame->dlc)); + uint32_t fifos[] = {FDCAN_RX_FIFO0, FDCAN_RX_FIFO1}; + for (unsigned int i = 0; i < sizeof(fifos) / sizeof(fifos[0]); i++) { + uint32_t fifo = fifos[i]; + if (HAL_FDCAN_GetRxFifoFillLevel(hfdcan, fifo) > 0) { + HAL_FDCAN_GetRxMessage(hfdcan, fifo, &pHeader, data); + frame->id = pHeader.IdType == FDCAN_STANDARD_ID ? pHeader.Identifier: pHeader.Identifier; + FDCAN_def_to_size(pHeader.DataLength, &(frame->dlc)); - memcpy(frame->data, data, sizeof(data)); - frame->extended = pHeader.IdType == FDCAN_EXTENDED_ID; - return; - } + memcpy(frame->data, data, sizeof(data)); + frame->extended = pHeader.IdType == FDCAN_EXTENDED_ID; + return; + } } } From 1d390d2c25b1b9acc5bc308df5494b1abddec69e Mon Sep 17 00:00:00 2001 From: Mingfei Date: Sat, 26 Feb 2022 20:02:59 -0500 Subject: [PATCH 09/11] Remove silly unneeded conditional --- src/drivers/src/stm32h7xx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/drivers/src/stm32h7xx.c b/src/drivers/src/stm32h7xx.c index 0d48af5..886b11a 100644 --- a/src/drivers/src/stm32h7xx.c +++ b/src/drivers/src/stm32h7xx.c @@ -87,7 +87,7 @@ void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = uint32_t fifo = fifos[i]; if (HAL_FDCAN_GetRxFifoFillLevel(hfdcan, fifo) > 0) { HAL_FDCAN_GetRxMessage(hfdcan, fifo, &pHeader, data); - frame->id = pHeader.IdType == FDCAN_STANDARD_ID ? pHeader.Identifier: pHeader.Identifier; + frame->id = pHeader.Identifier; FDCAN_def_to_size(pHeader.DataLength, &(frame->dlc)); memcpy(frame->data, data, sizeof(data)); From 992af88d44ad73ccc70c3947cf321d8d85847068 Mon Sep 17 00:00:00 2001 From: Mingfei Date: Sat, 26 Feb 2022 20:06:49 -0500 Subject: [PATCH 10/11] Remove unnecessary memcpy --- src/drivers/src/stm32f4xx.c | 5 +---- src/drivers/src/stm32h7xx.c | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/drivers/src/stm32f4xx.c b/src/drivers/src/stm32f4xx.c index ae01576..af3fcb7 100644 --- a/src/drivers/src/stm32f4xx.c +++ b/src/drivers/src/stm32f4xx.c @@ -68,15 +68,12 @@ void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { return; } - uint8_t data[8] = {}; CAN_RxHeaderTypeDef pHeader; for (int fifo = 0; fifo < 2; fifo++) { // There are 2 receive FIFOs if (HAL_CAN_GetRxFifoFillLevel(hcan, fifo) > 0) { - HAL_CAN_GetRxMessage(hcan, fifo, &pHeader, data); + HAL_CAN_GetRxMessage(hcan, fifo, &pHeader, frame->data); frame->id = pHeader.IDE == CAN_ID_STD ? pHeader.StdId : pHeader.ExtId; frame->dlc = pHeader.DLC; - - memcpy(frame->data, data, sizeof(data)); frame->extended = pHeader.IDE == CAN_ID_EXT; return; } diff --git a/src/drivers/src/stm32h7xx.c b/src/drivers/src/stm32h7xx.c index 886b11a..ac3f6aa 100644 --- a/src/drivers/src/stm32h7xx.c +++ b/src/drivers/src/stm32h7xx.c @@ -80,17 +80,14 @@ void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { CAN_Raw_Bus_T raw_bus = // There are 2 receive FIFOs // note that the macros are not equal to 0 and 1 - uint8_t data[8] = {}; FDCAN_RxHeaderTypeDef pHeader; uint32_t fifos[] = {FDCAN_RX_FIFO0, FDCAN_RX_FIFO1}; for (unsigned int i = 0; i < sizeof(fifos) / sizeof(fifos[0]); i++) { uint32_t fifo = fifos[i]; if (HAL_FDCAN_GetRxFifoFillLevel(hfdcan, fifo) > 0) { - HAL_FDCAN_GetRxMessage(hfdcan, fifo, &pHeader, data); + HAL_FDCAN_GetRxMessage(hfdcan, fifo, &pHeader, frame->data); frame->id = pHeader.Identifier; FDCAN_def_to_size(pHeader.DataLength, &(frame->dlc)); - - memcpy(frame->data, data, sizeof(data)); frame->extended = pHeader.IdType == FDCAN_EXTENDED_ID; return; } From 43339f78b23e5ab0f0b8f3058c3559bc46dcea00 Mon Sep 17 00:00:00 2001 From: Mingfei Date: Sat, 26 Feb 2022 20:22:33 -0500 Subject: [PATCH 11/11] Use HAL macros instead of 0 for some txHeader params I don't expect this to make a difference --- src/drivers/src/stm32h7xx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/drivers/src/stm32h7xx.c b/src/drivers/src/stm32h7xx.c index ac3f6aa..b09bd6b 100644 --- a/src/drivers/src/stm32h7xx.c +++ b/src/drivers/src/stm32h7xx.c @@ -43,8 +43,8 @@ HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { FDCAN_TxHeaderTypeDef pHeader; - pHeader.ErrorStateIndicator = 0; - pHeader.BitRateSwitch = 0; + pHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE; + pHeader.BitRateSwitch = FDCAN_BRS_OFF; pHeader.Identifier= frame->id; pHeader.IdType = frame->extended ? FDCAN_EXTENDED_ID: FDCAN_STANDARD_ID; pHeader.FDFormat = FDCAN_CLASSIC_CAN;