Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shared/tinyusb: Buffer startup stdout (banner) to send once usb cdc is connected. #14485

Merged

Conversation

andrewleech
Copy link
Sponsor Contributor

@andrewleech andrewleech commented May 15, 2024

At startup, buffer stdout / microypthon banner so that it can be printed on initial connection of mpremote, just like stm port does.

Requires: hathach/tinyusb#2629 (merged) and hathach/tinyusb#2647 (merged)

Extra details: hathach/tinyusb#2595

Sitting on top of #14462

functional change

This change is most obvious when you've first plugged in a micropython device (or hit reset) when it's a board that uses USB (CDC) serial in the chip itself for the repl interface. This doesn't apply to UART going via a separate usb-serial chip.

On all ports other than stm32 currently, nothing happens when you first run a terminal like mpremote on a freshly booted board.
A user might typically first hit enter to look for a repl prompt >>> to ensure they are connected to a micropython board, then maybe hit ctrl-d to soft reboot and see the banner to check they're connected to the right board.
Code_dZZf1qJ3Un

On stm however, on a freshly started board when you connect mpremote the banner is immediately shown!.
Code_Wsv7praNrC

This PR extends this behaviour to rp2, mimxrt, samd and renesas. It's also extended to esp32s2 / s3 in the follow up PR.

Copy link

github-actions bot commented May 15, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:  +168 +0.046% TEENSY40
        rp2:  +204 +0.023% RPI_PICO_W
       samd:  +156 +0.059% ADAFRUIT_ITSYBITSY_M4_EXPRESS[incl +4(bss)]

@andrewleech andrewleech force-pushed the tinyusb_startup_banner_buffer branch from 7fddddc to 4d29eec Compare May 15, 2024 04:40
Copy link

codecov bot commented May 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.39%. Comparing base (e138baf) to head (8809ae7).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #14485   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files         161      161           
  Lines       21204    21204           
=======================================
  Hits        20864    20864           
  Misses        340      340           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@andrewleech andrewleech changed the title DRAFT: shared/tinyusb: Buffer startup stdout (banner) to send once usb cdc is connected. shared/tinyusb: Buffer startup stdout (banner) to send once usb cdc is connected. May 15, 2024
@andrewleech
Copy link
Sponsor Contributor Author

andrewleech commented May 15, 2024

Initial tests working on rp2!

anl@STEP: ~ $ mpremote a1
Connected to MicroPython at /dev/ttyACM1
Use Ctrl-] or Ctrl-x to exit this shell
MicroPython v1.23.0-preview.377.g754ae8b76d.dirty on 2024-05-15; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>>

@andrewleech
Copy link
Sponsor Contributor Author

anl@STEP: ~ $ mpremote
Connected to MicroPython at /dev/ttyACM0
Use Ctrl-] or Ctrl-x to exit this shell
MicroPython v1.23.0-preview.380.g7ab4754386.dirty on 2024-05-16; Seeed Xiao with SAMD21G18A
Type "help()" for more information.
>>> 

@andrewleech
Copy link
Sponsor Contributor Author

anl@STEP: ~ $ mpremote
Connected to MicroPython at /dev/ttyACM0
Use Ctrl-] or Ctrl-x to exit this shell
MicroPython v1.23.0-preview.378.g9958203b3d.dirty on 2024-05-15; i.MX RT1010 EVK with MIMXRT1011DAE5A
Type "help()" for more information.
>>> 

@andrewleech andrewleech force-pushed the tinyusb_startup_banner_buffer branch from 3b8e785 to 526af51 Compare May 16, 2024 06:13
@andrewleech
Copy link
Sponsor Contributor Author

anl@STEP: ~ $ mpremote
Connected to MicroPython at /dev/ttyACM0
Use Ctrl-] or Ctrl-x to exit this shell
MicroPython v1.23.0-preview.381.gd3a95507b6.dirty on 2024-05-16; PCA10059 with NRF52840
Type "help()" for more information.
>>>

@andrewleech
Copy link
Sponsor Contributor Author

@mattytrentini

anl@STEP: ~ $ mpremote
Connected to MicroPython at /dev/ttyACM0
Use Ctrl-] or Ctrl-x to exit this shell
MicroPython v1.23.0-preview.378.g526af512b4.dirty on 2024-05-16; ItsyBitsy M4 Express with SAMD51G19A
Type "help()" for more information.
>>>

Copy link
Contributor

@projectgus projectgus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be a really nice addition, thanks @andrewleech 😁

shared/tinyusb/mp_usbd_cdc.c Outdated Show resolved Hide resolved
shared/tinyusb/mp_usbd_cdc.c Show resolved Hide resolved
@andrewleech
Copy link
Sponsor Contributor Author

Note this PR currently has the TinyUSB submodule pinned to my PR with required SOF updates, so it won't currently build easily.

This upstream PR will need to be merged first, it's blocked by the need to test on renesas.

@projectgus
Copy link
Contributor

Note this PR currently has the TinyUSB submodule pinned to my PR with required SOF updates, so it won't currently build easily.

FWIW I think we could emulate something similar by adding a callback in the mp_usbd_task, or maybe even soft timer. However I think better to stick with the approach you've already used, if it's going to be available soon.

@andrewleech
Copy link
Sponsor Contributor Author

Yes I'd originally thought of using a soft timer however it does have some overheads, and I'm not sure if all needed ports have it enabled.

I think the upstream change should be achievable, I've had very productive discussions on it already.

I'm just blocked by renesas port testing, I can't figure out the auto generated code being used in the board profiles there enough to enable USB on the evk board I've loaned.

@andrewleech
Copy link
Sponsor Contributor Author

I've now got a renesas board working finally and have finished the implementation there, it's all working too.

I just need to clean up some of the commented code in the nrf stuff and it's all good to go!

@andrewleech
Copy link
Sponsor Contributor Author

This PR has tested working on rp2, mimxrt, samd and renesas.

esp32 support is in follow-up PR #15108.
nrf support is in follow-up PR #15158.

The required updates to TinyUSB have been merged upstream and the submodule here is now pinned to the matching master commit.

As such I consider this PR complete and ready for review.

@andrewleech andrewleech force-pushed the tinyusb_startup_banner_buffer branch from 9b8d07d to b3216eb Compare June 4, 2024 00:59
@dpgeorge dpgeorge added this to the release-1.24.0 milestone Jun 4, 2024
@andrewleech andrewleech force-pushed the tinyusb_startup_banner_buffer branch 2 times, most recently from a38ae6e to 94381cf Compare June 4, 2024 02:33
@andrewleech
Copy link
Sponsor Contributor Author

Branch has been rebased and re-tested on:

  • renesas EK-RA4W1 (build modded to enable usb port)
  • samd ADAFRUIT_ITSYBITSY_M4_EXPRESS
  • rp2 RPI_PICO_W
  • mimxrt MIMXRT1010_EVK

Copy link
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see tinyusb has two extra commits compared to this PR's updated submodule. Is it worth pulling in those two extra commits? They seem to fix an esp32 include.

shared/tinyusb/mp_usbd_cdc.c Outdated Show resolved Hide resolved
shared/tinyusb/mp_usbd_cdc.c Outdated Show resolved Hide resolved
@andrewleech
Copy link
Sponsor Contributor Author

They seem to fix an esp32 include.

That's in espressif's custom driver, which is not the one used in #15108
There's no harm in bringing it in as well though.

@andrewleech andrewleech force-pushed the tinyusb_startup_banner_buffer branch from 94381cf to 4d9c33d Compare June 4, 2024 05:36
Signed-off-by: Andrew Leech <andrew@alelec.net>
At startup, buffer initial stdout / MicroyPthon banner so that it can be
sent to the host on initial connection of the USB serial port.  This
buffering also works for when the CDC becomes disconnected and the device
is still printing to stdout, and when CDC is reconnected the most recent
part of stdout (depending on how big the internal USB FIFO is) is flushed
to the host.

This change is most obvious when you've first plugged in a MicroPython
device (or hit reset), when it's a board that uses USB (CDC) serial in the
chip itself for the REPL interface.  This doesn't apply to UART going via a
separate USB-serial chip.

The stm32 port already has this buffering behaviour (it doesn't use
TinyUSB) and this commit extends such behaviour to rp2, mimxrt, samd and
renesas-ra ports, which do use TinyUSB.

Signed-off-by: Andrew Leech <andrew@alelec.net>
@dpgeorge dpgeorge force-pushed the tinyusb_startup_banner_buffer branch from 4d9c33d to 8809ae7 Compare June 5, 2024 02:57
@dpgeorge dpgeorge merged commit 8809ae7 into micropython:master Jun 5, 2024
62 checks passed
@dpgeorge
Copy link
Member

dpgeorge commented Jun 5, 2024

Now merged! This is a very nice quality-of-life improvement, thanks for the effort getting it all worked out!

(I made some minor changes to this PR before merge, to get the ARDUINO_NANO_33_BLE_SENSE board building, same issue as last time.)

@andrewleech
Copy link
Sponsor Contributor Author

Ah yes, that's a good point thanks! The ARDUINO_NANO_33_BLE_SENSE has been fixed "properly" in #15158

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants