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

Thread: Setting a variable to 0 requires two writes?

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    10

    Setting a variable to 0 requires two writes?

    https://www.pjrc.com/teensy/interrupts.html sez:
    Even a simple operation like setting the variable to zero needs to be protected with cli() and sei(), because the compiler will need to make 2 writes
    Why two writes?

  2. #2
    Senior Member MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    2,382
    Looking at the code, that page was written for the Teensy 2.0. The AVR processor used in the Teensy 2.0 is an 8-bit processor, so a write of a 16-bit value involved two writes. The Teensy 3.x/LC boards are 32-bit processors, and in general 8/16/32-bit values should be written with one write to memory, assuming the value is properly aligned. If the value is not aligned, it likely will take multiple writes if you are looking at the memory, and an interrupt may occur between the writes.

  3. #3
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    Makes sense. Thank you, MichaelMeissner!

  4. #4
    Senior Member Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    827
    Additional question: is there a way to make sure that everything is aligned and that writes are thus atomic up to 32bit ? Doesn't the compiler do this automatically?

Posting Permissions

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