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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/**
* @file main.cpp
* @brief Program entry point.
*
* Copyright (C) 2020 Clyne Sullivan
*
* Distributed under the GNU GPL v3 or later. You should have received a copy of
* the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
#include "ch.h"
#include "hal.h"
#include "adc.hpp"
#include "dac.hpp"
#include "elf_load.hpp"
#include "usbserial.hpp"
#include <array>
constexpr unsigned int MAX_SAMPLE_BUFFER_SIZE = 2048;
enum class RunStatus : char
{
Idle = '1',
Converting,
Recovered
};
static RunStatus run_status = RunStatus::Idle;
#define MSG_CONVFIRST (1)
#define MSG_CONVSECOND (2)
#define MSG_CONVFIRST_MEASURE (3)
#define MSG_CONVSECOND_MEASURE (4)
static msg_t conversionMBBuffer[8];
static MAILBOX_DECL(conversionMB, conversionMBBuffer, 8);
static THD_WORKING_AREA(conversionThreadWA, 1024);
static THD_FUNCTION(conversionThread, arg);
static time_measurement_t conversion_time_measurement;
static_assert(sizeof(adcsample_t) == sizeof(uint16_t));
static_assert(sizeof(dacsample_t) == sizeof(uint16_t));
#if CACHE_LINE_SIZE > 0
CC_ALIGN(CACHE_LINE_SIZE)
#endif
static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, MAX_SAMPLE_BUFFER_SIZE)> adc_samples;
#if CACHE_LINE_SIZE > 0
CC_ALIGN(CACHE_LINE_SIZE)
#endif
static std::array<dacsample_t, CACHE_SIZE_ALIGN(dacsample_t, MAX_SAMPLE_BUFFER_SIZE)> dac_samples;
static uint8_t elf_file_store[8192];
static elf::entry_t elf_entry = nullptr;
static void signal_operate(adcsample_t *buffer, size_t count);
static void signal_operate_measure(adcsample_t *buffer, size_t count);
static void main_loop();
int main()
{
// Initialize the RTOS
halInit();
chSysInit();
// Enable FPU
SCB->CPACR |= 0xF << 20;
// Prepare LED
palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL);
palClearPad(GPIOA, 5);
adc::init();
dac::init();
usbserial::init();
// Start the conversion manager thread
chTMObjectInit(&conversion_time_measurement);
chThdCreateStatic(conversionThreadWA, sizeof(conversionThreadWA),
NORMALPRIO,
conversionThread, nullptr);
main_loop();
}
void main_loop()
{
static unsigned int dac_sample_count = MAX_SAMPLE_BUFFER_SIZE;
while (1) {
if (usbserial::is_active()) {
// Attempt to receive a command packet
if (char cmd[3]; usbserial::read(&cmd, 1) > 0) {
// Packet received, first byte represents the desired command/action
switch (cmd[0]) {
// 'r' - Conduct a single sample of the ADC, and send the results back over USB.
case 'r':
// Get the next two bytes of the packet to determine the desired sample size
if (run_status != RunStatus::Idle || usbserial::read(&cmd[1], 2) < 2)
break;
if (unsigned int desiredSize = cmd[1] | (cmd[2] << 8); desiredSize <= adc_samples.size()) {
adc::read(&adc_samples[0], desiredSize);
usbserial::write(adc_samples.data(), desiredSize * sizeof(adcsample_t));
}
break;
// 'R' - Begin continuous sampling/conversion of the ADC. Samples will go through
// the conversion code, and will be sent out over the DAC.
case 'R':
//if (run_status != RunStatus::Idle)
// break;
run_status = RunStatus::Converting;
dac_samples.fill(0);
adc::read_start(signal_operate, &adc_samples[0], adc_samples.size());
dac::write_start(&dac_samples[0], dac_samples.size());
break;
// 'M' - Begins continuous sampling, but measures the execution time of the first
// sample processing. This duration can be later read through 'm'.
case 'M':
run_status = RunStatus::Converting;
dac_samples.fill(0);
adc::read_start(signal_operate_measure, &adc_samples[0], adc_samples.size());
dac::write_start(&dac_samples[0], dac_samples.size());
break;
// 'm' - Returns the last measured sample processing time, presumably in processor
// ticks.
case 'm':
usbserial::write(&conversion_time_measurement.last, sizeof(rtcnt_t));
break;
// 's' - Sends the current contents of the DAC buffer back over USB.
case 's':
usbserial::write(dac_samples.data(), 1/*dac_samples.size()*/ * sizeof(dacsample_t));
break;
// 'S' - Stops the continuous sampling/conversion.
case 'S':
//if (run_status != RunStatus::Converting)
// break;
dac::write_stop();
adc::read_stop();
run_status = RunStatus::Idle;
break;
// 'e' - Reads in and loads the compiled conversion code binary from USB.
case 'e':
// Get the binary's size
if (usbserial::read(&cmd[1], 2) < 2)
break;
// Only load the binary if it can fit in the memory reserved for it.
if (unsigned int binarySize = cmd[1] | (cmd[2] << 8); binarySize < sizeof(elf_file_store)) {
usbserial::read(elf_file_store, binarySize);
elf_entry = elf::load(elf_file_store);
}
break;
// 'E' - Unloads the currently loaded conversion code
case 'E':
elf_entry = nullptr;
break;
// 'W' - Sets the number of samples for DAC writing with command 'w'.
// If the provided count is zero, DAC writing is stopped.
case 'W':
if (usbserial::read(&cmd[1], 2) < 2)
break;
if (unsigned int sampleCount = cmd[1] | (cmd[2] << 8); sampleCount <= dac_samples.size()) {
if (sampleCount > 0)
dac_sample_count = sampleCount;
else
dac::write_stop();
}
break;
// 'w' - Starts the DAC, looping over the given data (data size set by command 'W').
case 'w':
if (usbserial::read(&dac_samples[0], dac_sample_count * sizeof(dacsample_t) !=
dac_sample_count * sizeof(dacsample_t)))
{
break;
} else {
dac::write_start(&dac_samples[0], dac_sample_count);
}
break;
// 'i' - Sends an identifying string to confirm that this is the stmdsp device.
case 'i':
usbserial::write("stmdsp", 6);
break;
// 'I' - Sends the current run status.
case 'I':
usbserial::write(&run_status, sizeof(run_status));
break;
default:
break;
}
}
}
chThdSleepMilliseconds(1);
}
}
void conversion_abort()
{
elf_entry = nullptr;
dac::write_stop();
adc::read_stop();
run_status = RunStatus::Recovered;
}
THD_FUNCTION(conversionThread, arg)
{
(void)arg;
while (1) {
msg_t message;
if (chMBFetchTimeout(&conversionMB, &message, TIME_INFINITE) == MSG_OK) {
adcsample_t *samples = nullptr;
auto halfsize = adc_samples.size() / 2;
if (message == MSG_CONVFIRST) {
if (elf_entry)
samples = elf_entry(&adc_samples[0], halfsize);
if (!samples)
samples = &adc_samples[0];
std::copy(samples, samples + halfsize, &dac_samples[0]);
} else if (message == MSG_CONVSECOND) {
if (elf_entry)
samples = elf_entry(&adc_samples[adc_samples.size() / 2], halfsize);
if (!samples)
samples = &adc_samples[adc_samples.size() / 2];
std::copy(samples, samples + halfsize, &dac_samples[dac_samples.size() / 2]);
} else if (message == MSG_CONVFIRST_MEASURE) {
chTMStartMeasurementX(&conversion_time_measurement);
if (elf_entry)
samples = elf_entry(&adc_samples[adc_samples.size() / 2], halfsize);
chTMStopMeasurementX(&conversion_time_measurement);
if (!samples)
samples = &adc_samples[adc_samples.size() / 2];
std::copy(samples, samples + halfsize, &dac_samples[dac_samples.size() / 2]);
} else if (message == MSG_CONVSECOND_MEASURE) {
chTMStartMeasurementX(&conversion_time_measurement);
if (elf_entry)
samples = elf_entry(&adc_samples[adc_samples.size() / 2], halfsize);
chTMStopMeasurementX(&conversion_time_measurement);
if (!samples)
samples = &adc_samples[adc_samples.size() / 2];
std::copy(samples, samples + halfsize, &dac_samples[dac_samples.size() / 2]);
}
}
}
}
void signal_operate(adcsample_t *buffer, [[maybe_unused]] size_t count)
{
if (chMBGetUsedCountI(&conversionMB) > 1)
conversion_abort();
else
chMBPostI(&conversionMB, buffer == &adc_samples[0] ? MSG_CONVFIRST : MSG_CONVSECOND);
}
void signal_operate_measure(adcsample_t *buffer, [[maybe_unused]] size_t count)
{
chMBPostI(&conversionMB, buffer == &adc_samples[0] ? MSG_CONVFIRST_MEASURE : MSG_CONVSECOND_MEASURE);
adc::read_set_operation_func(signal_operate);
}
extern "C" {
__attribute__((naked))
void HardFault_Handler()
{
//asm("push {lr}");
uint32_t *stack;
uint32_t lr;
asm("\
tst lr, #4; \
ite eq; \
mrseq %0, msp; \
mrsne %0, psp; \
mov %1, lr; \
" : "=r" (stack), "=r" (lr));
//stack++;
stack[7] |= (1 << 24); // Keep Thumb mode enabled
conversion_abort();
// TODO test lr and decide how to recover
//if (run_status == RunStatus::Converting) {
stack[6] = stack[5]; // Escape from elf_entry code
//} else /*if (run_status == RunStatus::Recovered)*/ {
// stack[6] = (uint32_t)main_loop & ~1; // Return to safety
//}
//asm("pop {lr}; bx lr");
asm("bx lr");
}
} // extern "C"
|