Commit 6204cdc5 authored by schneider's avatar schneider
Browse files

fix(pycardium): Prevent underflow in sleep function

The loop condition in the epic_sleep based sleep could underflow if an
interrupt introduced too much delay at the end.
parent ebe5578b
......@@ -63,7 +63,7 @@ void pycardium_hal_init(void)
/*
* Configure SysTick timer for SYSTICK_INTERVAL_US period.
*/
systick_config(SYSTICK_INTERVAL_US * 32768ULL / 1000000);
systick_config(SYSTICK_INTERVAL_US * 32768LL / 1000000);
}
/******************************************************************************
......@@ -157,7 +157,7 @@ void mp_hal_set_interrupt_char(char c)
* SysTick timer at 1000 Hz
*/
static volatile uint64_t systick_count = 0;
static volatile int64_t systick_count = 0;
void SysTick_Handler(void)
{
......@@ -167,7 +167,7 @@ void SysTick_Handler(void)
/*
* Get an absolute "timestamp" in microseconds.
*/
static uint64_t systick_get_us()
static int64_t systick_get_us()
{
uint32_t val, count;
uint32_t irqsaved = __get_PRIMASK();
......@@ -182,8 +182,8 @@ static uint64_t systick_get_us()
__set_PRIMASK(irqsaved);
} while (val == 0);
uint64_t us = count * SYSTICK_INTERVAL_US +
(SysTick->LOAD - val) * 1000000ULL / 32768;
int64_t us = count * SYSTICK_INTERVAL_US +
(SysTick->LOAD - val) * 1000000LL / 32768;
return us;
}
......@@ -244,8 +244,8 @@ static void systick_delay(uint32_t us)
if (us < 10000) {
systick_delay_precise(us);
} else {
uint64_t now = systick_get_us();
uint64_t final_time = now + us;
int64_t now = systick_get_us();
int64_t final_time = now + us;
while (final_time - systick_get_us() > 10000) {
uint32_t sleep_time =
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment