-
Notifications
You must be signed in to change notification settings - Fork 15
/
pilight_usb_nano.c
367 lines (319 loc) · 7.77 KB
/
pilight_usb_nano.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
/*
Copyright (C) 2015 CurlyMo
This file is part of pilight.
pilight 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.
pilight 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 pilight. If not, see <http://www.gnu.org/licenses/>
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#define BUFFER_SIZE 256
#define MAX_PULSE_TYPES 10
#define BAUD 57600
/* Number devided by 10 */
#define MIN_PULSELENGTH 8 //tested to work down to 30us pulsewidth (=2)
#define MAX_PULSELENGTH 1600
#define VERSION 1
volatile uint32_t minrawlen = 1000;
volatile uint32_t maxrawlen = 0;
volatile uint32_t mingaplen = 10000;
volatile uint32_t maxgaplen = 5100;
const uint16_t MIN_2X_BAUD = F_CPU / (4 * (2 * 0XFFF + 1)) + 1;
#include <util/setbaud.h>
// Code formatting meant for sending
// on c:102020202020202020220202020020202200202200202020202020220020202203;p:279,2511,1395,9486;r:5@
// off c:102020202020202020220202020020202200202200202020202020202020202203;p:279,2511,1395,9486;r:5@
// Code formatting outputted by receiver
// on c:102020202020202020220202020020202200202200202020202020220020202203;p:279,2511,1395,9486@
// off c:102020202020202020220202020020202200202200202020202020202020202203;p:279,2511,1395,9486@
char data[BUFFER_SIZE];
volatile unsigned long ten_us_counter1 = 0;
volatile uint16_t ten_us_counter = 0, codes[BUFFER_SIZE], plstypes[MAX_PULSE_TYPES];
volatile uint8_t state = 0, codelen = 0, repeats = 0, pos = 0;
volatile uint8_t valid_buffer = 0x00, r = 0, q = 0, rawlen = 0, nrpulses = 0;
void initUART(void) {
uint16_t x = 0;
if((F_CPU != 16000000UL || BAUD != 57600) && BAUD > MIN_2X_BAUD) {
UCSR0A = 1 << U2X0;
x = (F_CPU / 4 / BAUD - 1) / 2;
} else {
UCSR0A = 0;
x = (F_CPU / 8 / BAUD - 1) / 2;
}
UBRR0H = x >> 8;
UBRR0L = x;
UCSR0B |= _BV(RXEN0);
UCSR0B |= _BV(RXCIE0);
UCSR0B |= _BV(TXEN0);
UCSR0C |= _BV(USBS0);
UCSR0C |= _BV(UCSZ01) | _BV(UCSZ00);
}
/* From the Arduino library */
void delayMicroseconds(unsigned int us) {
if(--us == 0)
return;
us <<= 2;
us -= 2;
__asm__ __volatile__ (
"1: sbiw %0,1" "\n\t" // 2 cycles
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
);
}
uint8_t getByte(void) {
/* Wait for data to be buffer */
while(!(UCSR0A & (1 << RXC0)));
return (uint8_t)UDR0;
}
void putByte(unsigned char data) {
/* Wait for empty transmit buffer */
while(!(UCSR0A & (1 << UDRE0)));
UDR0 = (unsigned char) data;
}
/*! \brief Writes an ASCII string to the TX buffer */
void writeString(char *line) {
while(*line != '\0') {
putByte(*line);
++line;
}
}
char *readString(void) {
static char rxstr[32];
static char *temp;
temp = rxstr;
while((*temp = getByte()) != '\n') {
++temp;
}
return rxstr;
}
void setup() {
uint8_t oldSREG = SREG;
cli();
/* We initialize our array to zero
* here to spare resources while
* running.
*/
for(r=0;r<MAX_PULSE_TYPES;r++) {
plstypes[r] = 0;
}
ADCSRA &= ~_BV(ADEN);
ACSR = _BV(ACD);
DIDR0 = 0x3F;
DIDR1 |= _BV(AIN1D) | _BV(AIN0D);
power_twi_disable();
power_spi_disable();
power_timer0_disable();
power_timer1_disable();
//power_timer2_disable();
DDRD |= _BV(DDD5);
DDRB |= _BV(DDB5);
SREG = oldSREG;
// TIMER = (F_CPU / PRESCALER)
// OCR = ((F_CPU / PRESCALER) * SECONDS) - 1
OCR2A = 0x13;
TIMSK2 |= _BV(OCIE2A);
TCCR2A = TCCR2A | (1 << WGM21);
TCCR2B = TCCR2B | (1 << CS21);
PCMSK2 |= _BV(PCINT18);
PCICR |= _BV(PCIE2);
initUART();
sei();
}
/* Everything is parsed on-the-fly to preserve memory */
void receive() {
unsigned int scode = 0, spulse = 0, srepeat = 0, sstart = 0;
unsigned int i = 0, s = 0, z = 0, x = 0;
nrpulses = 0;
z = strlen(data);
for(i = 0; i < z; i++) {
if(data[i] == 's') {
sstart = i + 2;
break;
}
if(data[i] == 'c') {
scode = i + 2;
}
if(data[i] == 'p') {
spulse = i + 2;
}
if(data[i] == 'r') {
srepeat = i + 2;
}
if(data[i] == ';') {
data[i] = '\0';
}
}
/*
* Tune the firmware with pilight-daemon values
*/
if(sstart > 0) {
z = strlen(&data[sstart]);
s = sstart;
x = 0;
for(i = sstart; i < sstart + z; i++) {
if(data[i] == ',') {
data[i] = '\0';
if(x == 0) {
minrawlen = atol(&data[s]);
}
if(x == 1) {
maxrawlen = atol(&data[s]);
}
if(x == 2) {
mingaplen = atoi(&data[s])/10;
}
x++;
s = i+1;
}
}
if(x == 3) {
maxgaplen = atol(&data[s])/10;
}
/*
* Once we tuned our firmware send back our settings + fw version
*/
sprintf(data, "v:%lu,%lu,%lu,%lu,%d,%d,%d@", minrawlen, maxrawlen, mingaplen*10, maxgaplen*10, VERSION, MIN_PULSELENGTH, MAX_PULSELENGTH);
writeString(data);
} else if(scode > 0 && spulse > 0 && srepeat > 0) {
z = strlen(&data[spulse]);
s = spulse;
nrpulses = 0;
for(i = spulse; i < spulse + z; i++) {
if(data[i] == ',') {
data[i] = '\0';
plstypes[nrpulses++] = atoi(&data[s]);
s = i+1;
}
}
plstypes[nrpulses++] = atoi(&data[s]);
codelen = strlen(&data[scode]);
repeats = atoi(&data[srepeat]);
cli();
for(i=0;i<repeats;i++) {
for(z = scode; z < scode + codelen; z++) {
PORTD ^= _BV(PORTD5);
delayMicroseconds(plstypes[data[z] - '0']);
}
}
PORTD &= ~_BV(PORTD5);
for(r=0;r<MAX_PULSE_TYPES;r++) {
plstypes[r] = 0;
}
q = 0;
sei();
}
}
ISR(USART_RX_vect) {
char c = UDR0;
data[q++] = c;
if(c == '@') {
data[q++] = '\0';
receive();
q = 0;
}
}
/* Our main timer */
ISR(TIMER2_COMPA_vect) {
cli();
ten_us_counter++;
ten_us_counter1++;
if(ten_us_counter1 > 100000) {
putByte('\n');
ten_us_counter1 = 0;
}
sei();
}
void broadcast() {
int i = 0, x = 0, match = 0, p = 0;
putByte('c');
putByte(':');
for(i=0;i<nrpulses;i++) {
match = 0;
for(x=0;x<MAX_PULSE_TYPES;x++) {
/* We device these numbers by 10 to normalize them a bit */
if(((plstypes[x]/10)-(codes[i]/10)) <= 2) {
/* Every 'space' is followed by a 'pulse'.
* All spaces are stripped to spare
* resources. The spaces can easily be
* added afterwards.
*/
if((i%2) == 1) {
/* Write numbers */
putByte(48+x);
}
match = 1;
break;
}
}
if(match == 0) {
plstypes[p++] = codes[i];
/* See above */
if((i%2) == 1) {
putByte(48+p-1);
}
}
}
putByte(';');
putByte('p');
putByte(':');
for(i=0;i<p;i++) {
itoa(plstypes[i]*10, data, 10);
writeString(data);
if(i+1 < p) {
putByte(',');
}
plstypes[i] = 0;
}
putByte('@');
nrpulses = 0;
}
ISR(PCINT2_vect){
cli();
/* We first do some filtering (same as pilight BPF) */
if(ten_us_counter > MIN_PULSELENGTH) {
if(ten_us_counter < MAX_PULSELENGTH) {
/* All codes are buffered */
codes[nrpulses++] = ten_us_counter;
if(nrpulses > BUFFER_SIZE) {
nrpulses = 0;
}
/* Let's match footers */
if(ten_us_counter > mingaplen) {
/* Only match minimal length pulse streams */
if(nrpulses >= minrawlen && nrpulses <= maxrawlen) {
/*
* Sending pulses over serial requires
* a lot of cpu ticks. We therefor have
* to be sure that we send valid codes.
* Therefor, only streams we at least
* received twice communicated.
*/
if(rawlen == nrpulses) {
broadcast();
}
rawlen = nrpulses;
}
nrpulses = 0;
}
}
}
ten_us_counter = 0;
TCNT1 = 0;
sei();
}
int main(void) {
setup();
while(1);
}