Timing problem

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Timing problem

Post by hazelnusse »

I'm using an STM32F4 Timer (TIM5) in count up mode at 4MHz to keep track of when various sensors are read. This gives me a time increment of 0.25 microseconds. I have a thread that loops at 200Hz (5ms period, uses ChibiOS/RT functionality) that looks like:

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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Timing problem

Post by Giovanni »

Weird...

Both numbers end with "1573", coincidence? is it possible you are reading something else?

Giovanni
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: Timing problem

Post by hazelnusse »

No, I don't think so. The code that reads the samples is very straightforward and has been working solidly for quite a while. The struct is written to the SD in packed binary format on the STM32, and the code on my PC that reads it uses the same exact structure definition when reading it back, plus all of the other data looks normal (I am also logging accelerometer and gyroscope data in the Sample structure, and these look fine).

The fact that they both end in 1573 was what made me think there was some sort of weird bit error. Either a memory read error (this seems less likely to me) or a SDC write/read error.

I've been writing to the exact same file on the exact same SD card for a while, I probably have used this sd card a couple hundred times to log the data. I'll try formatting the card and maybe changing the file name to see if that changes anything. Maybe there are some bad blocks and by using the same filename I'm stuck on the same page of the SD card?

Luke
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Timing problem

Post by Giovanni »

Try another timer, just in case.

Giovanni
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: Timing problem

Post by hazelnusse »

That is a good idea too. I'll report back on Monday.
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: Timing problem

Post by hazelnusse »

I tried a different microSD card and this problem went away. So I think it was likely some sort of defective block or page on my microSD card.
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: Timing problem

Post by hazelnusse »

Well, I thought it was the SD card, but now it is happening on another SD card. I will try another timer to see if that might be the issue.

Is it possible that when I read the timer count, it is in the middle of updating the register and therefore I can potentially get a corrupted value? I would have thought that this couldn't happen but maybe the peripheral can update the register at the same time a read is occurring and cause issues?

If it isn't the timer, I guess it must be related to the SD card, but I no idea on how to debug this in that case.

Luke
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: Timing problem

Post by hazelnusse »

I added the following code right before my 200Hz for loop, and for some reason it screws things up. With it removed, I have no issues. Here was the offending code:

Code: Select all

STM32_TIM5->CR1 = 0; // disable timer
STM32_TIM5->CNT = 0; // reset count
STM32_TIM5->EGR = 1; // generate an update event
STM32_TIM5->CR1 = 1; // enable timer

when this is changed to

Code: Select all

STM32_TIM5->CNT = 0; // reset count

then I no longer see the weird time issues I was observing before. The STM32 reference manual says the count can be changed while the timer is running, so stopping and updating and restarting was not necessary. I'm glad I fixed the problem, but I can't say that I really understand why this would have caused the problem. Maybe updating registers in peripherals can be somewhat delayed if the peripheral is being clocked at a lower speed? This timer is being clocked at 84MHz and I have a prescalar of 20, so the counter is at 4MHz. Setting the UG bit in EGR also clears the prescalar counter (doesn't change the prescalar ratio though), so maybe this had something to do with it.

Luke

Luke
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Timing problem

Post by Giovanni »

It is possible it affected the prescaler, the answer is buried somewhere in the RM.

Giovanni
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: Timing problem

Post by hazelnusse »

Yeah, I thought so too but now I'm not 100% certain. I changed it to just clear TIM5->CNT without touching EGR or CR1 and I still observed some non-monotonically increasing time data. I finally tried adding a small delay after the point where I clear TIM5->CNT, and that seems to have done the trick, at least so far. I've tried about 10 different runs this way and so far everything has looked good. I really hope is stays that way :)

Luke
Post Reply