summaryrefslogtreecommitdiff
path: root/platforms
diff options
context:
space:
mode:
authorDasky <32983009+daskygit@users.noreply.github.com>2024-10-25 18:10:17 +0100
committerGitHub <noreply@github.com>2024-10-25 18:10:17 +0100
commit5c85271e48b4f2be7da47d1728ad1ddb95364ad7 (patch)
treeb529dc681017349e509bb454a49fb220176400d4 /platforms
parentf486605bab51c858edad09deeedbdcefc398f222 (diff)
Add timer_save and _restore functions. (#23887)
Co-authored-by: Sergey Vlasov <sigprof@gmail.com> Co-authored-by: Nick Brassel <nick@tzarc.org>
Diffstat (limited to 'platforms')
-rw-r--r--platforms/avr/timer.c19
-rw-r--r--platforms/chibios/timer.c21
-rw-r--r--platforms/timer.h2
3 files changed, 42 insertions, 0 deletions
diff --git a/platforms/avr/timer.c b/platforms/avr/timer.c
index 9fb671ae8d..26ba0e29fa 100644
--- a/platforms/avr/timer.c
+++ b/platforms/avr/timer.c
@@ -25,6 +25,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
25// counter resolution 1ms 25// counter resolution 1ms
26// NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }} 26// NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }}
27volatile uint32_t timer_count; 27volatile uint32_t timer_count;
28static uint32_t saved_ms;
28 29
29/** \brief timer initialization 30/** \brief timer initialization
30 * 31 *
@@ -78,6 +79,24 @@ inline void timer_clear(void) {
78 } 79 }
79} 80}
80 81
82/** \brief timer save
83 *
84 * Set saved_ms to current time.
85 */
86void timer_save(void) {
87 saved_ms = timer_read32();
88}
89
90/** \brief timer restore
91 *
92 * Set timer_count to saved_ms
93 */
94void timer_restore(void) {
95 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
96 timer_count = saved_ms;
97 }
98}
99
81/** \brief timer read 100/** \brief timer read
82 * 101 *
83 * FIXME: needs doc 102 * FIXME: needs doc
diff --git a/platforms/chibios/timer.c b/platforms/chibios/timer.c
index 5e01ea6372..4a347b445d 100644
--- a/platforms/chibios/timer.c
+++ b/platforms/chibios/timer.c
@@ -5,6 +5,7 @@
5static uint32_t ticks_offset = 0; 5static uint32_t ticks_offset = 0;
6static uint32_t last_ticks = 0; 6static uint32_t last_ticks = 0;
7static uint32_t ms_offset = 0; 7static uint32_t ms_offset = 0;
8static uint32_t saved_ms = 0;
8#if CH_CFG_ST_RESOLUTION < 32 9#if CH_CFG_ST_RESOLUTION < 32
9static uint32_t last_systime = 0; 10static uint32_t last_systime = 0;
10static uint32_t overflow = 0; 11static uint32_t overflow = 0;
@@ -73,6 +74,26 @@ void timer_clear(void) {
73 chSysUnlock(); 74 chSysUnlock();
74} 75}
75 76
77__attribute__((weak)) void platform_timer_save_value(uint32_t value) {
78 saved_ms = value;
79}
80
81__attribute__((weak)) uint32_t platform_timer_restore_value(void) {
82 return saved_ms;
83}
84
85void timer_restore(void) {
86 chSysLock();
87 ticks_offset = get_system_time_ticks();
88 last_ticks = 0;
89 ms_offset = platform_timer_restore_value();
90 chSysUnlock();
91}
92
93void timer_save(void) {
94 platform_timer_save_value(timer_read32());
95}
96
76uint16_t timer_read(void) { 97uint16_t timer_read(void) {
77 return (uint16_t)timer_read32(); 98 return (uint16_t)timer_read32();
78} 99}
diff --git a/platforms/timer.h b/platforms/timer.h
index d55f40f0b0..fb8ff6bc54 100644
--- a/platforms/timer.h
+++ b/platforms/timer.h
@@ -38,6 +38,8 @@ extern volatile uint32_t timer_count;
38 38
39void timer_init(void); 39void timer_init(void);
40void timer_clear(void); 40void timer_clear(void);
41void timer_save(void);
42void timer_restore(void);
41uint16_t timer_read(void); 43uint16_t timer_read(void);
42uint32_t timer_read32(void); 44uint32_t timer_read32(void);
43uint16_t timer_elapsed(uint16_t last); 45uint16_t timer_elapsed(uint16_t last);