문제

I have following define:

#define DEVICE_ID   ((uint8_t)0x3f)

and I have following function:

void LIS3DSH_Init(LIS3DSH_InitTypeDef* LIS3DSH_InitStruct)
{
//    uint8_t ctrl=0x00;
    uint8_t ident=0x00;

    LIS3DSH_LowLevel_Init();

    LIS3DSH_Read(&ident,
                 LIS3DSH_WHOAMI_REG_ADDR,
                 1);

    if(DEVICE_ID==ident)
    {
        // LIS3DSH detected
    }
    else
    {
        // LIS3DSH not detected

        failureHandler();
    }

}   // LIS3DSH_Init

Now, if I go step-by-step in this function, the ident variable gets value 0x3f after LIS3DSH_Read function call, which is ok. My question is, why the hell if clause jumps to failureHandler? The values of DEVICE_ID and ident are the same - both are 0x3f, if should not jump to failureHanlder(). I am working on LIS3DSH accelerator library using IAR C/C++ and STM32F4 Discovery Board. Here is a screenshot of situation:If clause going wrong?!

도움이 되었습니까?

해결책

You should type-cast the if(DEVICE_ID==ident) to be if( (uint8_t)DEVICE_ID == (uint8_t)ident)

This has been an issue for me in the past.

And yes, declare ident as volatile, and for debugging purposes, try adding a delay before the comparison via a for-loop with __no_operation(); inside it. Note that there are 2 underscores in front of that, not 1 (intrinsic NOP instruction), and that a single NOP takes roughly ~29ns on a 168MHz board, measured via scope.

Also, since you have IAR, you might as well pop open the "assembly" view and look at what registers and/or constants are actually being compared. Open the "register" view as well...so you can see the register values themselves.

다른 팁

Is the function

failureHandler()

processed? If not, the view of the debugger is just confusing due to compiler optimizations. It is likely that the optimized code uses a common "return" code for both the good case and the failure case. The debugger stops at the "exit" of the failure path even in good case.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top