From 97af9e3332435cfd0fbed9487df2b5c08096e973 Mon Sep 17 00:00:00 2001 From: "William D. Jones" Date: Sun, 6 Oct 2019 20:44:06 -0400 Subject: [PATCH] msp_exp430f5529lp: Make board_millis atomic. --- hw/bsp/msp_exp430f5529lp/msp_exp430f5529lp.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/hw/bsp/msp_exp430f5529lp/msp_exp430f5529lp.c b/hw/bsp/msp_exp430f5529lp/msp_exp430f5529lp.c index 4eed41a4f..937c35d92 100644 --- a/hw/bsp/msp_exp430f5529lp/msp_exp430f5529lp.c +++ b/hw/bsp/msp_exp430f5529lp/msp_exp430f5529lp.c @@ -200,6 +200,15 @@ void __attribute__ ((interrupt(TIMER0_A0_VECTOR))) TIMER0_A0_ISR (void) uint32_t board_millis(void) { - return system_ticks; + uint32_t systick_mirror; + + // 32-bit update is not atomic on MSP430. We can read the bottom 16-bits, + // an interrupt occurs, updates _all_ 32 bits, and then we return a + // garbage value. And I've seen it happen! + TA0CCTL0 &= ~CCIE; + systick_mirror = system_ticks; + TA0CCTL0 |= CCIE; + + return systick_mirror; } #endif