8 years, 11 months ago.

wait_ms() and Ticker slow to a crawl, but only after about an hour?!

Ok, so I have this weird as hell problem that is impossible to debug. I have code like this:

void flash()
{
	led1 = 1;
	wait_ms(300);
	led1 = 0;
}

int main()
{
	Ticker t;
	t.attach(flash, 1.0f);
	for (;;)
	{
		led2 = 1;
		wait_ms(300);
		led2 = 0;
		wait_ms(300);
	}
}

If I compile it and upload it to my board (a custom one without a 32 kHz crystal, so I'm using the HRM1017 target), then it works fine. For about an hour. I've narrowed it down to between 80 and 110 minutes, assuming it isn't stochastic. Edit: see below.

After that, the for() loop flashing still works, but when the ISR runs it stays on for ages. I'm not sure exactly how long, but 60 seconds wouldn't be implausible. It does eventually exit the ISR (turning off led1) and then led2 flashes again.

On an earlier test I found that the ticker was running every 512 seconds instead of every 1 second. I thought it might be something to do with the RTC overflow ISR but that is every 8.5 minutes (2^24/32768 seconds) and it definitely lasts longer than that.

(Yes I know it's not a good idea to block for so long in ISRs but the code *should* work and it does for an hour. Why should it then stop?)

So the questions are: 1. Why does the ISR get called so infrequently? 2. Why does wait_ms() take longer than it should in the ISR, but not in the main loop? 3. Why does this only happen after about an hour?!

This is really weird.

Edit 1

Ok I timed it more precisely a couple of times. Both times it was about 63 minutes and 20 seconds, which is exactly 3800 seconds. Stranger and stranger... That doesn't correspond with any interesting binary numbers I would expect. E.g. 3800*32768 in hex is 0x76C0000. I'm going to try changing the ticker timeout and see if that affects things.

Answer

This was fixed in revision 97 of the mBed code.

Question relating to:

The nRF51822-mKIT is a low cost ARM mbed enabled development board for Bluetooth® Smart designs with the nRF51822 SoC. The kit gives access to all GPIO pins via pin headers …

Are you using the latest mbed lib? (Right mouse button, update, in the lib). There were some problems with ticker when overflowing (which happens for 32-bit after 80 minutes, so there you got it), but they should be fixed now. Only one I am aware of is that AFAIK there is still a race condition on which they ignored me :D.

posted by Erik - 28 May 2015

I agree it's weird. The underlying timers use us to count, 2^32 us is about 70 minutes so it could be something related to that.

Having said that waiting for 300ms in an ISR is a Bad Idea (it deserves the capitals). You are doing something that you should never do and then complaining that it doesn't work right after a while. That's a bit like saying I've never put petrol in my car, it ran for hundreds of miles so I don't see why it's stopped working now.

Get rid of the wait and use a timeout to turn the LED back off. If that stops working after a while then it's worth digging into what's going on.

posted by Andy A 28 May 2015

As I said I know this isn't best practice, but it clearly shows a bug in mBed which I'd like to fix. I updated the question - the problem seems to reliably happen after 3800 seconds +/- a few seconds.

posted by Tim H 28 May 2015

To repeat my comment: Which version of the mbed lib are you using?

posted by Erik - 28 May 2015

Hmm I thought I was on the latest because I updated the library yesterday, and in fact it even said "Updated 1 day ago", but the revision was 94 which apparently is from February. I just updated to 100. Revision 97 says the nRF51 us ticker was "fixed" so hopefully that solves the problem! I shall report back...

posted by Tim H 29 May 2015

Aha, seems like revision 97 fixed it! Weird that the update didn't work... but never mind - I'll just have to double check the revision numbers next time. Thanks!

posted by Tim H 29 May 2015

Good to know, there have been enough bugs in the NRF ticker code, so nice if not more are introduced :D.

posted by Erik - 29 May 2015

Erik, be positive :-)

posted by Martin Kojtal 29 May 2015
Be the first to answer this question.