Skip to content

Questiona about GPIO Output (LED) example #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wz2b opened this issue Jul 15, 2022 · 3 comments
Closed

Questiona about GPIO Output (LED) example #78

wz2b opened this issue Jul 15, 2022 · 3 comments

Comments

@wz2b
Copy link

wz2b commented Jul 15, 2022

WRITE_RTC_REG(RTC_GPIO_ENABLE_W1TS_REG, RTC_GPIO_ENABLE_W1TS_S + gpio, 1, 1)

Do I understand in this bit of code that you're turning the led on and off by enabling or disabling the GPIO output? I'm curious why you would have done that, rather than always initializing the pin as an output, then writing to RTCIO_RTC_GPIO_OUT_REG, RTCIO_RTC_GPIO_OUT_W1TS_REG, or RTCIO_RTC_GPIO_OUT_W1TC_REG?

@wnienhaus
Copy link
Collaborator

You are very correct. That code is indeed incorrect. Some bad copy-pasting that happened to work(ish).

The correct way would be to set the port to output enabled once with this instruction:

WRITE_RTC_REG(RTC_GPIO_ENABLE_W1TS_REG, RTC_GPIO_ENABLE_W1TS_S + gpio, 1, 1)

and then to toggle the LED on and off with these instructions:

//on
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + gpio, 1, 1)

//off
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + gpio, 1, 0)

Alternatively one can use the RTC_GPIO_OUT_W1TS_REG and RTC_GPIO_OUT_W1TC_REG registers, but I prefer the RTC_GPIO_OUT_REG approach. I am not sure why there are two ways to do this.

I just tested this with a power profiler (nordic PPK2), which supports reading in logic signals alongside the power profile. When I connect my GPIO output to the logic port with the current (wrong) blink.py code, I can see the initial low to high transition, but the logic port never "sees" a low again (I guess with "output enable" turned off the pull-down is also disabled and the pin is left floating).

When I instead fix the code as per above, I correctly see the highs and lows of the GPIO output from via the logic port.

Thanks for picking this up. I will create a PR for this soon.

@wz2b
Copy link
Author

wz2b commented Jul 21, 2022

Great, thanks explaining - I thought that was what was going on, but I'm using these examples to learn how to make a virtual SPI port in the ULP and I'm not quite used to the GPIO/RTC muxes in this thing yet.

wnienhaus added a commit to wnienhaus/micropython-esp32-ulp that referenced this issue Jul 28, 2022
The blink example was incorrect (as pointed out in issue micropython#78).

The example was toggling the GPIOs "output enable" state,
instead of keeping "output enable" on and then toggling the
output value of the pin.

It "accidentally" worked, because we initally set the output
value to 1 (high) and thus, whenever "output enable" was set,
the LED lit up, and when "output enable" was turned off, the
pin was left floating without any output voltage and the LED
therefore turned off.

The new approach correctly enables "output enable" at the
start, and then only toggles the output value of the pin on
and off.

This change was also tested with a logic analyser, showing
that before this change, the logic analyser could not correcly
detect a LOW state after a HIGH state, because the pin was
floating without a pull-down resistor in place, whenever it
was not in "output enable" mode.

After this change, the logic analyser correctly detected all
LOW and HIGH states, because the pin remained in "output
enable" mode (and the ESP32 has a pull-down configured by
default) and only the output value was toggled.
@wnienhaus
Copy link
Collaborator

@wz2b I have now merged the fix. Thanks again for reporting it.

@wz2b wz2b closed this as completed Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants