mpfj
Joined: 09 Sep 2003 Posts: 95 Location: UK
|
Corrupt scratch variable (18F8720, PCWH 3.190) |
Posted: Tue Mar 01, 2005 2:03 pm |
|
|
Tough one to explain, but here goes ...
I have the following (simplified) snippet of code ...
Code: |
void barcode_check(void) {
int8 i;
/* if any chars rxed from com2 */
if (!queue_empty(&serial.com2.qrx)) {
// ... do stuff ...
}
}
int1 queue_empty(queue_struct *q) {
return(q->size == 0);
}
|
I also have a timer interrupt occuring at non-regular intervals.
Under MPLAB, I can breakpoint at the "do stuff" section and, when it breaks, I check the q->size value. It's zero.
This is a large piece of code, and I have several instances where this has happened, and not just with the queue_empty() function. It happens with other functions that return int1.
So, I have then changed the ...
Code: |
if (!queue_empty(&serial.com2.qrx)) {
|
... to ...
Code: |
if (serial.com2.qrx.size != 0) {
|
... and, hey presto, the problem goes away.
I have concluded that ...
(1) the queue_empty() function has just returned, with its true/false value returned in the 0x01 scratch variable
(2) my timer interrupt then occurs
(3) when the interrupt returns, somehow the 0x01 scratch variable gets corrupt
I have also tested this by placing global flags in the queue_empty() function that hold a copy of the true/false value the function is returning. When the "problem" occurs, the function appears to have returned false, but the global flag says otherwise
Turning off the interrupt gets rid of the problem (i.e. no false true/false values).
I am going to try and code a smaller, more concrete, example for others to try.
But in the meantime, has anyone ever encountered this before ?[/code] |
|