From 03715344a474a9954bd55f12d1b4e685d5003df9 Mon Sep 17 00:00:00 2001 From: Lephe Date: Wed, 8 Jul 2020 19:49:09 +0200 Subject: [PATCH] tmu: safer restore order The previous setup made it possible for TCNT to underflow to an uncontroller value of TCOR during the restore. --- src/tmu/tmu.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/tmu/tmu.c b/src/tmu/tmu.c index e690fad..2d7d731 100644 --- a/src/tmu/tmu.c +++ b/src/tmu/tmu.c @@ -335,12 +335,12 @@ static void init(void) /* Clear every timer to avoid surprises */ for(int id = 0; id < 3; id++) { - TMU[id].TCOR = 0xffffffff; - TMU[id].TCNT = 0xffffffff; - do TMU[id].TCR.word = 0; while(TMU[id].TCR.word); + TMU[id].TCOR = 0xffffffff; + TMU[id].TCNT = 0xffffffff; + /* Standard timers: TCR is provided to the interrupt handler */ timers[id]->TCR = &TMU[id].TCR; } @@ -455,13 +455,13 @@ static void ctx_save(void *buf) static void ctx_restore(void *buf) { ctx_t *ctx = buf; - *TSTR = ctx->TSTR; + *TSTR = 0; for(int i = 0; i < 3; i++) { tmu_t *c = &ctx->std[i]; - TMU[i].TCNT = c->TCNT; TMU[i].TCOR = c->TCOR; + TMU[i].TCNT = c->TCNT; TMU[i].TCR.word = c->TCR.word; } for(int i = 0; i < timer_count() - 3; i++) @@ -480,6 +480,8 @@ static void ctx_restore(void *buf) do T->TCR.byte = c->TCR.byte; while(T->TCR.byte != c->TCR.byte); } + + *TSTR = ctx->TSTR; } //---