timer: fix race condition in timFireAsNeededAndUpdateAlarms

timFireAsNeededAndUpdateAlarms() may be called from
timer interrupt and from timTimerSetEx()

if interrupted, and reentered it may incorrectly update
internal state, and produce unwanted side effects,
e.g. invoke the same timer handler multiple times.

interrupts disabled inside timFireAsNeededAndUpdateAlarms()
to prevent that from happening

Change-Id: I6484925cbe91f9deb5d33eb50e34b32254dda607
Signed-off-by: Alexey Polyudov <apolyudov@google.com>
diff --git a/firmware/src/timer.c b/firmware/src/timer.c
index 313d8d8..3a9d49f 100644
--- a/firmware/src/timer.c
+++ b/firmware/src/timer.c
@@ -94,6 +94,9 @@
     uint32_t i, id;
     void *callData;
 
+    // protect from concurrent execution [timIntHandler() and timTimerSetEx()]
+    uint64_t intSta = cpuIntsOff();
+
     do {
         somethingDone = false;
         nextTimer = 0;
@@ -135,6 +138,8 @@
     if (!nextTimer)
         platSleepClockRequest(0, 0, 0, 0);
 
+    cpuIntsRestore(intSta);
+
     return totalSomethingDone;
 }