Interval Timer and shared varianles

Robber

Member
Hi! I have a couple of questions about IntervalTimer and sharing variables safely
https://www.pjrc.com/teensy/td_timing_IntervalTimer.html (Interrupt Context Issues)

I'm working on some steppermotor code with Teensy 4.1

I understand it's considered common practice to disable interrupts when changing shared variables in the main loop as shown in the IntervalTimer example.
But my question/concern is, if i disable interrupts temporarily and that period of no interrupts happens to coincide with the tick of the interval timer, is that interval tick completely skipped altogether ? am i possibly missing a beat of the clock ?
or is this attached interrupt routine just called a tiny bit later, as soon I enable interrupts again ?

If i use volatile variables of 1 byte, like uint8_t, do i still need to disable interrupts when reading the value from the main loop ? what about writing ?

Thanx,
Rob
 
if i disable interrupts temporarily and that period of no interrupts happens to coincide with the tick of the interval timer, is that interval tick completely skipped altogether ? am i possibly missing a beat of the clock ? or is this attached interrupt routine just called a tiny bit later, as soon I enable interrupts again ?

The interrupt would occur after you re-enable interrupts.

If i use volatile variables of 1 byte, like uint8_t, do i still need to disable interrupts when reading the value from the main loop ? what about writing ?

The page you linked says...

When accessing shared variables, usually interrupts must be disabled. Even with volatile, if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly. If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled for the entire sequence of your code which accesses the data.

... so it sounds like 1-byte variables are okay. I may be corrected by one of the more senior members, but I would think single 8/16/32-bit variables would be okay. One method I use frequently is for an ISR to write to a FIFO and for the non-ISR code to read from the FIFO. You can do that without disabling interrupts. Not too long ago this was discussed on a thread and unfortunately I can't find it right now. See library FreqMeasureMulti as an example where data read in an ISR is pushed into a FIFO and can be read safely from non-ISR without disabling interrupts.

EDIT: Here is a link to the thread where lock-free ring buffers were discussed. This particular message contains a link to a useful blog post on the topic.

https://forum.pjrc.com/threads/5496...n-the-same-pin?p=301287&viewfull=1#post301287
 
Last edited:
The interrupt would occur after you re-enable interrupts.

Right so the interrupt flag is set by the timer even when interrupts are disabled, and it is serviced as soon as interrupts are enabled again, correct ?

but I would think single 8/16/32-bit variables would be okay.

I would think so too as the cpu is 32bit it could write 4 bytes in 1 instruction ??


!Thanks for linking the post about circular buffer, that is very helpful! as well as the link to this page that is mentioned there (https://www.downtowndougbrown.com/2013/01/microcontrollers-interrupt-safe-ring-buffers/)
 
Right so the interrupt flag is set by the timer even when interrupts are disabled, and it is serviced as soon as interrupts are enabled again, correct ?

Yes, that's right, and hopefully you won't have to disable interrupts.

I would think so too as the cpu is 32bit it could write 4 bytes in 1 instruction ??

Yes, that's my thinking, but it may not be as simple as that.

Thanks for linking the post about circular buffer, that is very helpful! as well as the link to this page that is mentioned there (https://www.downtowndougbrown.com/2013/01/microcontrollers-interrupt-safe-ring-buffers/)

There's a lot of good information on this forum. It takes some practice to figure out how to search it, though. Some of the search features don't seem to work the way I would expect. For example, there doesn't seem to be a way to search for posts by a certain person and also with some set of key words. Maybe someone will tell me I'm wrong about that.
 
.....there doesn't seem to be a way to search for posts by a certain person and also with some set of key words. Maybe someone will tell me I'm wrong about that.
There's sort of a way. Click on a persons name/id, then select view forum posts. A page of that person's posts will appear. To search on the page, if using edge, click the ... at the top of the page and select find on page or ctrl+F.
 
There's sort of a way. Click on a persons name/id, then select view forum posts. A page of that person's posts will appear. To search on the page, if using edge, click the ... at the top of the page and select find on page or ctrl+F.

Yes, I saw that, but it seems to limit the results to an arbitrary (and relatively small) number of posts, so it's not very useful for looking at history for someone who posts frequently.
 
There's a lot of good information on this forum. It takes some practice to figure out how to search it, though. Some of the search features don't seem to work the way I would expect. For example, there doesn't seem to be a way to search for posts by a certain person and also with some set of key words. Maybe someone will tell me I'm wrong about that.

Google is much better than the forum search. If, for example, you search for this:
site:forum.pjrc.com joepasquariello after:2022-01-01
you'll get all your forum posts from this year.

Some time ago I summarized the search syntax I'm aware of here: https://github.com/TeensyUser/doc/wiki/Forum-Search

You can also setup your own search engine and a shortcut which makes forum searches super convenient: https://www.youtube.com/watch?v=SUcB7obrRXI
 
Google is much better than the forum search. If, for example, you search for this:
site:forum.pjrc.com joepasquariello after:2022-01-01
you'll get all your forum posts from this year.

Some time ago I summarized the search syntax I'm aware of here: https://github.com/TeensyUser/doc/wiki/Forum-Search

You can also setup your own search engine and a shortcut which makes forum searches super convenient: https://www.youtube.com/watch?v=SUcB7obrRXI

Thanks,@luni. I do use google, but I didn't know about this syntax. I'll watch the video, too.
 
Thanks,@luni. I do use google, but I didn't know about this syntax. I'll watch the video, too.

Thanx that has helped enormously in finding things on the forum, could be a sticky forum post ?


I'm still wandering if i can safely (atomic) copy a uint32_t without need to disable interrupts on teensy4.1 ?
 
@BriComp: Yes the link is into the user wiki

@Robber: Yes you can atomically copy a uint32_t on the 32bit ARM Teensies. No need to disable interrupts
 
Back
Top