Accessing Global Variable in ISR

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

cross mob
lock attach
Attachments are accessible only for community members.
RAP_2366941
Level 1
Level 1

Dear All,

   

I'm trying to access global volatile variable in UART ISR. But it shows zero always.

   

However once in a blue moon it shows correct data...

   

I'm struggling from last two days.... Help me please...

   

 

   

Plz find the snapshot of code attached below:

0 Likes
6 Replies
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

'buffer' is defined volatile, but its never accessed in the ISR. All other fields accessed by the ISR are not volatile.

0 Likes
Anonymous
Not applicable

Setting query and response to volatile should fix it (as hli said above)

0 Likes
RAP_2366941
Level 1
Level 1

Hli: buffer is accessed in prepareResponse() function called in ISR.

   

Responselength = prepareResponse(); //copying bytes from buffer array to response array

   

 

   

e.pratt_1639216 : I tried making query and response to volatile, but with no success.

0 Likes
RAP_2366941
Level 1
Level 1

If I write in buffer array in ISR, it works. But if I access  buffer in main() and write into it, It wont reflect when I'm in ISR..

0 Likes
Anonymous
Not applicable

Basically, any variable you use in the ISR and use elsewhere you will need to declare as volatile to prevent the compiler from mixing up RAM reference locations. Hence why we suggested starting with that first.

   

Could the buffer variable be going out of scope somehow? (Probably not, just a thought)

   

Have you tried disabling the interrupts when writing to the Buffer array in main? The ISR could be firing mid-buffer write. (Atomicity issue)

   

Also, can you give an example of how you are writing to the buffer in main?

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Can you create a simple project showing the problem (as simple as possible) and then share it here ('create workspace bundle')?

0 Likes