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).
This commit is contained in:
Ayke van Laethem
2025-11-28 09:36:36 +01:00
committed by Ron Evans
parent 01723025d7
commit 571f34e0e6
7 changed files with 20 additions and 12 deletions
+1 -3
View File
@@ -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()
}
+1 -3
View File
@@ -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()
}
+1 -3
View File
@@ -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()
+1 -3
View File
@@ -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()
}