Watchdog or not to Watchdog?

15 Mar 2011

Hi Guys, First time posting here so I'll try and be concise! I'm working on creating a library for a Mifare RFID module (that hasn't been written yet). It communicates via serial and I would like a way to effectively "timeout" when I wait for communication on RX.

Whilst I've tried to make the way it works with interrupts as reliable as possible, there will/may be a point where it transmits data, waits for a response and misses it completely (for whatever reason). In which case the best thing to do would be to restart.

Is this the kind of thing Watchdog was designed for or am I getting it confused with something else?

15 Mar 2011

The watchdog is really for reseting the LPC1768 when it's basically become unstable or entered an unforseen state which it cannot recover from. Really, you should be handling timeouts at the protocols application layer. As a library writer you have to think about your users. They won't be pleased if your comms protocol fails and brings the biggest hammer to the table to resolve it. As a library it really should callback to report the failure to teh next level above it using the library.

Think like this. Some minor component in your car, I dunno, a tyre pressure sensor, fails to send a value to the car's main management system. Would you like you car to reboot at 70mph?

15 Mar 2011

Andy Kirkham wrote:

The watchdog is really for reseting the LPC1768 when it's basically become unstable or entered an unforseen state which it cannot recover from. Really, you should be handling timeouts at the protocols application layer. As a library writer you have to think about your users. They won't be pleased if your comms protocol fails and brings the biggest hammer to the table to resolve it. As a library it really should callback to report the failure to teh next level above it using the library.

Think like this. Some minor component in your car, I dunno, a tyre pressure sensor, fails to send a value to the car's main management system. Would you like you car to reboot at 70mph?

You're exactly right though I'm not sure how I would effectively "timeout" when its hanging waiting for serial input that doesn't arrive. (For example if the RFID module lost power). I've tried to minimise this possibility as much as possible however if for some reason it did happen, should the library worry about this or should this be something the user should monitor?

15 Mar 2011

If it's a library it must pass the fact that a timeout occured to the application using the library so it can take action as required. If you are implementing a proctol then you'd probably, for example, reset your internal state machine on timeout and then notify the app.

Take a look at some libraries out there, there's plenty to learn from. I have many that do lots like this. Here's some "psuedo code" that kind of shows the setup you need.

Psuedo code

#include "mbed.h"

class MyProtocol {
protected:
    FunctionPointer TOcallback;
    Timeout TO;
    void timedOut(void) { 
        reset(); // do whatever you need to do in the event of a timeout.
        TOcallback.call(); // inform the app a timeout occured.
    }
public:
    friend class Timeout;

    int send(char *message) {
       printf(message); // or however you are sending your message.
       TO.attach(this, MyProtocol::timedOut, 1);
    }

    int receive() {
       TO.detach(); // Got reply, stop timeout timer from firing.
       // do whatever you need with your reply.
    }

    void attach_timeout_callback(void (*fptr)(void)) {  
        TOcallback.attach(fptr);                 
    }

    template<typename T>
    void attach_timeout_callback(T* tptr, void (T::*mptr)(void)) {  
        if((mptr != NULL) && (tptr != NULL)) {
            TOcallback.attach(tptr, mptr);         
        }
    }

};

15 Mar 2011

Thanks for the help Andy, that pseudo code makes a lot of sense! I'll have a look at some other libraries and experiment; thanks again for the help! (If they had a rep system on here I'd give you a star :) )

15 Mar 2011

Watchdog timers are often used in critical process control and similar embedded apps. They're in the microprocessors by request.