@@ -144,6 +144,14 @@ func Test(pkgName string, options *compileopts.Options) error {
144
144
145
145
// Flash builds and flashes the built binary to the given serial port.
146
146
func Flash (pkgName , port string , options * compileopts.Options ) error {
147
+ if port == "" {
148
+ var err error
149
+ port , err = getDefaultPort ()
150
+ if err != nil {
151
+ return err
152
+ }
153
+ }
154
+
147
155
config , err := builder .NewConfig (options )
148
156
if err != nil {
149
157
return err
@@ -255,6 +263,14 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
255
263
// Note: this command is expected to execute just before exiting, as it
256
264
// modifies global state.
257
265
func FlashGDB (pkgName , port string , ocdOutput bool , options * compileopts.Options ) error {
266
+ if port == "" {
267
+ var err error
268
+ port , err = getDefaultPort ()
269
+ if err != nil {
270
+ return err
271
+ }
272
+ }
273
+
258
274
config , err := builder .NewConfig (options )
259
275
if err != nil {
260
276
return err
@@ -480,6 +496,30 @@ func parseSize(s string) (int64, error) {
480
496
return n , err
481
497
}
482
498
499
+ // getDefaultPort returns the default serial port depending on the operating system.
500
+ // Currently only supports macOS and Linux.
501
+ func getDefaultPort () (port string , err error ) {
502
+ var portPath string
503
+ switch runtime .GOOS {
504
+ case "darwin" :
505
+ portPath = "/dev/cu.usb*"
506
+ case "linux" :
507
+ portPath = "/dev/ttyACM*"
508
+ default :
509
+ return "" , errors .New ("unable to search for a default USB device to be flashed on this OS" )
510
+ }
511
+
512
+ d , err := filepath .Glob (portPath )
513
+ if err != nil {
514
+ return "" , err
515
+ }
516
+ if d == nil {
517
+ return "" , errors .New ("unable to locate a USB device to be flashed" )
518
+ }
519
+
520
+ return d [0 ], nil
521
+ }
522
+
483
523
func usage () {
484
524
fmt .Fprintln (os .Stderr , "TinyGo is a Go compiler for small places." )
485
525
fmt .Fprintln (os .Stderr , "version:" , version )
@@ -547,7 +587,7 @@ func main() {
547
587
printSize := flag .String ("size" , "" , "print sizes (none, short, full)" )
548
588
nodebug := flag .Bool ("no-debug" , false , "disable DWARF debug symbol generation" )
549
589
ocdOutput := flag .Bool ("ocd-output" , false , "print OCD daemon output during debug" )
550
- port := flag .String ("port" , "/dev/ttyACM0 " , "flash port" )
590
+ port := flag .String ("port" , "" , "flash port" )
551
591
programmer := flag .String ("programmer" , "" , "which hardware programmer to use" )
552
592
cFlags := flag .String ("cflags" , "" , "additional cflags for compiler" )
553
593
ldFlags := flag .String ("ldflags" , "" , "additional ldflags for linker" )
0 commit comments