-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
506 lines (445 loc) · 14.2 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
501
502
503
504
505
506
/*
* main.c
*
* Created: 06.11.2014 20:32:10
* Author: ole
*/
#define USE_7SEG
#define USE_RC5
#define F_CPU 32000000
#include "board.h"
#include "clksys_driver.h"
#include "usart_driver.h"
#include "twi_master_driver.h"
#include "cs3318_driver.h"
#include "midi_driver.h"
#include "rc5_driver.h"
#include "led7seg_driver.h"
#include "cli.h"
#include "preferences.h"
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>
#include <avr/wdt.h>
#include <stdio.h>
#include <string.h>
typedef struct {
bool (*taskfunc)(void);
const Task_flag_t bitmask;
} Tasklist_t;
struct Preferences_t preferences;
struct Preferences_t EEMEM eeprom_preferences = {
.vol_stepsize = 2 << 2, // in quarter-dB
.vol_startup = -20, // in dB
.vol_min = -96, // in dB
.vol_max = 22, // in dB
.ch_powerdown = { 0 }, // all on
.vol_ch_offset = { 0 }, // in Q5_2 format
};
struct Preferences_t * get_preferences(void) {
return &preferences;
}
void cmd_MasterVol(char *);
void cmd_MasterVol_help(void);
void cmd_Reboot(char *);
void cmd_Reboot_help(void);
void cmd_Debug(char *);
void cmd_Debug_help(void);
void cmd_Prefs(char *);
void cmd_Prefs_help(void);
bool rotary_task(void);
bool IR_rx_task(void);
static bool mutestate = false;
uint8_t debuglevel = 0;
inline uint8_t get_debuglevel() {
return debuglevel;
}
Task_flag_t taskflags = 0;
void set_taskflags(Task_flag_t bitmask) {
taskflags |= bitmask;
}
int main(void)
{
int i;
/* Define a list of tasks */
Tasklist_t tasklist[] =
{
{cli_task, Task_CLI_bm},
{rotary_task, Task_Rotary_bm},
{MIDI_rx_task, Task_MIDI_RX_bm},
{IR_rx_task, Task_IR_RX_bm},
{NULL, 0}
};
/* Set clock to 32MHz */
CLKSYS_Enable( OSC_RC32MEN_bm );
do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 );
CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc );
CLKSYS_Disable( OSC_RC2MEN_bm | OSC_RC32KEN_bm );
/* Initialize sleep mode to idle */
set_sleep_mode(SLEEP_SMODE_IDLE_gc);
/* Register cli command functions */
register_cli_command("help", cmd_help, cmd_help_help);
register_cli_command("iicr", cmd_iicr, cmd_iicr_help);
register_cli_command("iicw", cmd_iicw, cmd_iicw_help);
register_cli_command("vol", cmd_MasterVol, cmd_MasterVol_help);
register_cli_command("prefs", cmd_Prefs, cmd_Prefs_help);
register_cli_command("debug", cmd_Debug, cmd_Debug_help);
register_cli_command("reboot", cmd_Reboot, cmd_Reboot_help);
/* Set debug LED pin to output */
DEBUGLED_PORT.DIRSET = DEBUGLED_PIN_bm;
/* Initialize debug USART */
USART_init(&USARTD0);
/* Initialize TWI master. */
TWI_MasterInit(&TWIC, TWI_MASTER_INTLVL_LO_gc, TWI_BAUD(F_CPU, 100000));
/* Set up rotary encoder input and irq */
ROT_PORT.DIRCLR = ROT_A_PIN_bm | ROT_B_PIN_bm;
ROT_PORT.PIN0CTRL = PORT_OPC_PULLUP_gc;
ROT_PORT.PIN1CTRL = PORT_OPC_PULLUP_gc;
ROT_PORT.INTMASK = ROT_A_PIN_bm | ROT_B_PIN_bm;
ROT_PORT.INTCTRL = PORT_INTLVL_MED_gc;
PMIC.CTRL |= PMIC_MEDLVLEN_bm;
/* Enable global interrupts */
sei();
/* Read preferences from EEPROM to SRAM */
eeprom_read_block(&preferences, &eeprom_preferences, sizeof(preferences));
/* Initialize MIDI USART */
MIDI_init(&USARTC0);
#ifdef USE_RC5
/* Initialize RC5 IR reciever */
rc5_init();
#endif
/* Init CS3318 */
cs3318_init();
#ifdef USE_7SEG
/* Init 7-segment LED display */
display_init(preferences.vol_startup << 2);
#endif
/* Turn off mute transistors */
MUTE_PORT.DIRSET = MUTE_PIN_bm;
MUTE_PORT.OUTSET = MUTE_PIN_bm;
/* Ready to run */
printf_P(PSTR("\nWelcome to MCPA!\n"));
switch (RST.STATUS & 0x3F) {
case 1:
printf_P(PSTR("Power-on reset\n"));
break;
case 2:
printf_P(PSTR("External reset\n"));
break;
case 4:
printf_P(PSTR("Brownout reset\n"));
break;
case 8:
printf_P(PSTR("Watchdog reset\n"));
break;
case 16:
printf_P(PSTR("Debug reset\n"));
break;
case 32:
printf_P(PSTR("Software reset\n"));
break;
}
RST.STATUS |= 0x3F;
printf_P(PSTR("Type help for list of commands\n%s"), CLI_PROMPT);
wdt_enable(WDT_PER_512CLK_gc); //512ms timeout
while(1)
{
while (taskflags) { //run as long as any taskflag is set
i=0;
while (tasklist[i].taskfunc) { // go through all tasks
if (taskflags & tasklist[i].bitmask) { //check taskflag
if (tasklist[i].taskfunc()) { //run taskfunction
taskflags &= ~tasklist[i].bitmask; //if it returns true (done), clear the taskflag
}
}
i++;
}
}
DEBUGLED_PORT.OUTSET = DEBUGLED_PIN_bm;
wdt_reset();
}
}
void cmd_MasterVol(char * stropt)
{
int numparams;
char subcmd[10];
float vol_db = -99.0f;
q13_2 vol_int;
numparams = sscanf(stropt, "%9s %f\n", subcmd, &vol_db);
if (numparams > 2) {
printf("Unknown options\n");
cmd_MasterVol_help();
return;
}
if (numparams < 1) {
uint8_t chip = 0; //Master volume should be the same on all slave boards
q13_2 volume_in_db_x4 = cs3318_getVolReg(chip, 0x11);
printf("Master volume: %.2f dB\n", Q13_2_TO_FLOAT(volume_in_db_x4));
}
else if (!strncmp(subcmd, "up", 2)) {
vol_int = cs3318_stepMasterVol(1);
display_volume(vol_int);
MIDI_send_mastervol(vol_int);
}
else if (!strncmp(subcmd, "down", 4)) {
vol_int = cs3318_stepMasterVol(-1);
display_volume(vol_int);
MIDI_send_mastervol(vol_int);
}
else if (!strncmp(subcmd, "mute", 4)) {
mutestate = true;
cs3318_mute(numparams == 1 ? 0 : (uint8_t)vol_db, true);
display_mute();
//TODO: Send MIDI mute cmd
}
else if (!strncmp(subcmd, "unmute", 6)) {
mutestate = false;
cs3318_mute(numparams == 1 ? 0 : (uint8_t)vol_db, false);
display_volume(cs3318_getVolReg(0, 0x11));
//TODO: Send MIDI unmute cmd
}
else if (!strncmp(subcmd, "set", 3)) {
struct Preferences_t * prefs = get_preferences();
vol_int = FLOAT_TO_Q13_2(vol_db);
if (vol_int <= prefs->vol_max << 2 && vol_int >= prefs->vol_min << 2) {
DEBUG_PRINT(1, "Set mastervolume to %.2fdB in %d boards\n", vol_db, cs3318_get_nslaves());
for (uint8_t i = 0; i < cs3318_get_nslaves(); i++) {
cs3318_setVolReg(i, 0x11, vol_int);
}
display_volume(vol_int);
MIDI_send_mastervol(vol_int);
}
}
else if (!strncmp(subcmd, "offset", 6)) {
int chip, channel;
numparams = sscanf(stropt, "offset %d %f", &channel, &vol_db);
if (numparams < 1) {
for (channel = 0; channel < 8*cs3318_get_nslaves(); channel++) {
chip = channel >> 3;
printf("Offset channel %d %.2f dB\n", channel+1, Q13_2_TO_FLOAT(cs3318_getVolReg(chip, (channel & 0x7) + 1)));
}
}
else {
chip = (channel - 1) >> 3;
channel = ((channel - 1) & 0x7) + 1;
if (numparams == 1 || channel < 1 || channel > 8 || chip >= cs3318_get_nslaves()) {
printf("Invalid channel %d\n", channel);
return;
}
cs3318_setVolReg(chip, channel, FLOAT_TO_Q13_2(vol_db));
}
}
else {
printf("Unknown sub-command\n");
cmd_MasterVol_help();
}
}
void cmd_MasterVol_help()
{
printf_P(PSTR( \
"vol up\n" \
"vol down\n" \
"vol mute [channel]\n" \
"vol unmute [channel]\n" \
"vol set [master value in dB]\n" \
"vol offset [channel number >= 1] [offset value in dB]\n" \
"Example: vol set -23.75\n" \
"Example: vol offset 3 -2.5\n"));
}
static void unmute(void) {
if (mutestate) {
mutestate = false;
cs3318_mute(0, false);
}
}
bool rotary_task(void)
{
//direction: old,new:idx
//CW: 0,1:1 1,3:7 3,2:e 2,0:8
//CCW: 0,2:2 2,3:b 3,1:d 1,0:4
static const int8_t direction_table[16] = {0, 1, -1, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, -1, 1, 0};
static uint8_t current_rotaryval = 0;
uint8_t new_rotaryval = ROT_PORT.IN & 3;
int8_t direction = direction_table[current_rotaryval<<2 | new_rotaryval];
current_rotaryval = new_rotaryval;
if (direction != 0) {
q13_2 vol_int = cs3318_stepMasterVol(direction);
display_volume(vol_int);
MIDI_send_mastervol(vol_int);
}
return true;
}
ISR(PORTD_INT_vect)
{
DEBUGLED_PORT.OUTCLR = DEBUGLED_PIN_bm;
if (ROT_PORT.INTFLAGS & ROT_A_PIN_bm) {
ROT_PORT.INTFLAGS |= ROT_A_PIN_bm;
set_taskflags(Task_Rotary_bm);
}
else if (ROT_PORT.INTFLAGS & ROT_B_PIN_bm) {
ROT_PORT.INTFLAGS |= ROT_B_PIN_bm;
set_taskflags(Task_Rotary_bm);
}
rc5_irqhandler();
}
#define MAX_DEBUGLEVEL 3
void cmd_Debug(char * stropt)
{
int numparams, dbglvl;
numparams = sscanf(stropt, "%d\n", &dbglvl);
if (numparams < 1) {
printf("Debuglevel = %d\n", debuglevel);
return;
}
if (dbglvl >= 0 && dbglvl <= MAX_DEBUGLEVEL) {
debuglevel = dbglvl;
}
}
void cmd_Debug_help()
{
printf_P(PSTR( \
"debug [number]\n" \
"number is >=0 and less than %d\n"), MAX_DEBUGLEVEL);
}
void cmd_Prefs(char * stropt)
{
int numparams;
char subcmd[16];
float vol_db;
numparams = sscanf(stropt, "%15s %f\n", subcmd, &vol_db);
if (numparams > 3) {
printf("Unknown options\n");
cmd_Prefs_help();
return;
}
else if (numparams < 1) {
printf("Preferences:\n");
printf(" stepsize: %.2f dB\n", Q13_2_TO_FLOAT(preferences.vol_stepsize));
printf(" startup master: %d dB\n", preferences.vol_startup);
printf(" max limit: %d dB\n", preferences.vol_max);
printf(" min limit: %d dB\n", preferences.vol_min);
for (uint8_t i = 0; i < cs3318_get_nslaves(); i++) {
printf(" Board %d:\n", i);
printf(" Powerdown: 0x%02X\n", preferences.ch_powerdown[i]);
for (uint8_t ch = 0; ch < 8; ch++) {
printf(" Offset channel %d: %.2f dB\n", ch + 1, Q5_2_TO_FLOAT(preferences.vol_ch_offset[i * 8 + ch]));
}
}
return;
}
else if (!strncmp(subcmd, "save", 4)) {
eeprom_update_block(&preferences, &eeprom_preferences, sizeof(preferences));
}
else if (!strncmp(subcmd, "max", 3)) {
if (vol_db <= 22.0f && vol_db >= preferences.vol_min) {
preferences.vol_max = vol_db;
}
}
else if (!strncmp(subcmd, "min", 3)) {
if (vol_db <= preferences.vol_max && vol_db >= -96.0f) {
preferences.vol_min = vol_db;
}
}
else if (!strncmp(subcmd, "startup", 7)) {
DEBUG_PRINT(1, "Set startup to %.2f dB\n", vol_db);
if (vol_db <= preferences.vol_max && vol_db >= preferences.vol_min) {
preferences.vol_startup = vol_db;
}
}
else if (!strncmp(subcmd, "stepsize", 8)) {
if (vol_db <= 20.0f && vol_db > 0.0f) {
preferences.vol_stepsize = FLOAT_TO_Q13_2(vol_db);
}
}
else if (!strncmp(subcmd, "offset", 6)) {
int channel;
numparams = sscanf(stropt, "offset %d %f", &channel, &vol_db);
uint8_t chip = (channel - 1) >> 3;
channel = ((channel - 1) & 0x7) + 1;
if (numparams != 2 || channel < 1 || channel > 8 || chip >= cs3318_get_nslaves()) {
printf("Invalid channel %d\n", channel);
return;
}
if (vol_db <= 32.0f && vol_db >= -32.0f) {
preferences.vol_ch_offset[chip * 8 + channel - 1] = FLOAT_TO_Q5_2(vol_db);
}
}
else if (!strncmp(subcmd, "powerdown", 9)) {
int bitmask, chip;
numparams = sscanf(stropt, "powerdown %i %i\n", &chip, &bitmask);
if (numparams != 2 || chip > cs3318_get_nslaves()) {
printf("Invalid parameters\n");
return;
}
DEBUG_PRINT(1, "Set powerdown for board %d = 0x%02X\n", chip, bitmask);
preferences.ch_powerdown[chip - 1] = bitmask;
}
else {
printf("Unknown sub-command\n");
cmd_Prefs_help();
}
}
void cmd_Prefs_help(void)
{
printf_P(PSTR( \
"prefs save <- write values to eeprom"
"prefs max [value in dB]\n" \
"prefs min [value in dB]\n" \
"prefs startup [value in dB]\n" \
"prefs stepsize [value in 1/4 dB resolution]\n" \
"prefs offset [channel number >= 1] [value in dB <-33,32>]\n" \
"prefs powerdown [board number >= 1] [8-bit bitmask, 0 = powerdown]\n" \
"example: prefs stepsize 2.5\n" \
"example: prefs offset 7 -3.25\n"));
}
bool IR_rx_task(void)
{
static uint8_t toggle = 0;
q13_2 vol_int;
uint16_t rc5_code = get_rc5_code();
DEBUG_PRINT(2, "IR: 0x%X\n", rc5_code);
if (!(rc5_code & 0x3000)) //must have two start bits
return true;
toggle = (toggle << 1) & 0x7;
if (rc5_code & 0x0800)
toggle |= 1;
if (toggle == 4 || toggle == 3) //wait for third consecutive code before repeating
return true;
switch (rc5_code & 0x07FF) {
case 0x0010:
vol_int = cs3318_stepMasterVol(1);
unmute();
display_volume(vol_int);
MIDI_send_mastervol(vol_int);
break;
case 0x0011:
vol_int = cs3318_stepMasterVol(-1);
unmute();
display_volume(vol_int);
MIDI_send_mastervol(vol_int);
break;
case 0x000D:
mutestate = !mutestate;
cs3318_mute(0, mutestate);
mutestate ? display_mute() : display_volume(cs3318_getVolReg(0, 0x11));
//TODO: Send MIDI mute cmd
break;
case 0x000C:
DEBUG_PRINT(1, "IR Power on/off\n");
break;
default:
printf("Unknown IR code!\n");
}
return true;
}
void cmd_Reboot(char * stropt)
{
(void)*stropt;
CCPWrite( &RST.CTRL, RST_SWRST_bm ); /* Issue a Software Reset to initilize the CPU */
}
void cmd_Reboot_help()
{
printf_P(PSTR( \
"reboot\n" \
"Reboot the uC. Run before SW upgrade with avrdude\n"));
}