-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
500 lines (462 loc) · 10.6 KB
/
main.c
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* This file is part of the openloop hardware looper project.
*
* Copyright (C) 2018 Jonathan Halmen <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define COUNTERRORS 0
#define dprintf(...)
#include "hardware.h"
#include "wm8778.h"
#include "swo.h"
#include <stdlib.h>
/* function prototypes */
int16_t get_sample(void);
void put_sample(int16_t s);
uint8_t card_busy(void);
uint8_t sd_dma_done(void);
void handle_sd(void);
void end_record(void);
void startaudio(void);
void leds_update(void);
void loop_reset(void);
/* globals */
volatile uint32_t tick = 0; // system tick
const uint8_t tickhz = 5; // frequency of system tick
const uint8_t norepeat = 4; // software debounce. [in ticks]
const uint8_t resetloop = 10; // ticks to hold for reset
uint8_t heartbeat = 10; // heartbeat delay [in ticks]
volatile uint16_t chanvol[3] = {0,0,0}; // adc volume values. updated by dma
#if COUNTERRORS
enum ERRNUM {
ERROR_GETSAMPLE,
ERROR_PUTSAMPLE,
ERROR_RXFER,
ERROR_TXFER,
ERROR_I2S,
ERROR_LAST};
volatile uint8_t error[ERROR_LAST]; // error accumulator
#endif
struct dma_channel volumes = {
.rcc = RCC_DMA2,
.dma = DMA2,
.stream = DMA_STREAM4,
.direction = DMA_SxCR_DIR_PERIPHERAL_TO_MEM,
.channel = DMA_SxCR_CHSEL_0,
.psize = DMA_SxCR_PSIZE_16BIT,
.paddress = (uint32_t)&ADC_DR(ADC1),
.msize = DMA_SxCR_MSIZE_16BIT,
.maddress = (uint32_t)chanvol,
.doublebuf = 0,
.minc = 1,
.circ = 1,
.pinc = 0,
.prio = DMA_SxCR_PL_LOW,
.pburst = DMA_SxCR_PBURST_SINGLE,
.periphflwctrl = 0,
.numberofdata = 3,
.interrupts = DMA_SxCR_TCIE,
.nvic = NVIC_DMA2_STREAM4_IRQ
};
struct i2sfreq f48k = {
// relative error: 0.0198%
.plln = 215,
.pllr = 5,
.div = 3,
.odd = 1
};
struct i2sfreq f44k = {
// relative error: 0.1223%
.plln = 203,
.pllr = 4,
.div = 4,
.odd = 1
};
struct i2sfreq f32k = {
// relative error: 0.0976%
.plln = 205,
.pllr = 5,
.div = 5,
.odd = 0
};
struct i2sfreq f22k = {
// 22k050
// relative error: 0.1047%
.plln = 203,
.pllr = 4,
.div = 9,
.odd = 0
};
struct i2sfreq f16k = {
// relative error: 0.00375%
.plln = 213,
.pllr = 4,
.div = 13,
.odd = 0
};
enum {
START = 1,
STOP = 2,
MENU = 4
} action = 0;
volatile enum s{
STANDBY = 0,
RECORD = 1,
PLAY = 2,
OVRDUB = 3
} state = STANDBY;
const enum s trans[][5] = {
{0, PLAY, STANDBY, 0, STANDBY},
{0, PLAY, STANDBY, 0, RECORD},
{0, OVRDUB, STANDBY, 0, PLAY},
{0, PLAY, STANDBY, 0, OVRDUB}};
struct {
uint32_t start;
uint32_t len;
int16_t end_idx;
} loop = {10000, 0, 0};
struct {
/* buffers */
int16_t in[512]; // from card to codec
int16_t out[512]; // from codec to card
/* addresses */
uint16_t idx; // in buffers
uint32_t addr; // in card
/* pointers */
int16_t *txfer; // from pointer to card
int16_t *rxfer; // from card to pointer
/* flags */
uint8_t r:1; // sample read from in buffer
uint8_t w:1; // sample written to out buffer
uint8_t rx:1;
uint8_t tx:1;
} sd = {
.addr = 10000
};
int main(void)
{
///////////////////////// INIT STUFF /////////////////////////
pll_setup();
//enable_swo(230400);
/* TODO increase i2c frequency to 400kHz and test */
i2c_setup();
encoder_setup();
buttons_setup();
leds_setup();
sddetect_setup();
codec_send_cmd(WM8778_RESET());
/* for testing purposes */
/* enable volume control zero cross detection */
codec_send_cmd(WM8778_DAC_C1(1,0,0,0, 0b1001));
dma_channel_init(&volumes);
nvic_set_priority(NVIC_DMA2_STREAM4_IRQ, 0b11110000);
adc_setup();
if (sddetect()) {
if (sd_init() != SUCCESS)
while (1) __asm__("wfi");
}
sound_setup(&f32k);
systick_setup(tickhz);
startaudio();
///////////////////////// LOOP STUFF /////////////////////////
while (1) {
handle_sd();
leds_update();
__asm__("nop");
}
return 0;
}
void leds_update(void)
{
if (state & PLAY) {
gpio_set(GPIOB, GPIO3); // green
} else {
gpio_clear(GPIOB, GPIO3);
}
if (state & RECORD) {
gpio_set(GPIOB, GPIO10); // red
} else {
gpio_clear(GPIOB, GPIO10);
}
static uint32_t lasttick = 0;
if (tick > lasttick + heartbeat) {
lasttick += heartbeat;
gpio_toggle(GPIOA, GPIO10);
}
}
int16_t get_sample(void)
{
#if COUNTERRORS
static int over = 0;
static unsigned int atidx[512];
static unsigned int ataddr[512];
if (sd.r) {
error[ERROR_GETSAMPLE]++;
atidx[over] = sd.idx;
ataddr[over++] = sd.addr;
if (over >= 512)
over = 511;
}
#endif
sd.r = 1;
return sd.in[sd.idx];
}
void put_sample(int16_t s)
{
#if COUNTERRORS
static int overwritten = 0;
static unsigned int atidx[512];
if (sd.w) {
error[ERROR_PUTSAMPLE]++;
atidx[overwritten++] = sd.idx;
if (overwritten >= 512)
overwritten = 511;
}
#endif
sd.out[sd.idx] = s;
sd.w = 1;
}
uint8_t sd_dma_done(void)
{
return !(DMA_SCR(DMA2, DMA_STREAM3) & DMA_SxCR_EN)
|| SDIO_STA & SDIO_STA_DATAEND;
}
uint8_t card_busy(void)
{
return !gpio_get(GPIOC, GPIO8);
}
void handle_sd(void)
{
if (sd.r || sd.w) {
/* if (state && state == ((uint8_t)(sd.r << 1) | sd.w)) { */
sd.r = 0;
sd.w = 0;
sd.idx %= 512;
if (sd.idx == 0) {
if (state & PLAY) {
#if COUNTERRORS
if (sd.rxfer)
error[ERROR_RXFER]++;
#endif
sd.rxfer = sd.in + 256;
}
if (state & RECORD) {
#if COUNTERRORS
if (sd.txfer)
error[ERROR_TXFER]++;
#endif
sd.txfer = sd.out + 256;
}
/* sd.txfer = (int16_t*)((int32_t)(sd.out + 256) * !!(state & RECORD)); */
}
if (sd.idx == 256) {
if (state & PLAY) {
#if COUNTERRORS
if (sd.rxfer)
error[ERROR_RXFER]++;
#endif
sd.rxfer = sd.in;
}
/* sd.txfer = (int16_t*)((int32_t)(sd.out) * !!(state & RECORD)); */
if (state & RECORD) {
#if COUNTERRORS
if (sd.txfer)
error[ERROR_TXFER]++;
#endif
sd.txfer = sd.out;
/* handle copying data to inbuffer on first recording */
/* if (sd.addr == loop.start) { */
/* for (int i = 0; i < 256; ++i) */
/* sd.in[i] = sd.out[i]; */
/* } */
}
}
}
if (sd.rxfer && sd_dma_done() && !card_busy()) {
read_single_block((uint32_t*)sd.rxfer, sd.addr);
sd.rxfer = NULL;
sd.rx = 1;
}
if (sd.txfer && sd_dma_done() && !card_busy()) {
write_single_block((uint32_t*)sd.txfer, sd.addr);
sd.txfer = NULL;
sd.tx = 1;
}
if (state && state == ((uint8_t)(sd.rx << 1) | sd.tx)) {
/* TODO this block sometimes... */
sd.tx = 0;
sd.rx = 0;
sd.addr++;
}
if (loop.len && sd.addr == loop.start + loop.len){
if (sd.idx % 256 >= loop.end_idx) {
sd.addr = loop.start;
sd.idx = 0;
/* TODO probably send last tx buffer */
}
}
}
void startaudio(void)
{
spi_enable_rx_buffer_not_empty_interrupt(I2S2ext);
spi_enable_tx_buffer_empty_interrupt(I2S2);
}
void loop_reset(void)
{
loop.len = 0;
loop.end_idx = 0;
heartbeat = 10;
}
void sys_tick_handler(void)
{
++tick;
// update volumes
adc_start_conversion_regular(ADC1);
}
void dma2_stream4_isr(void) // VOLUMES
{
if (!dma_get_interrupt_flag(volumes.dma, volumes.stream, DMA_TCIF))
return;
dma_clear_interrupt_flags(volumes.dma, volumes.stream, DMA_TCIF);
static uint16_t oldvol[3] = {0xffff, 0xffff, 0xffff};
/* input gains and output volume */
if (abs(chanvol[2] - oldvol[2]) > 5) {
codec_send_cmd(WM8778_MASTDA(chanvol[2] >> 2,1));
oldvol[2] = (chanvol[2] >> 2) << 2;
}
if (abs(chanvol[1] - oldvol[1]) > 5) {
codec_send_cmd(WM8778_ADCR(chanvol[1] >> 2,1));
oldvol[1] = (chanvol[1] >> 2) << 2;
}
if (abs(chanvol[0] - oldvol[0] > 5)) {
codec_send_cmd(WM8778_ADCL(chanvol[0] >> 2,1));
oldvol[0] = (chanvol[0] >> 2) << 2;
}
}
void exti15_10_isr(void) // START & STOP BUTTONS
{
if (exti_get_flag_status(EXTI11)){ // START BUTTON
exti_reset_request(EXTI11);
static uint32_t laststart = 0;
if (tick >= laststart + norepeat) {
laststart = tick;
/* start stomped! */
dprintf(0, "start stomped!\n");
action |= START;
}
}
if (exti_get_flag_status(EXTI12)){ // STOP BUTTON
static uint32_t laststop = 0;
exti_reset_request(EXTI12);
if (tick >= laststop + norepeat) {
if (state == STANDBY ) {
loop_reset();
}
/* stop stomped! */
laststop = tick;
dprintf(0, "stop stomped!\n");
action |= STOP;
}
}
}
void exti2_isr(void) // MENU BUTTON
{
exti_reset_request(EXTI2);
static uint32_t lastmenu = 0;
if (tick >= lastmenu + norepeat) {
lastmenu = tick;
/* menu button pressed */
dprintf(0, "menu pressed!\n");
action |= MENU;
}
}
void spi2_isr(void) // I2S data interrupt
{
static int16_t ldata = 0, rdata = 0, data = 0;
// TX
uint32_t sr = SPI_SR(I2S2);
#if COUNTERRORS
if (sr & (SPI_SR_UDR | SPI_SR_OVR))
error[ERROR_I2S]++; // i2s error! should never happen!
#endif
if (sr & SPI_SR_TXE) { // I2S DATA TX
SPI_DR(I2S2) = data;
/* if (sr & SPI_SR_CHSIDE) { */
/* SPI_DR(I2S2) = rdata; */
/* } else { */
/* SPI_DR(I2S2) = ldata; */
/* } */
}
// RX
sr = SPI_SR(I2S2ext);
#if COUNTERRORS
if (sr & (SPI_SR_UDR | SPI_SR_OVR))
error[ERROR_I2S]++; // i2s error! should never happen!
#endif
if (sr & SPI_SR_RXNE) { // I2S has data
int16_t audio = SPI_DR(I2S2ext);
if (sr & SPI_SR_CHSIDE) {
int32_t temp = audio + ldata;
__asm__("ssat %[dst], #16, %[src]"
:[dst] "=r" (audio)
:[src] "r" (temp));
if (state & PLAY) {
temp = audio + get_sample();
__asm__("ssat %[dst], #16, %[src]" //SIGNED SATURATE
: [dst] "=r" (audio)
: [src] "r" (temp));
}
if (state & RECORD)
put_sample(audio);
if (state)
sd.idx++;
data = audio;
enum s nextstate = 0;
if (action && (nextstate = trans[state][action]) != state) {
action = 0;
// initial recording ends here
if (!loop.len && state & RECORD) {
loop.end_idx = sd.idx % 256;
/* TODO addr should be set in handle_sd */
/* when it's sure we handled the */
/* last block */
loop.len = sd.addr - loop.start;
heartbeat = 2;
}
if (nextstate == STANDBY) {
sd.addr = loop.start;
sd.idx = 0;
}
if (state & RECORD && !(nextstate & RECORD)) {
// TODO this is a hack to get around
// loop sometimes blocking upon ending
// recording
sd.txfer = 0;
sd.tx = 0;
}
state = nextstate;
if (state == PLAY && !loop.len) {
state = RECORD;
}
} else {
// action doesn't change state
// remove it either way
action = 0;
}
} else {
ldata = audio;
}
}
}