GPIO_OUTPUT_SET and expression as bit value

User avatar
kolban
Posts: 131
Joined: Tue Jun 16, 2015 1:09 pm
Location: Fort Worth, Texas, USA

GPIO_OUTPUT_SET and expression as bit value

Postby kolban » Wed Jul 15, 2015 10:04 pm

When using the function called GPIO_OUTPUT_SET(gpio_no, bit_value) we may be tempted to supply bit_value as an expressison. For example:

GPIO_OUTPUT_SET(14, count > 10)

we normally think we can do this as in C, the expression "count > 10" evaluates to a numeric where 0 = false and not 0 = true which matches the bit_value. However, take great care because GPIO_OUTPUT_SET is not a function but is instead a macro and if one looks at its internals ... you will find that supplying an evaluating expression simply won't work. What you have to do is wrap the expression in parenthesis ... for example:

GPIO_OUTPUT_SET(14, (count > 10))

If you don't, your code will compile cleanly but at runtime, you will get unexpected results.

I raised this as a bug with Espressif and supplied the workaround (adding parenthesis around the parameters inside the macro). I thought I'd chance my arm at a bounty :-) ... they responded that the bit_value has to be 0 or 1 and, at this time, can not be an expression ... fair enough ... but I couldn't find that documented ... so a heads-up chums ... be careful if you supply expressions in GPIO_OUTPUT_SET.

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: GPIO_OUTPUT_SET and expression as bit value

Postby ESP_Faye » Thu Jul 16, 2015 5:26 pm

Hi,

Thanks very much for all valuable advice you gave !

Sorry that I didn't reply your topics one by one, but we have already recorded your suggestions on BBS and we will optimize our SDK and IOT_Demo in next version.

Thanks again !

Who is online

Users browsing this forum: No registered users and 84 guests