forked from arpruss/custom-brainlink-firmware
/
mainFirmware.c
719 lines (671 loc) · 44.7 KB
/
mainFirmware.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/**
Main program running on the Brainlink. Initializes subsystems, and implements the communications protocol.
Author: Tom Lauwers tlauwers@birdbraintechnologies.com
*/
// Includes all header files for libraries/drivers
#include "brainlink.h"
#define MAX_ARGUMENTS 7
uint8_t arguments[MAX_ARGUMENTS];
// Caller has the responsibility of ensuring count <= MAX_ARGUMENTS
uint8_t bt_to_buffer(uint8_t* buffer, uint8_t count) {
for (uint8_t i = 0 ; i < count ; i++) {
int x = bt_getchar_timeout_echo();
if (x == 256) {
err();
return 0;
}
buffer[i] = (uint8_t)x;
}
return count;
}
// Caller has the responsibility of ensuring count <= MAX_ARGUMENTS
uint8_t get_arguments(uint8_t count) {
return bt_to_buffer(arguments, count);
}
uint32_t get_24bit_argument(uint8_t position) {
return ((uint32_t)arguments[position] << 16) |
((uint32_t)arguments[position+1] << 8) |
((uint32_t)arguments[position+2]);
}
#define GET_16BIT_ARGUMENT(position) ( ((uint16_t)arguments[(position)] << 8) | ((uint16_t)arguments[(position)+1]) )
//uint16_t GET_16BIT_ARGUMENT(uint8_t position) {
// return ((uint16_t)arguments[position] << 8) | (uint16_t)arguments[position+1];
//}
int main(void)
{
uint8_t sensor; // Stores analog readings for transmission
int i; // multipurpose counter
char choice = 0; // Holds the top-level menu character
// int red = 0; // For the red LED intensity
// int green = 0; // For the green LED intensity
// int blue = 0; // For the blue LED intensity
uint8_t exit = 0; // Flag that gets set if we need to go back to idle mode.
unsigned int temph=0; // Temporary variable (typically stores high byte of a 16-bit int )
unsigned int templ=0; // Temporary variable (typically stores low byte of a 16-bit int)
uint8_t location = 0; // Holds the EEPROM location of a stored IR signal
// unsigned int frequency = 0; // For PWM control - PWM frequency, also used to set buzzer frequency
// int amplitude;
int channel;
unsigned int duty; // For PWM control - PWM duty cycle
int baud; // Baud rate selection for auxiliary UART
char scale; // For auxiliary UART
long int time_out=0; // Counter which counts to a preset level corresponding to roughly 1 minute
// Initialize system
// Turn off JTAG to save a bit of battery life
CCP = CCP_IOREG_gc;
MCU.MCUCR = 1;
init_clock();
init_led();
init_adc();
init_ir();
init_BT();
init_dac();
init_buzzer();
initAccel();
init_aux_uart(131, -3); // Set the auxiliary uart to 115200 8n1
EEPROM_DisableMapping();
// Enable global interrupts
sei();
// Do the following indefinitely
while(1) {
// Turn off LED
set_led(0,0,0);
// Reset flags (in case this isn't the first time through the loop)
exit = 0;
choice = 0;
time_out = 0;
stop_ir_timer();
// Sing a BL song in idle mode so you can be found. Stop as soon as you get a *
while(choice != 42) {
bt_putchar('B');
bt_putchar('L');
if (USART_RXBufferData_Available(&BT_data)) {
choice = USART_RXBuffer_GetByte(&BT_data);
if (choice == 128) {
// Something is trying to connect directly to an iRobot
set_aux_baud_rate( ROOMBA_UART_SETTINGS ); // depends on model
aux_putchar( 128); // pass through to iRobot
serial_bridge(); // currently never returns
}
else if (choice == 0) {
// Something may be trying to connect directly to a MindFlex headset
set_aux_baud_rate( 135, -2); // 57600
aux_putchar( 0); // pass through to headset
serial_bridge(); // currently never returns;
}
else {
bt_putchar(choice);
}
}
if (choice != 42)
_delay_ms(500);
}
// Active part of the program - listens for commands and responds as necessary
while(exit == 0) {
// Checks if we haven't heard anything for a long time, in which case we exit loop and go back to idle mode
time_out++;
// Corresponds to roughly 60 seconds
if(time_out > 33840000) {
exit = 1;
}
// Check for a command character
if (USART_RXBufferData_Available(&BT_data)) {
choice = USART_RXBuffer_GetByte(&BT_data);
}
else {
choice = 0;
}
// If it exists, act on command
if(choice != 0) {
// Reset the time out
time_out = 0;
// Return the command so the host knows we got it
bt_putchar(choice);
// Giant switch statement to decide what to do with the command
switch(choice) {
// Return the currect accelerometer data - X, Y, Z, and status (contains tapped and shaken bits)
case 'A':
updateAccel();
bt_putchar(_acc.x);
bt_putchar(_acc.y);
bt_putchar(_acc.z);
bt_putchar(_acc.status);
break;
// Set the buzzer
case 'B': // frequency_divider(2)
if (get_arguments(2)) {
set_buzzer(GET_16BIT_ARGUMENT(0));
}
break;
// Turn off the buzzer
case 'b':
turn_off_buzzer();
break;
// Returns the value of the light sensor
case 'L':
sensor = read_analog(LIGHT, 0);
bt_putchar(sensor);
break;
// Returns the Xmegas internal temperature read - this is undocumented because the value returned is very erratic
case 'T':
sensor = read_internal_temperature();
bt_putchar(sensor);
break;
// Returns the battery voltage
case 'V':
sensor = read_analog(BATT_VOLT, 0);
bt_putchar(sensor);
break;
// Differential ADC mode
case 'D': // active(1) numgainstages(1)
if (get_arguments(2)) {
if (arguments[0] == '0') {
adcResolution = ADC_RESOLUTION_8BIT_gc;
adcConvMode = ADC_ConvMode_Unsigned;
adcGain = ADC_DRIVER_CH_GAIN_NONE;
adcInputMode = ADC_CH_INPUTMODE_SINGLEENDED_gc;
init_adc();
}
else if (arguments[0] == '1') {
adcResolution = ADC_RESOLUTION_12BIT_gc;
adcConvMode = ADC_ConvMode_Signed;
if (arguments[1] >= '1' && arguments[1] <= '6') { // number of gain stages
adcGain = (arguments[1] - '0') << ADC_CH_GAINFAC_gp;
adcInputMode = ADC_CH_INPUTMODE_DIFFWGAIN_gc;
init_adc();
}
else if (arguments[1] == '0') {
adcGain = ADC_DRIVER_CH_GAIN_NONE;
adcInputMode = ADC_CH_INPUTMODE_DIFF_gc;
init_adc();
}
else {
err();
}
}
else {
err();
}
}
break;
// Returns the readings on all six ADC ports
case 'X':
for (i=0; i<6; i++) {
sensor = read_analog(pgm_read_byte_near(muxPosPins+i), 0);
bt_putchar(sensor);
}
break;
// Returns differential measurement on pair of ADC ports
// Assumes we are in differential mode
case 'x':
if (get_arguments(2)) {
i = read_differential(arguments[0], arguments[1]);
bt_putchar(0xFF&(i >> 8));
bt_putchar(0xFF&i);
}
break;
case 'e': // this is a bit of testing code, which will eventually be changed
// do not rely on it
if (get_arguments(2)) {
char a1 = arguments[0];
char a2 = arguments[1];
while(1) {
_delay_ms(2);
i = read_differential(a1, a2);
itoa((i<<4)>>4, (char*)arguments, 10);
for (i=0; arguments[i]; i++)
bt_putchar(arguments[i]);
bt_putchar('\r');
bt_putchar('\n');
}
}
break;
// Sets the full-color LED
case 'O': // red(1) green(1) blue(1);
if (get_arguments(3)) {
set_led(arguments[0], arguments[1], arguments[2]);
}
break;
// Switches serial between irDA and standard serial
// Currently, this works over the same port numbers as
// standard serial, instead of using the IR receiver
// and IR LED. This may change when I get the IR receiver
// and LED working reliably.
case 'J':
temph = bt_getchar_timeout_echo();
if(/*temph == 256 || */ temph < '0' || temph > '1') {
err();
break;
}
set_irda_mode(temph - '0');
break;
// Sets up the IR transmitter with signal characteristics
case 'I':
init_ir();
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
// Set the frequency of the IR carrier
robotData.frequency = ((temph)<<8) + templ;
set_ir_carrier(robotData.frequency);
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
else {
// Set the length of the start up pulses
robotData.startUpPulseLength = templ;
}
if(robotData.startUpPulseLength > 16) {
err();
break;
}
// Read in the start up pulse timing data
for(i=0; i < robotData.startUpPulseLength; i++) {
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
robotData.startUpPulse[i] = ((temph)<<8) + templ;
}
if(temph == 256 || templ == 256) {
break;
}
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
// Set the bit encoding to one of four pre-determined settings (see protocol instructions for more information)
robotData.bitEncoding = templ;
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
// Set the number of bits (and bytes) contained in an IR command
robotData.numBits = templ;
robotData.numBytes = (robotData.numBits-1)/8 + 1;
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
// Set timing data for a high bit
robotData.highBitTime = ((temph)<<8) + templ;
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
templ = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
// Set timing data for a low bit
robotData.lowBitTime = ((temph)<<8) + templ;
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
// Set timing data for on or off
robotData.offTime = ((temph)<<8) + templ;
break;
// Transmit an IR signal according to the previously determined configuration
case 'i':
init_ir();
// Get the signal data as one or more bytes
for(i = 0; i < robotData.numBytes; i++) {
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
robotData.irBytes[i] = templ;
}
if(templ == 256) {
break;
}
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
// Determine if the signal is repeated or not, and if so, with what frequency
robotData.repeatTime = ((temph)<<8) + templ;
if(robotData.repeatTime != 0) {
robotData.repeatFlag = 1;
}
else {
robotData.repeatFlag = 0;
}
// Startup timer interrupts
start_ir_timer();
break;
// Turn off any repeating IR signal
case '!':
robotData.repeatFlag = 0;
stop_ir_timer();
break;
// Capture a signal from the IR receiver
case 'R':
init_ir_read();
while(ir_read_flag!=0);
break;
// Store the captured signal in an EEPROM location
case 'S':
location = bt_getchar_timeout_echo()-48; // Subtracting 48 converts from ASCII to numeric numbers
if((location >= 0) && (location < 5) && (signal_count > 4)) {
write_data_to_eeprom(location);
}
else {
err();
}
break;
// Receive a raw IR signal over bluetooth and transmit it with the IR LED
case 's':
if(read_data_from_serial()) {
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
// Set if the signal should repeat and if so, with what frequency
robotData.repeatTime = ((temph)<<8) + templ;
if(robotData.repeatTime != 0) {
robotData.repeatFlag = 1;
}
else {
robotData.repeatFlag = 0;
}
// Set frequency to 38KHz, since raw signals must have come from the receiver at some point
robotData.frequency = 0x0349;
robotData.startUpPulseLength = 0;
robotData.bitEncoding = 0x04;
start_ir_timer();
}
else {
err();
break;
}
break;
// Get a stored signal from an EEPROM location and transmit it over the IR LED (and repeat as desired)
case 'G':
location = bt_getchar_timeout_echo()-48;
if(location >= 0 && location < 5) {
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
robotData.repeatTime = ((temph)<<8) + templ;
if(robotData.repeatTime != 0) {
robotData.repeatFlag = 1;
}
else {
robotData.repeatFlag = 0;
}
read_data_from_eeprom(location);
robotData.frequency = 0x0349;
robotData.startUpPulseLength = 0;
robotData.bitEncoding = 0x04;
start_ir_timer();
}
else {
err();
}
break;
// Get a stored signal from EEPROM and print it over bluetooth to the host
case 'g':
location = bt_getchar_timeout_echo()-48;
if(location >= 0 && location < 5) {
print_data_from_eeprom(location);
}
else {
err();
}
break;
// Output on digital I/O
case '>':
// Set port
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
// Get value
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
set_output(temph, (templ-48));
break;
// Input on digital I/O
case '<':
// Get port
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
// Get value (1 or 0)
templ = read_input(temph)+48;
bt_putchar(templ);
break;
// Configure PWM frequency
case 'P':
if (get_arguments(2))
pwm_frequency = GET_16BIT_ARGUMENT(0);
break;
// Set PWM duty cycle for a specific port
case 'p':
if (get_arguments(3)) {
set_pwm();
duty = GET_16BIT_ARGUMENT(1);
if (arguments[0] == '0')
set_pwm0(duty);
else if (arguments[1] == '1')
set_pwm1(duty);
// could add else err();, but original firmware doesn't do this
}
break;
// Set DAC voltage on one of the two DAC ports
case 'd':
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
if(temph == '0') {
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
else {
set_dac0(temph);
}
}
else if(temph == '1') {
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
else {
set_dac1(temph);
}
}
break;
// Go back to idle mode so you can be found again. Should be sent at end of host program.
case 'Q':
exit = 1;
break;
// Human-readable uart speed setting
case 'u':
temph = bt_getchar_timeout_echo();
if(temph == 256) {
err();
break;
}
templ = bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
baud = ((temph)<<8) + templ;
switch(baud) {
case ('1'<<8) + '2': // 1200
baud = 6663;
scale = -2;
break;
case ('4'<<8) + '8': // 4800
baud = 3325;
scale = -3;
break;
case ('9'<<8) + '6': // 9600
baud = 829;
scale = -2;
break;
case ('1'<<8) + '9': // 19200
baud = 825;
scale = -3;
break;
case ('3'<<8) + '8': // 38400
baud = 204;
scale = -2;
break;
case ('5'<<8) + '7': // 57600
baud = 135;
scale = -2;
break;
case ('1'<<8) + '1': // 115200
baud = 131;
scale = -3;
break;
default:
err();
goto BAUD_DONE;
}
set_aux_baud_rate(baud, scale);
BAUD_DONE:
break;
// Configures the baud rate of the auxiliary UART
case 'C': // baud(2) scale(1)
// e.g., 9600 = C\x03\x3D\xFE
if (! get_arguments(3))
break;
bt_putchar(arguments[2]); // duplicate buggy behavior from official firmware; delete if unwanted
set_aux_baud_rate( GET_16BIT_ARGUMENT(0), arguments[2]);
break;
// BT-serial high speed bridge mode
case 'Z':
serial_bridge();
break;
case 'w': // channel(1) type(1) duty(1) amplitude(1) frequency(3)
// w0s\x20\xFF\x02\x00\x00
if (!get_arguments(7))
break;
if (arguments[0] < '0' || arguments[0] > '1' || arguments[2] > WAVEFORM_SIZE) {
err();
break;
}
play_wave_dac(arguments[0]-'0', (char)arguments[1], arguments[2], arguments[3], get_24bit_argument(4));
break;
case 'W': // channel(1) frequency(3) length(1) data(length)
if (!get_arguments(5))
break;
if (arguments[0] < '0' || arguments[0] > '1' || arguments[4] > WAVEFORM_SIZE || arguments[4] == 0 ) {
err();
break;
}
channel = arguments[0] - '0';
if (bt_to_buffer(waveform[channel], arguments[4])) {
disable_waveform(channel);
play_arb_wave(channel, waveform[channel], arguments[4], get_24bit_argument(1));
}
break;
case '@':
channel = bt_getchar_timeout_echo();
if (channel == '0')
disable_waveform0();
else if (channel == '1')
disable_waveform1();
else
err();
break;
case 'r':
i = USART_RXBufferData_AvailableCount(&AUX_data);
bt_putchar((uint8_t)(i+1));
while(i-- > 0) {
bt_putchar(USART_RXBuffer_GetByte(&AUX_data));
}
break;
// Transmit a stream of characters from bluetooth to auxiliary serial
case 't':
temph= bt_getchar_timeout_echo();
if (temph == 256) {
err();
break;
}
for(; temph>0; temph--) {
templ= bt_getchar_timeout_echo();
if(templ == 256) {
err();
break;
}
else {
aux_putchar( templ);
}
}
break;
default:
break;
}
}
}
}
return 0;
}