I have a 120 Hz source feeding data line D2 which is set up as an input and triggers a simple ISR on the RISING edge. The ISR simply adds 120 a global 32 bit uint. So now I can make my own version of a millis() routine (call it isrMillis() ) that can read the ISR driven number, do some simple math, and return the number of miliseconds passed. granted my resolution will only be a resolution of just over 8mS, but that's plenty accurate for my app.
My dilemma is that I'm not 100% sure of my plan for returning a copy of the current 32 bit counter, without risking the access being interrupted. That would be a no-no because it would result in situations where all 4 bytes of the 32 bit number are not atomically connected. Disabling the interrupt while fetching accessing the 32 bit number is no good either, because I could then miss a transition on the interrupt pin, and losing time in the long run.
So I thought of maybe adding some kind of atomic semaphore flag to the ISR, that is set and cleared as the ISR starts and ends. Then my new isrMillis() call could simply wait for it to be clear in a while() loop before reading. But I don't know what the NANO processor and language offer for such an atomic operation. When I had a problem like this in Visual C++, I had calls like "InterlockedExchange()" to work with, that would allow such atomic ops. Not sure what I have here.
While I'm spinning the wheels of my brain on this, I thought maybe I'd ask some advise.
Boring background: I've decided that my application will be more useful if I have more accurate time of day, and fall back to system time (millis() ) when its unavailable. I've built my own simple brute force power supply using a 6VAC transformer (60hz in the USA) , a rectifier bridge, and a filter cap, to power my Arduino NANO project, and to make a time base available, I've added another diode between the rectifier bridge and the filter capacitor. That offers me a filtered 9VDC for the board, as well as a convenient source of 120 Hz between the bridge and the extra diode. I then feed that point to two 10K resistors, so the midpoint will then offer pulses of about 4V to my data pin.