Loading...
Pages: 1 [2]   Go Down
Author Topic: Reset millis() clock  (Read 1616 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the thorough description Bill.

If the variables in question have been confirmed to be ISR access only by the Timer0 overflow ISR and we have disabled the interrupt, is it necessary to provide explicit atomic access protection or is 'standard' access sufficient from the function in question?

Best Regards,
George
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 129
Posts: 10374
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


As written, @MGeo's code essentially accesses the variables atomically.  The timer is stopped (and the interrupt disabled) during the access so there is no possibility of a conflict with the interrupt service routine.

However, @MGeo restarts the timer without ensuring the new values have been first written to SRAM.  That's why volatile is necessary.


Quote
If the variables in question have been confirmed to be ISR access only by the Timer0 overflow ISR and we have disabled the interrupt, is it necessary to provide explicit atomic access protection


No.

Quote
or is 'standard' access sufficient from the function in question?

Yes.


Edit: spelling.
« Last Edit: April 30, 2012, 04:16:27 am by Coding Badly » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

However, @MGeo restarts the timer without ensuring the new values have been first written to SRAM.  That's why volatile is necessary.

Thank you, that adds much clarity to the subject.

About this insistence that the millisecond timer be reset: expect the Arduino to react about as violently as I would react if you grabbed my wrist and tried setting the time on my watch.

That's a bit much, particularly considering as programmer it is in the end my watch to set.

------------------------------------

In case others come across this thread with the same question:

After all the input here and my testing and studying of wiring.c, I'm realizing you can in fact reset millis, mostly.  The fractional counter variable timer0_fract within wiring.c is used for the microseconds calculation and is declared as follows (in bold italic):

volatile unsigned long timer0_overflow_count = 0;
volatile unsigned long timer0_millis = 0;
static unsigned char timer0_fract = 0;

This can not be reached from a sketch.  So you can clear most but not all of the counter state variables from a sketch.  The only way I can see around this is to modify wiring.c.  If that is the case, one would be better off placing a custom millis_reset() in the modified wiring.c.  All a bit much for the casual user.

George
« Last Edit: May 05, 2012, 09:03:28 am by MGeo » Logged

Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 226
Posts: 14091
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

For my applciation I desire to reset the millis() clock.  

For my application I desire to reset the year to 1984.
Logged


Pages: 1 [2]   Go Up
Print
 
Jump to: