1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-30 03:18:20 +00:00

embed/trezorhal: simplify systick, remove microsecond precision

This commit is contained in:
Pavol Rusnak 2018-02-23 20:06:53 +01:00
parent 02358b3932
commit fae8ffda9f
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -90,51 +90,10 @@ void mp_hal_delay_us(mp_uint_t usec) {
}
}
bool sys_tick_has_passed(uint32_t start_tick, uint32_t delay_ms) {
return HAL_GetTick() - start_tick >= delay_ms;
}
// waits until at least delay_ms milliseconds have passed from the sampling of
// startTick. Handles overflow properly. Assumes stc was taken from
// HAL_GetTick() some time before calling this function.
void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) {
while (!sys_tick_has_passed(start_tick, delay_ms)) {
__WFI(); // enter sleep mode, waiting for interrupt
}
}
mp_uint_t mp_hal_ticks_ms(void) {
return uwTick;
}
// The SysTick timer counts down at 168 MHz, so we can use that knowledge
// to grab a microsecond counter.
//
// We assume that HAL_GetTickis returns milliseconds.
mp_uint_t mp_hal_ticks_us(void) {
mp_uint_t irq_state = disable_irq();
uint32_t counter = SysTick->VAL;
uint32_t milliseconds = HAL_GetTick();
uint32_t status = SysTick->CTRL;
enable_irq(irq_state);
// It's still possible for the countflag bit to get set if the counter was
// reloaded between reading VAL and reading CTRL. With interrupts disabled
// it definitely takes less than 50 HCLK cycles between reading VAL and
// reading CTRL, so the test (counter > 50) is to cover the case where VAL
// is +ve and very close to zero, and the COUNTFLAG bit is also set.
if ((status & SysTick_CTRL_COUNTFLAG_Msk) && counter > 50) {
// This means that the HW reloaded VAL between the time we read VAL and the
// time we read CTRL, which implies that there is an interrupt pending
// to increment the tick counter.
milliseconds++;
}
uint32_t load = SysTick->LOAD;
counter = load - counter; // Convert from decrementing to incrementing
// ((load + 1) / 1000) is the number of counts per microsecond.
//
// counter / ((load + 1) / 1000) scales from the systick clock to microseconds
// and is the same thing as (counter * 1000) / (load + 1)
return milliseconds * 1000 + (counter * 1000) / (load + 1);
return uwTick * 1000;
}