1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
//go:build baremetal && serial.rtt
// Implement Segger RTT support.
// This is mostly useful for targets that only have a debug connection
// available, and no serial output (or input). It is somewhat like semihosting,
// but not unusably slow.
// It was originally specified by Segger, but support is available in OpenOCD
// for at least the DAPLink debuggers so I assume it works on any SWD debugger.
package machine
import (
"runtime/interrupt"
"runtime/volatile"
"unsafe"
)
// This symbol name is known by the compiler, see monitor.go.
var rttSerialInstance rttSerial
var Serial = &rttSerialInstance
func InitSerial() {
Serial.Configure(UARTConfig{})
}
const (
// Some constants, see:
// https://github.com/SEGGERMicro/RTT/blob/master/RTT/SEGGER_RTT.h
rttMaxNumUpBuffers = 1
rttMaxNumDownBuffers = 1
rttBufferSizeUp = 1024
rttBufferSizeDown = 16
rttModeNoBlockSkip = 0
rttModeNoBlockTrim = 1
rttModeBlockIfFifoFull = 2
)
// The debugger knows about the layout of this struct, so it must not change.
// This is SEGGER_RTT_CB.
type rttControlBlock struct {
id [16]volatile.Register8
maxNumUpBuffers int32
maxNumDownBuffers int32
buffersUp [rttMaxNumUpBuffers]rttBuffer
buffersDown [rttMaxNumDownBuffers]rttBuffer
}
// Up or down buffer.
// This is SEGGER_RTT_BUFFER_UP and SEGGER_RTT_BUFFER_DOWN.
type rttBuffer struct {
name *byte
buffer *volatile.Register8
bufferSize uint32
writeOffset volatile.Register32
readOffset volatile.Register32
flags uint32
}
// Static buffers, for the default up and down buffer.
var (
rttBufferUpData [rttBufferSizeUp]volatile.Register8
rttBufferDownData [rttBufferSizeDown]volatile.Register8
)
type rttSerial struct {
rttControlBlock
}
func (s *rttSerial) Configure(config UARTConfig) error {
s.maxNumUpBuffers = rttMaxNumUpBuffers
s.maxNumDownBuffers = rttMaxNumDownBuffers
s.buffersUp[0].name = &[]byte("Terminal\x00")[0]
s.buffersUp[0].buffer = &rttBufferUpData[0]
s.buffersUp[0].bufferSize = rttBufferSizeUp
s.buffersUp[0].flags = rttModeNoBlockSkip
s.buffersDown[0].name = &[]byte("Terminal\x00")[0]
s.buffersDown[0].buffer = &rttBufferDownData[0]
s.buffersDown[0].bufferSize = rttBufferSizeDown
s.buffersDown[0].flags = rttModeNoBlockSkip
id := "SEGGER RTT"
for i := 0; i < len(id); i++ {
s.id[i].Set(id[i])
}
return nil
}
func (b *rttBuffer) writeByte(c byte) {
state := interrupt.Disable()
readOffset := b.readOffset.Get()
writeOffset := b.writeOffset.Get()
newWriteOffset := writeOffset + 1
if newWriteOffset == b.bufferSize {
newWriteOffset = 0
}
if newWriteOffset != readOffset {
unsafe.Slice(b.buffer, b.bufferSize)[writeOffset].Set(c)
b.writeOffset.Set(newWriteOffset)
}
interrupt.Restore(state)
}
func (b *rttBuffer) readByte() byte {
readOffset := b.readOffset.Get()
writeOffset := b.writeOffset.Get()
for readOffset == writeOffset {
readOffset = b.readOffset.Get()
}
c := unsafe.Slice(b.buffer, b.bufferSize)[readOffset].Get()
b.readOffset.Set(readOffset + 1)
return c
}
func (b *rttBuffer) buffered() int {
readOffset := b.readOffset.Get()
writeOffset := b.writeOffset.Get()
return int((writeOffset - readOffset) % rttBufferSizeDown)
}
func (s *rttSerial) WriteByte(b byte) error {
s.buffersUp[0].writeByte(b)
return nil
}
func (s *rttSerial) ReadByte() (byte, error) {
return s.buffersDown[0].readByte(), errNoByte
}
func (s *rttSerial) Buffered() int {
return s.buffersDown[0].buffered()
}
func (s *rttSerial) Write(data []byte) (n int, err error) {
for _, v := range data {
s.WriteByte(v)
}
return len(data), nil
}
|