Code: Select all
systime_t time = chTimeNow(); // Initial time
Sample s; // Structure to save time, sensor data, and various system states each time through the loop
for (uint32_t i = 0; !chThdShouldTerminate(); ++i) {
time += MS2ST(5); // Next deadline
s.SystemTime = STM32_TIM5->CNT; // Save value of monotonically increasing timer in a Sample structure
// collect sensor data
// place s in a buffer that gets written to SD card.
chThdSleepUntil(time);
}
The Sample structures are being logged to an SD card using the SDC interface. Until just recently, this had been working perfectly. Now, I am noticing that occasionally, the timer value that is recorded to the SD card is not monotonically increasing. For example:
Code: Select all
t[6463] = 129261573
t[6464] = 128001573
Notice that t[6464] < t[6463], even though this should never happen unless the timer was somehow made to count down. All other times before and after this discrepancy are consistent with what should be happening. There is no code that touches STM32_TIM5->CNT anywhere besides this one read, so I don't think I have a problem there. I find it hard to believe that the timer peripheral itself is incorrectly updating the CNT register because later times all seem to be consistent.
I'm guessing this is a write error with the SD card? I will try another sd card to see if that fixes it. If it persists, does anybody have any ideas what it might be? Could it be a memory read error on the STM32, so reading STM32_TIM5->CNT has some bits flipped? How would people recommend I debug this?
Thanks,
Luke