Skip to content

Commit 96e863f

Browse files
aykevldeadprogram
authored andcommitted
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.
1 parent 75298bb commit 96e863f

File tree

114 files changed

+185
-104
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+185
-104
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ endif
436436
@$(MD5SUM) test.hex
437437
$(TINYGO) build -size short -o test.hex -target=pca10040 -opt=1 examples/blinky1
438438
@$(MD5SUM) test.hex
439+
$(TINYGO) build -size short -o test.hex -target=pca10040 -serial=none examples/echo
440+
@$(MD5SUM) test.hex
439441
$(TINYGO) build -o test.nro -target=nintendoswitch examples/serial
440442
@$(MD5SUM) test.nro
441443
$(TINYGO) build -size short -o test.hex -target=pca10040 -opt=0 ./testdata/stdlib.go

compileopts/config.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *Config) GOARCH() string {
5555

5656
// BuildTags returns the complete list of build tags used during this build.
5757
func (c *Config) BuildTags() []string {
58-
tags := append(c.Target.BuildTags, []string{"tinygo", "gc." + c.GC(), "scheduler." + c.Scheduler()}...)
58+
tags := append(c.Target.BuildTags, []string{"tinygo", "gc." + c.GC(), "scheduler." + c.Scheduler(), "serial." + c.Serial()}...)
5959
for i := 1; i <= c.GoMinorVersion; i++ {
6060
tags = append(tags, fmt.Sprintf("go1.%d", i))
6161
}
@@ -113,6 +113,18 @@ func (c *Config) Scheduler() string {
113113
return "coroutines"
114114
}
115115

116+
// Serial returns the serial implementation for this build configuration: uart,
117+
// usb (meaning USB-CDC), or none.
118+
func (c *Config) Serial() string {
119+
if c.Options.Serial != "" {
120+
return c.Options.Serial
121+
}
122+
if c.Target.Serial != "" {
123+
return c.Target.Serial
124+
}
125+
return "none"
126+
}
127+
116128
// OptLevels returns the optimization level (0-2), size level (0-2), and inliner
117129
// threshold as used in the LLVM optimization pipeline.
118130
func (c *Config) OptLevels() (optLevel, sizeLevel int, inlinerThreshold uint) {

compileopts/options.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
var (
1010
validGCOptions = []string{"none", "leaking", "extalloc", "conservative"}
1111
validSchedulerOptions = []string{"none", "tasks", "coroutines"}
12+
validSerialOptions = []string{"none", "uart", "usb"}
1213
validPrintSizeOptions = []string{"none", "short", "full"}
1314
validPanicStrategyOptions = []string{"print", "trap"}
1415
validOptOptions = []string{"none", "0", "1", "2", "s", "z"}
@@ -22,6 +23,7 @@ type Options struct {
2223
GC string
2324
PanicStrategy string
2425
Scheduler string
26+
Serial string
2527
PrintIR bool
2628
DumpSSA bool
2729
VerifyIR bool
@@ -59,6 +61,15 @@ func (o *Options) Verify() error {
5961
}
6062
}
6163

64+
if o.Serial != "" {
65+
valid := isInArray(validSerialOptions, o.Serial)
66+
if !valid {
67+
return fmt.Errorf(`invalid serial option '%s': valid values are %s`,
68+
o.Serial,
69+
strings.Join(validSerialOptions, ", "))
70+
}
71+
}
72+
6273
if o.PrintSizes != "" {
6374
valid := isInArray(validPrintSizeOptions, o.PrintSizes)
6475
if !valid {

compileopts/target.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type TargetSpec struct {
3131
BuildTags []string `json:"build-tags"`
3232
GC string `json:"gc"`
3333
Scheduler string `json:"scheduler"`
34+
Serial string `json:"serial"` // which serial output to use (uart, usb, none)
3435
Linker string `json:"linker"`
3536
RTLib string `json:"rtlib"` // compiler runtime library (libgcc, compiler-rt)
3637
Libc string `json:"libc"`

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ func main() {
10091009
gc := flag.String("gc", "", "garbage collector to use (none, leaking, extalloc, conservative)")
10101010
panicStrategy := flag.String("panic", "print", "panic strategy (print, trap)")
10111011
scheduler := flag.String("scheduler", "", "which scheduler to use (none, coroutines, tasks)")
1012+
serial := flag.String("serial", "", "which serial output to use (none, uart, usb)")
10121013
printIR := flag.Bool("printir", false, "print LLVM IR")
10131014
dumpSSA := flag.Bool("dumpssa", false, "dump internal Go SSA")
10141015
verifyIR := flag.Bool("verifyir", false, "run extra verification steps on LLVM IR")
@@ -1081,6 +1082,7 @@ func main() {
10811082
GC: *gc,
10821083
PanicStrategy: *panicStrategy,
10831084
Scheduler: *scheduler,
1085+
Serial: *serial,
10841086
PrintIR: *printIR,
10851087
DumpSSA: *dumpSSA,
10861088
VerifyIR: *verifyIR,

src/machine/board_arduino_mkr1000.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ const (
4747
LED = D6
4848
)
4949

50-
var Serial = USB
51-
5250
// USBCDC pins
5351
const (
5452
USBCDC_DM_PIN Pin = PA24

src/machine/board_arduino_zero.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ const (
3535
LED3 Pin = PB03 // RX LED
3636
)
3737

38-
var Serial = USB
39-
4038
// ADC pins
4139
const (
4240
AREF Pin = PA03

src/machine/board_atsamd21.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,3 @@ const (
7474
PB30 Pin = 62
7575
PB31 Pin = 63
7676
)
77-
78-
var Serial = USB

src/machine/board_atsame54-xpro.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ const (
1515
BUTTON = PB31
1616
)
1717

18-
var Serial = USB
19-
2018
const (
2119
// https://ww1.microchip.com/downloads/en/DeviceDoc/70005321A.pdf
2220

src/machine/board_bluepill.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
BUTTON = PA0
1818
)
1919

20-
var Serial = UART1
20+
var DefaultUART = UART1
2121

2222
// UART pins
2323
const (

0 commit comments

Comments
 (0)