-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAudioUpload.cpp
More file actions
executable file
·295 lines (262 loc) · 7.63 KB
/
AudioUpload.cpp
File metadata and controls
executable file
·295 lines (262 loc) · 7.63 KB
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
/**
* @file AudioUpload.cpp
* @author Christoph Haberer
* @version 2.0
*
* @section LICENSE
*
* Copyright (C) 2011 -C-H-R-I-S-T-O-P-H- -H-A-B-E-R-E-R-
*
* http://www.hobby-roboter.de/forum/viewtopic.php?f=4&t=127
*
* v0.1 19.6.2008 C. -H-A-B-E-R-E-R- Bootloader for IR-Interface
* v1.0 03.9.2011 C. -H-A-B-E-R-E-R- Bootloader for audio signal
* v1.1 05.9.2011 C. -H-A-B-E-R-E-R- changing pin setup, comments, and exitcounter=3
* v1.2 12.5.2012 C. -H-A-B-E-R-E-R- Atmega8 Support added, java programm has to be addeptet too
* v1.3 20.5.2012 C. -H-A-B-E-R-E-R- now interrupts of user programm are working
* v1.4 05.6.2012 C. -H-A-B-E-R-E-R- signal coding changed to differential manchester code
* v2.0 13.6.2012 C. -H-A-B-E-R-E-R- setup for various MCs
*
* AudioUpload 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.
*
* AudioUpload 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 AudioUpload. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "AudioUpload.h"
void(* resetFunc) (void) = 0;
uint8_t FrameData[FRAMESIZE];
void writeEeprom(uint16_t address, uint8_t data){
Wire.beginTransmission(0x50);
Wire.write((uint8_t)(address >> 8)); // Address High Byte
Wire.write((uint8_t)(address & 0xFF)); // Address Low Byte
Wire.write(data);
Wire.endTransmission();
}
uint8_t readEeprom(uint16_t address){
uint8_t rdata = 0xFF;
Wire.beginTransmission(0x50);
Wire.write((uint8_t)(address >> 8)); // Address High Byte
Wire.write((uint8_t)(address & 0xFF)); // Address Low Byte
Wire.endTransmission();
Wire.requestFrom(0x50, 1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
int8_t tryReceive(){
beginAudioProcessing();
Serial.println("waiting for signal");
uint8_t p;
#define WAITBLINKTIME 10000
uint16_t time = WAITBLINKTIME;
uint8_t timeout = 6;
//*************** wait for toggling input pin or timeout ******************************
uint8_t exitcounter = 6;
while (1) {
if (TIMER > 100) { // timedelay ==> frequency @16MHz= 16MHz/8/100=20kHz
TIMER = 0;
time--;
if (time == 0) {
TOGGLELED;
time = WAITBLINKTIME;
timeout--;
if (timeout == 0) {
LEDOFF; // timeout,
// leave bootloader and run program
Serial.println("timeout");
exit();
return 1;
}
}
}
if (p != PINVALUE) {
p = PINVALUE;
exitcounter--;
}
if (exitcounter == 0) break; // signal received, leave this loop and go on
}
//*************** start command interpreter *************************************
LEDON;
while (1) {
if (!receiveFrame()) {
//***** error: blink fast, press reset to restart *******************
Serial.println("error");
exit();
return -1;
} else { // succeed
TOGGLELED;
switch (FrameData[COMMAND]) {
case TESTCOMMAND: // not used yet
{
//Serial.println("testcommand");
return 0;
}
break;
case RUNCOMMAND:
{
//Serial.println("runcommand");
exit();
return 0;
}
break;
case RESETCOMMAND:
{
//Serial.println("reset");
delay(1000);
wdt_enable(WDTO_2S);
for (;;);
//resetFunc();
}
break;
case WRITEEEPROM:
{
int address = (int) FrameData[PAGEINDEXLOW] + 256 * FrameData[PAGEINDEXHIGH];
address *= PAGE_MAX;
//Serial.print("writing eeprom :");
Serial.println(address);
for (int j = 0; j < PAGE_MAX; j++) {
writeEeprom(address + j, FrameData[DATAPAGESTART + j]);
delay(5);
}
boolean ok = true;
for (int j = 0; j < PAGE_MAX; j++) {
if (FrameData[DATAPAGESTART + j] != readEeprom(address + j)) {
ok = false;
}
}
//Serial.println(ok);
exit();
return 0;
}
break;
}
FrameData[COMMAND] = NOCOMMAND; // delete command
}
}
}
uint8_t receiveFrame(){
uint16_t store[16];
uint16_t counter = 0;
volatile uint16_t time = 0;
volatile uint16_t delayTime;
uint8_t p, t;
uint8_t k = 8;
uint8_t dataPointer = 0;
uint16_t n;
//*** synchronisation and bit rate estimation **************************
time = 0;
// wait for edge
p = PINVALUE;
while (p == PINVALUE);
p = PINVALUE;
TIMER = 0; // reset timer
for (n = 0; n < 16; n++) {
// wait for edge
while (p == PINVALUE);
t = TIMER;
TIMER = 0; // reset timer
p = PINVALUE;
store[counter++] = t;
if (n >= 8)time += t; // time accumulator for mean period calculation only the last 8 times are used
}
delayTime = time * 3 / 4 / 8;
// delay 3/4 bit
while (TIMER < delayTime);
//p=1;
//****************** wait for start bit ***************************
while (p == PINVALUE) { // while not startbit ( no change of pinValue means 0 bit )
// wait for edge
while (p == PINVALUE);
p = PINVALUE;
TIMER = 0;
// delay 3/4 bit
while (TIMER < delayTime);
TIMER = 0;
counter++;
}
p = PINVALUE;
//****************************************************************
//receive data bits
k = 8;
for (n = 0; n < (FRAMESIZE * 8); n++) {
// wait for edge
while (p == PINVALUE);
TIMER = 0;
p = PINVALUE;
// delay 3/4 bit
while (TIMER < delayTime);
t = PINVALUE;
counter++;
FrameData[dataPointer] = FrameData[dataPointer] << 1;
if (p != t) FrameData[dataPointer] |= 1;
p = t;
k--;
if (k == 0) {
dataPointer++;
k = 8;
};
}
uint16_t crc = (uint16_t)FrameData[CRCLOW] + FrameData[CRCHIGH] * 256;
#ifdef ARDUINO_DEBUG
//********************** debug ********************************
for (n = 0; n < 10; n++) {
Serial.println(store[n]);
}
Serial.print("mean time:");
Serial.println(time / 8);
Serial.print("3/4 time:");
Serial.println(delayTime);
Serial.print("counter:");
Serial.println(counter);
Serial.println("*********************");
for (n = 0; n < FRAMESIZE; n++) {
Serial.print(FrameData[n], HEX);
Serial.print(" ");
Serial.println((int)FrameData[n]);
}
Serial.println("*********************");
Serial.print("COMMAND:");
Serial.println((int)FrameData[COMMAND]);
Serial.print("PAGEINDEX:");
Serial.println((int) FrameData[PAGEINDEXLOW] + 256 * FrameData[PAGEINDEXHIGH]);
Serial.print("CRC:");
Serial.println(crc, HEX);
Serial.println("*********************");
#endif
return (crc == 0x55AA);
}
void beginAudioProcessing(){
//Serial.println(INPUTAUDIOPIN);
// Timer 2 normal mode, clk/8, count up from 0 to 255
// ==> frequency @16MHz= 16MHz/8/256=7812.5Hz
#ifdef ATMEGA8_MICROCONTROLLER
TCCR2 = _BV(CS21);
#endif
#ifdef ATMEGA168_MICROCONTROLLER
TCCR2B = _BV(CS21);
#endif
}
void exit(){
// reintialize registers to default
//DDRB=0;
//DDRC=0;
//DDRD=0;
//cli();
#ifdef ATMEGA8_MICROCONTROLLER
TCCR2 = 0; // turn off timer2
// make sure that interrupt vectors point to user space
GICR = (1 << IVSEL);
GICR = 0;
#endif
#ifdef ATMEGA168_MICROCONTROLLER
TCCR2B = 0; // turn off timer2
#endif
}