From 8ffba482deb91de69c408c78d78b22989c03e537 Mon Sep 17 00:00:00 2001 From: dario Date: Thu, 10 Aug 2023 22:54:18 +0200 Subject: [PATCH] Fixes to make I2C work for SMT32 --- include/sta/bus/interface.hpp | 4 +- include/sta/config.hpp | 2 +- include/sta/devices/stm32/adc.hpp | 9 +++- include/sta/devices/stm32/bus/i2c.hpp | 2 +- src/bus/device.cpp | 12 ++--- src/bus/interface.cpp | 8 ++-- src/devices/stm32/adc.cpp | 4 +- src/devices/stm32/bus/i2c.cpp | 8 ++-- src/devices/stm32/delay.cpp | 68 ++++++++++++++------------- 9 files changed, 64 insertions(+), 53 deletions(-) diff --git a/include/sta/bus/interface.hpp b/include/sta/bus/interface.hpp index a26151a..6e53b9e 100644 --- a/include/sta/bus/interface.hpp +++ b/include/sta/bus/interface.hpp @@ -75,10 +75,10 @@ namespace sta /** * @returns true if the interface has been aquired. */ - bool isAquired(); + bool isAcquired(); private: Mutex * mutex_; - bool aquired_ = false; + bool acquired_ = false; }; } // namespace sta diff --git a/include/sta/config.hpp b/include/sta/config.hpp index b9d1958..f798ba8 100644 --- a/include/sta/config.hpp +++ b/include/sta/config.hpp @@ -8,4 +8,4 @@ // #define STA_ASSERT_FORCE -#endif // STA_CONFIG_HPP \ No newline at end of file +#endif // STA_CONFIG_HPP diff --git a/include/sta/devices/stm32/adc.hpp b/include/sta/devices/stm32/adc.hpp index b1411be..c04e2c6 100644 --- a/include/sta/devices/stm32/adc.hpp +++ b/include/sta/devices/stm32/adc.hpp @@ -3,8 +3,13 @@ #include #ifdef STA_PLATFORM_STM32 +# include +# ifdef HAL_ADC_MODULE_ENABLED +# define STA_STM32_ADC_ENABLED +# endif +#endif -#include +#if defined(STA_STM32_ADC_ENABLED) || defined(DOXYGEN) namespace sta { @@ -34,6 +39,6 @@ namespace sta }; } // namespace sta -#endif // STA_PLATFORM_STM32 +#endif // STA_STM32_ADC_ENABLED #endif // STA_CORE_STM32_ADC_HPP diff --git a/include/sta/devices/stm32/bus/i2c.hpp b/include/sta/devices/stm32/bus/i2c.hpp index b2b34d1..ce71294 100644 --- a/include/sta/devices/stm32/bus/i2c.hpp +++ b/include/sta/devices/stm32/bus/i2c.hpp @@ -30,7 +30,7 @@ namespace sta void fill(uint8_t value, size_t count) override; private: I2C_HandleTypeDef * handle_; - const uint32_t timeout_ = HAL_MAX_DELAY; + const uint32_t timeout_ = 1000; }; class STM32I2CDevice : public I2CDevice diff --git a/src/bus/device.cpp b/src/bus/device.cpp index 7696143..e42646a 100644 --- a/src/bus/device.cpp +++ b/src/bus/device.cpp @@ -25,7 +25,7 @@ namespace sta void Device::transfer(uint8_t value) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); intf_->transfer(value); @@ -33,7 +33,7 @@ namespace sta void Device::transfer16(uint16_t value) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); intf_->transfer16(value); @@ -41,7 +41,7 @@ namespace sta void Device::transfer(const uint8_t * buffer, size_t size) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); STA_ASSERT(buffer != nullptr); @@ -50,7 +50,7 @@ namespace sta void Device::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); STA_ASSERT(txBuffer != nullptr); STA_ASSERT(rxBuffer != nullptr); @@ -60,7 +60,7 @@ namespace sta void Device::receive(uint8_t * buffer, size_t size) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); STA_ASSERT(buffer != nullptr); @@ -69,7 +69,7 @@ namespace sta void Device::fill(uint8_t value, size_t count) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); intf_->fill(value, count); diff --git a/src/bus/interface.cpp b/src/bus/interface.cpp index 627e863..b7ff0a7 100644 --- a/src/bus/interface.cpp +++ b/src/bus/interface.cpp @@ -5,7 +5,7 @@ namespace sta { Interface::Interface(Mutex * mutex) - : mutex_{mutex} + : mutex_{mutex}, acquired_{false} { STA_ASSERT(mutex != nullptr); } @@ -14,16 +14,18 @@ namespace sta { if (mutex_ != nullptr) mutex_->acquire(); + acquired_ = true; } void Interface::release() { if (mutex_ != nullptr) + acquired_ = false; mutex_->release(); } - bool Interface::isAquired() + bool Interface::isAcquired() { - return aquired_; + return acquired_; } } // namespace sta diff --git a/src/devices/stm32/adc.cpp b/src/devices/stm32/adc.cpp index 01c9eb7..5c5d663 100644 --- a/src/devices/stm32/adc.cpp +++ b/src/devices/stm32/adc.cpp @@ -1,6 +1,6 @@ #include -#ifdef STA_PLATFORM_STM32 +#ifdef STA_STM32_ADC_ENABLED #include @@ -30,4 +30,4 @@ namespace sta } } // namespace sta -#endif // STA_PLATFORM_STM32 \ No newline at end of file +#endif // STA_STM32_ADC_ENABLED diff --git a/src/devices/stm32/bus/i2c.cpp b/src/devices/stm32/bus/i2c.cpp index 9577eec..5eae138 100644 --- a/src/devices/stm32/bus/i2c.cpp +++ b/src/devices/stm32/bus/i2c.cpp @@ -3,7 +3,7 @@ #include #include -#ifdef STA_PLATFORM_STM32 +#ifdef STA_STM32_I2C_ENABLED namespace sta { @@ -42,7 +42,7 @@ namespace sta } else { if (master_) { - res = HAL_I2C_Slave_Transmit(handle_, reinterpret_cast(&value), 2, timeout_); + res = HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast(&value), 2); } else { res = HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast(&value), 2); } @@ -93,7 +93,7 @@ namespace sta HAL_StatusTypeDef res; if (blocking_) { - if (!master_) { + if (master_) { res = HAL_I2C_Master_Receive(handle_, address_, buffer, size, timeout_); } else { res = HAL_I2C_Slave_Receive(handle_, buffer, size, timeout_); @@ -129,4 +129,4 @@ namespace sta } // namespace sta -#endif // STA_PLATFORM_STM32 +#endif // STA_STM32_I2C_ENABLED diff --git a/src/devices/stm32/delay.cpp b/src/devices/stm32/delay.cpp index a7bfd3c..8f51d73 100644 --- a/src/devices/stm32/delay.cpp +++ b/src/devices/stm32/delay.cpp @@ -1,4 +1,5 @@ #include + #ifdef STA_PLATFORM_STM32 #include @@ -29,44 +30,47 @@ namespace sta { uint32_t gDelayUsMul = 1; + bool isValidDelayUsTIM() + { + // Get PCLK multiplier for TIM clock + uint32_t pclkMul = 1; + switch (STA_STM32_DELAY_US_TIM.Init.ClockDivision) + { + case TIM_CLOCKDIVISION_DIV1: + pclkMul = 1; + break; + case TIM_CLOCKDIVISION_DIV2: + pclkMul = 2; + break; + case TIM_CLOCKDIVISION_DIV4: + pclkMul = 4; + break; + default: + STA_ASSERT(false); + STA_UNREACHABLE(); + } + + // Calculate TIM clock frequency + uint32_t clkFreq = pclkMul * STA_STM32_GET_HANDLE_PCLK_FREQ_FN(STA_STM32_DELAY_US_TIM)(); + // Calculate update frequency based on prescaler value + uint32_t prescaler = (STA_STM32_DELAY_US_TIM.Init.Prescaler) ? STA_STM32_DELAY_US_TIM.Init.Prescaler : 1; + uint32_t updateFreq = clkFreq / prescaler; + + gDelayUsMul = updateFreq / 1000000; + + // TIM must have at least microsecond precision (>= 1 MHz frequency) + return (updateFreq >= 1000000); + } + void delayUs(uint32_t us) { + // Check if the specified timer is usable for microsecond delays. + STA_ASSERT(isValidDelayUsTIM()); + __HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0); while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us * gDelayUsMul); } - - bool isValidDelayUsTIM() - { - // Get PCLK multiplier for TIM clock - uint32_t pclkMul = 1; - switch (STA_STM32_DELAY_US_TIM.Init.ClockDivision) - { - case TIM_CLOCKDIVISION_DIV1: - pclkMul = 1; - break; - case TIM_CLOCKDIVISION_DIV2: - pclkMul = 2; - break; - case TIM_CLOCKDIVISION_DIV4: - pclkMul = 4; - break; - default: - STA_ASSERT(false); - STA_UNREACHABLE(); - } - - // Calculate TIM clock frequency - uint32_t clkFreq = pclkMul * STA_STM32_GET_HANDLE_PCLK_FREQ_FN(STA_STM32_DELAY_US_TIM)(); - // Calculate update frequency based on prescaler value - uint32_t prescaler = (STA_STM32_DELAY_US_TIM.Init.Prescaler) ? STA_STM32_DELAY_US_TIM.Init.Prescaler : 1; - uint32_t updateFreq = clkFreq / prescaler; - - gDelayUsMul = updateFreq / 1000000; - - // TIM must have at least microsecond precision (>= 1 MHz frequency) - return (updateFreq >= 1000000); - } } // namespace sta #endif // STA_STM32_DELAY_US_TIM