parent
1159fef33b
commit
94e87c74d3
@ -0,0 +1,98 @@ |
||||
//
|
||||
// Created by MightyPork on 2018/01/27.
|
||||
//
|
||||
|
||||
#include "platform.h" |
||||
#include "timebase.h" |
||||
|
||||
#define TIMEBASE_TIMER TIM14 |
||||
|
||||
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) |
||||
{ |
||||
// TIM14 is a simple 16-bit timer timer with no special features.
|
||||
// This makes it a good choice for the timebase generation. We set it to generate
|
||||
// an interrupt every 1 ms
|
||||
|
||||
// - TIM14 is always up-counting
|
||||
// - using APB1 clock
|
||||
__HAL_RCC_TIM14_CLK_ENABLE(); |
||||
NVIC_SetPriority(TIM14_IRQn, TickPriority); // highest possible priority
|
||||
NVIC_EnableIRQ(TIM14_IRQn); |
||||
|
||||
/* Compute TIM1 clock */ |
||||
uint32_t uwTimclock = HAL_RCC_GetPCLK1Freq(); |
||||
/* Get a 1 MHz clock for the timer */ |
||||
uint16_t uhPrescalerValue = (uint16_t) ((uwTimclock / 1000000) - 1); |
||||
/* Get 1 kHz interrupt */ |
||||
uint16_t uhPeriod = (1000000 / 1000) - 1; |
||||
|
||||
LL_TIM_SetPrescaler(TIMEBASE_TIMER, uhPrescalerValue); |
||||
LL_TIM_SetAutoReload(TIMEBASE_TIMER, uhPeriod); |
||||
LL_TIM_EnableARRPreload(TIMEBASE_TIMER); |
||||
LL_TIM_EnableIT_UPDATE(TIMEBASE_TIMER); |
||||
LL_TIM_GenerateEvent_UPDATE(TIMEBASE_TIMER); |
||||
|
||||
LL_TIM_EnableCounter(TIMEBASE_TIMER); |
||||
|
||||
/* Return function status */ |
||||
return HAL_OK; |
||||
} |
||||
|
||||
static volatile uint32_t uwUptimeMs = 0; |
||||
|
||||
/* TIMEBASE TIMER ISR */ |
||||
void TIM14_IRQHandler(void) |
||||
{ |
||||
uwUptimeMs++; |
||||
LL_TIM_ClearFlag_UPDATE(TIMEBASE_TIMER); |
||||
} |
||||
|
||||
void HAL_IncTick(void) |
||||
{ |
||||
uwUptimeMs++; |
||||
} |
||||
|
||||
uint32_t HAL_GetTick(void) |
||||
{ |
||||
return uwUptimeMs; |
||||
} |
||||
|
||||
void HAL_SuspendTick(void) |
||||
{ |
||||
LL_TIM_DisableIT_UPDATE(TIMEBASE_TIMER); |
||||
} |
||||
|
||||
void HAL_ResumeTick(void) |
||||
{ |
||||
LL_TIM_EnableIT_UPDATE(TIMEBASE_TIMER); |
||||
} |
||||
|
||||
// -------------------------------------------------------------------------------------
|
||||
|
||||
// Timestamping functions ...
|
||||
|
||||
/*
|
||||
* I wanted to freeze time. I wanted to savor that moment, to live in that moment |
||||
* for a week. But I couldn't stop it, only slow it. And before I knew it, she was gone. |
||||
* -- Ben Willis, "Cashback" |
||||
*/ |
||||
uint64_t PTIM_GetMicrotime(void) |
||||
{ |
||||
uint32_t uwMicros; |
||||
uint32_t uwMillis; |
||||
|
||||
vPortEnterCritical(); |
||||
{ |
||||
uwMicros = TIMEBASE_TIMER->CNT; |
||||
uwMillis = uwUptimeMs; |
||||
|
||||
if (LL_TIM_IsActiveFlag_UPDATE(TIM14)) { |
||||
// This means the timer has overflown after we disabled IRQ
|
||||
// Use the last CNT value before the overflow
|
||||
uwMicros = TIM14->ARR; // this is 999us
|
||||
} |
||||
} |
||||
vPortExitCritical(); |
||||
|
||||
return (uint64_t)uwMillis*1000 + uwMicros; |
||||
} |
@ -0,0 +1,22 @@ |
||||
//
|
||||
// Created by MightyPork on 2018/01/27.
|
||||
//
|
||||
// Configures and manages the high priority timer used for timeouts and precision delays.
|
||||
//
|
||||
// SysTick can't be used for this because, under FreeRTOS, the system tick interrupt
|
||||
// has the lowest priority to not interfere with more important application processes
|
||||
// and interrupts.
|
||||
//
|
||||
|
||||
#ifndef GEX_F072_TIMEBASE_H |
||||
#define GEX_F072_TIMEBASE_H |
||||
|
||||
/**
|
||||
* Precision timer: get microtime as uint64_t |
||||
* This timestamp should be monotonously increasing with a precision of ±0.5µs |
||||
* |
||||
* @return time in microseconds |
||||
*/ |
||||
uint64_t PTIM_GetMicrotime(void); |
||||
|
||||
#endif //GEX_F072_TIMEBASE_H
|
Loading…
Reference in new issue