mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-24 22:39:04 +00:00
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:
committed by
Ron Evans
parent
01723025d7
commit
571f34e0e6
@@ -519,6 +519,10 @@ type Serialer interface {
|
|||||||
RTS() bool
|
RTS() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initUSB() {
|
||||||
|
// nothing to do here
|
||||||
|
}
|
||||||
|
|
||||||
// USB Serial/JTAG Controller
|
// USB Serial/JTAG Controller
|
||||||
// See esp32-c3_technical_reference_manual_en.pdf
|
// See esp32-c3_technical_reference_manual_en.pdf
|
||||||
// pg. 736
|
// pg. 736
|
||||||
|
|||||||
@@ -6,5 +6,6 @@ package machine
|
|||||||
var Serial Serialer
|
var Serial Serialer
|
||||||
|
|
||||||
func InitSerial() {
|
func InitSerial() {
|
||||||
|
initUSB()
|
||||||
Serial = USBCDC
|
Serial = USBCDC
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,17 @@ var (
|
|||||||
USBCDC Serialer
|
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 {
|
type Serialer interface {
|
||||||
WriteByte(c byte) error
|
WriteByte(c byte) error
|
||||||
Write(data []byte) (n int, err error)
|
Write(data []byte) (n int, err error)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"device/arm"
|
"device/arm"
|
||||||
"device/sam"
|
"device/sam"
|
||||||
"machine"
|
"machine"
|
||||||
"machine/usb/cdc"
|
_ "machine/usb/cdc"
|
||||||
"runtime/interrupt"
|
"runtime/interrupt"
|
||||||
"runtime/volatile"
|
"runtime/volatile"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@@ -26,8 +26,6 @@ func init() {
|
|||||||
initUSBClock()
|
initUSBClock()
|
||||||
initADCClock()
|
initADCClock()
|
||||||
|
|
||||||
cdc.EnableUSBCDC()
|
|
||||||
machine.USBDev.Configure(machine.UARTConfig{})
|
|
||||||
machine.InitSerial()
|
machine.InitSerial()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"device/arm"
|
"device/arm"
|
||||||
"device/sam"
|
"device/sam"
|
||||||
"machine"
|
"machine"
|
||||||
"machine/usb/cdc"
|
_ "machine/usb/cdc"
|
||||||
"runtime/interrupt"
|
"runtime/interrupt"
|
||||||
"runtime/volatile"
|
"runtime/volatile"
|
||||||
)
|
)
|
||||||
@@ -27,8 +27,6 @@ func init() {
|
|||||||
initADCClock()
|
initADCClock()
|
||||||
enableCache()
|
enableCache()
|
||||||
|
|
||||||
cdc.EnableUSBCDC()
|
|
||||||
machine.USBDev.Configure(machine.UARTConfig{})
|
|
||||||
machine.InitSerial()
|
machine.InitSerial()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"device/arm"
|
"device/arm"
|
||||||
"device/nrf"
|
"device/nrf"
|
||||||
"machine"
|
"machine"
|
||||||
"machine/usb/cdc"
|
_ "machine/usb/cdc"
|
||||||
"runtime/interrupt"
|
"runtime/interrupt"
|
||||||
"runtime/volatile"
|
"runtime/volatile"
|
||||||
)
|
)
|
||||||
@@ -26,8 +26,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cdc.EnableUSBCDC()
|
|
||||||
machine.USBDev.Configure(machine.UARTConfig{})
|
|
||||||
machine.InitSerial()
|
machine.InitSerial()
|
||||||
initLFCLK()
|
initLFCLK()
|
||||||
initRTC()
|
initRTC()
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"device/rp"
|
"device/rp"
|
||||||
"internal/task"
|
"internal/task"
|
||||||
"machine"
|
"machine"
|
||||||
"machine/usb/cdc"
|
_ "machine/usb/cdc"
|
||||||
"runtime/interrupt"
|
"runtime/interrupt"
|
||||||
"runtime/volatile"
|
"runtime/volatile"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@@ -360,8 +360,6 @@ func machineInit()
|
|||||||
func init() {
|
func init() {
|
||||||
machineInit()
|
machineInit()
|
||||||
|
|
||||||
cdc.EnableUSBCDC()
|
|
||||||
machine.USBDev.Configure(machine.UARTConfig{})
|
|
||||||
machine.InitSerial()
|
machine.InitSerial()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user