added profiler and updated delay implementation

This commit is contained in:
dario 2024-05-22 13:07:36 +02:00
parent 7aac2badc5
commit 767bd19c36
6 changed files with 99 additions and 3 deletions

View File

@ -58,6 +58,7 @@ namespace sta
*/
#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.
*

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

@ -52,6 +52,7 @@ namespace sta
// 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;
@ -66,10 +67,29 @@ namespace sta
{
// Check if the specified timer is usable for microsecond delays.
STA_ASSERT(isValidDelayUsTIM());
STA_ASSERT(us < 1000);
__HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0);
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us * gDelayUsMul);
// Use millisecond delays for us > 1000 and use microseconds delay for the remainder.
delayMs(us / 1000);
us = us % 1000;
// __HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0);
uint32_t startTime = __HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM);
// Check if an overflow is expected during the delay time.
if (startTime < __HAL_TIM_GET_AUTORELOAD(&STA_STM32_DELAY_US_TIM) - us * gDelayUsMul)
{
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < startTime + us * gDelayUsMul);
}
else
{
uint32_t overflowTime = __HAL_TIM_GET_AUTORELOAD(&STA_STM32_DELAY_US_TIM) - startTime;
// Wait until the overflow happens
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) > startTime);
// Wait the remaining time after the overflow.
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us * gDelayUsMul - overflowTime);
}
}
} // namespace sta

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

26
src/time.cpp Normal file
View File

@ -0,0 +1,26 @@
/*
* 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();
}
STA_WEAK
uint32_t timeUs()
{
STA_NOT_IMPLEMENTED();
}
} // namespace sta