Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 11 of 11

Thread: Interval Timer and shared varianles

  1. #1
    Junior Member
    Join Date
    Jun 2022
    Location
    The Hague
    Posts
    7

    Interval Timer and shared varianles

    Hi! I have a couple of questions about IntervalTimer and sharing variables safely
    https://www.pjrc.com/teensy/td_timin...rvalTimer.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

  2. #2
    Quote Originally Posted by Robber View Post
    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/54962...l=1#post301287
    Last edited by joepasquariello; 06-20-2022 at 11:34 PM.

  3. #3
    Junior Member
    Join Date
    Jun 2022
    Location
    The Hague
    Posts
    7
    Quote Originally Posted by joepasquariello View Post
    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 ?

    Quote Originally Posted by joepasquariello View Post
    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/20...-ring-buffers/)

  4. #4
    Quote Originally Posted by Robber View Post
    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/20...-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.

  5. #5
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    756
    .....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.

  6. #6
    Quote Originally Posted by BriComp View Post
    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.

  7. #7
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,835
    Quote Originally Posted by joepasquariello View Post
    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

  8. #8
    Quote Originally Posted by luni View Post
    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.

  9. #9
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    756
    Some time ago I summarized the search syntax I'm aware of here: https://github.com/TeensyUser/doc/wiki/Forum-Search
    Is this in the WiKi?

  10. #10
    Junior Member
    Join Date
    Jun 2022
    Location
    The Hague
    Posts
    7
    Quote Originally Posted by joepasquariello View Post
    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 ?

  11. #11
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,835
    @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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •