Is there a watchdog functionality in IMP

Hi,
I’m running a code, where I’m polling ADXL345 for available data in FIFO, if there is nothing, my code waits until I receive some valid number in the status register.
For the fail safe functionality, In case ADXL stops working, status returned will always be zero, and my code goes for a toss.
In a normal microcontroller I’d have added a watchdog for a specified period and would have cancelled watchdog, if I return from the function call before watchdog fires.
Is there any similar option in IMP?

Here, you’d just implement a ready timeout in your code, so you don’t loop forever. You can then also return a sensible error code for support purposes.

A future release will catch obscenely long squirrel loops, but all it can do is reboot the device. The device is pretty likely to go and do the same infinite loop again. This doesn’t actually help the product much in real world use, but it does help development when you’ve inadvertently introduced a bug.

Can you provide some code snippet for the same??
Should it be like this
`myTimer.adxl <- imp.wakeup(10,function() { nv.sleep_reason = “unable to sample adxl”; server.sleepfor(5) } );

adxl345.spi_write(0x2d,0x00); // put device in standby mode
adxl345.spi_write(0x2d,0x08); // put device in measurement mode
adxl345.start_sampler();
adxl345.spi_write(0x2d,0x04); // put device in sleep mode

 if("adxl" in myTimer) 
           {
              imp.cancelwakeup(myTimer.adxl);
              delete myTimer.adxl;
           };

`

I don’t see any loops in this code at all?

here adxl345.start_sampler is a function which reads samples from sensor in a loop until configured number of samples are read.
For unknown reasons, if sensor is not able to provide the data, device will be stuck in the loop and never comes back and will keep polling.
To avoid that scenario, what we want is a watchdog functionality which fires say after 10 seconds if execution doesn’t come back from sampling, otherwise stop the timer after the return.

algorithm is something like this

count = 0; // num_samples is configured number of samples to read eg. 2000 while(count < num_samples) { fifo_status = spi_read(Fifo_status_reg) if((fifo_status& 0x1f) >0) // lsb 5 bits represent number of fifo locations filled { for (i = 0; i<fifo_status;i++) { spi_read_sensor_value(); // reads all three axis data } } }

so if fifo_status returned by sensor is zero, it will keep polling

is that syntax correct?

Just remember to update count, @shatruddha, or you’ll get an infinite loop (count is always less than num_samples) and that will cause all sorts of bother.

No, there’s currently no way to write a watchdog in Squirrel: Squirrel code cannot interrupt or pre-empt other Squirrel code. If you’re writing a loop that you’re worried might become an infinite loop, you should add an explicit timeout to the loop condition: “&& (my_timeout - hardware.micros()) > 0”.

Peter

thanks Peter