10 years, 2 months ago.

mbed-rtos breaks my code using Ticker

Hi:

I have a code that uses Ticker to sample the joystick every 0.2s. It works fine until I added a function that uses NTP to set the RTC time (based on the program called NTPClient_HelloWorld). That program uses EthernetInterface and NTPClient libraries, which in turn use the mbed-rtos library. I don't know anything about RTOS.

But now my mbed hangs whenever I call the Ticker attach routine. I don't need the RTOS feature, but I want the NTP function. What can I do to get my Ticker code working again with this NTP function? Is there an NTP program that does not use RTOS?

Thank you! T.H.

Hi Erik: Oh, I didn't know Ticker is an interrupt routine, but it makes sense to me that it is. Thank you very much for your help! Your suggestion is very interesting. I will do that.

Thanks! TH

posted by T.H. Lu 23 Feb 2014

1 Answer

10 years, 2 months ago.

Could you post the code you use for your Ticker function (between <<code>> and <</code>>)? I don't know if regular ethernet requires RTOS, and if not how much issue it would be to remove it from the NTP client, but getting your code working with RTOS is probably easier.

There is one big rule when using RTOS which causes the majority of the programs hanging: You are not allowed to use printf/putc/getc in an interrupt routine, so do you send your sampled value to a serial terminal for example? If that is the case, fear not, because there is a solution: Replace 'Serial' with 'RawSerial'. For just printing stuff same functionality, but works within interrupt when using RTOS.

(You do need to define a RawSerial pc(USBTX, USBRX), you cannot just printf directly, but then need pc.printf).

Hi Erik:

Yes, I do have printf in my Ticker function. Actually, they are lcd.printf's to be exact. What I can do is to move those LCD printf's into the main function instead. This is getting complicated for a newbie like me. What do you mean by interrupt routine? I don't have any interrupt service routine in my code. Is this something specific to RTOS? I don't know much about RTOS

Thank you, T.H.

posted by T.H. Lu 22 Feb 2014

Ah an LCD, it can be changed, but that also isn't exactly trivial.

First of all I checked which part actually uses RTOS, but that is the Ethernet Interface, so removing rtos from that is not an option.

A ticker if an interrupt routine (timer interrupt to be precise), so you do have them in your code ;). One possible way to rewrite code is something like:

//Global variable:
volatile bool tickerCalled = false;

//Ticker function:
void tickerFunc(void) {
  //Do stuff:

  //Finally:
  tickerCalled = true;
}

//Somewhere in your main loop:
if (tickerCalled == true) {
  lcd.printf("whatever it should printf");
  tickerCalled = false;
}
posted by Erik - 22 Feb 2014