elcritch / nesper

Program the ESP32 with Nim! Wrappers around ESP-IDF API's.
Apache License 2.0
202 stars 17 forks source link

ESP crashes when using UART `read()` #19

Open Psirus opened 1 year ago

Psirus commented 1 year ago

Disclaimer: I don't know what I'm doing, I have very little experience with electronics and embedded development. Maybe its just a daft beginner mistake.

I'm trying to use the UART module to read in MIDI data. This is the code:

import nesper
import nesper/gpios
import nesper/timers
import nesper/uarts

app_main():
  var uart_config = newUartConfig(baud_rate=31250)
  var uart = newUart(uart_config, UART_NUM_2, GPIO_PIN(14), GPIO_PIN(34), buffer=1024.SzBytes)
  while true:
    delayMillis(100)
    var buff = uart.read()
    if buff.len != 0:
      echo buff

And it works, for a little while. After a couple of seconds (it varies), the ESP either becomes unresponsive or crashes. A backtrace from one of the crashes is here. Thanks for your help.

Edit: I was able to work around this problem by copying the nim code from read() and moving the creation of the buffer outside of the loop. Maybe the temporary buffer isn't garbage collected, or read isn't supposed to be used in a loop like this, or something else I don't quite grasp.