nodemcu / nodemcu-firmware

Lua based interactive firmware for ESP8266, ESP8285 and ESP32

Home Page:https://nodemcu.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ESP32: No way to flush UART buffers before node.sleep()

tomsci opened this issue · comments

Missing feature

There's no way to ensure that data written to a UART or printed to the console is actually transmitted before a call to node.sleep(). I've tested a fix for this which exposes the SDK function uart_tx_flush(<port_num>), which seems to do the trick when called immediately before the sleep.

print("I want this to appear now not in five seconds' time")
uart.txflush(0) -- NEW. Required otherwise you won't see this appear until after the wakeup
node.sleep({secs=5})

Without the txflush call, you'd see something like this appear on the other end of the serial port:

I want this to appe

And the rest of the output wouldn't appear until after the sleep completed (in this example, 5 seconds later).

Justification

As mentioned in the docs for node.sleep() UART buffers are not automatically flushed before the FIFOs are suspended (this mirrors the behaviour of the underlying SDK API esp_light_sleep_start()). However there's no way in the Lua API to manually flush the UART buffers if you do need to guarantee their data is transmitted before the device goes to sleep.

Workarounds

I tried padding my print statements to see if that would help but I couldn't find a good way to guarantee output short of coding up an inefficient (and slow) spin loop.

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I guess this was resolved with the merging of #3390 so I'm closing it.