MCPcopy
hub / github.com/tinygo-org/tinygo / Monitor

Function Monitor

monitor.go:29–226  ·  view source on GitHub ↗

Monitor connects to the given port and reads/writes the serial port.

(executable, port string, config *compileopts.Config)

Source from the content-addressed store, hash-verified

27
28// Monitor connects to the given port and reads/writes the serial port.
29func Monitor(executable, port string, config *compileopts.Config) error {
30 const timeout = time.Second * 3
31 var exit func() // function to be called before exiting
32 var serialConn io.ReadWriter
33
34 if config.Options.Serial == "rtt" {
35 // Use the RTT interface, which is documented (in part) here:
36 // https://wiki.segger.com/RTT
37
38 // Try to find the "_SEGGER_RTT" symbol (machine.rttSerialInstance)
39 // symbol, which is the RTT control block.
40 file, err := elf.Open(executable)
41 if err != nil {
42 return fmt.Errorf("could not open ELF file to determine RTT control block: %w", err)
43 }
44 defer file.Close()
45 symbols, err := file.Symbols()
46 if err != nil {
47 return fmt.Errorf("could not read ELF symbol table to determine RTT control block: %w", err)
48 }
49 var address uint64
50 for _, symbol := range symbols {
51 if symbol.Name == "_SEGGER_RTT" {
52 address = symbol.Value
53 break
54 }
55 }
56 if address == 0 {
57 return fmt.Errorf("could not find RTT control block in ELF file")
58 }
59
60 // Start an openocd process in the background.
61 args, err := config.OpenOCDConfiguration()
62 if err != nil {
63 return err
64 }
65 args = append(args,
66 "-c", fmt.Sprintf("rtt setup 0x%x 16 \"SEGGER RTT\"", address),
67 "-c", "init",
68 "-c", "rtt server start 0 0")
69 cmd := executeCommand(config.Options, "openocd", args...)
70 stderr, err := cmd.StderrPipe()
71 if err != nil {
72 return err
73 }
74 cmd.Stdout = os.Stdout
75 err = cmd.Start()
76 if err != nil {
77 return err
78 }
79 defer cmd.Process.Kill()
80 exit = func() {
81 // Make sure the openocd process is terminated at exit.
82 // This does not happen through the defer above when exiting through
83 // os.Exit.
84 cmd.Process.Kill()
85 }
86

Callers 2

FlashFunction · 0.85
mainFunction · 0.85

Calls 15

ReadMethod · 0.95
WriteMethod · 0.95
executeCommandFunction · 0.85
getDefaultPortFunction · 0.85
newOutputWriterFunction · 0.85
OpenMethod · 0.80
OpenOCDConfigurationMethod · 0.80
KillMethod · 0.80
NowMethod · 0.80
ErrorfMethod · 0.65
CloseMethod · 0.65
StartMethod · 0.65

Tested by

no test coverage detected