diff --git a/FT_CAN_AL.c b/FT_CAN_AL.c index 0b0b829..f1d7b7c 100644 --- a/FT_CAN_AL.c +++ b/FT_CAN_AL.c @@ -2,7 +2,7 @@ #include -#ifdef FTCAN_IS_BXCAN +#if defined(FTCAN_IS_BXCAN) static CAN_HandleTypeDef *hcan; HAL_StatusTypeDef ftcan_init(CAN_HandleTypeDef *handle) { @@ -43,7 +43,7 @@ HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) { } filter.FilterFIFOAssignment = CAN_FILTER_FIFO0; filter.FilterBank = next_filter_no / 2; - if (filter.FilterBank > FTCAN_MAX_FILTER_NO) { + if (filter.FilterBank > FTCAN_NUM_FILTERS + 1) { return HAL_ERROR; } filter.FilterMode = CAN_FILTERMODE_IDMASK; @@ -53,9 +53,13 @@ HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) { // Disable slave filters // TODO: Some STM32 have multiple CAN peripherals, and one uses the slave // filter bank - filter.SlaveStartFilterBank = FTCAN_MAX_FILTER_NO + 1; + filter.SlaveStartFilterBank = FTCAN_NUM_FILTERS; - return HAL_CAN_ConfigFilter(hcan, &filter); + HAL_StatusTypeDef status = HAL_CAN_ConfigFilter(hcan, &filter); + if (status == HAL_OK) { + next_filter_no++; + } + return status; } void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *handle) { @@ -74,7 +78,146 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *handle) { ftcan_msg_received_cb(header.StdId, header.DLC, data); } -#endif // CAN_IS_BXCAN +#elif defined(FTCAN_IS_FDCAN) +static FDCAN_HandleTypeDef *hcan; + +HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle) { + hcan = handle; + + HAL_StatusTypeDef status = + HAL_FDCAN_ActivateNotification(hcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); + if (status != HAL_OK) { + return status; + } + // Reject non-matching messages + status = + HAL_FDCAN_ConfigGlobalFilter(hcan, FDCAN_REJECT, FDCAN_REJECT, + FDCAN_REJECT_REMOTE, FDCAN_REJECT_REMOTE); + if (status != HAL_OK) { + return status; + } + + return HAL_FDCAN_Start(hcan); +} + +HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data, + size_t datalen) { + static FDCAN_TxHeaderTypeDef header; + header.Identifier = id; + header.IdType = FDCAN_STANDARD_ID; + header.TxFrameType = FDCAN_DATA_FRAME; + switch (datalen) { + case 0: + header.DataLength = FDCAN_DLC_BYTES_0; + break; + case 1: + header.DataLength = FDCAN_DLC_BYTES_1; + break; + case 2: + header.DataLength = FDCAN_DLC_BYTES_2; + break; + case 3: + header.DataLength = FDCAN_DLC_BYTES_3; + break; + case 4: + header.DataLength = FDCAN_DLC_BYTES_4; + break; + case 5: + header.DataLength = FDCAN_DLC_BYTES_5; + break; + case 6: + header.DataLength = FDCAN_DLC_BYTES_6; + break; + case 7: + header.DataLength = FDCAN_DLC_BYTES_7; + break; + case 8: + default: + header.DataLength = FDCAN_DLC_BYTES_8; + break; + } + header.ErrorStateIndicator = FDCAN_ESI_PASSIVE; + header.BitRateSwitch = FDCAN_BRS_OFF; + header.FDFormat = FDCAN_CLASSIC_CAN; + header.TxEventFifoControl = FDCAN_NO_TX_EVENTS; + + return HAL_FDCAN_AddMessageToTxFifoQ(hcan, &header, data); +} + +HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) { + static uint32_t next_filter_no = 0; + static FDCAN_FilterTypeDef filter; + filter.IdType = FDCAN_STANDARD_ID; + filter.FilterIndex = next_filter_no; + if (filter.FilterIndex > FTCAN_NUM_FILTERS + 1) { + return HAL_ERROR; + } + filter.FilterType = FDCAN_FILTER_MASK; + filter.FilterConfig = FDCAN_FILTER_TO_RXFIFO0; + filter.FilterID1 = id; + filter.FilterID2 = mask; + + HAL_StatusTypeDef status = HAL_FDCAN_ConfigFilter(hcan, &filter); + if (status == HAL_OK) { + next_filter_no++; + } + return status; +} + +void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle, + uint32_t RxFifo0ITs) { + if (handle != hcan || (RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) == RESET) { + return; + } + + static FDCAN_RxHeaderTypeDef header; + static uint8_t data[8]; + if (HAL_FDCAN_GetRxMessage(hcan, FDCAN_RX_FIFO0, &header, data) != HAL_OK) { + return; + } + + if (header.FDFormat != FDCAN_CLASSIC_CAN || + header.RxFrameType != FDCAN_DATA_FRAME || + header.IdType != FDCAN_STANDARD_ID) { + return; + } + + size_t datalen; + switch (header.DataLength) { + case FDCAN_DLC_BYTES_0: + datalen = 0; + break; + case FDCAN_DLC_BYTES_1: + datalen = 1; + break; + case FDCAN_DLC_BYTES_2: + datalen = 2; + break; + case FDCAN_DLC_BYTES_3: + datalen = 3; + break; + case FDCAN_DLC_BYTES_4: + datalen = 4; + break; + case FDCAN_DLC_BYTES_5: + datalen = 5; + break; + case FDCAN_DLC_BYTES_6: + datalen = 6; + break; + case FDCAN_DLC_BYTES_7: + datalen = 7; + break; + case FDCAN_DLC_BYTES_8: + datalen = 8; + break; + default: + return; + } + + ftcan_msg_received_cb(header.Identifier, datalen, data); +} +#endif __weak void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data) {} diff --git a/FT_CAN_AL.h b/FT_CAN_AL.h index 1653f97..cc84ffd 100644 --- a/FT_CAN_AL.h +++ b/FT_CAN_AL.h @@ -4,14 +4,21 @@ #if defined(STM32F3) #include "stm32f3xx_hal.h" #define FTCAN_IS_BXCAN -#define FTCAN_MAX_FILTER_NO 13 +#define FTCAN_NUM_FILTERS 13 +#elif defined(STM32H7) +#include "stm32h7xx_hal.h" +#define FTCAN_IS_FDCAN #else #error "Couldn't detect STM family" -#endif // STM32F3 +#endif -#ifdef FTCAN_IS_BXCAN +#if defined(FTCAN_IS_BXCAN) HAL_StatusTypeDef ftcan_init(CAN_HandleTypeDef *handle); -#endif // CAN_IS_BXCAN +#elif defined(FTCAN_IS_FDCAN) +HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle); +#else +#error "Unknown CAN peripheral" +#endif HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data, size_t datalen); diff --git a/README.md b/README.md index d189681..a26ec82 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,6 @@ This repository contains an abstraction layer to provide a simplified & unified interface to the STM32 bxCAN and FDCAN peripherals. -**Warning**: Currently, only bxCAN (for the STM32F3 series) is supported. - ## Installation Simply add the repository to your `Core/Lib` directory. You can also add it as a @@ -18,7 +16,12 @@ The library needs to be told what STM family you're using, so make sure one of the following symbols is defined when `FT_CAN_AL.c` is compiled or `FT_CAN_AL.h` is included: -- STM32F3 +- `STM32F3` +- `STM32H7` + +When using the FDCAN peripheral (H7 series), you also need to define +`FTCAN_NUM_FILTERS` (and set it to the value of "Std Filters Nbr" you configured +in your `.ioc`). ## Usage