API HAL OS: replace CMP based ticks with ARR based one, hard reset lptimer on reconfiguration. (#377)

This commit is contained in:
あく
2021-03-11 09:47:07 +03:00
committed by GitHub
parent 2f14f6dac5
commit b920248693
6 changed files with 172 additions and 314 deletions

View File

@@ -1,42 +1,58 @@
#pragma once
#include <stm32wbxx_ll_lptim.h>
#include <stdbool.h>
static inline void assert(bool value) {
if (!value) asm("bkpt 1");
}
#include <stm32wbxx_ll_bus.h>
#include <stdint.h>
// Timer used for system ticks
#define API_HAL_OS_TIMER_MAX 0xFFFF
#define API_HAL_OS_TIMER_REG_LOAD_DLY 0x1
#define API_HAL_OS_TIMER LPTIM2
#define API_HAL_OS_TIMER_IRQ LPTIM2_IRQn
#define API_HAL_OS_TIMER_CLOCK_INIT() \
{ \
LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE_LSE); \
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_LPTIM2); \
} \
static inline void api_hal_os_timer_init() {
API_HAL_OS_TIMER_CLOCK_INIT();
LL_LPTIM_Enable(API_HAL_OS_TIMER);
while(!LL_LPTIM_IsEnabled(API_HAL_OS_TIMER)) {}
LL_LPTIM_SetClockSource(API_HAL_OS_TIMER, LL_LPTIM_CLK_SOURCE_INTERNAL);
LL_LPTIM_SetPrescaler(API_HAL_OS_TIMER, LL_LPTIM_PRESCALER_DIV1);
LL_LPTIM_SetPolarity(API_HAL_OS_TIMER, LL_LPTIM_OUTPUT_POLARITY_REGULAR);
LL_LPTIM_SetUpdateMode(API_HAL_OS_TIMER, LL_LPTIM_UPDATE_MODE_IMMEDIATE);
LL_LPTIM_SetCounterMode(API_HAL_OS_TIMER, LL_LPTIM_COUNTER_MODE_INTERNAL);
LL_LPTIM_TrigSw(API_HAL_OS_TIMER);
LL_LPTIM_SetInput1Src(API_HAL_OS_TIMER, LL_LPTIM_INPUT1_SRC_GPIO);
LL_LPTIM_SetInput2Src(API_HAL_OS_TIMER, LL_LPTIM_INPUT2_SRC_GPIO);
// Configure clock source
LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE_LSE);
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_LPTIM2);
// Set interrupt priority and enable them
NVIC_SetPriority(API_HAL_OS_TIMER_IRQ, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 15, 0));
NVIC_EnableIRQ(API_HAL_OS_TIMER_IRQ);
}
static inline void api_hal_os_timer_continuous(uint32_t count) {
// Enable timer
LL_LPTIM_Enable(API_HAL_OS_TIMER);
while(!LL_LPTIM_IsEnabled(API_HAL_OS_TIMER));
// Enable rutoreload match interrupt
LL_LPTIM_EnableIT_ARRM(API_HAL_OS_TIMER);
// Set autoreload and start counter
LL_LPTIM_SetAutoReload(API_HAL_OS_TIMER, count);
LL_LPTIM_StartCounter(API_HAL_OS_TIMER, LL_LPTIM_OPERATING_MODE_CONTINUOUS);
}
static inline void api_hal_os_timer_single(uint32_t count) {
// Enable timer
LL_LPTIM_Enable(API_HAL_OS_TIMER);
while(!LL_LPTIM_IsEnabled(API_HAL_OS_TIMER));
// Enable compare match interrupt
LL_LPTIM_EnableIT_CMPM(API_HAL_OS_TIMER);
// Set compare, autoreload and start counter
// Include some marging to workaround ARRM behaviour
LL_LPTIM_SetCompare(API_HAL_OS_TIMER, count-3);
LL_LPTIM_SetAutoReload(API_HAL_OS_TIMER, count);
LL_LPTIM_StartCounter(API_HAL_OS_TIMER, LL_LPTIM_OPERATING_MODE_ONESHOT);
}
static inline void api_hal_os_timer_reset() {
// Hard reset timer
// THE ONLY RELIABLEWAY to stop it according to errata
LL_LPTIM_DeInit(API_HAL_OS_TIMER);
}
static inline uint32_t api_hal_os_timer_get_cnt() {
uint32_t counter = LL_LPTIM_GetCounter(API_HAL_OS_TIMER);
uint32_t counter_shadow = LL_LPTIM_GetCounter(API_HAL_OS_TIMER);
@@ -46,50 +62,3 @@ static inline uint32_t api_hal_os_timer_get_cnt() {
}
return counter;
}
static inline bool api_hal_os_timer_arr_is_ok() {
return LL_LPTIM_IsActiveFlag_ARROK(API_HAL_OS_TIMER);
}
static inline uint32_t api_hal_os_timer_get_arr() {
return LL_LPTIM_GetAutoReload(API_HAL_OS_TIMER);;
}
static inline void api_hal_os_timer_set_arr(uint32_t value) {
value &= API_HAL_OS_TIMER_MAX;
if (value != api_hal_os_timer_get_arr()) {
assert(api_hal_os_timer_arr_is_ok());
LL_LPTIM_ClearFlag_ARROK(API_HAL_OS_TIMER);
LL_LPTIM_SetAutoReload(API_HAL_OS_TIMER, value);
}
}
static inline bool api_hal_os_timer_cmp_is_ok() {
return LL_LPTIM_IsActiveFlag_CMPOK(API_HAL_OS_TIMER);
}
static inline uint32_t api_hal_os_timer_get_cmp() {
return LL_LPTIM_GetCompare(API_HAL_OS_TIMER);
}
static inline void api_hal_os_timer_set_cmp(uint32_t value) {
value &= API_HAL_OS_TIMER_MAX;
if (value != api_hal_os_timer_get_cmp()) {
assert(api_hal_os_timer_cmp_is_ok());
LL_LPTIM_ClearFlag_CMPOK(API_HAL_OS_TIMER);
LL_LPTIM_SetCompare(API_HAL_OS_TIMER, value);
}
}
static inline bool api_hal_os_timer_is_safe() {
uint16_t cmp = api_hal_os_timer_get_cmp();
uint16_t cnt = api_hal_os_timer_get_cnt();
uint16_t margin = (cmp > cnt) ? cmp - cnt : cnt - cmp;
if (margin < 8) {
return false;
}
if (!api_hal_os_timer_cmp_is_ok()) {
return false;
}
return true;
}