nanoframework / Home

:house: The landing page for .NET nanoFramework repositories.
https://www.nanoframework.net
MIT License
861 stars 79 forks source link

SerialPort.Open() throws exception when using default settings on ESP32-S3 #1455

Closed patagonaa closed 6 months ago

patagonaa commented 6 months ago

Library/API/IoT binding

nanoFramework.System.IO.Ports

Visual Studio version

VS2022

.NET nanoFramework extension version

No response

Target name(s)

ESP32_S3

Firmware version

1.9.1.20

Device capabilities

No response

Description

When opening a Serial Port / UART on an ESP32-S3, an "ArgumentException" is thrown. As @alberk8 has already found out (after I had asked for help on Discord), the issue is the read buffer size.

The default ReadBufferSize is 256, which is then halved to 128 when creating the UART with IDF: https://github.com/nanoframework/nf-interpreter/blob/1701e7f92458882d85d921c6faf293150197ced7/targets/ESP32/_nanoCLR/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort.cpp#L905 In IDF, the rx buffer length must be above UART_HW_FIFO_LEN: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/uart.html#_CPPv419uart_driver_install11uart_port_tiiiP13QueueHandle_ti

UART_HW_FIFO_LEN seems to be 128 by default, so this default value causes an error while creating the UART.

Setting the SerialPort.ReadBufferSize above 256 (like 512) makes the UART work as it should.

How to reproduce

  1. Upload and debug sample code to ESP32-S3
  2. Debugger shows exception is being thrown

Expected behaviour

By default, SerialPort should work on any platform using the default configuration. If this is not possible, a more helpful error message should be thrown so it's possible to find the issue without diving into interpreter/idf code.

Screenshots

No response

Sample project or code

public static void Main()
{
    Configuration.SetPinFunction(Gpio.IO14, DeviceFunction.COM2_RX);
    Configuration.SetPinFunction(Gpio.IO15, DeviceFunction.COM2_TX);

    var sp = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
    sp.Open();

    Thread.Sleep(Timeout.Infinite);
}

Aditional information

No response