Merge branch 'main' into arduino-support

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

View File

@ -85,6 +85,12 @@ Configuration:
TIM time base must be started before using `sta::delayUs` by calling `sta::initHAL`.
When using the startup system task this is handled automatically.
Steps to enable delayUs:
* Include sta/devices/stm32/delay.hpp in the file where the delayUs function is called
* Enable a timer TIM in .ioc file with the settings: Slave Mode=Disable, Trigger Mode=Disable, Clock Source=Internal Clock, all other channels =Disable)
* Define STA_STM32_DELAY_US_TIM in App/Inc/sta/config.hpp as the timer handle (can be found in Core/Inc/tim.h). For TIM1, this would be htim1
## Interfaces

View File

@ -3,9 +3,8 @@
* @brief Assertion handling.
*
* Configuration:
* * STA_ASSERT_FORCE: Ignore debug defines and always enable assertions
* * DEBUG: Enables assertions when defined
* * NDEBUG: Disables assertions when defined (overrides DEBUG)
* * STA_ASSERT_ENABLED: Enable assertions
* * STA_ASSERT_FORCE: Enable assertions. Still there for backwards compatibility.
*/
#ifndef STA_CORE_ASSERT_HPP
#define STA_CORE_ASSERT_HPP
@ -24,18 +23,10 @@
#include <sta/config.hpp>
// Determine if module should be enabled
// Condition:
// STA_ASSERT_FORCE is defined
// or
// DEBUG is defined but not NDEBUG
// Keep STA_ASSERT_FORCE for backwards comapatibility.
#ifdef STA_ASSERT_FORCE
# define STA_ASSERT_ENABLED
#else // !STA_ASSERT_FORCE
# if defined(DEBUG) && !defined(NDEBUG)
# define STA_ASSERT_ENABLED
# endif // DEBUG && !NDEBUG
#endif // !STA_ASSERT_FORCE
# define STA_ASSERT_ENABLED
#endif // STA_ASSERT_FORCE
#if defined(STA_ASSERT_ENABLED) || defined(DOXYGEN)

View File

@ -0,0 +1,47 @@
/*
* profile.hpp
*
* Created on: May 22, 2024
* Author: Dario
*/
#ifndef STA_DEBUGGING_PROFILING_HPP
#define STA_DEBUGGING_PROFILING_HPP
#include <sta/debug/debug.hpp>
#ifdef STA_PROFILING_ENABLED
#ifndef STA_DEBUGGING_ENABLED
# error "Debugging has to be enabled in order to use profiling."
#endif // STA_DEBUGGING_ENABLED
#ifndef STA_STM32_DELAY_US_TIM
# error "A microsecond timer has to be defined in order to use profiling."
#endif // STA_STM32_DELAY_US_TIM
namespace sta
{
class Profiler {
public:
Profiler(const char* name);
~Profiler();
private:
const char* name_;
uint32_t start_;
};
} // namespace sta
/**
*
*/
#define STA_TIME_IT(name) sta::Profiler profiler(name);
#else
#define STA_TIME_IT(name) ((void)0)
#endif // // STA_PROFILING_ENABLED
#endif // STA_DEBUGGING_PROFILING_HPP

View File

@ -31,6 +31,14 @@ namespace sta
*/
void start();
/**
* @brief Starts conversion of the incoming analog signal in DMA mode.
*
* @param buffer The buffer to write the converted values to.
* @param length The length of the buffer to write the converted values to.
*/
void startDMA(uint32_t * buffer, size_t length);
/**
* @brief Polls for the converted analog signal.
*

View File

@ -80,6 +80,14 @@ namespace sta
void disableFilter(uint8_t idx) override;
void clearFilters() override;
// Return pending RX FIFOs as iterable container
CanPendingRxFifos getPendingRxFifos() override;
// Const getters
uint8_t maxFilterCount() const override;
uint8_t maxFifoCount() const override;
uint8_t maxPayloadSize() const override;
private:
/**
* @brief Initialize filter settings.

View File

@ -39,7 +39,7 @@ namespace sta
*
* @param n Index of peripheral clock
*/
#define STA_STM32_GET_PCLK_FREQ_FN(n) HAL_RCC_GetPCLK ## n ## Freq
#define STA_STM32_GET_PCLK_FREQ_FN(n) HAL_RCC_GetPCLK ## n ## Freq
/**
* @brief Internal helper for macro expansion.
@ -47,7 +47,31 @@ namespace sta
* @param n PCLK index
* @return Function returning PCLK frequency
*/
#define _STA_STM32_GET_PCLK_FREQ_FN(n) STA_STM32_GET_PCLK_FREQ_FN(n)
#define _STA_STM32_GET_PCLK_FREQ_FN(n) STA_STM32_GET_PCLK_FREQ_FN(n)
/**
* @brief Get the PCLK frequency.
*
* @param n PCLK index
* @return The PCLK frequency
*/
#define _STA_STM32_GET_PCLK_FREQ(n) STA_STM32_GET_PCLK_FREQ_FN(n)()
/**
* @brief Gets the prescaler from APBx to APBx timer clocks.
*
* @param n PCLK index
* @return The prescaler from PCLK to TIM
*/
#define STA_STM32_GET_PCLK_TIM_PRESCALER(n) STA_STM32_TIM_PCLK_ ## n ## ## _PRESCALER
/**
* @brief Internal helper for macro expansion.
*
* @param n PCLK index
* @return Function returning PCLK frequency
*/
#define _STA_STM32_GET_PCLK_TIM_PRESCALER(n) STA_STM32_GET_PCLK_TIM_PRESCALER(n)
/**
* @brief Map instance name to PCLK index.
@ -56,52 +80,67 @@ namespace sta
* @param idx Instance index
* @return PCLK index
*/
#define _STA_STM32_PCLK_IDX_MAP(type, idx) STA_STM32_ ## type ## _ ## idx ## _PCLK_IDX
#define _STA_STM32_PCLK_IDX_MAP(type, idx) STA_STM32_ ## type ## _ ## idx ## _PCLK_IDX
// Get HAL handle to PCLK index map macro
/**
* @brief Map instance handle to PCLK index.
*
* @param handle HAL handle
* @return PCLK index
*/
#define _STA_STM32_HANDLE_PCLK_IDX_MAP(handle) STA_STM32_ ## handle ## _PCLK_IDX
#define _STA_STM32_HANDLE_PCLK_IDX_MAP(handle) STA_STM32_ ## handle ## _PCLK_IDX
/**
* @brief Get function returning frequency of PCLK used by TIM.
* @brief Get a function returning the frequency of PCLK used by TIM.
*
* @param n TIM index
*/
#define STA_STM32_GET_TIM_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(TIM, n))
#define STA_STM32_GET_TIM_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(TIM, n))
/**
* @brief Get function returning frequency of PCLK used by SPI interface.
* @brief Internal helper for macro expansion.
*
* @param n TIM index
*/
#define _STA_STM32_GET_TIM_PCLK_FREQ_FN(n) STA_STM32_GET_TIM_PCLK_FREQ_FN(n)
/**
* @brief Get a function returning the frequency of PCLK used by SPI interface.
*
* @param n SPI interface index
*/
#define STA_STM32_GET_SPI_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(SPI, n))
#define STA_STM32_GET_SPI_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(SPI, n))
/**
* @brief Get function returning frequency of PCLK used by I2C interface.
* @brief Get a function returning the frequency of PCLK used by I2C interface.
*
* @param n I2C interface index
*/
#define STA_STM32_GET_I2C_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(I2C, n))
#define STA_STM32_GET_I2C_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(I2C, n))
/**
* @brief Get function returning frequency of PCLK used by USART interface.
* @brief Get a function returning the frequency of PCLK used by USART interface.
*
* @param n USART interface index
*/
#define STA_STM32_GET_USART_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(USART, n))
#define STA_STM32_GET_USART_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(USART, n))
/**
* @brief Get function returning frequency of PCLK used by HAL instance.
* @brief Get a function returning the frequency of PCLK used by HAL instance.
*
* @param handle Instance handle
*/
#define STA_STM32_GET_HANDLE_PCLK_FREQ_FN(handle) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_HANDLE_PCLK_IDX_MAP(handle))
#define STA_STM32_GET_HANDLE_PCLK_FREQ_FN(handle) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_HANDLE_PCLK_IDX_MAP(handle))
/**
* @brief Get the PCLK to timer prescaler for TIM given by the handle.
*
* @param handle TIM handle
* @return PCLK-TIM prescaler
*/
#define STA_STM32_GET_HANDLE_PCLK_TIM_PRESCALER(handle) _STA_STM32_GET_PCLK_TIM_PRESCALER(_STA_STM32_HANDLE_PCLK_IDX_MAP(handle))
/** @} */

View File

@ -5,6 +5,7 @@
#ifndef STA_CORE_STM32_INIT_HPP
#define STA_CORE_STM32_INIT_HPP
#include <sta/devices/stm32/hal.hpp>
namespace sta
{

View File

@ -14,9 +14,10 @@
#include <sta/devices/stm32/mcu/common.hpp>
// uart setup
// uart/CAN setup
#ifdef STA_STM32_ASEAG
# define STA_STM32_USART_HANDLE huart1
# define STA_STM32_CAN_HANDLE hcan1
#else
# ifdef STA_STM32_SWD_USART_IDX
# define STA_STM32_USART_HANDLE CONCAT(huart, STA_STM32_SWD_USART_IDX)
@ -63,6 +64,11 @@
#define STA_STM32_USART_3_PCLK_IDX 1 /**< USART3 to PCLK index */
#define STA_STM32_USART_6_PCLK_IDX 2 /**< USART6 to PCLK index */
// prescaler from APBx to APBx timer clocks
#define STA_STM32_TIM_PCLK_1_PRESCALER 2 /**< PCLK1 has prescaler of 2 */
#define STA_STM32_TIM_PCLK_2_PRESCALER 2 /**< PCLK2 has prescaler of 1 */
// HAL handle mappings
//
@ -100,4 +106,4 @@
/** @} */
#endif // STA_CORE_STM32_MCU_STM32F407xx_HPP
#endif // STA_CORE_STM32_MCU_STM32F407xx_HPP

View File

@ -63,8 +63,13 @@
#define STA_STM32_USART_2_PCLK_IDX 1 /**< USART2 to PCLK index 1 */
#define STA_STM32_USART_6_PCLK_IDX 2 /**< USART6 to PCLK index 2 */
// prescaler from APBx to APBx timer clocks
#define STA_STM32_TIM_PCLK_1_PRESCALER 2 /**< PCLK1 has prescaler of 2 */
#define STA_STM32_TIM_PCLK_2_PRESCALER 1 /**< PCLK2 has prescaler of 1 */
// HAL handle mappings
//
// TIM to PCLK
#define STA_STM32_htim1_PCLK_IDX STA_STM32_TIM_1_PCLK_IDX /**< HAL TIM1 to PCLK index */

View File

@ -37,6 +37,24 @@ namespace sta
*/
virtual void setState(GpioPinState state) = 0;
/**
* @brief Set the GPIO pin to high.
*
*/
void setHigh();
/**
* @brief Set the GPIO pin to low.
*
*/
void setLow();
/**
* @brief Set the GPIO pin to the opposite of the current state.
*
*/
void toggle();
/**
* @brief Get pin input state.
*

View File

@ -5,9 +5,16 @@
#ifndef STA_CORE_MUTEX_HPP
#define STA_CORE_MUTEX_HPP
#include <mutex>
namespace sta
{
/**
* @brief Hippity hoppity, this is now namespace sta's property.
*/
using std::lock_guard;
/**
* @brief Interface for mutex objects.
*
@ -25,6 +32,16 @@ namespace sta
*/
virtual void release() = 0;
/**
* @brief Identical to acquire(). Added for compatibility with std::lock_guard.
*/
void lock();
/**
* @brief Identical to release(). Added for compatibility with std::lock_guard.
*/
void unlock();
static Mutex * ALWAYS_FREE; /**< Fake mutex that can always be acquired */
};
} // namespace sta

View File

@ -16,12 +16,27 @@ namespace sta
* @return Time in milliseconds
*/
using TimeMsFn = uint32_t (*)();
/**
* @brief Signature for microseconds precision time.
*
* @return Time in microseconds
*/
using TimeUsFn = uint32_t (*)();
/**
* @brief Gets the current time in milliseconds.
*
* @return Time in milliseconds
*/
uint32_t timeMs();
/**
* @brief Gets the current time in microseconds.
*
* @return Time in microseconds
*/
uint32_t timeUs();
} // namespace sta

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