1 Reply Latest reply on Mar 7, 2017 1:09 PM by user_1377889

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

    alexey.monastyrenko

      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?