AnsweredAssumed Answered

Problem with interrupt and interrupt counter

Question asked by sebasira on Jan 24, 2013
Latest reply on Feb 8, 2013 by TomE

Hello everybody!


Well, I didn't know how to name this topic and I guess this one fit the most.


I've got a program wich receive data from SCI in interrupt mode. Everytime it receive a byte, a counter is increased (interrupt counter). This counter helps me track down how many bytes I've received and were not processed yet.

Then, in the main loop, if the counter is greater than zero, it means that a new byte (or bytes) need to be processed. So after I've done with it, I decrease the counter. The problem I was having (the symptom) was that there were bytes left to be processed and the counter has already reach zero. I was burning my head until I figured out what was happening.


I look at the asm generated code for the 'counter--' instruction, and it was something like this:


word counter;



MOVE.W     1902(A5), D0         gets counter value and store it in D0

SUBQ.L     #1, D0               decrease D0 (counter value)

MOVE.W     D0,1902(A5)          update counter value


Well, I think the problem arise when I receive a byte after the counter value is copied in D0, so the counter is increased in the IRQ, but when it's updated from D0 it loses the received byte count.



I've been working with this approach for several years and never had a problem. But I always use it in 16bits processors, and now I'm working with a 32bits one (MCF51QE). Maybe that's the problem and counter should be 32bits long so asm operation is done in a single instruction? (I haven't tryed this yet and I don't know the answer for this question for sure, so please could you answer it?)Or should I 'do' anything else so the operation is done in an atomic instruction?



I know I can use another approach such as counting how many bytes I've received and how many bytes I've processed independently and if the difference is not zero then some bytes need to be taken care of. But if possible, I'd like to continue with the first approach in order to reduce code update.


Thanks in advance!



Best Regards,




Mensaje editado por: Sebastian M. Irazabal  Well, I've declared counter as 32 bits long and now the decrease instruction takes place on a single instruction