Merge branch 'main' into arduino-support

This commit is contained in:
dario
2024-06-03 22:56:37 +02:00
22 changed files with 382 additions and 42 deletions

View File

@@ -23,7 +23,9 @@ namespace sta
void PrintableUART::print(const char * str, size_t length)
{
intf_->acquire();
intf_->transfer(reinterpret_cast<const uint8_t *>(str), length);
intf_->release();
}
} // namespace sta

29
src/debug/profile.cpp Normal file
View File

@@ -0,0 +1,29 @@
/*
* profiler.cpp
*
* Created on: May 22, 2024
* Author: Dario
*/
#include <sta/debug/profile.hpp>
#ifdef STA_PROFILING_ENABLED
#include <sta/time.hpp>
namespace sta
{
Profiler::Profiler(const char* name)
: name_{name},
start_{timeUs()}
{
}
Profiler::~Profiler()
{
STA_DEBUG_PRINTF("[PROFILER] %s took %d us", name_, timeUs() - start_);
}
} // namespace sta
#endif // STA_PROFILING_ENABLED

View File

@@ -17,6 +17,17 @@ namespace sta
HAL_ADC_Start(handle_);
}
void STM32ADC::startDMA(uint32_t * buffer, size_t length)
{
STA_ASSERT(buffer != nullptr);
STA_ASSERT(length != 0);
STA_ASSERT(handle_->DMA_Handle != nullptr);
HAL_StatusTypeDef res = HAL_ADC_Start_DMA(handle_, buffer, length);
STA_ASSERT(res == HAL_OK);
}
void STM32ADC::poll(uint32_t timeout)
{
HAL_StatusTypeDef res = HAL_ADC_PollForConversion(handle_, timeout);

View File

@@ -166,21 +166,54 @@ namespace sta
config->FilterScale = CAN_FILTERSCALE_32BIT;
config->FilterActivation = CAN_FILTER_DISABLE;
config->SlaveStartFilterBank = MAX_FILTER_COUNT;
HAL_CAN_ConfigFilter(handle_, config);
}
}
CanPendingRxFifos STM32CanController::getPendingRxFifos(){
uint32_t rxFlags = 0;
// Conditions to set the least significant bits
if (HAL_CAN_GetRxFifoFillLevel(handle_, CAN_RX_FIFO0) != 0) {
// Set the first least significant bit
rxFlags |= 0x01; // 0x01 is 00000001 in binary (LSB set, others cleared)
}
if (HAL_CAN_GetRxFifoFillLevel(handle_, CAN_RX_FIFO1) != 0) {
// Set the second least significant bit
rxFlags |= 0x02; // 0x02 is 00000010 in binary (2nd LSB set, others cleared)
}
return CanPendingRxFifos(rxFlags, MAX_FIFO_COUNT);;
}
uint8_t STM32CanController::maxFilterCount() const{
return MAX_FILTER_COUNT;
}
uint8_t STM32CanController::maxFifoCount() const {
return MAX_FIFO_COUNT;
}
uint8_t STM32CanController::maxPayloadSize() const {
return MAX_PAYLOAD_SIZE;
}
} // namespace sta
#ifdef STA_STM32_CAN_GLOBAL
#ifdef STA_STM32_CAN_HANDLE
#include <can.h>
namespace sta
{
STM32CanController CanBus(&STA_STM32_CAN_GLOBAL);
STM32CanController CanBus(&STA_STM32_CAN_HANDLE);
STA_WEAK
void CanBus_RxPendingCallback()
void CanBus_RxPendingCallback(uint32_t fifo)
{}
} // namespace sta
@@ -189,17 +222,17 @@ extern "C"
{
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
if (hcan == &STA_STM32_CAN_GLOBAL)
if (hcan == &STA_STM32_CAN_HANDLE)
{
sta::CanBus_RxPendingCallback();
sta::CanBus_RxPendingCallback(CAN_RX_FIFO0);
}
}
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
if (hcan == &STA_STM32_CAN_GLOBAL)
if (hcan == &STA_STM32_CAN_HANDLE)
{
sta::CanBus_RxPendingCallback();
sta::CanBus_RxPendingCallback(CAN_RX_FIFO1);
}
}
}

View File

@@ -51,7 +51,8 @@ namespace sta
}
// Calculate TIM clock frequency
uint32_t clkFreq = pclkMul * STA_STM32_GET_HANDLE_PCLK_FREQ_FN(STA_STM32_DELAY_US_TIM)();
uint32_t clkFreq = pclkMul * STA_STM32_GET_HANDLE_PCLK_FREQ_FN(STA_STM32_DELAY_US_TIM)() * STA_STM32_GET_HANDLE_PCLK_TIM_PRESCALER(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;
@@ -67,13 +68,22 @@ namespace sta
// 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);
// Save the current value of the counter.
uint32_t current = __HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM);
// Set the timer counter to zero to avoid overflows during delay-
__HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0);
// Wait for the desired microseconds.
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us * gDelayUsMul);
// Set the timer counter to the previous value and add the waited duration to it.
// This avoids collisions with code relying on timeUs measurements.
__HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, current + us * gDelayUsMul);
}
} // namespace sta
#endif // STA_STM32_DELAY_US_TIM
#endif // STA_PLATFORM_STM32

View File

@@ -23,6 +23,8 @@ namespace sta
STA_ASSERT(isValidDelayUsTIM());
// Start timer base
HAL_TIM_Base_Start(&STA_STM32_DELAY_US_TIM);
#else
#endif // STA_STM32_DELAY_US_TIM
}
} // namespace sta

View File

@@ -0,0 +1,32 @@
/*
* time.cpp
*
* Created on: May 22, 2024
* Author: Dario
*/
#include <sta/time.hpp>
#include <sta/config.hpp>
#ifdef STA_PLATFORM_STM32
#include <sta/devices/stm32/hal.hpp>
#include <sta/devices/stm32/clocks.hpp>
namespace sta
{
uint32_t timeMs()
{
return HAL_GetTick();
}
#ifdef STA_STM32_DELAY_US_TIM
extern uint32_t gDelayUsMul;
uint32_t timeUs()
{
return __HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) / gDelayUsMul;
}
#endif // STA_STM32_DELAY_US_TIM
}
#endif // STA_PLATFORM_STM32

20
src/gpio_pin.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include <sta/gpio_pin.hpp>
namespace sta
{
void GpioPin::setHigh()
{
setState(GpioPinState::GPIO_HIGH);
}
void GpioPin::setLow()
{
setState(GpioPinState::GPIO_LOW);
}
void GpioPin::toggle()
{
setState(getState() == GpioPinState::GPIO_HIGH ? GpioPinState::GPIO_LOW : GpioPinState::GPIO_HIGH);
}
} // namespace sta

View File

@@ -3,6 +3,16 @@
namespace sta
{
void Mutex::lock()
{
acquire();
}
void Mutex::unlock()
{
release();
}
/**
* @brief Dummy mutex implementation with no access control.
*/

30
src/time.cpp Normal file
View File

@@ -0,0 +1,30 @@
/*
* time.cpp
*
* Created on: May 22, 2024
* Author: Dario
*/
#include <sta/time.hpp>
#include <sta/lang.hpp>
namespace sta
{
STA_WEAK
uint32_t timeMs()
{
STA_NOT_IMPLEMENTED();
return 0;
}
STA_WEAK
uint32_t timeUs()
{
STA_NOT_IMPLEMENTED();
return 0;
}
} // namespace sta