c - Why is my function static variable never different despite being incremented? -


i writing callback function in c. intended initialise i2c sensor, , called @ conclusion of each (split-phase) configuration step; after 9th call, device ready use.

the basic idea of function this:

void callback(void) {     static uint8_t calls = 0;      if (++calls == 9) {         // finalise device setup (literally single line of code)     } } 

my problem above if statement never being entered, despite function being called 9 times.

the (dis)assembly code function seems sane (with exception of subi . 0xff trick increment, despite inclusion of inc instruction):

00000a4c <callback>:      a4c:   80 91 9e 02     lds r24, 0x029e      a50:   8f 5f           subi    r24, 0xff   ; 255      a52:   80 93 9e 02     sts 0x029e, r24      a56:   89 30           cpi r24, 0x09   ; 9      a58:   09 f0           breq    .+2         ; 0xa5c <callback+0x10>      a5a:   08 95           ret      a5c:   2e e1           ldi r18, 0x1e   ; 30      a5e:   35 e0           ldi r19, 0x05   ; 5      a60:   41 e0           ldi r20, 0x01   ; 1      a62:   60 e0           ldi r22, 0x00   ; 0      a64:   84 e7           ldi r24, 0x74   ; 116      a66:   0c 94 c7 02     jmp 0x58e   ; 0x58e <twi_set_register> 

i writing code atmel avr chip, , compiling avr-gcc. have no meaningful code debugging capabilities (i don't have access jtag programmer, , function asynchronous/split-phase in case; usart printing slow).

however, have access logic analyser, , have been able determine number of things placing while (1) ; statements inside code:

  • the function called - if place infinite loop @ start of function, microcontroller hangs
  • the function should called 9 times - trigger function i2c communication, , in previous step hangs after first communication; can observe 9 complete , valid i2c communications
  • calls incremented within function - if add if (calls == 0) { while (1) ; } after increment, not hang
  • calls never non-zero @ start of function - if add if (calls) { while(1) ; } before increment, not hang

i'm @ loss ideas.

does have suggestions cause this, or new debugging steps take?

for can think of 3 possibilities: 1) assumption function being called on every i2c communication incorrect, 2) program has bug (maybe memory leak) in unrelated function causes variable calls become corrupted. or 3) 2 or more threads calling function simultaneously , calls being incremented in different way expect, use > instead of ==, if solves problem, running in milti-threaded environment , didn't konw.

you need accurate method know exact value of calls, if don't have debugger , don't have means output text either, thing have left play time. don't know compiler, sure contains useful timing functions, would loop before increment 10+calls seconds, , after increment again 10+calls seconds, example:

sleep(1000*(10+calls)); ++calls; sleep(1000*(10+calls));  if(calls>8){    // actual code } 

i (chronometer in hand) expect delay of (10+0 plus 10+1) = 21 seconds on first call, 23 seconds on second call, 25 in third , on. way sure value of calls started 0 , progressively increased until 9.

also, must test expect not don't expect, instead of doing this:

++calls; if (calls==0) while (1); 

do this:

++calls; if (calls==1) while (1); 

that way if program hangs can sure value of calls 1, , not whatever different zero. if count 1 valid i2c communication , program hangs transition 0 1 done correctly, change hang statement accordingly:

++calls; if (calls==2) while (1); 

again, if count 2 valid i2c communications before program hangs means transition 1 2 correct, , on.

another suggestion, try this:

uint8_t times(void){    static uint8_t calls = 0;    return ++calls; }   void callback(void){     if (times()>8) {        // actual code    }  } 

and this:

void callback(void){ static uint8_t calls = 0;     if (calls++>7) {        // code.    } } 

hope helps.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -