mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 11:37:46 +00:00
all: add a flag to the command line to select the serial implementation
This can be very useful for some purposes:
* It makes it possible to disable the UART in cases where it is not
needed or needs to be disabled to conserve power.
* It makes it possible to disable the serial output to reduce code
size, which may be important for some chips. Sometimes, a few kB can
be saved this way.
* It makes it possible to override the default, for example you might
want to use an actual UART to debug the USB-CDC implementation.
It also lowers the dependency on having machine.Serial defined, which is
often not defined when targeting a chip. Eventually, we might want to
make it possible to write `-target=nrf52` or `-target=atmega328p` for
example to target the chip itself with no board specific assumptions.
The defaults don't change. I checked this by running `make smoketest`
before and after and comparing the results.
This commit is contained in:
committed by
Ron Evans
parent
75298bb84b
commit
96e863f0f3
@@ -47,8 +47,6 @@ const (
|
||||
LED = D6
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// USBCDC pins
|
||||
const (
|
||||
USBCDC_DM_PIN Pin = PA24
|
||||
|
||||
@@ -35,8 +35,6 @@ const (
|
||||
LED3 Pin = PB03 // RX LED
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// ADC pins
|
||||
const (
|
||||
AREF Pin = PA03
|
||||
|
||||
@@ -74,5 +74,3 @@ const (
|
||||
PB30 Pin = 62
|
||||
PB31 Pin = 63
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
@@ -15,8 +15,6 @@ const (
|
||||
BUTTON = PB31
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
const (
|
||||
// https://ww1.microchip.com/downloads/en/DeviceDoc/70005321A.pdf
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ const (
|
||||
BUTTON = PA0
|
||||
)
|
||||
|
||||
var Serial = UART1
|
||||
var DefaultUART = UART1
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
|
||||
@@ -57,8 +57,6 @@ const (
|
||||
UART_RX_PIN = P0_30 // PORTB
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// I2C pins
|
||||
const (
|
||||
SDA_PIN = P0_05 // I2C0 external
|
||||
|
||||
@@ -104,11 +104,6 @@ const (
|
||||
UART_TX_PIN = D1
|
||||
)
|
||||
|
||||
// Serial is the USB device
|
||||
var (
|
||||
Serial = USB
|
||||
)
|
||||
|
||||
// I2C pins
|
||||
const (
|
||||
SDA_PIN = D20 // I2C0 external
|
||||
|
||||
@@ -68,8 +68,6 @@ const (
|
||||
ADC3 Pin = IO39
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
|
||||
// UART0 pins
|
||||
const (
|
||||
UART_TX_PIN = IO1
|
||||
|
||||
@@ -47,8 +47,6 @@ const (
|
||||
WS2812 = D8
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// USBCDC pins
|
||||
const (
|
||||
USBCDC_DM_PIN = PA24
|
||||
|
||||
@@ -40,8 +40,6 @@ const (
|
||||
WS2812 = D8
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// USBCDC pins
|
||||
const (
|
||||
USBCDC_DM_PIN = PA24
|
||||
|
||||
@@ -76,11 +76,6 @@ const (
|
||||
UART_TX_PIN = D1
|
||||
)
|
||||
|
||||
// Serial is the USB device
|
||||
var (
|
||||
Serial = USB
|
||||
)
|
||||
|
||||
// I2C pins
|
||||
const (
|
||||
SDA_PIN = D22 // I2C0 external
|
||||
|
||||
@@ -141,7 +141,7 @@ var (
|
||||
TxAltFuncSelector: AF7_USART1_2_3,
|
||||
RxAltFuncSelector: AF7_USART1_2_3,
|
||||
}
|
||||
Serial = UART1
|
||||
DefaultUART = UART1
|
||||
)
|
||||
|
||||
func initUART() {
|
||||
|
||||
@@ -142,8 +142,6 @@ const (
|
||||
WS2812 = NEOPIXEL_PIN
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
UART1_RX_PIN = D0 // (PB25)
|
||||
|
||||
@@ -35,7 +35,7 @@ const (
|
||||
LED_BLUE = P21
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
const (
|
||||
// TODO: figure out the pin numbers for these.
|
||||
|
||||
@@ -37,8 +37,6 @@ const (
|
||||
LED = D13
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// USBCDC pins
|
||||
const (
|
||||
USBCDC_DM_PIN = PA24
|
||||
|
||||
@@ -70,11 +70,6 @@ const (
|
||||
UART_TX_PIN = D1
|
||||
)
|
||||
|
||||
// Serial is the USB device
|
||||
var (
|
||||
Serial = USB
|
||||
)
|
||||
|
||||
// I2C pins
|
||||
const (
|
||||
SDA_PIN = D21 // I2C0 external
|
||||
|
||||
@@ -54,7 +54,7 @@ const (
|
||||
I2C0_SDA_PIN = PA10
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
var (
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ const (
|
||||
LED_BLUE = D14
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
// Default pins for UARTHS.
|
||||
const (
|
||||
|
||||
@@ -41,8 +41,6 @@ const (
|
||||
WS2812 = D40
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// USBCDC pins
|
||||
const (
|
||||
USBCDC_DM_PIN = PA24
|
||||
|
||||
@@ -12,7 +12,7 @@ const (
|
||||
BUTTONB Pin = P11
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
|
||||
@@ -12,7 +12,7 @@ const (
|
||||
BUTTONB Pin = 26
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
|
||||
@@ -54,11 +54,6 @@ const (
|
||||
UART_TX_PIN = P0_08
|
||||
)
|
||||
|
||||
// Serial is the USB device
|
||||
var (
|
||||
Serial = USB
|
||||
)
|
||||
|
||||
// I2C pins
|
||||
const (
|
||||
SDA_PIN = P0_17 // I2C0 external
|
||||
|
||||
@@ -20,8 +20,6 @@ const (
|
||||
// Onboard blue LED (on the AI-Thinker module).
|
||||
const LED = D4
|
||||
|
||||
var Serial = UART0
|
||||
|
||||
// SPI pins
|
||||
const (
|
||||
SPI0_SCK_PIN = D5
|
||||
|
||||
@@ -23,9 +23,6 @@ const (
|
||||
UART_RX_PIN Pin = NoPin
|
||||
)
|
||||
|
||||
// Serial is the USB device
|
||||
var Serial = USB
|
||||
|
||||
// I2C pins (unused)
|
||||
const (
|
||||
SDA_PIN = NoPin
|
||||
|
||||
@@ -18,9 +18,6 @@ const (
|
||||
UART_RX_PIN Pin = 19
|
||||
)
|
||||
|
||||
// Serial is the USB device
|
||||
var Serial = USB
|
||||
|
||||
// I2C pins (unused)
|
||||
const (
|
||||
SDA_PIN = NoPin
|
||||
|
||||
@@ -34,7 +34,7 @@ var (
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART2,
|
||||
}
|
||||
Serial = UART2
|
||||
DefaultUART = UART2
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -38,7 +38,7 @@ var (
|
||||
TxAltFuncSelector: UART_ALT_FN,
|
||||
RxAltFuncSelector: UART_ALT_FN,
|
||||
}
|
||||
Serial = UART1
|
||||
DefaultUART = UART1
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -76,7 +76,7 @@ var (
|
||||
TxAltFuncSelector: 4,
|
||||
RxAltFuncSelector: 4,
|
||||
}
|
||||
Serial = UART1
|
||||
DefaultUART = UART1
|
||||
|
||||
// I2C1 is documented, alias to I2C0 as well
|
||||
I2C1 = &I2C{
|
||||
|
||||
@@ -78,7 +78,7 @@ var (
|
||||
TxAltFuncSelector: 7,
|
||||
RxAltFuncSelector: 3,
|
||||
}
|
||||
Serial = UART1
|
||||
DefaultUART = UART1
|
||||
|
||||
// I2C1 is documented, alias to I2C0 as well
|
||||
I2C1 = &I2C{
|
||||
|
||||
@@ -38,7 +38,7 @@ var (
|
||||
TxAltFuncSelector: UART_ALT_FN,
|
||||
RxAltFuncSelector: UART_ALT_FN,
|
||||
}
|
||||
Serial = UART1
|
||||
DefaultUART = UART1
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -41,7 +41,7 @@ const (
|
||||
|
||||
// UART
|
||||
var (
|
||||
Serial = UART0
|
||||
DefaultUART = UART0
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -41,7 +41,7 @@ const (
|
||||
|
||||
// UART
|
||||
var (
|
||||
Serial = UART0
|
||||
DefaultUART = UART0
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -41,7 +41,7 @@ const (
|
||||
|
||||
// UART
|
||||
var (
|
||||
Serial = UART0
|
||||
DefaultUART = UART0
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -19,7 +19,7 @@ const (
|
||||
LED_BLUE Pin = 23
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
|
||||
@@ -23,7 +23,7 @@ const (
|
||||
BUTTON4 Pin = 16
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
// UART pins for NRF52840-DK
|
||||
const (
|
||||
|
||||
@@ -22,7 +22,7 @@ const (
|
||||
BUTTON4 Pin = 25
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
|
||||
@@ -34,11 +34,6 @@ const (
|
||||
UART_RX_PIN Pin = NoPin
|
||||
)
|
||||
|
||||
// Serial is the USB device
|
||||
var (
|
||||
Serial = USB
|
||||
)
|
||||
|
||||
// I2C pins (unused)
|
||||
const (
|
||||
SDA_PIN = NoPin
|
||||
|
||||
@@ -17,7 +17,7 @@ const (
|
||||
LED3 = LCD_BACKLIGHT_LOW
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
// UART pins for PineTime. Note that RX is set to NoPin as RXD is not listed in
|
||||
// the PineTime schematic 1.0:
|
||||
|
||||
@@ -67,8 +67,6 @@ const (
|
||||
BUTTON_B_MASK = 128
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// USBCDC pins
|
||||
const (
|
||||
USBCDC_DM_PIN = PA24
|
||||
|
||||
@@ -70,8 +70,6 @@ const (
|
||||
BUTTON_B_MASK = 128
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// USBCDC pins
|
||||
const (
|
||||
USBCDC_DM_PIN = PA24
|
||||
|
||||
@@ -95,8 +95,6 @@ const (
|
||||
LED = D13
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// USBCDC pins
|
||||
const (
|
||||
USBCDC_DM_PIN = PA24
|
||||
|
||||
@@ -29,7 +29,7 @@ const (
|
||||
BUTTON Pin = 7
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
|
||||
@@ -38,7 +38,7 @@ var (
|
||||
TxAltFuncSelector: AF7_USART1_2_3,
|
||||
RxAltFuncSelector: AF7_USART1_2_3,
|
||||
}
|
||||
Serial = UART1
|
||||
DefaultUART = UART1
|
||||
)
|
||||
|
||||
// set up RX IRQ handler. Follow similar pattern for other UARTx instances
|
||||
|
||||
@@ -87,6 +87,8 @@ var (
|
||||
TeensyUART5 = UART4
|
||||
)
|
||||
|
||||
var DefaultUART = UART0
|
||||
|
||||
const (
|
||||
defaultUART0RX = D00
|
||||
defaultUART0TX = D01
|
||||
|
||||
@@ -136,9 +136,9 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
Serial = UART1
|
||||
UART1 = &_UART1
|
||||
_UART1 = UART{
|
||||
DefaultUART = UART1
|
||||
UART1 = &_UART1
|
||||
_UART1 = UART{
|
||||
Bus: nxp.LPUART6,
|
||||
Buffer: NewRingBuffer(),
|
||||
txBuffer: NewRingBuffer(),
|
||||
|
||||
@@ -325,8 +325,6 @@ const (
|
||||
OUTPUT_CTR_3V3 = PC15
|
||||
)
|
||||
|
||||
var Serial = USB
|
||||
|
||||
// USBCDC pins
|
||||
const (
|
||||
USBCDC_DM_PIN = PIN_USB_DM
|
||||
|
||||
@@ -27,4 +27,4 @@ const (
|
||||
|
||||
const HasLowFrequencyCrystal = true
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
@@ -122,7 +122,7 @@ func (i2c *I2C) readByte() byte {
|
||||
}
|
||||
|
||||
// Always use UART0 as the serial output.
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
// UART
|
||||
var (
|
||||
|
||||
@@ -251,6 +251,8 @@ func (p Pin) mux() *volatile.Register32 {
|
||||
}
|
||||
}
|
||||
|
||||
var DefaultUART = UART0
|
||||
|
||||
var (
|
||||
UART0 = &_UART0
|
||||
_UART0 = UART{Bus: esp.UART0, Buffer: NewRingBuffer()}
|
||||
|
||||
@@ -139,6 +139,8 @@ func (p Pin) PortMaskClear() (*uint32, uint32) {
|
||||
return &esp.GPIO.GPIO_OUT_W1TC.Reg, 1 << p
|
||||
}
|
||||
|
||||
var DefaultUART = UART0
|
||||
|
||||
// UART0 is a hardware UART that supports both TX and RX.
|
||||
var UART0 = &_UART0
|
||||
var _UART0 = UART{Buffer: NewRingBuffer()}
|
||||
|
||||
@@ -11,6 +11,12 @@ var (
|
||||
USB = &UART{100}
|
||||
)
|
||||
|
||||
// The Serial port always points to the default UART in a simulated environment.
|
||||
//
|
||||
// TODO: perhaps this should be a special serial object that outputs via WASI
|
||||
// stdout calls.
|
||||
var Serial = UART0
|
||||
|
||||
const (
|
||||
PinInput PinMode = iota
|
||||
PinOutput
|
||||
@@ -118,12 +124,6 @@ type UART struct {
|
||||
Bus uint8
|
||||
}
|
||||
|
||||
type UARTConfig struct {
|
||||
BaudRate uint32
|
||||
TX Pin
|
||||
RX Pin
|
||||
}
|
||||
|
||||
// Configure the UART.
|
||||
func (uart *UART) Configure(config UARTConfig) {
|
||||
uartConfigure(uart.Bus, config.TX, config.RX)
|
||||
|
||||
@@ -108,7 +108,7 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
var Serial = UART0
|
||||
var DefaultUART = UART0
|
||||
|
||||
func init() {
|
||||
UART0.Interrupt = interrupt.New(rp.IRQ_UART0_IRQ, _UART0.handleInterrupt)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// +build baremetal,serial.none
|
||||
|
||||
package machine
|
||||
|
||||
// Serial is a null device: writes to it are ignored.
|
||||
var Serial = NullSerial{}
|
||||
@@ -0,0 +1,6 @@
|
||||
// +build baremetal,serial.uart
|
||||
|
||||
package machine
|
||||
|
||||
// Serial is implemented via the default (usually the first) UART on the chip.
|
||||
var Serial = DefaultUART
|
||||
@@ -0,0 +1,6 @@
|
||||
// +build baremetal,serial.usb
|
||||
|
||||
package machine
|
||||
|
||||
// Serial is implemented via USB (USB-CDC).
|
||||
var Serial = USB
|
||||
@@ -0,0 +1,46 @@
|
||||
package machine
|
||||
|
||||
import "errors"
|
||||
|
||||
var errNoByte = errors.New("machine: no byte read")
|
||||
|
||||
// UARTConfig is a struct with which a UART (or similar object) can be
|
||||
// configured. The baud rate is usually respected, but TX and RX may be ignored
|
||||
// depending on the chip and the type of object.
|
||||
type UARTConfig struct {
|
||||
BaudRate uint32
|
||||
TX Pin
|
||||
RX Pin
|
||||
}
|
||||
|
||||
// NullSerial is a serial version of /dev/null (or null router): it drops
|
||||
// everything that is written to it.
|
||||
type NullSerial struct {
|
||||
}
|
||||
|
||||
// Configure does nothing: the null serial has no configuration.
|
||||
func (ns NullSerial) Configure(config UARTConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteByte is a no-op: the null serial doesn't write bytes.
|
||||
func (ns NullSerial) WriteByte(b byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadByte always returns an error because there aren't any bytes to read.
|
||||
func (ns NullSerial) ReadByte() (byte, error) {
|
||||
return 0, errNoByte
|
||||
}
|
||||
|
||||
// Buffered returns how many bytes are buffered in the UART. It always returns 0
|
||||
// as there are no bytes to read.
|
||||
func (ns NullSerial) Buffered() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Write is a no-op: none of the data is being written and it will not return an
|
||||
// error.
|
||||
func (ns NullSerial) Write(p []byte) (n int, err error) {
|
||||
return len(p), nil
|
||||
}
|
||||
@@ -23,12 +23,6 @@ const (
|
||||
ParityOdd UARTParity = 2
|
||||
)
|
||||
|
||||
type UARTConfig struct {
|
||||
BaudRate uint32
|
||||
TX Pin
|
||||
RX Pin
|
||||
}
|
||||
|
||||
// To implement the UART interface for a board, you must declare a concrete type as follows:
|
||||
//
|
||||
// type UART struct {
|
||||
|
||||
Reference in New Issue
Block a user