View previous topic :: View next topic |
Author |
Message |
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
Quick question about ISR global var... |
Posted: Thu Aug 20, 2015 3:00 pm |
|
|
Hi,
Normally, it's best practice to disable interrupts on anything higher than 8-bit ints to prevent corruption (when shared with an ISR).
But in the 24 bit op-code world ... is the 16-bit ints safe to read/write without disabling ISR (i.e. atomic)?
Thanks! _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Thu Aug 20, 2015 4:07 pm |
|
|
On any processor it's best practice to disable interrupts on any variable that's not the native width of the processor, whether that's narrower or wider than the data bus.
But then again, it's always good practice to disable interrupts when working on/with a variable that's used inside of an ISR whether or not there will actually be an issue. You never know when you're going to migrate to a different processor or processor family. |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Thu Aug 20, 2015 4:15 pm |
|
|
newguy wrote: | On any processor it's best practice to disable interrupts on any variable that's not the native width of the processor, whether that's narrower or wider than the data bus.. |
I'm working on a PIC24 right now... you are saying that even a 8-bit int is not atomic because it's narrower than it's native 16-bit?
I though that 8-bit still a single atomic operation on a wider bus!!
I try to avoid disabling the interrupts unless it is really necessary (I have few global flags and var to changes) ... _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Thu Aug 20, 2015 9:53 pm |
|
|
It depends how the compiler handles whatever it is you're asking it to do. Never assume that it's atomic. Look at the list file when in doubt.
I've been burned enough both by code that someone else wrote and stuff that I wrote that I don't take anything for granted anymore. Since I stopped assuming/trusting my projects work better/more reliably. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Aug 21, 2015 12:02 am |
|
|
If you declare a 16bit int, 'normally', it will be aligned onto an even address in memory (this is why sometimes structures will be larger than expected). This is done, so they can be written/read using single 16bit accesses.
The compiler will then always use these accesses, unless you deliberately access the value by something like using a pointer to an 8 bit variable, and rebuilding it yourself.
So by default, a 16bit integer will always use an atomic write/read on the PIC24, but it is possible to write code that will bypass this.
A simple look at the assembly will show that single instructions are used to access the variable. |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
|
|