CESARDOLO / narcoleptic

Automatically exported from code.google.com/p/narcoleptic
0 stars 0 forks source link

Time overflow for delays shorter than 16-17ms #9

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. call Narcoleptic.delay(10)

What is the expected output? What do you see instead?
Expected: MCU should sleep for 10ms and then resume working
Actual outcome: MCU freezes apparently - in reality the counters in delay 
method underflow/overflow and MCU sleeps for ~15 minutes.

The problem is in these 2 lines in delay():
  microseconds -= watchdogTime_us;
  if (microseconds > 0) {
- microseconds variable is defined as uint32_t, so it can never be negative - 
if the requested delay is shorter than watchdog time, it gets a very high value 
(which will again overflow in 16-bit sleep_periods).
Solution would be to change the two lines above to:
  if (microseconds > watchdogTime_us) {
    microseconds -= watchdogTime_us;
- functionally the same, only without annoying freezes ;-)
It might also make sense to change milliseconds parameter to uint32_t for 
sleeps longer than ~32s, and also same for sleep_periods to get it over ~15 
minutes (maximum sleep time would then be some 1h15m).

I'm attaching the patch with all my local changes, if anyone's interested 
(can't push it directly into the repo):
- fixed microseconds underflow bug in delay()
- delay() parameter changed to uint32_t with microseconds overflow protection
- sleep_periods changed to uint32_t

Original issue reported on code.google.com by peterva...@gmail.com on 28 Apr 2015 at 7:42

Attachments: