@@ -403,24 +403,34 @@ func Flash(pkgName, target, port string, config *BuildConfig) error {
403
403
// determine the type of file to compile
404
404
var fileExt string
405
405
406
- switch {
407
- case strings .Contains (spec .Flasher , "{hex}" ):
406
+ switch spec .FlashMethod {
407
+ case "command" , "" :
408
+ switch {
409
+ case strings .Contains (spec .FlashCommand , "{hex}" ):
410
+ fileExt = ".hex"
411
+ case strings .Contains (spec .FlashCommand , "{elf}" ):
412
+ fileExt = ".elf"
413
+ case strings .Contains (spec .FlashCommand , "{bin}" ):
414
+ fileExt = ".bin"
415
+ case strings .Contains (spec .FlashCommand , "{uf2}" ):
416
+ fileExt = ".uf2"
417
+ default :
418
+ return errors .New ("invalid target file - did you forget the {hex} token in the 'flash-command' section?" )
419
+ }
420
+ case "msd" :
421
+ if spec .FlashFilename == "" {
422
+ return errors .New ("invalid target file: flash-method was set to \" msd\" but no msd-firmware-name was set" )
423
+ }
424
+ fileExt = filepath .Ext (spec .FlashFilename )
425
+ case "openocd" :
408
426
fileExt = ".hex"
409
- case strings .Contains (spec .Flasher , "{elf}" ):
410
- fileExt = ".elf"
411
- case strings .Contains (spec .Flasher , "{bin}" ):
412
- fileExt = ".bin"
413
- case strings .Contains (spec .Flasher , "{uf2}" ):
414
- fileExt = ".uf2"
427
+ case "native" :
428
+ return errors .New ("unknown flash method \" native\" - did you miss a -target flag?" )
415
429
default :
416
- return errors .New ("invalid target file - did you forget the {hex} token in the 'flash' section?" )
430
+ return errors .New ("unknown flash method: " + spec . FlashMethod )
417
431
}
418
432
419
433
return Compile (pkgName , fileExt , spec , config , func (tmppath string ) error {
420
- if spec .Flasher == "" {
421
- return errors .New ("no flash command specified - did you miss a -target flag?" )
422
- }
423
-
424
434
// do we need port reset to put MCU into bootloader mode?
425
435
if spec .PortReset == "true" {
426
436
err := touchSerialPortAt1200bps (port )
@@ -432,7 +442,25 @@ func Flash(pkgName, target, port string, config *BuildConfig) error {
432
442
}
433
443
434
444
// this flashing method copies the binary data to a Mass Storage Device (msd)
435
- if spec .FlashMethod == "msd" {
445
+ switch spec .FlashMethod {
446
+ case "" , "command" :
447
+ // Create the command.
448
+ flashCmd := spec .FlashCommand
449
+ fileToken := "{" + fileExt [1 :] + "}"
450
+ flashCmd = strings .Replace (flashCmd , fileToken , tmppath , - 1 )
451
+ flashCmd = strings .Replace (flashCmd , "{port}" , port , - 1 )
452
+
453
+ // Execute the command.
454
+ cmd := exec .Command ("/bin/sh" , "-c" , flashCmd )
455
+ cmd .Stdout = os .Stdout
456
+ cmd .Stderr = os .Stderr
457
+ cmd .Dir = sourceDir ()
458
+ err := cmd .Run ()
459
+ if err != nil {
460
+ return & commandError {"failed to flash" , tmppath , err }
461
+ }
462
+ return nil
463
+ case "msd" :
436
464
switch fileExt {
437
465
case ".uf2" :
438
466
err := flashUF2UsingMSD (spec .FlashVolume , tmppath )
@@ -449,24 +477,23 @@ func Flash(pkgName, target, port string, config *BuildConfig) error {
449
477
default :
450
478
return errors .New ("mass storage device flashing currently only supports uf2 and hex" )
451
479
}
480
+ case "openocd" :
481
+ args , err := spec .OpenOCDConfiguration ()
482
+ if err != nil {
483
+ return err
484
+ }
485
+ args = append (args , "-c" , "program " + tmppath + " reset exit" )
486
+ cmd := exec .Command ("openocd" , args ... )
487
+ cmd .Stdout = os .Stdout
488
+ cmd .Stderr = os .Stderr
489
+ err = cmd .Run ()
490
+ if err != nil {
491
+ return & commandError {"failed to flash" , tmppath , err }
492
+ }
493
+ return nil
494
+ default :
495
+ return fmt .Errorf ("unknown flash method: %s" , spec .FlashMethod )
452
496
}
453
-
454
- // Create the command.
455
- flashCmd := spec .Flasher
456
- fileToken := "{" + fileExt [1 :] + "}"
457
- flashCmd = strings .Replace (flashCmd , fileToken , tmppath , - 1 )
458
- flashCmd = strings .Replace (flashCmd , "{port}" , port , - 1 )
459
-
460
- // Execute the command.
461
- cmd := exec .Command ("/bin/sh" , "-c" , flashCmd )
462
- cmd .Stdout = os .Stdout
463
- cmd .Stderr = os .Stderr
464
- cmd .Dir = sourceDir ()
465
- err := cmd .Run ()
466
- if err != nil {
467
- return & commandError {"failed to flash" , tmppath , err }
468
- }
469
- return nil
470
497
})
471
498
}
472
499
@@ -485,9 +512,33 @@ func FlashGDB(pkgName, target, port string, ocdOutput bool, config *BuildConfig)
485
512
}
486
513
487
514
return Compile (pkgName , "" , spec , config , func (tmppath string ) error {
488
- if len (spec .OCDDaemon ) != 0 {
515
+ // Find a good way to run GDB.
516
+ gdbInterface := spec .FlashMethod
517
+ switch gdbInterface {
518
+ case "msd" , "command" , "" :
519
+ if gdbInterface == "" {
520
+ gdbInterface = "command"
521
+ }
522
+ if spec .OpenOCDInterface != "" && spec .OpenOCDTarget != "" {
523
+ gdbInterface = "openocd"
524
+ }
525
+ }
526
+
527
+ // Run the GDB server, if necessary.
528
+ var gdbCommands []string
529
+ switch gdbInterface {
530
+ case "native" :
531
+ // Run GDB directly.
532
+ gdbCommands = append (gdbCommands , "run" )
533
+ case "openocd" :
534
+ gdbCommands = append (gdbCommands , "target remote :3333" , "monitor halt" , "load" , "monitor reset" , "c" )
535
+
489
536
// We need a separate debugging daemon for on-chip debugging.
490
- daemon := exec .Command (spec .OCDDaemon [0 ], spec .OCDDaemon [1 :]... )
537
+ args , err := spec .OpenOCDConfiguration ()
538
+ if err != nil {
539
+ return err
540
+ }
541
+ daemon := exec .Command ("openocd" , args ... )
491
542
if ocdOutput {
492
543
// Make it clear which output is from the daemon.
493
544
w := & ColorWriter {
@@ -512,6 +563,10 @@ func FlashGDB(pkgName, target, port string, ocdOutput bool, config *BuildConfig)
512
563
// Maybe we should send a .Kill() after x seconds?
513
564
daemon .Wait ()
514
565
}()
566
+ case "msd" :
567
+ return errors .New ("gdb is not supported for drag-and-drop programmable devices" )
568
+ default :
569
+ return fmt .Errorf ("gdb is not supported with interface %#v" , gdbInterface )
515
570
}
516
571
517
572
// Ignore Ctrl-C, it must be passed on to GDB.
@@ -526,7 +581,7 @@ func FlashGDB(pkgName, target, port string, ocdOutput bool, config *BuildConfig)
526
581
// By default: gdb -ex run <binary>
527
582
// Exit GDB with Ctrl-D.
528
583
params := []string {tmppath }
529
- for _ , cmd := range spec . GDBCmds {
584
+ for _ , cmd := range gdbCommands {
530
585
params = append (params , "-ex" , cmd )
531
586
}
532
587
cmd := exec .Command (spec .GDB , params ... )
@@ -606,7 +661,7 @@ func flashUF2UsingMSD(volume, tmppath string) error {
606
661
return err
607
662
}
608
663
if d == nil {
609
- return errors .New ("unable to locate UF2 device:" + volume )
664
+ return errors .New ("unable to locate UF2 device: " + volume )
610
665
}
611
666
612
667
return moveFile (tmppath , filepath .Dir (d [0 ])+ "/flash.uf2" )
@@ -624,7 +679,7 @@ func flashHexUsingMSD(volume, tmppath string) error {
624
679
return err
625
680
}
626
681
if d == nil {
627
- return errors .New ("unable to locate device:" + volume )
682
+ return errors .New ("unable to locate device: " + volume )
628
683
}
629
684
630
685
return moveFile (tmppath , d [0 ]+ "/flash.hex" )
0 commit comments