Only logging in VSC Terminal after upload, not after restart

User avatar
Basalt
Posts: 28
Joined: Wed Aug 16, 2023 7:59 pm

Only logging in VSC Terminal after upload, not after restart

Postby Basalt » Wed Dec 13, 2023 12:31 pm

Using lolin_s2_mini board (ESP32-S2), connected with USB-C to my PC, running VSC/Platformio with Arduino framework. Using "Upload and Monitor" works fine, I see the upload and the VSC Terminal connects properly:

Code: Select all

Hard resetting via RTS pin...
--- Terminal on COM8 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
Next I can see all serial logging from my program in the Terminal. So far so good.

However, when I restart my device (or when it crashes), the serial connection gets lost. I can reconnect by typing Ctrl-T in the VSC Terminal window (says: Reconnecting to COM8, Connected!), but that's too late for seeing the restart or crash info from the device.

I tried things with monitor_rts and monitor_dtr without success.

How can I keep the VSC Terminal connected to my device permanently, or reconnect automatically?

User avatar
Basalt
Posts: 28
Joined: Wed Aug 16, 2023 7:59 pm

Re: Only logging in VSC Terminal after upload, not after restart

Postby Basalt » Thu Dec 14, 2023 8:43 am

Update:
I have this in startup():

Code: Select all

void setup() {
    Serial.begin(115200);
    delay(5000); // wait until Platformio monitor is active
If I would add something like "while (!Serial) yield();" the device would not startup, unless I use a Terminal to send a character to it first. Only then the USB serial connection gets opened.

I also tried with PuTTY as terminal. PuTTY fires the popup "Error reading from serial device" when I reset the ESP32-S2 device (or let it crash deliberately). I have to open a new PuTTY session to get connected again.

So it seems to me that the ESP32-S2 is explicitly closing the USB serial connection on restart (or crash) of the device. This causes the terminal (either VSC Terminal or PuTTY) to loose connection, and I have to do some manual action to get connected again.

How can can I keep the terminal connected, so I can see the whole restart process (incl crash info)?

User avatar
Basalt
Posts: 28
Joined: Wed Aug 16, 2023 7:59 pm

Re: Only logging in VSC Terminal after upload, not after restart

Postby Basalt » Fri Dec 15, 2023 12:08 pm

Anyone knows how to solve this?
Or am I the only one?


User avatar
Basalt
Posts: 28
Joined: Wed Aug 16, 2023 7:59 pm

Re: Only logging in VSC Terminal after upload, not after restart

Postby Basalt » Fri Dec 15, 2023 7:52 pm

So what I understand from the above link is that I should add build_flags in my platformio.ini
This is what I have now:

Code: Select all

[env:lolin_s2_mini]
platform = espressif32
board = lolin_s2_mini
build_flags = -DARDUINO_USB_CDC_ON_BOOT=1 -D ARDUINO_USB_MODE=0
framework = arduino
lib_deps = ...my libs...
board_build.filesystem = littlefs
monitor_speed = 115200
However, this does not solve my problem:
I'm still not getting any logging when a device restart, unless:
- In the Terminal of VSC, I send a character with the keyboard
OR
- I restart PuTTY, after it stopped with "Error reading from serial device"

I checked my lolin_s2_mini.json and there ARDUINO_USB_CDC_ON_BOOT is already defined. I assume that's why adding it to the build_flags ís actually not needed, and thus not helping

Code: Select all

{
  "build": {
    "arduino": {
      "ldscript": "esp32s2_out.ld"
    },
    "core": "esp32",
    "extra_flags": [
      "-DARDUINO_LOLIN_S2_MINI",
      "-DBOARD_HAS_PSRAM",
      "-DARDUINO_USB_CDC_ON_BOOT=1"
    ],
    "f_cpu": "240000000L",
    ...etc...
I experimented with ARDUINO_USB_MODE, defining it (=1) results in "error: 'Serial' was not declared in this scope".
Removing ARDUINO_USB_MODE from build_flags or putting it in build_unflags doesn't help either.

What else can I try?

User avatar
Basalt
Posts: 28
Joined: Wed Aug 16, 2023 7:59 pm

Re: Only logging in VSC Terminal after upload, not after restart

Postby Basalt » Fri Dec 22, 2023 10:27 pm

...continuing my monologue...

Today I tried logging on an ESP32-S3 board (Lilygo T7 S3), and here I don't have an issue. Straightforward platformio.ini and main.cpp. Here restarting is shown properly, when crashing intentionally I get backtrace info that can be decoded and leads to the faulty source line. Just what I would expect.

The electronic circuit of the USB connection of this S3 board is very similar to the S2 that I actually want to use.

:arrow: Why doesn't logging work properly on S2?
Anyone?

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Only logging in VSC Terminal after upload, not after restart

Postby lbernstone » Sat Dec 23, 2023 1:24 am

Make sure you are using the latest version (v2.0.14). You want to use CDC_ON_BOOT. Hold down the boot button, and program it once with a firmware set to that. You then need to hard restart once. After that, it should behave as you expect, and persist even through an OTA.
The last line of my troubleshooting blurb is critical. If the device is boot looping, it will behave poorly, exhibiting this behavior where it always makes a new tty device name. HWCDC is great for write-once programming on an assembly line, but it is not appropriate for low level development work. Hook up a serial console if you need to see the whole boot sequence.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Only logging in VSC Terminal after upload, not after restart

Postby lbernstone » Sat Dec 23, 2023 2:15 am

Note that a lot of the mini boards are just all around poorly made.
https://www.reddit.com/r/esp32/comments ... ect_these/

User avatar
Basalt
Posts: 28
Joined: Wed Aug 16, 2023 7:59 pm

Re: Only logging in VSC Terminal after upload, not after restart

Postby Basalt » Sat Dec 23, 2023 10:42 pm

I'm not sure what you meant with:
The last line of my troubleshooting blurb is critical
can you please explain?

I checked all the rest you mentioned, that ia all okay.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Only logging in VSC Terminal after upload, not after restart

Postby lbernstone » Sun Dec 24, 2023 4:26 am

USB has some negotiation at startup. That takes more than the 50ms it takes for the device to reboot loop, so it never properly inits.

Who is online

Users browsing this forum: No registered users and 35 guests