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

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