ricardgb opened a new issue, #22467:
URL: https://github.com/apache/nuttx/issues/22467

   **Description**
   
   Two defects of `arch/xtensa/src/esp32s3/esp32s3_usbserial.c` 
(`CONFIG_ESP32S3_USBSERIAL`, the
   USB-Serial/JTAG port of the chip, used as the console by several boards).
   
   **1. A write blocks for ever when the host does not read the port**
   
   The data written to the port leave the chip only when the host takes them. 
When no program has
   the port open on the host, the hardware buffer stays full, the "IN
   empty" interrupt never comes, and `write()` blocks without limit once the 64 
bytes of the
   transmit buffer are full. With the console on this port, everything that 
prints stops: a board
   that is only powered does not start its applications.
   
   **2. The transmission is not started**
   
   The driver sends only from the "IN empty" interrupt, which is raised when 
the host takes a
   packet. `esp32s3_txint()` enables the interrupt and relies on the flag of an 
earlier packet
   still being pending. As far as I can tell from the code, when that flag was 
already cleared and
   no packet is on its way to the host, no interrupt comes, nothing is sent, 
and the writers block as in 1, also with a host that
   reads. Seen after a reset with the port open on the host: no output of the 
application at all.
   
   **Reproduce (esp32s3-devkit:nsh, master `a63275cdce8`)**
   
   Replace `apps/examples/hello/hello_main.c` with 
`nuttx-esp32s3-usbserial-stall-repro.c` below
   (the init task: prints 50 lines at start-up, measures how long that took, 
reports it every
   2 seconds), then:
   
   ```sh
   ./tools/configure.sh esp32s3-devkit:nsh
   kconfig-tweak --disable UART0_SERIAL_CONSOLE --disable ESP32S3_UART0 \
                 --enable ESP32S3_USBSERIAL \
                 --disable SYSTEM_NSH --disable NSH_LIBRARY --enable 
EXAMPLES_HELLO \
                 --set-str INIT_ENTRYPOINT hello_main --set-str INIT_ENTRYNAME 
hello
   make olddefconfig && make
   ```
   
   On an ESP32-S3 (rev v0.2) connected to a Linux host through the 
USB-Serial/JTAG port
   (`/dev/ttyACM0`):
   
   *Defect 1:* flash, leave the port closed for 30 s, then open it without a 
reset (DTR and RTS
   both left asserted):
   
   ```
   the 50 lines took 30190 ms; now at 30 s after boot
   the 50 lines took 30190 ms; now at 32 s after boot
   ```
   
   The 50 lines took as long as the port stayed closed.
   
   *Defect 2:* open the port with a reset (DTR, then RTS released) and read for 
7 s: the messages
   of the ROM and of the boot arrive, and then nothing from the application, in 
6 of 6 runs.
   
   **Seen first on a board**
   
   ESP32-S3 board with the console on this port, WiFi and an HTTP server 
started at boot: after a
   power-up with nothing reading the port the board did not join the network.
   
   **Proposed fix**
   
   PR to follow, two changes: send from `esp32s3_txint()` when the hardware 
buffer can take data,
   and a transmit timeout (`CONFIG_ESP32S3_USBSERIAL_TXTIMEOUT`) after which 
the output is
   discarded until the host reads again. The second is a change of behaviour, 
see the PR.
   
   **Not checked**
   
   `arch/risc-v/src/common/espressif/esp_usbserial.c` has the same transmit 
code; not tested on
   RISC-V.
   
   <details>
   <summary>nuttx-esp32s3-usbserial-stall-repro.c (replaces 
apps/examples/hello/hello_main.c)</summary>
   
   ```c
   /****************************************************************************
    * Reproducer: a write to the USB-Serial/JTAG console of the ESP32-S3 blocks
    * until a host reads the port.  Replaces apps/examples/hello/hello_main.c
    * and runs as the init task.
    *
    * Prints 4000 bytes at start-up, measures how long that took, and then
    * reports it every 2 seconds.
    
****************************************************************************/
   
   #include <nuttx/config.h>
   
   #include <stdio.h>
   #include <time.h>
   #include <unistd.h>
   
   int main(int argc, FAR char *argv[])
   {
     struct timespec t0;
     struct timespec t1;
     long ms;
     int i;
   
     clock_gettime(CLOCK_MONOTONIC, &t0);
   
     for (i = 0; i < 50; i++)
       {
         printf("line %02d ........................................"
                "..............................\n", i);
       }
   
     fflush(stdout);
     clock_gettime(CLOCK_MONOTONIC, &t1);
     ms = (t1.tv_sec - t0.tv_sec) * 1000 + (t1.tv_nsec - t0.tv_nsec) / 1000000;
   
     for (; ; )
       {
         clock_gettime(CLOCK_MONOTONIC, &t1);
         printf("the 50 lines took %ld ms; now at %ld s after boot\n",
                ms, (long)t1.tv_sec);
         fflush(stdout);
         sleep(2);
       }
   
     return 0;
   }
   ```
   
   </details>
   
   *Disclosure: this report and the patch were prepared with the help of an AI 
agent (Claude Code,
   Anthropic); the failures and the fixes were reproduced on real hardware and 
human-reviewed.*
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to