Search code examples
cdebuggingincrementmicrocontrolleriar

Incrementation doesn't go further than 0x01


So I've got functions:

int f1(uint8_t* a, int b)
{ 
    for(int i = 0; i < b; i++)
        f2(&a[i]);

    return 1;
}

static void f2(uint8_t* a)
{
    REG1 = *a;
    ...
    *a = REG2;
}

"a" is a data register that is used as a buffer to put the data all the way from the main loop into the target function f2() through different wrapper functions.

"REG1" and "REG2" are I/O registers into and out of which the data is passed with "a". The value of "REG2" implicitly changes while in f2() through hardware operations.

When I watch the variable changes in debug mode, the following happens:

  1. b = 2
  2. declare i = 0, i < b
  3. call f2
  4. increment i to i = 1, i < b
  5. call f2
  6. increment i to i = 0, i < b

And since "i" never reaches 2, the loop never ends. The program is compiled and debugged with IAR EW for AVR. The optimisation for the compiler is turned off.


Solution

  • @Lundin is correct, I was passing an address of a pointer instead of the pointer itself with:

    f2(&a[i]);