1 /**
2  * @file systick.c
3  * Provide access to the system tick with 1 millisecond resolution
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_hal_tick.h"
10 #include <stddef.h>
11 
12 #if LV_TICK_CUSTOM == 1
13     #include LV_TICK_CUSTOM_INCLUDE
14 #endif
15 
16 /*********************
17  *      DEFINES
18  *********************/
19 
20 /**********************
21  *      TYPEDEFS
22  **********************/
23 
24 /**********************
25  *  STATIC PROTOTYPES
26  **********************/
27 
28 /**********************
29  *  STATIC VARIABLES
30  **********************/
31 static uint32_t sys_time = 0;
32 static volatile uint8_t tick_irq_flag;
33 
34 /**********************
35  *      MACROS
36  **********************/
37 
38 /**********************
39  *   GLOBAL FUNCTIONS
40  **********************/
41 
42 /**
43  * You have to call this function periodically
44  * @param tick_period the call period of this function in milliseconds
45  */
lv_tick_inc(uint32_t tick_period)46 LV_ATTRIBUTE_TICK_INC void lv_tick_inc(uint32_t tick_period)
47 {
48     tick_irq_flag = 0;
49     sys_time += tick_period;
50 }
51 
52 /**
53  * Get the elapsed milliseconds since start up
54  * @return the elapsed milliseconds
55  */
lv_tick_get(void)56 uint32_t lv_tick_get(void)
57 {
58 #if LV_TICK_CUSTOM == 0
59 
60     /* If `lv_tick_inc` is called from an interrupt while `sys_time` is read
61      * the result might be corrupted.
62      * This loop detects if `lv_tick_inc` was called while reading `sys_time`.
63      * If `tick_irq_flag` was cleared in `lv_tick_inc` try to read again
64      * until `tick_irq_flag` remains `1`. */
65     uint32_t result;
66     do {
67         tick_irq_flag = 1;
68         result        = sys_time;
69     } while(!tick_irq_flag); /*Continue until see a non interrupted cycle */
70 
71     return result;
72 #else
73     return LV_TICK_CUSTOM_SYS_TIME_EXPR;
74 #endif
75 }
76 
77 /**
78  * Get the elapsed milliseconds since a previous time stamp
79  * @param prev_tick a previous time stamp (return value of systick_get() )
80  * @return the elapsed milliseconds since 'prev_tick'
81  */
lv_tick_elaps(uint32_t prev_tick)82 uint32_t lv_tick_elaps(uint32_t prev_tick)
83 {
84     uint32_t act_time = lv_tick_get();
85 
86     /*If there is no overflow in sys_time simple subtract*/
87     if(act_time >= prev_tick) {
88         prev_tick = act_time - prev_tick;
89     }
90     else {
91         prev_tick = UINT32_MAX - prev_tick + 1;
92         prev_tick += act_time;
93     }
94 
95     return prev_tick;
96 }
97 
98 /**********************
99  *   STATIC FUNCTIONS
100  **********************/
101