PSoc 101 Lesson 10 - why there is no volatile, compiler fence etc?

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

cross mob
Anonymous
Not applicable

First - thanks for the course. Probably it's best IT tutorial videos I have ever seen (usually I just say "Why can't you just write it down so I can read in one minute instead of listening ten minutes?" but your videos are different).

   

But why when you use some variables modified in interrupt - you don't write in code "this variable modified somewhere"? At least volatile?

   

There are two problems in sample code:

   
        
  1. If you set configuration to release - code will not work at all (i2cbuf[0] in main never read because of it "never modified").
  2.     
  3. You read i2cbuf[0] in one line (comparing with "compare" variable) then read again to assign to "compare". Probably it will not break here but it's definitely "smelly code" (value can be changed between these points). Looks like there should be used CyEnterCriticalSection/CyExitCriticalSection pair (which means we don't need volatile afaik) or some kind of atomic read.
  4.    
   

I understand you try to make code as simple as possible but it's bad idea to teach students such bad practices.

   

Also I have a technical question: is there __asm__ __volatile__ ("" : : : "memory") code or something like it in CyEnterCriticalSection/CyExitCriticalSection? Can I rely on fact any memory byte I read after CyEnterCriticalSection will be read from memory _after_ CyEnterCriticalSection call?

   

And one more question: is there some kind of atomic operations (CAS, atomic increment etc) in Cypress library or should I use CyEnterCriticalSection/CyExitCriticalSection?

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

Not declaring a global variable as "volatile" when it is changed in an interrupt handler is a common error.

   

Enter- and exit critical section just work on the interrupt state and ensures that the program doesn't get interrupted.

   

ARM CPUs are RISC processors, there are probably no atomic increments and nobody knows how the C-compiler will use it. So stick to the critical section.

   

 

   

Bob

View solution in original post

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

Not declaring a global variable as "volatile" when it is changed in an interrupt handler is a common error.

   

Enter- and exit critical section just work on the interrupt state and ensures that the program doesn't get interrupted.

   

ARM CPUs are RISC processors, there are probably no atomic increments and nobody knows how the C-compiler will use it. So stick to the critical section.

   

 

   

Bob

0 Likes