/
hyper_ringbuffer_test.spin2
237 lines (204 loc) · 5.72 KB
/
hyper_ringbuffer_test.spin2
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
con
_clkfreq = 300_000_000
' A propplug on a header
RX0 = 11
TX0 = 13
BR_SERIAL = 230_400
DEBUG_BAUD = 230_400
DATAGRAM_SIZE = 8 ' two longs, timestamp:mux:value
BUFFER_CAPACITY = 7500 * 3 '16_000_000 / DATAGRAM_SIZE
'BUFFER_CAPACITY = 500 * 3
ADC_BUFFER_SIZE = 100
ADC_CLK = 16
ADC_SDO = 17
ADC_SDI = 18
ADC_DRDY = 19
ADC_CS = 20
ADC_PDWN = 21
' P2 Eval Board Assignment
SD_DO = 58
SD_DI = 59
SD_CS = 60
SD_CLK = 61
'
SD_STATUS_PIN = 25
LINE_SIZE = 40 '<8>:<2>:<6>\r\n, times two
FLUSH_AFTER_BYTES = 512
' buttons and leds
IO_OFFSET = 0
LED_0 = 0
LED_1 = 1
LED_2 = 3
LED_3 = 2
SWITCH_0 = 4
SWITCH_1 = 5
SWITCH_2 = 7
SWITCH_3 = 6
var
long stack[2048]
'
LONG values_read
' here comes the mux buffer that
' holds the channel selection for
' the ADC
BYTE mux_buffer[8]
BYTE mux_buffer_length
' all things sd card writing
word file_index
byte filename[13]
long byte_counter
byte line_buffer[LINE_SIZE]
' The following three items are the ringbuffer
' for the buffered reader.
LONG read
LONG write
LONG buffer[ADC_BUFFER_SIZE * 2]
obj
adc: "buffered_ads1256"
rb: "hyper_ringbuffer"
serial: "jm_fullduplexserial"
fat: "FAT32"
util: "util"
pub main() | timestamp, mux, value, reader_cog, h, start, use_sd, count
pinsetup()
rb.init(DATAGRAM_SIZE, BUFFER_CAPACITY, 0)
debug("capacity:", udec(BUFFER_CAPACITY))
adc_init()
repeat
use_sd := waitpins()
rb.reset()
rb.fill(@fill_buffer, @fill_buffer_end - @fill_buffer)
debug("start ", udec(use_sd))
values_read := 0
reader_cog := start_reader(use_sd)
adc.start_continuous_read(@mux_buffer, mux_buffer_length)
start := getsec()
'count := 0
repeat values_read from 0 to BUFFER_CAPACITY - 2
timestamp, mux, value := adc.read_continuous()
'timestamp := count
'value := count +// 1024
feed(timestamp, mux, value)
'count += 1
adc.stop_continuous_read()
h := getsec() - start
debug("writing took: ", udec(h))
'reader_cog := start_reader(use_sd)
' notify the reader cog
' that we are done
cogatn(1 << reader_cog)
pri start_reader(use_sd) : result
result := cogspin(NEWCOG, reader(use_sd, cogid()), @stack)
waitatn()
debug("reader started")
pri waitpins(): result | use_sd
result := 0
repeat
result := pinread(IO_OFFSET + SWITCH_0 addpins 3)
use_sd := (result & 2) ? 1 : 0
if result & 1
quit
result := use_sd
pri pinsetup() | btnpins
btnpins := IO_OFFSET + SWITCH_0 addpins 3 ' buttons are base+4..base+7
wrpin(btnpins, P_LOW_15K) ' select 15K pull-downs
pinlow(btnpins) ' activate pull-downs
pri adc_init()
read := ADC_BUFFER_SIZE
adc.start(ADC_SDI, ADC_SDO, ADC_CLK, ADC_CS, ADC_DRDY, ADC_PDWN, @read)
mux_buffer[0] := $08 ' channel 0, single-sided
mux_buffer_length := 1
adc.wreg(adc.RDRATE, %11010000) ' 7500
'adc.wreg(adc.RDRATE, %10010010) ' 500
'adc.wreg(adc.RDRATE, %00000011) ' 2.5
pri feed(timestamp, mux, value) | entry[2]
entry[0] := timestamp
entry[1] := mux << 24 | value & $00ffffff
rb.store(@entry)
pri reader(use_sd, caller_cog) | entry[2], count, success, pos
count := 0
serial.start(RX0, TX0, %0000, BR_SERIAL)
if use_sd
start_fat()
cogatn(1 << caller_cog)
repeat until pollatn() == -1
repeat while rb.available()
rb.fetch(@entry)
count += 1
pos := prepare_line(entry[0], entry[1])
'pos := prepare_line(count, count +// 1024)
if use_sd
write_to_fat(pos)
serial.str(@line_buffer)
serial.fstr0(string("done\r\n"))
serial.txflush()
if use_sd
stop_fat()
debug("reading done")
pri stop_fat()
fat.flushData()
fat.closeFile()
pri start_fat() | success
fat.start(SD_DO, SD_CLK, SD_DI, SD_CS, 15_000_000, 0, @serial.fstr1)
debug(zstr(string("mount:")))
success := \fat.mountPartition(0)
if success > 0
debug(zstr(string("mounting failed")))
debug(zstr(success))
else
file_index := find_last_file_index() + 1
debug(zstr(string("file_index")))
debug(uhex_word(file_index))
pinlow(SD_STATUS_PIN)
bytemove(@filename, string("RQADSXXX.DAT"), 12)
filename[12] := 0
util.encode_hex_nibbles(@filename + 5, file_index, 3)
file_index += 1
debug(zstr(@filename))
success := \fat.newFile(@filename)
if success <> 0
debug("fat.newFile:error", zstr(success))
fat.openFile(@filename, "W")
pri find_last_file_index() : result | name, h
result := 0
repeat
fat.listEntries("N")
name := fat.listName()
if strsize(name) == 0
quit
if strsize(name) == 12 and util.startswith(string("RQADS"), name) and !util.endswith(string("~1.DAT"), name)
h := util.decode_hex_nibbles(name + 5, 3)
debug("found RQADSXXX.DAT", zstr(name), udec(fat.listSize()))
result #>= h
result &= $fff
pri write_to_fat(pos) | err
err := fat.writeData(@line_buffer, pos)
if err <> pos
'debug("writeData:error", zstr(err))
pinhigh(SD_STATUS_PIN)
byte_counter += pos
if byte_counter >= FLUSH_AFTER_BYTES
fat.flushData()
byte_counter := 0
pri prepare_line(timestamp, value):result | mux, pos, err
pos := 0
mux := value >> 24
value &= $ffffff
line_buffer[0] := "D"
line_buffer[1] := ":"
pos := 2
pos += util.encode_hex_nibbles(@line_buffer + pos, timestamp, 8)
line_buffer[pos] := ":"
pos += 1
pos += util.encode_hex_byte(@line_buffer + pos, mux)
line_buffer[pos] := ":"
pos += 1
pos += util.encode_hex_nibbles(@line_buffer + pos, value, 6)
line_buffer[pos] := 13
line_buffer[pos + 1] := 10
pos += 2
line_buffer[pos] := 0
return pos
DAT
fill_buffer long $a5a5a5a5, $ff00ff00, $13579BDF, $00000000
fill_buffer_end