diff options
| author | Dasky <32983009+daskygit@users.noreply.github.com> | 2024-10-25 18:10:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-25 18:10:17 +0100 |
| commit | 5c85271e48b4f2be7da47d1728ad1ddb95364ad7 (patch) | |
| tree | b529dc681017349e509bb454a49fb220176400d4 /platforms | |
| parent | f486605bab51c858edad09deeedbdcefc398f222 (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.c | 19 | ||||
| -rw-r--r-- | platforms/chibios/timer.c | 21 | ||||
| -rw-r--r-- | platforms/timer.h | 2 |
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; }} |
| 27 | volatile uint32_t timer_count; | 27 | volatile uint32_t timer_count; |
| 28 | static 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 | */ | ||
| 86 | void timer_save(void) { | ||
| 87 | saved_ms = timer_read32(); | ||
| 88 | } | ||
| 89 | |||
| 90 | /** \brief timer restore | ||
| 91 | * | ||
| 92 | * Set timer_count to saved_ms | ||
| 93 | */ | ||
| 94 | void 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 @@ | |||
| 5 | static uint32_t ticks_offset = 0; | 5 | static uint32_t ticks_offset = 0; |
| 6 | static uint32_t last_ticks = 0; | 6 | static uint32_t last_ticks = 0; |
| 7 | static uint32_t ms_offset = 0; | 7 | static uint32_t ms_offset = 0; |
| 8 | static uint32_t saved_ms = 0; | ||
| 8 | #if CH_CFG_ST_RESOLUTION < 32 | 9 | #if CH_CFG_ST_RESOLUTION < 32 |
| 9 | static uint32_t last_systime = 0; | 10 | static uint32_t last_systime = 0; |
| 10 | static uint32_t overflow = 0; | 11 | static 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 | |||
| 85 | void 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 | |||
| 93 | void timer_save(void) { | ||
| 94 | platform_timer_save_value(timer_read32()); | ||
| 95 | } | ||
| 96 | |||
| 76 | uint16_t timer_read(void) { | 97 | uint16_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 | ||
| 39 | void timer_init(void); | 39 | void timer_init(void); |
| 40 | void timer_clear(void); | 40 | void timer_clear(void); |
| 41 | void timer_save(void); | ||
| 42 | void timer_restore(void); | ||
| 41 | uint16_t timer_read(void); | 43 | uint16_t timer_read(void); |
| 42 | uint32_t timer_read32(void); | 44 | uint32_t timer_read32(void); |
| 43 | uint16_t timer_elapsed(uint16_t last); | 45 | uint16_t timer_elapsed(uint16_t last); |
