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>
This fixes the build for some esp32 and nrf boards (for example
`ARDUINO_NANO_33_BLE_SENSE` and `ARDUINO_NANO_ESP32`) due to commit
c98789a6d8. Changes are:
- Allow the CDC TX/RX functions in `mp_usbd_cdc.c` to be enabled
separately to those needed for `MICROPY_HW_USB_CDC_1200BPS_TOUCH`.
- Add `MICROPY_EXCLUDE_SHARED_TINYUSB_USBD_CDC` option as a temporary
workaround for the nrf port to use.
- Declare `mp_usbd_line_state_cb()` in a header as a public function.
- Fix warning with type cast of `.callback_line_state_changed`.
Signed-off-by: Damien George <damien@micropython.org>
There are a few TinyUSB CDC functions used for stdio that are currently
replicated across a number of ports. Not surprisingly in a couple of cases
these have started to diverge slightly, with additional features added to
one of them.
This commit consolidates a couple of key shared functions used directly by
TinyUSB based ports, and makes those functions available to all.
Signed-off-by: Andrew Leech <andrew@alelec.net>
Only when dynamic USB devices are enabled.
The issue here is that when the USB reset triggers, the dynamic USB device
reset callback is called from inside the TinyUSB task.
If that callback tries to print something then it'll call through to
tud_cdc_write_flush(), but TinyUSB hasn't finished updating state yet to
know it's no longer configured. Subsequently it may try to queue a transfer
and then the low-level DCD layer panics.
By explicitly stalling the endpoint first, usbd_edpt_claim() will fail and
tud_cdc_write_flush() returns immediately.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
In the case where an OUT control transfer triggers with wLength==0 (i.e.
all data sent in the SETUP phase, and no additional data phase) the
callbacks were previously implemented to return b"" (i.e. an empty buffer
for the data phase).
However this didn't actually work as intended because b"" can't provide a
RW buffer (needed for OUT transfers with a data phase to write data into),
so actually the endpoint would stall.
The symptom was often that the device process the request (if processing
it in the SETUP phase when all information was already available), but the
host sees the endpoint stall and eventually returns an error.
This commit changes the behaviour so returning True from the SETUP phase of
a control transfer queues a zero length status response.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
When defining custom USB devices, longer strings may be needed. Eventually
the memory for string descriptors can be allocated on demand, but for now
this bigger value should be reasonable.
Signed-off-by: Damien George <damien@micropython.org>
Updates a few code comments that were out of date or poorly worded. No code
changes.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Previously, constructing the singleton USBDevice object was enough to
trigger a USB disconnect on soft reset. Now it also has to be active.
The only case where this changes the behaviour is if the USBDevice object
has been constructed but never set to active (no more disconnect in this
case). Otherwise, behaviour is the same.
This change was requested by hippy on the raspberrypi forums.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
This new machine-module driver provides a "USBDevice" singleton object and
a shim TinyUSB "runtime" driver that delegates the descriptors and all of
the TinyUSB callbacks to Python functions. This allows writing arbitrary
USB devices in pure Python. It's also possible to have a base built-in
USB device implemented in C (eg CDC, or CDC+MSC) and a Python USB device
added on top of that.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
The STATIC macro was introduced a very long time ago in commit
d5df6cd44a. The original reason for this was
to have the option to define it to nothing so that all static functions
become global functions and therefore visible to certain debug tools, so
one could do function size comparison and other things.
This STATIC feature is rarely (if ever) used. And with the use of LTO and
heavy inline optimisation, analysing the size of individual functions when
they are not static is not a good representation of the size of code when
fully optimised.
So the macro does not have much use and it's simpler to just remove it.
Then you know exactly what it's doing. For example, newcomers don't have
to learn what the STATIC macro is and why it exists. Reading the code is
also less "loud" with a lowercase static.
One other minor point in favour of removing it, is that it stops bugs with
`STATIC inline`, which should always be `static inline`.
Methodology for this commit was:
1) git ls-files | egrep '\.[ch]$' | \
xargs sed -Ei "s/(^| )STATIC($| )/\1static\2/"
2) Do some manual cleanup in the diff by searching for the word STATIC in
comments and changing those back.
3) "git-grep STATIC docs/", manually fixed those cases.
4) "rg -t python STATIC", manually fixed codegen lines that used STATIC.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
For mimxrt, nrf, renesas-ra, rp2 and samd ports, this commit implements
similar behaviour to the stm32 port, where USB is only brought up after
boot.py completes execution.
Currently this doesn't add any useful functionality (and may break
workflows that depend on USB-CDC being live in boot.py), however it's a
precondition for more usable workflows with USB devices defined in
Python (allows setting up USB interfaces in boot.py before the device
enumerates for the first time).
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Change the rp2 and renesas-ra ports to use the helper function.
Saves copy-pasta, at the small cost of one more function call in the
firmware (if not using LTO).
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
dcd_event_handler() is called from the IRQ when a new DCD event is queued
for processing by the TinyUSB thread mode task. This lets us queue the
handler to run immediately when MicroPython resumes.
Currently this relies on a linker --wrap hack to work, but a PR has been
submitted to TinyUSB to allow the function to be called inline from
dcd_event_handler() itself.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
This was previously hard-coded to "Micropy" / "Mass Storage" / "1.0".
Now allow it to be overridden by a board.
Also change "Micropy" to "MicroPy" and "1.0" to "1.00" to match stm32.
This work was funded through GitHub Sponsors.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
On targets that provide a reference TinyUSB implementation, like ESP32,
the SDK already defines and implements standard callback functions such
as tud_cdc_line_state_cb(). This causes a symbol clash when enabling
shared implementations like the MicroPython 1200 touch functionality.
To avoid this symbol clash, add an optional macro to allow ports to
use a different function name in the shared implementation.
Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
This reverts commit 0613d3e356.
The value of CFG_TUD_CDC_EP_BUFSIZE should match the endpoint size,
otherwise data may stay in the lower layers of the USB stack (and not
passed up to the higher layers) until a total of CFG_TUD_CDC_EP_BUFSIZE
bytes have been received, which may not happen for a long time.
Fixes issue #11253.
Signed-off-by: Damien George <damien@micropython.org>
Previously, setting MICROPY_HW_ENABLE_USBDEV to 0 caused build errors. The
change affects the nrf and samd ports as well, so MICROPY_HW_ENABLE_USBDEV
had to be explicitly enabled there.
The configuration options MICROPY_HW_ENABLE_USBDEV and
MICROPY_HW_ENABLE_UART_REPL are independent, and can be enabled or disabled
by a board.
Signed-off-by: Damien George <damien@micropython.org>
App the mp_ prefix to usbd_ symbols and files which are defined here and
not in TinyUSB.
rp2 only for now. This includes some groundwork for dynamic USB devices
(defined in Python).
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>