CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Quick question about ISR global var...

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

Quick question about ISR global var...
PostPosted: Thu Aug 20, 2015 3:00 pm     Reply with quote

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: 1903

View user's profile Send private message

PostPosted: Thu Aug 20, 2015 4:07 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 20, 2015 4:15 pm     Reply with quote

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: 1903

View user's profile Send private message

PostPosted: Thu Aug 20, 2015 9:53 pm     Reply with quote

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: 19260

View user's profile Send private message

PostPosted: Fri Aug 21, 2015 12:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 21, 2015 3:51 am     Reply with quote

Thank you guys!

I will double check in the code assembly...

Have a wonderful week-end ! Smile
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group