From a6c92c9adca60b6e1c540aa9cdb9fa3a1105d5af Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Sat, 24 Aug 2024 11:34:00 +0200 Subject: [PATCH 1/7] feat: secondary can bus callback --- include/sta/devices/stm32/can.hpp | 9 +++++++++ src/devices/stm32/can.cpp | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/sta/devices/stm32/can.hpp b/include/sta/devices/stm32/can.hpp index 598b2bd..eff37a4 100644 --- a/include/sta/devices/stm32/can.hpp +++ b/include/sta/devices/stm32/can.hpp @@ -117,6 +117,15 @@ namespace sta * @ingroup sta_core_stm32_can */ void CanBus_RxPendingCallback(); + + /** + * @brief Interrupt handler for pending RX frames. Secondary handler. + * + * May be implemented by application. + * + * @ingroup sta_core_stm32_can + */ + void CanBus_RxPendingCallbackSecondary(); #endif // STA_STM32_CAN_GLOBAL } // namespace sta diff --git a/src/devices/stm32/can.cpp b/src/devices/stm32/can.cpp index 47e9c97..ffd6a41 100644 --- a/src/devices/stm32/can.cpp +++ b/src/devices/stm32/can.cpp @@ -215,6 +215,10 @@ namespace sta STA_WEAK void CanBus_RxPendingCallback(uint32_t fifo) {} + + STA_WEAK + void CanBus_RxPendingCallbackSecondary(uint32_t fifo) + {} } // namespace sta @@ -226,6 +230,10 @@ extern "C" { sta::CanBus_RxPendingCallback(CAN_RX_FIFO0); } + else + { + sta::CanBus_RxPendingCallbackSecondary(CAN_RX_FIFO0); + } } void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan) @@ -234,6 +242,9 @@ extern "C" { sta::CanBus_RxPendingCallback(CAN_RX_FIFO1); } + else{ + sta::CanBus_RxPendingCallbackSecondary(CAN_RX_FIFO1); + } } } From f7f1b0bde0dcb69139ca90f9a6805b74463419e5 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Wed, 11 Sep 2024 16:28:58 +0200 Subject: [PATCH 2/7] 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(){ From b0b775ffb09a17d9fe90cd373c77842de5ca6d1a Mon Sep 17 00:00:00 2001 From: dario Date: Thu, 1 Aug 2024 15:06:07 +0200 Subject: [PATCH 3/7] Incredibly ugly workaround --- src/debug/spatz.cpp | 35 ++++++++++++++++++++++++++-------- src/devices/stm32/bus/uart.cpp | 2 +- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/debug/spatz.cpp b/src/debug/spatz.cpp index 2f22c0b..a665b3d 100644 --- a/src/debug/spatz.cpp +++ b/src/debug/spatz.cpp @@ -6,6 +6,10 @@ # error "The ID used to communicate SPATZ initialization was not defined." #endif // STA_INIT_SPATZ_ID +#include +#include +#include + namespace sta { namespace spatz @@ -30,26 +34,41 @@ namespace sta { sta::lock_guard lock(*mutex_); - Debug->println("%d", id); - Debug->read(buffer, length); + Debug->printf("%d", id); + Debug->read(buffer, length); } void request(uint8_t id, float * buffer, size_t length) { - sta::lock_guard lock(*mutex_); + mutex_->acquire(); - // Debug->println(id, IntegerBase::DEC); + bool success = false; - Debug->printf("%d", id); - Debug->read(buffer, length); + while (!success) + { + Debug->printf("%d", id); + + HAL_StatusTypeDef state = HAL_UART_Receive(&huart1, reinterpret_cast(buffer), length * sizeof(float), 100); + + if (state == HAL_TIMEOUT) + { + success = false; + Debug->println(">ERROR"); + break; + } + + success = true; + } + + mutex_->release(); } void request(uint8_t id, double * buffer, size_t length) { sta::lock_guard lock(*mutex_); - Debug->println("%d", id); - Debug->read(buffer, length); + Debug->printf("%d", id); + Debug->read(buffer, length); } } // namespace spatz } // namespace sta diff --git a/src/devices/stm32/bus/uart.cpp b/src/devices/stm32/bus/uart.cpp index 300f49f..d8eb3e6 100644 --- a/src/devices/stm32/bus/uart.cpp +++ b/src/devices/stm32/bus/uart.cpp @@ -33,7 +33,7 @@ namespace sta { STA_ASSERT(buffer != nullptr); - HAL_UART_Receive(handle_, buffer, size, HAL_MAX_DELAY); + HAL_UART_Receive(handle_, buffer, size, 1); } void STM32UART::fill(uint8_t value, size_t count) From ac1344747bf834d9b90e76e49cc0ec712aeb2e35 Mon Sep 17 00:00:00 2001 From: dario Date: Sat, 3 Aug 2024 22:47:01 +0200 Subject: [PATCH 4/7] Updated uart comunication for spatz --- include/sta/debug/spatz.hpp | 3 ++- src/debug/profile.cpp | 2 +- src/debug/spatz.cpp | 37 ++++++++++++++++--------------------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/include/sta/debug/spatz.hpp b/include/sta/debug/spatz.hpp index 7b69f4c..eb3ec98 100644 --- a/include/sta/debug/spatz.hpp +++ b/include/sta/debug/spatz.hpp @@ -2,6 +2,7 @@ #define STA_CORE_SPATZ_HPP #include +#include #include #ifdef STA_SPATZ_ENABLED @@ -20,7 +21,7 @@ namespace sta * * @param mutex A mutex used to make SPATZ thread-safe. */ - void init(Mutex * mutex); + void init(Mutex * mutex, Signal * signal); /** * @brief Request bytes for a specific sensor id via the printable. diff --git a/src/debug/profile.cpp b/src/debug/profile.cpp index 4e7eec6..e1e541a 100644 --- a/src/debug/profile.cpp +++ b/src/debug/profile.cpp @@ -22,7 +22,7 @@ namespace sta Profiler::~Profiler() { - STA_DEBUG_PRINTF("[PROFILER] %s took %d us", name_, timeUs() - start_); + STA_DEBUG_PRINTF(">[PROFILER] %s took %d us", name_, timeUs() - start_); } } // namespace sta diff --git a/src/debug/spatz.cpp b/src/debug/spatz.cpp index a665b3d..6bf5880 100644 --- a/src/debug/spatz.cpp +++ b/src/debug/spatz.cpp @@ -6,19 +6,20 @@ # error "The ID used to communicate SPATZ initialization was not defined." #endif // STA_INIT_SPATZ_ID -#include -#include #include namespace sta { namespace spatz { - sta::Mutex * mutex_; + sta::Mutex * mutex_ = nullptr; + sta::Signal * signal_ = nullptr; - void init(sta::Mutex * mutex) + void init(sta::Mutex * mutex, Signal * signal) { mutex_ = mutex; + signal_ = signal; + uint8_t msg = 0xFF; sta::lock_guard lock(*mutex_); @@ -42,23 +43,9 @@ namespace sta { mutex_->acquire(); - bool success = false; - - while (!success) - { - Debug->printf("%d", id); - - HAL_StatusTypeDef state = HAL_UART_Receive(&huart1, reinterpret_cast(buffer), length * sizeof(float), 100); - - if (state == HAL_TIMEOUT) - { - success = false; - Debug->println(">ERROR"); - break; - } - - success = true; - } + HAL_UART_Receive_IT(&huart1, reinterpret_cast(buffer), length * sizeof(float)); + Debug->printf("%d", id); + signal_->wait(); mutex_->release(); } @@ -73,6 +60,14 @@ namespace sta } // namespace spatz } // namespace sta + +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) +{ + if (sta::spatz::signal_ != nullptr) + sta::spatz::signal_->notify(); +} + + #else namespace sta From f57212bf6d38a7b3411cdfcc029361b9f94f8d8c Mon Sep 17 00:00:00 2001 From: dario Date: Fri, 16 Aug 2024 21:49:45 +0200 Subject: [PATCH 5/7] Some fixes for UART receiving with interrupts for SPATZ --- include/sta/debug/spatz.hpp | 5 ++++- include/sta/event.hpp | 2 +- src/debug/spatz.cpp | 25 +++++++++++++++---------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/include/sta/debug/spatz.hpp b/include/sta/debug/spatz.hpp index eb3ec98..2411b69 100644 --- a/include/sta/debug/spatz.hpp +++ b/include/sta/debug/spatz.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #ifdef STA_SPATZ_ENABLED @@ -21,7 +22,7 @@ namespace sta * * @param mutex A mutex used to make SPATZ thread-safe. */ - void init(Mutex * mutex, Signal * signal); + void init(Mutex * mutex, Event * signal); /** * @brief Request bytes for a specific sensor id via the printable. @@ -49,6 +50,8 @@ namespace sta * @param length The number of doubles to request. */ void request(uint8_t id, double * buffer, size_t length); + + void notify(uint8_t id); } // namespace spatz } // namespace sta diff --git a/include/sta/event.hpp b/include/sta/event.hpp index 56b05a9..7ef5c44 100644 --- a/include/sta/event.hpp +++ b/include/sta/event.hpp @@ -40,7 +40,7 @@ namespace sta * @param flags flag nr. to wait for. * @param timeout timeout in ms., default to forever. */ - virtual uint32_t wait(uint32_t flags, uint32_t timeout = osWaitForever) = 0; + virtual uint32_t wait(uint32_t flags, uint32_t timeout = 0xFFFFFFFF) = 0; }; } // namespace sta diff --git a/src/debug/spatz.cpp b/src/debug/spatz.cpp index 6bf5880..ccff7e7 100644 --- a/src/debug/spatz.cpp +++ b/src/debug/spatz.cpp @@ -12,18 +12,16 @@ namespace sta { namespace spatz { - sta::Mutex * mutex_ = nullptr; - sta::Signal * signal_ = nullptr; + Mutex * mutex_ = nullptr; + Event * signal_ = nullptr; - void init(sta::Mutex * mutex, Signal * signal) + void init(sta::Mutex * mutex, Event * signal) { mutex_ = mutex; signal_ = signal; uint8_t msg = 0xFF; - sta::lock_guard lock(*mutex_); - // Wait until SPATZ sends the init byte. while (msg != STA_INIT_SPATZ_ID) { @@ -41,13 +39,11 @@ namespace sta void request(uint8_t id, float * buffer, size_t length) { - mutex_->acquire(); + sta::lock_guard lock(*mutex_); HAL_UART_Receive_IT(&huart1, reinterpret_cast(buffer), length * sizeof(float)); Debug->printf("%d", id); - signal_->wait(); - - mutex_->release(); + signal_->wait(0x01); } void request(uint8_t id, double * buffer, size_t length) @@ -57,6 +53,11 @@ namespace sta Debug->printf("%d", id); Debug->read(buffer, length); } + + void notify(uint8_t id) + { + Debug->printf("%d", id); + } } // namespace spatz } // namespace sta @@ -64,7 +65,11 @@ namespace sta void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (sta::spatz::signal_ != nullptr) - sta::spatz::signal_->notify(); + { + sta::spatz::signal_->set(0x01); + sta::spatz::signal_->clear(0x01); + } + } From 6d6840a97432739a74fb89e5e35de0f0e66f1160 Mon Sep 17 00:00:00 2001 From: dario Date: Mon, 19 Aug 2024 22:21:48 +0200 Subject: [PATCH 6/7] Added two macros for SPATZ init and notify --- include/sta/debug/spatz.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/sta/debug/spatz.hpp b/include/sta/debug/spatz.hpp index 2411b69..0dce873 100644 --- a/include/sta/debug/spatz.hpp +++ b/include/sta/debug/spatz.hpp @@ -55,6 +55,10 @@ namespace sta } // namespace spatz } // namespace sta +#define STA_SPATZ_NOTIFY(id) sta::spatz::notify(id) + +#define STA_SPATZ_INIT(mutex, signal) sta::spatz::init(mutex, signal) + #else namespace sta @@ -65,6 +69,10 @@ namespace sta } // namespace spatz } // namespace sta +#define STA_SPATZ_NOTIFY(id) ((void)0) + +#define STA_SPATZ_INIT(mutex, signal) ((void)0) + #endif // STA_SPATZ_ENABLED #endif // STA_CORE_SPATZ_HPP From 5f0cb1d46fbc18433f5c0315ec6f2da18ac5cbbe Mon Sep 17 00:00:00 2001 From: dario Date: Thu, 3 Oct 2024 14:20:06 +0200 Subject: [PATCH 7/7] Removed race condition --- src/bus/interface.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bus/interface.cpp b/src/bus/interface.cpp index 8560b3b..8f4809e 100644 --- a/src/bus/interface.cpp +++ b/src/bus/interface.cpp @@ -18,8 +18,8 @@ namespace sta void Interface::release() { + acquired_ = false; mutex_->release(); - acquired_ = false; } bool Interface::isAcquired()