forked from pichenettes/eurorack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootloader.cc
executable file
·250 lines (219 loc) · 6.39 KB
/
bootloader.cc
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
// Copyright 2011 Emilie Gillet.
//
// 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/>.
//
// -----------------------------------------------------------------------------
//
// Bootloader supporting MIDI SysEx update.
//
// Caveat: assumes the firmware flashing is always done from first to last
// block, in increasing order. Random access flashing is not supported!
#include <avr/boot.h>
#include <avr/pgmspace.h>
#include <avr/delay.h>
#include "avrlib/gpio.h"
#include "avrlib/serial.h"
#include "avrlib/watchdog_timer.h"
#include "avr_audio_bootloader/crc32.h"
#include "avr_audio_bootloader/fsk/decoder.h"
#include "grids/hardware_config.h"
using namespace avrlib;
using namespace grids;
using namespace avr_audio_bootloader;
MidiInput midi;
Inputs inputs;
Leds leds;
Decoder decoder;
uint16_t page = 0;
uint8_t rx_buffer[SPM_PAGESIZE + 4];
void (*main_entry_point)(void) = 0x0000;
inline void Init() {
cli();
leds.set_mode(DIGITAL_OUTPUT);
inputs.set_mode(DIGITAL_INPUT);
inputs.EnablePullUpResistors();
}
void WriteBufferToFlash() {
uint16_t i;
const uint8_t* p = rx_buffer;
eeprom_busy_wait();
boot_page_erase(page);
boot_spm_busy_wait();
for (i = 0; i < SPM_PAGESIZE; i += 2) {
uint16_t w = *p++;
w |= (*p++) << 8;
boot_page_fill(page + i, w);
}
boot_page_write(page);
boot_spm_busy_wait();
boot_rww_enable();
}
void FlashLedsOk() {
for (uint8_t i = 0; i < 8; ++i) {
_delay_ms(50);
leds.Write(0xf);
_delay_ms(50);
leds.Write(LED_CLOCK);
}
}
void FlashLedsError() {
for (uint8_t i = 0; i < 5; ++i) {
_delay_ms(120);
leds.Write(0xf);
_delay_ms(120);
leds.Write(0x0);
}
}
// MIDI loader -----------------------------------------------------------------
static const uint8_t sysex_header[] = {
0xf0, // <SysEx>
0x00, 0x21, 0x02, // Mutable instruments manufacturer id.
0x00, 0x09, // Product ID for Grids.
};
enum SysExReceptionState {
MATCHING_HEADER = 0,
READING_COMMAND = 1,
READING_DATA = 2,
};
inline void LoaderLoop() {
uint8_t byte;
uint16_t bytes_read = 0;
uint16_t rx_buffer_index;
uint8_t state = MATCHING_HEADER;
uint8_t checksum;
uint8_t sysex_commands[2];
uint8_t status = 0;
uint8_t page_byte = 0;
midi.Init();
decoder.Init();
decoder.Sync();
decoder.set_packet_destination(rx_buffer);
page = 0;
TCCR2A = 0;
TCCR2B = 2;
while (1) {
leds.Write(LED_CLOCK | (LED_BD >> ((page_byte + 3) & 0x3)));
// Sample the clock input at 20kHz and feed to the FSK decoder.
if (TCNT2 >= (F_CPU / 8 / 40000 - 1)) {
TCNT2 = 0;
switch (decoder.PushSample(inputs.Read() & INPUT_CLOCK)) {
case DECODER_STATE_ERROR_SYNC:
FlashLedsError();
decoder.Sync();
break;
case DECODER_STATE_END_OF_TRANSMISSION:
return;
break;
case DECODER_STATE_PACKET_RECEIVED:
{
uint32_t crc = crc32(0, rx_buffer, SPM_PAGESIZE);
uint32_t expected_crc = \
(static_cast<uint32_t>(rx_buffer[SPM_PAGESIZE + 0]) << 24) | \
(static_cast<uint32_t>(rx_buffer[SPM_PAGESIZE + 1]) << 16) | \
(static_cast<uint32_t>(rx_buffer[SPM_PAGESIZE + 2]) << 8) | \
(static_cast<uint32_t>(rx_buffer[SPM_PAGESIZE + 3]) << 0);
if (crc == expected_crc) {
WriteBufferToFlash();
page += SPM_PAGESIZE;
++page_byte;
} else {
FlashLedsError();
}
}
decoder.Sync();
break;
default:
break;
}
}
// Poll the MIDI input.
if (midi.readable()) {
byte = midi.ImmediateRead();
if (byte > 0xf0 && byte != 0xf7) {
continue;
}
switch (state) {
case MATCHING_HEADER:
if (byte == sysex_header[bytes_read]) {
++bytes_read;
if (bytes_read == sizeof(sysex_header)) {
bytes_read = 0;
state = READING_COMMAND;
}
} else {
bytes_read = 0;
}
break;
case READING_COMMAND:
if (byte < 0x80) {
sysex_commands[bytes_read++] = byte;
if (bytes_read == 2) {
bytes_read = 0;
rx_buffer_index = 0;
checksum = 0;
state = READING_DATA;
}
} else {
state = MATCHING_HEADER;
status = 0;
bytes_read = 0;
}
break;
case READING_DATA:
if (byte < 0x80) {
if (bytes_read & 1) {
rx_buffer[rx_buffer_index] |= byte & 0xf;
if (rx_buffer_index < SPM_PAGESIZE) {
checksum += rx_buffer[rx_buffer_index];
}
++rx_buffer_index;
} else {
rx_buffer[rx_buffer_index] = (byte << 4);
}
++bytes_read;
} else if (byte == 0xf7) {
if (sysex_commands[0] == 0x7f &&
sysex_commands[1] == 0x00 &&
bytes_read == 0) {
// Reset.
return;
} else if (rx_buffer_index == SPM_PAGESIZE + 1 &&
sysex_commands[0] == 0x7e &&
sysex_commands[1] == 0x00 &&
rx_buffer[rx_buffer_index - 1] == checksum) {
// Block write.
WriteBufferToFlash();
page += SPM_PAGESIZE;
++page_byte;
} else {
FlashLedsError();
}
state = MATCHING_HEADER;
bytes_read = 0;
}
break;
}
}
}
}
int main(void) {
ResetWatchdog();
Init();
_delay_ms(40);
if (!(inputs.Read() & INPUT_SW_RESET)) {
FlashLedsOk();
LoaderLoop();
FlashLedsOk();
FlashLedsOk();
}
main_entry_point();
}