Multiple Tickers and interrupt detach strangeness

23 Feb 2010 . Edited: 23 Feb 2010

I am working on an application that needs to switch tickers on and off. I can switch more than one ticker on without any problems but when I start to detach the interrupts then strange things happen.

In the code example below I switch on four tickers in turn for each LED. Then I switch them off in turn. On my MBed when I switch off the first ticker, the other three spontaneously stop. After a while the first ticker starts up again and the cycle repeats.

Can anyone verify this behaviour and suggest a workaround/fix?

 

#include "mbed.h"


DigitalOut  led1(LED1);
DigitalOut  led2(LED2);
DigitalOut  led3(LED3);
DigitalOut  led4(LED4);
Ticker tick1;
Ticker tick2;
Ticker tick3;
Ticker tick4;

void int1(void);
void int2(void);
void int3(void);
void int4(void);

void int1(void) {
    led1=!led1;
}
void int2(void) {
    led2=!led2;
}
void int3(void) {
    led3=!led3;
}
void int4(void) {
    led4=!led4;
}

int main() {
    while(1) {
        printf("Starting first ticker.\n");
        tick1.attach(&int1,0.18);
        wait(2);
        printf("Starting second ticker.\n");
        tick2.attach(&int2,0.12);
        wait(2);
        printf("Starting third ticker.\n");
        tick3.attach(&int3,0.1);
        wait(2);
        printf("Starting fourth ticker.\n");
        tick4.attach(&int4,0.15);
        wait(2);
        printf("Stopping first ticker.\n");
        tick1.detach();
        led1=0;
        wait(2);
        printf("Stopping second ticker.\n");
        tick2.detach();
        led2=0;
        wait(2);
        printf("Stopping third ticker.\n");
        tick3.detach();
        led3=0;
        wait(2);
        printf("Stopping fourth ticker.\n");
        tick4.detach();
        led4=0;
        wait(2);
     }    
}

 

23 Feb 2010

Hi Simon,

What baud rate are you using? The default 9600 is painfully slow and may be causing some problems?

I had a similar problem with a program. I ended up by using a single ticker (say 1 ms) to inc a count. I then referenced the count and fired events off when the count got to a certain level.

It was a bit of a bodge but it worked!

Martin

23 Feb 2010

 

Martin Smith wrote:

Hi Simon,

What baud rate are you using? The default 9600 is painfully slow and may be causing some problems?

I had a similar problem with a program. I ended up by using a single ticker (say 1 ms) to inc a count. I then referenced the count and fired events off when the count got to a certain level.

It was a bit of a bodge but it worked!

Martin

Hi Martin,

The printf statements are just to say what is happening in the example and are at 9600 baud.

To solve the multiple ticker problem I think your suggestion is probably the best solution as a workaround. The alternative would be to try and access the timer registers directly but this would be unsafe because I can't guarantee that no other part of a program would be doing the same e.g. by using "Ticker"s.

Would be nice if the tickers worked as intended though. I'm just doing a sanity check before filing it as a bug.

Simon

 

23 Feb 2010

Looks like a bug. I've filed it at:

For reference/interest, we've been building a test rig infrastructure to allow us to do regression testing for libraries which will be pretty cool. Once it is all up and running, we'll aim to do a maintenance release of the libraries and we'll fix this as part of it.

Another workaround for now could be to attach a null function.

Thanks for a report that makes it easy to reproduce the problem; very much appreciated.

Simon

24 Feb 2010

Hi Simon,

Thank you for filing as a bug.

I tried attaching a null handler. It seems to get gummed up and freeze after attaching a null handler to the 2nd ticker. I can only re-assign the interrupt without a freeze if I first detach the old one first. Of course, when I use the dreaded "detach" method I get the original problem again.

The only workaround I think would work is to detach all the interrupts then re-attach all except the one I want detached. The only problem with that is that it would probably re-set all the timer counter values in the process.

SimonB

13 May 2010

Maybe a little late, but today I ran into the same problem. Both "detach" and attaching a null handler give a problem.

However, switching between two handlers (one doing my thing, the other just doing nothing) works fine for me.

Nico

17 May 2010 . Edited: 17 May 2010

Hello, I would like to know if this bug will be fixed ? I'm stuck, cause I have 2 tickers, I want to detach one while the other one works, in the first run - it works, but repeating the sequence without resetting mbed stops the 2 ticker as well. Round ways aren't quite what I expect hence my question.

Regards.

17 May 2010

Consider it on the bugfix list for the next release; i'll aim to give you an idea of when that'll be out soon.

Simon

18 May 2010

Hi,

We've just released another update that should now fix this; see http://mbed.org/blog/entry/135/?page=1#comment-137

Please report back!

Simon

19 May 2010

Hello,

just tested the new library, and it works! Thanks!

I switch on and off, and also change the frequency regularly, and found out that between two calls to 

attach() you call

On the other hand, I found that you always can call a detach( before an attach(), whether the ticker is attached or not.

Nico