Fixes to make I2C work for SMT32

This commit is contained in:
dario 2023-08-10 22:54:18 +02:00
parent 266cc46a09
commit 8ffba482de
9 changed files with 64 additions and 53 deletions

View File

@ -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

View File

@ -8,4 +8,4 @@
// #define STA_ASSERT_FORCE
#endif // STA_CONFIG_HPP
#endif // STA_CONFIG_HPP

View File

@ -3,8 +3,13 @@
#include <sta/config.hpp>
#ifdef STA_PLATFORM_STM32
# include <sta/devices/stm32/hal.hpp>
# ifdef HAL_ADC_MODULE_ENABLED
# define STA_STM32_ADC_ENABLED
# endif
#endif
#include <sta/devices/stm32/hal.hpp>
#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

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -1,6 +1,6 @@
#include <sta/devices/stm32/adc.hpp>
#ifdef STA_PLATFORM_STM32
#ifdef STA_STM32_ADC_ENABLED
#include <sta/debug/assert.hpp>
@ -30,4 +30,4 @@ namespace sta
}
} // namespace sta
#endif // STA_PLATFORM_STM32
#endif // STA_STM32_ADC_ENABLED

View File

@ -3,7 +3,7 @@
#include <sta/debug/assert.hpp>
#include <cstring>
#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<uint8_t *>(&value), 2, timeout_);
res = HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast<uint8_t *>(&value), 2);
} else {
res = HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast<uint8_t *>(&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

View File

@ -1,4 +1,5 @@
#include <sta/devices/stm32/delay.hpp>
#ifdef STA_PLATFORM_STM32
#include <sta/devices/stm32/hal.hpp>
@ -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