Compilation error? (value<32 ) compiled as (value<31)
Posted: Wed Sep 13, 2017 9:38 am
I'm not sure about what I found here but this is quite worrying.
I actually work with the "blinky" example of the esp32_IDF and debug it.
Here is small extract of the code to make easier to understand the problem:
If you searcg for the API function "gpio_set_level" you have
where the macro GPIO_CHECK could be expanded to
the boolean expression is rewritten here for more clarity:
where a2 contains the value gpio_num
here we have three conditional jumps corresponding to the three Boolean expressions.
bltu = Branch if Less Than Unsigned -> (39<gpio_num)
beqz = Branch if Equal to Zero -> (a8==0)
bgeu = Branch if Greater Than or Equal Unsigned -> (33>=gpio_num)
we kann see that the constants 40 (GPIO_PIN_COUNT) and 34 have been replaced by 39 and 33 respectively.
and we have the following
(gpio_num < 34) is similar to (33 >= gpio_num)
(gpio_num < 40) is not similar to !(39 < gpio_num) should be !(39 <= gpio_num)
Actually if I replace in blinky the code:
gpio_set_level(BLINK_GPIO, 0);
with
gpio_set_level(39, 0);
I run into the error.
(console com4 >> E (24044) gpio: gpio_set_level(189): GPIO output gpio_num error)
This seems to me very strange and if confirmed has potential to produce many bugged code.
I actually work with the "blinky" example of the esp32_IDF and debug it.
Here is small extract of the code to make easier to understand the problem:
Code: Select all
void blink_task(void *pvParameter)
{
......
while(1) {
gpio_set_level(BLINK_GPIO, 0);
..........
}
}
Code: Select all
esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level)
{
GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
.........
}
Code: Select all
if (! (((gpio_num < GPIO_PIN_COUNT && GPIO_PIN_MUX_REG[gpio_num] != 0)) && (gpio_num < 34)))
{
Errror
return
}
The assembler code isif (! ( (gpio_num<40) && ( GPIO_PIN_MUX_REG[gpio_num] != 0) && (gpio_num < 34))
Code: Select all
400f49a3: movi.n a8, 39
400f49a5: bltu a8, a2, 0x400f49b8 <gpio_set_level+24>
400f49a8: l32r a8, 0x400d0504 <_stext+1260>
400f49ab: addx4 a8, a2, a8
400f49ae: l32i.n a8, a8, 0
400f49b0: beqz a8, 0x400f49b8 <gpio_set_level+24>
400f49b3: movi.n a8, 33
400f49b5: bgeu a8, a2, 0x400f49de <gpio_set_level+62>
here we have three conditional jumps corresponding to the three Boolean expressions.
bltu = Branch if Less Than Unsigned -> (39<gpio_num)
beqz = Branch if Equal to Zero -> (a8==0)
bgeu = Branch if Greater Than or Equal Unsigned -> (33>=gpio_num)
we kann see that the constants 40 (GPIO_PIN_COUNT) and 34 have been replaced by 39 and 33 respectively.
and we have the following
(gpio_num < 34) is similar to (33 >= gpio_num)
(gpio_num < 40) is not similar to !(39 < gpio_num) should be !(39 <= gpio_num)
Actually if I replace in blinky the code:
gpio_set_level(BLINK_GPIO, 0);
with
gpio_set_level(39, 0);
I run into the error.
(console com4 >> E (24044) gpio: gpio_set_level(189): GPIO output gpio_num error)
This seems to me very strange and if confirmed has potential to produce many bugged code.