Atomic Operations

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable
        Hello. I have two lines of code, one is to read a variable and the other is to reset it to zero. The problem is that the variable is only updated by interrupt, and I do not want to have an interrupt between these two lines. However, I do not want to miss an interrupt also, so disabling interrupts does not work for me. I figured out it should be done via atomic operations, but I couldn’t figured how or find an example. Could you guide me to do it, please.   
0 Likes
9 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

One possibility, set a flag in main() when you want to execute the two lines of code, and if

   

ISR fires and it finds the main() flag set, it sets another flag, saves value in a temp register,

   

exits. When 2 lines finish then test for flag from ISR, and update variable if set and clear ISR

   

flag.

   

 

   

Or look at disabling the interrupt, but leaving it pending, and re-enabling after two lines of code

   

have executed. Take a look at interrupt controller in TRM manual for further info.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable
        Hi frturan   
I didn't understand what you want to do clearly.   
and what is your ploblem was.   
Disabling the global interrupt doesn't work only for you? that is odd   
What is atomic operations?   
Is that like a kind of test-and-set operation on assembler language?   
You would be better write your code here.   
0 Likes
Anonymous
Not applicable

ATOMIC operation in computing

   

" There isn't any intermediate state that's visible before it completes.
So if your CPU gets interrupted, or if another task(in multitasking system)
is accessing the same memory, it doesn't affect the result, and these
other operations will observe it as either completed or not started."

   

In your case.

   

I think what you should do is

   

1. Ensuring there is no interrupt when you are reading/writing/updating that variable.

   

2.  The function needs to disable interrupt while reading the variable and set a flag to indicate that
 it was read. enable interrupt after reading the value.
 The ISR would disable interrupts that would access that variable first, then read and clear the flag
  and based on the flag, perform the correct operation of the variable.
  

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Exactly for this purpose there are CyEnterCriticalSection() and CyExitCriticalSection() are for. Have a look into the "System Reference Guide" (Creator 3.0 -> Help -> System Reference).

   

It does not make the instructions in between atomic, but it temporarily disables all interrupts for an area of code and safely re-enables it.

   

Additionally consider to declare those variables that are changed from an interrupt handler as "voilatile" which will prevent errors coming from the excessive (but very good) optimization step in GCC.

   

 

   

Bob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Some more explanation:

   

You will not "miss" any interrupt when disabling interrupts, you just will delay its handling for some CPU-instructions. The PSoC4-internal Interrupt Controller Unit (ICU) will remember any interrupt occuring during a disabled time. When enabled back again the interrupt will fire after execution of the next instruction. Only when an interrupt is applied twice while disabled there is the chance that the second one is missed.

   

 

   

Bob

0 Likes

hi bob ,

regarding your post:

Some more explanation:

  

You will not "miss" any interrupt when disabling interrupts, you just will delay its handling for some CPU-instructions. The PSoC4-internal Interrupt Controller Unit (ICU) will remember any interrupt occuring during a disabled time. When enabled back again the interrupt will fire after execution of the next instruction. Only when an interrupt is applied twice while disabled there is the chance that the second one is missed.

  

  

Bob

Is it documented in one of the psoc pdfs? I would like to send it to a client.

0 Likes

There is a documentation from ARM explaining in depth the exception (ARM terminology for interrupts) handling. Another source is the documentation for the PICU (Peripheral Interrupt Controller Unit) which is responsible for enabling, masking and priority chaining interrupts.

Bob

0 Likes
Anonymous
Not applicable
        Thank you for all your responses. Expecially, Bob Marlowe. I was thinking disabling interrupts will make me lose one, if occurs when dissabled. So it is a little bit interesting for me. Also I found CyEnterCriticalSection() in System Reference Guide, but I was stuck on using __ATOMIC_ACQUIRE; and __ATOMIC_RELEASE; type commands. Cretor comprises those commands (I think thanks to GCC); however, I could not found how to use them. Still your responses are so beneficial for me, and I think I will be able to. Thank you very much.   
0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Your  __ATOMIC... operations are ment to work on threads sharing the same memory. Since threading needs an operating system that really supports threads and your PSoC4 doesn't have got one this are the wrong functions. These just prevent task-switching while the shared memory is accessed.

   

 

   

Bob

0 Likes