From 6056bc3b956cf56b9aba9f01f317927398567b5f Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Fri, 26 Jun 2020 14:35:30 +0200 Subject: [PATCH] clraify the tricky loop in lv_tick_get --- src/lv_hal/lv_hal_tick.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/lv_hal/lv_hal_tick.c b/src/lv_hal/lv_hal_tick.c index a62ed7383..afdceb077 100644 --- a/src/lv_hal/lv_hal_tick.c +++ b/src/lv_hal/lv_hal_tick.c @@ -29,7 +29,7 @@ * STATIC VARIABLES **********************/ static uint32_t sys_time = 0; -static volatile uint8_t tick_irq_flag; +static volatile uint32_t tick_irq_flag; /********************** * MACROS @@ -56,12 +56,17 @@ LV_ATTRIBUTE_TICK_INC void lv_tick_inc(uint32_t tick_period) uint32_t lv_tick_get(void) { #if LV_TICK_CUSTOM == 0 + + /* If `lv_tick_inc` is called from an interrupt while `sys_time` is read + * the result might be corrupted. + * This loop detects if `lv_tick_inc` was called while reading `sys_time`. + * If `tick_irq_flag` was cleared in `lv_tick_inc` try to read again + * until `tick_irq_flag` remains `1`. */ uint32_t result; do { tick_irq_flag = 1; result = sys_time; - } while(!tick_irq_flag); /*'lv_tick_inc()' clears this flag which can be in an interrupt. - Continue until make a non interrupted cycle */ + } while(!tick_irq_flag); /*Continue until see a non interrupted cycle */ return result; #else