Watchdog can handle callbacks - VirtualManager can attach to the tick. This should simplify the logic.
Watchdog can tick on its own using tickers.
VirtualWatchdog uses attach to get a callback when Watchdog ticks - to process own
linked list of virtual watchdogs.
Watchdog should be usable on it's own - kicking it via ticker. No Virtual involved.
VirtualWatchdog as well - services should use this one to have multiple one in the system.
There's WatchdogManager - basically internal class to manage ticker for Watchdog.
Refactor old Watchdog (it was not a driver) to become VirtualWatchdog.
This is software virtual watchdog. This it the primary used watchdog in user application.
VirtualWatchdog: has-a watchdog. Initializes hw watchdog - start it when first used, stops it when there is no more VirtualWatchdog in the system -
list is empty.
Adding also check to watchdog to make sure there is only one in the system - runtime error if multiple objects created to already
running hw watchdog.
Watchdog is hardware driver. It interacts with HAL - provides wrapper to interact with the peripheral.
Provides basic functionality: start/stop, get timeout/max timeout.
It is automatically kicked by a timer, according to the timeout set in ctor.
Without freeing the DAC, the pin will continue to be configured as DAC. Which make it impossible to change it from Analogout to anything else.
Check this code that allow you to change the AnalogOut to DigitalOut
<<code>>
#include "mbed.h"
class myAnalog : public AnalogOut{
public:
myAnalog(PinName myname);
~myAnalog();
PinName _myname;
};
myAnalog::myAnalog(PinName myname):AnalogOut(myname),
_myname(myname){
;
}
myAnalog::~myAnalog(){
analogout_free(&this->_dac);
}
myAnalog *x=0;
DigitalOut *xx=0;
void do_something_analog() {
x=new myAnalog(PA_4);
double g=0.0;
while(g<0.5){
x->write(g);
g=g+0.1;
wait_ms(50);
}
if (x!=0){
delete x;
x=0;
}
}
void do_something_digital() {
xx=new DigitalOut(PA_4);
for(int i=0;i<10;i++){
*xx = 1;
wait_ms(50);
*xx = 0;
wait_ms(50);
}
if (xx!=0){
delete xx;
xx=0;
}
}
int main()
{
printf("\nAnalog loop example\n");
while(1) {
do_something_digital();
do_something_analog();
}
}
<</code>>