From 571f34e0e668a8bebe7d5e1933a87cec663af16f Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Fri, 28 Nov 2025 09:36:36 +0100 Subject: [PATCH] machine: only enable USB-CDC when needed Enabling USB consumes a lot of power. So it's better to keep it disabled by default when the serial port is set to something other than "usb" (USB-CDC). On my nice!nano clone, it reduces current consumption from 1mA to 0.13mA (and most of the rest is probably consumed by a connected SCD41 sensor). This change might break some expectations, when a board uses USB but not USB-CDC (I think this is rare, but I didn't check specifically). --- src/machine/machine_esp32c3.go | 4 ++++ src/machine/serial-usb.go | 1 + src/machine/usb.go | 11 +++++++++++ src/runtime/runtime_atsamd21.go | 4 +--- src/runtime/runtime_atsamd51.go | 4 +--- src/runtime/runtime_nrf52840.go | 4 +--- src/runtime/runtime_rp2.go | 4 +--- 7 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/machine/machine_esp32c3.go b/src/machine/machine_esp32c3.go index 727fcc1e6..eb2a18b5e 100644 --- a/src/machine/machine_esp32c3.go +++ b/src/machine/machine_esp32c3.go @@ -519,6 +519,10 @@ type Serialer interface { RTS() bool } +func initUSB() { + // nothing to do here +} + // USB Serial/JTAG Controller // See esp32-c3_technical_reference_manual_en.pdf // pg. 736 diff --git a/src/machine/serial-usb.go b/src/machine/serial-usb.go index 658708291..44dc63e0c 100644 --- a/src/machine/serial-usb.go +++ b/src/machine/serial-usb.go @@ -6,5 +6,6 @@ package machine var Serial Serialer func InitSerial() { + initUSB() Serial = USBCDC } diff --git a/src/machine/usb.go b/src/machine/usb.go index 434ee0f1b..968252103 100644 --- a/src/machine/usb.go +++ b/src/machine/usb.go @@ -19,6 +19,17 @@ var ( USBCDC Serialer ) +func initUSB() { + enableUSBCDC() + USBDev.Configure(UARTConfig{}) +} + +// Using go:linkname here because there's a circular dependency between the +// machine package and the machine/usb/cdc package. +// +//go:linkname enableUSBCDC machine/usb/cdc.EnableUSBCDC +func enableUSBCDC() + type Serialer interface { WriteByte(c byte) error Write(data []byte) (n int, err error) diff --git a/src/runtime/runtime_atsamd21.go b/src/runtime/runtime_atsamd21.go index 52bedf739..43e537708 100644 --- a/src/runtime/runtime_atsamd21.go +++ b/src/runtime/runtime_atsamd21.go @@ -6,7 +6,7 @@ import ( "device/arm" "device/sam" "machine" - "machine/usb/cdc" + _ "machine/usb/cdc" "runtime/interrupt" "runtime/volatile" "unsafe" @@ -26,8 +26,6 @@ func init() { initUSBClock() initADCClock() - cdc.EnableUSBCDC() - machine.USBDev.Configure(machine.UARTConfig{}) machine.InitSerial() } diff --git a/src/runtime/runtime_atsamd51.go b/src/runtime/runtime_atsamd51.go index f8d46275b..06fea8e11 100644 --- a/src/runtime/runtime_atsamd51.go +++ b/src/runtime/runtime_atsamd51.go @@ -6,7 +6,7 @@ import ( "device/arm" "device/sam" "machine" - "machine/usb/cdc" + _ "machine/usb/cdc" "runtime/interrupt" "runtime/volatile" ) @@ -27,8 +27,6 @@ func init() { initADCClock() enableCache() - cdc.EnableUSBCDC() - machine.USBDev.Configure(machine.UARTConfig{}) machine.InitSerial() } diff --git a/src/runtime/runtime_nrf52840.go b/src/runtime/runtime_nrf52840.go index 3c7deb031..6af1c11b2 100644 --- a/src/runtime/runtime_nrf52840.go +++ b/src/runtime/runtime_nrf52840.go @@ -6,7 +6,7 @@ import ( "device/arm" "device/nrf" "machine" - "machine/usb/cdc" + _ "machine/usb/cdc" "runtime/interrupt" "runtime/volatile" ) @@ -26,8 +26,6 @@ func main() { } func init() { - cdc.EnableUSBCDC() - machine.USBDev.Configure(machine.UARTConfig{}) machine.InitSerial() initLFCLK() initRTC() diff --git a/src/runtime/runtime_rp2.go b/src/runtime/runtime_rp2.go index 08ae86569..1cd23d6dc 100644 --- a/src/runtime/runtime_rp2.go +++ b/src/runtime/runtime_rp2.go @@ -7,7 +7,7 @@ import ( "device/rp" "internal/task" "machine" - "machine/usb/cdc" + _ "machine/usb/cdc" "runtime/interrupt" "runtime/volatile" "unsafe" @@ -360,8 +360,6 @@ func machineInit() func init() { machineInit() - cdc.EnableUSBCDC() - machine.USBDev.Configure(machine.UARTConfig{}) machine.InitSerial() }