From f7f1b0bde0dcb69139ca90f9a6805b74463419e5 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Wed, 11 Sep 2024 16:28:58 +0200 Subject: [PATCH] feat(can): Split filtering for multiple can peripherals --- include/sta/devices/stm32/can.hpp | 5 ++++- src/devices/stm32/can.cpp | 32 ++++++++++++++++++------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/include/sta/devices/stm32/can.hpp b/include/sta/devices/stm32/can.hpp index eff37a4..4da17b3 100644 --- a/include/sta/devices/stm32/can.hpp +++ b/include/sta/devices/stm32/can.hpp @@ -46,8 +46,10 @@ namespace sta public: /** * @param handle CAN handle + * + * @param filter_start Start index for filters (default: 0 for can1, 14 for can2) */ - STM32CanController(CAN_HandleTypeDef * handle); + STM32CanController(CAN_HandleTypeDef * handle, uint32_t filter_start = 0); /** * @brief Enable RX pending interrupts. @@ -97,6 +99,7 @@ namespace sta private: CAN_HandleTypeDef * handle_; /**< CAN handle */ CAN_FilterTypeDef filters_[MAX_FILTER_COUNT]; /**< Filter settings */ + uint32_t filter_start_; /**< Start index for filters */ }; diff --git a/src/devices/stm32/can.cpp b/src/devices/stm32/can.cpp index ffd6a41..c396625 100644 --- a/src/devices/stm32/can.cpp +++ b/src/devices/stm32/can.cpp @@ -7,8 +7,8 @@ namespace sta { - STM32CanController::STM32CanController(CAN_HandleTypeDef * handle) - : handle_{handle} + STM32CanController::STM32CanController(CAN_HandleTypeDef * handle, uint32_t filter_start) + : handle_{handle}, filter_start_{filter_start} { initFilters(); } @@ -141,7 +141,7 @@ namespace sta void STM32CanController::clearFilters() { - for (uint32_t i = 0; i < MAX_FILTER_COUNT; ++i) + for (uint32_t i = filter_start_; i < filter_start_ + MAX_FILTER_COUNT; ++i) { CAN_FilterTypeDef * config = &filters_[i]; @@ -157,17 +157,23 @@ namespace sta void STM32CanController::initFilters() { - for (uint32_t i = 0; i < MAX_FILTER_COUNT; ++i) - { - CAN_FilterTypeDef * config = &filters_[i]; - - config->FilterBank = i; - config->FilterMode = CAN_FILTERMODE_IDMASK; - config->FilterScale = CAN_FILTERSCALE_32BIT; - config->FilterActivation = CAN_FILTER_DISABLE; - config->SlaveStartFilterBank = MAX_FILTER_COUNT; - HAL_CAN_ConfigFilter(handle_, config); + CAN_FilterTypeDef * config = &filters_[filter_start_]; + if (filter_start_ == 0){ + config->FilterBank = 0; + }else{ + // Split can peripheral bank for any STM F4 series supporting multiple CAN peripherals + config->FilterBank = filter_start_+1; } + + config->FilterMode = CAN_FILTERMODE_IDMASK; + config->FilterScale = CAN_FILTERSCALE_32BIT; + config->FilterActivation = ENABLE; + config->FilterIdHigh = 0x0000; + config->FilterIdLow = 0x0000; + config->FilterMaskIdHigh = 0x0000; + config->FilterMaskIdLow = 0x0000; + config->SlaveStartFilterBank = filter_start_; + HAL_CAN_ConfigFilter(handle_, config); } CanPendingRxFifos STM32CanController::getPendingRxFifos(){