Suggestions on boolean type value judgment in Huada MCU function library

Recently, a problem was discovered in the development process of Huada HC32L176KATA.
Pin PC11 controls the LED indicator and calls the function Gpio_WriteOutputIO(GpioPortC, GpioPin11, x&0x80).
This function is used in the program to drive the LED light. The result is that it does not go off after it turns on. Debugging, measuring the IO pin with a multimeter is also low level, and looking at the waveform with an oscilloscope is always low level, I went, what happened, I have been a single-chip microcomputer for N years,
it is such a simple thing, I can’t handle it, it’s strange Weird.
Then I looked at the DEMO of Huada Company, and found that the two functions Gpio_SetIO() and Gpio_ClrIO()
were used to replace the Gpio_WriteOutputIO in the program with these two functions, and the indicator light showed normal.

Although it solves the problem, it is still very strange. These 3 functions are ultimately the output registers of the operation pins, and the results should be the same. Then why am I different here?
When debugging with this problem, trace into the function library. When x = 0x80, the value of Gpio_WriteOutputIO is also 0x80 after entering the function. The function calls
SetBit(((uint32_t)&M0P_GPIO->PAOUT + enPort), enPin, bVal);
Then enter the SetBit function,
void SetBit(uint32_t addr, uint32_t offset, boolean_t bFlag)
{     if(TRUE== bFlag)     {         *((volatile uint32_t *)(addr)) |= ((1UL)<<(offset)) ;     }




    else
    {         *((volatile uint32_t *)(addr)) &= (~(1UL<<(offset)));     }    } Found that this function first judges the expression (TRUE == bFlag), where TRUE is defined as   #define TRUE ((boolean_t) 1u) This can be understood. The third parameter bFlag passed to the SetBit function is 0x80. This value is not equal to TRUE (value 1). Therefore, the else statement is executed, and the result is an indicator. The always-on C99 standard defines the boolean type, which has 2 values, true and false. If the expression evaluates to 0, the type value is false, and the expression evaluates to non-zero, the type value is true, and In the Huada library function, the judgment (TRUE==bFlag), the value of TRUE is 1, and the bFlag value is 0x80, which is different. This is somewhat different from the C99 standard. It is recommended that the judgment condition in the Huada library function (TRUE== bFlag) is changed to (FALSE==bFlag), namely void SetBit(uint32_t addr, uint32_t offset, boolean_t bFlag) {     if(FALSE == bFlag)     {         *((volatile uint32_t *)(addr)) &= (~(1UL <<(offset)));     }     else     {




















        *((volatile uint32_t *)(addr)) |= ((1UL)<<(offset));
    }   
}


This is unified with the C99 standard and will not cause confusion for users.

Guess you like

Origin blog.csdn.net/qq_15548761/article/details/107108093