forked from pilight/pilight_firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pilight_firmware.c
executable file
·260 lines (229 loc) · 4.4 KB
/
pilight_firmware.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
/* attiny45 - 433 MHz prefilter - V 3.0 - written by mercuri0 & CurlyMo */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/wdt.h>
#define bool _Bool
#define true 1
#define false 0
#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
#define SET(a,b) a |= _BV(b)
#define CLEAR(a,b) a &= ~_BV(b)
#define HIGH(a,b) SET(a,b)
#define LOW(a,b) CLEAR(a,b)
#define GET(a,b) a & _BV(b)
#define TOGGLE(a,b) a ^= _BV(b)
#define SET_OUTPUT(a,b) a |= _BV(b)
#define SET_INPUT(a,b) a &= ~_BV(b)
#define D_PORT DDRB
#define D_PIN(a) PRIMITIVE_CAT(DDB,a)
#define V_PORT PORTB
#define V_PIN(a) PRIMITIVE_CAT(PORTB,a)
#define REC_OUT 4
#define PI_IN 3
#define MIN_PULSELENGTH 8 //tested to work down to 30us pulsewidth (=2)
#define MAX_PULSELENGTH 1600
#define PLSLEN 183
#define REPEATS 2
#define VERSION 3
volatile uint16_t ten_us_counter = 0;
volatile unsigned long ten_us_counter1 = 0;
volatile uint8_t valid_buffer = 0x00;
volatile uint8_t checksum = 0;
volatile uint16_t bit = 0;
volatile uint8_t state = 0;
volatile uint8_t lsb = 0;
volatile uint8_t nrrepeat = 0;
volatile uint16_t _version = 0;
volatile uint16_t _minplslen = 0;
volatile uint16_t _maxplslen = 0;
volatile uint8_t _chksum = 0;
void get_mcusr(void) __attribute__((naked)) __attribute__((section(".init3")));
void get_mcusr(void) {
MCUSR = 0;
wdt_disable();
}
void init_system(void){
cli();
SET(TCCR1, CS12);
SET(TCCR1, CTC1);
OCR1A = OCR1C = 0x14;
SET(TIMSK, OCIE1A);
SET(PCMSK, PCINT4);
SET(MCUCR, ISC00);
SET(GIMSK, PCIE);
SET_OUTPUT(D_PORT, D_PIN(PI_IN));
CLEAR(V_PORT, V_PIN(PI_IN));
sei();
power_adc_disable();
power_usi_disable();
power_timer0_disable();
wdt_enable(WDTO_4S);
int hpf = MAX_PULSELENGTH;
int lpf = MIN_PULSELENGTH;
int ver = VERSION;
while(hpf > 10) hpf /= 10;
while(lpf > 10) lpf /= 10;
while(ver > 10) ver /= 10;
checksum = ((ver&0xf)+(lpf&0xf)+(hpf&0xf))&0xf;
_chksum = checksum;
}
int main (void)
{
init_system();
while(1) {
asm volatile("NOP");
}
}
void send() {
TOGGLE(V_PORT, V_PIN(PI_IN));
ten_us_counter1 = 0;
state ^= 1;
}
void send1(int len) {
if(ten_us_counter1 > len) {
send();
bit++;
}
}
void send2(int len) {
if(ten_us_counter1 > len) {
send();
lsb++;
}
}
void shift(int a) {
lsb = 0;
bit++;
switch(a) {
case 1:
_version >>= 1;
break;
case 2:
_minplslen >>= 1;
break;
case 3:
_maxplslen >>= 1;
break;
case 4:
_chksum >>= 1;
break;
}
}
void reset() {
_version = VERSION;
_minplslen = MIN_PULSELENGTH;
_maxplslen = MAX_PULSELENGTH;
_chksum = checksum;
bit = 0;
ten_us_counter1 = 0;
lsb = 0;
}
void sendHigh() {
if(lsb == 1) {
send2((PLSLEN*3)/10);
} else {
send2(PLSLEN/10);
}
}
void sendLow() {
if(lsb == 3) {
send2((PLSLEN*3)/10);
} else {
send2(PLSLEN/10);
}
}
ISR(TIMER1_COMPA_vect){
wdt_reset();
ten_us_counter++;
ten_us_counter1++;
if(nrrepeat >= REPEATS && ten_us_counter1 >= 6000000) {
reset();
if(state == 1) {
send();
}
nrrepeat = 0;
}
if(nrrepeat < REPEATS) {
cli();
if(bit < 56) {
if(bit < 2) {
if(state == 0) {
send1(PLSLEN/10);
} else {
send1((PLSLEN*6)/10);
}
} else if(bit < 18) {
if((_version&0x0000000000000001) == 1) {
sendHigh();
} else {
sendLow();
}
if(lsb == 4) {
shift(1);
}
} else if(bit < 34) {
if((_minplslen&0x0000000000000001) == 1) {
sendHigh();
} else {
sendLow();
}
if(lsb == 4) {
shift(2);
}
} else if(bit < 50) {
if((_maxplslen&0x0000000000000001) == 1) {
sendHigh();
} else {
sendLow();
}
if(lsb == 4) {
shift(3);
}
} else if(bit < 54) {
if((_chksum&0x0001) == 1) {
sendHigh();
} else {
sendLow();
}
if(lsb == 4) {
shift(4);
}
} else {
if(state == 0) {
send1(PLSLEN/10);
} else {
send1((PLSLEN*34)/10);
}
}
} else {
nrrepeat++;
reset();
if(state == 1) {
send();
}
}
} else {
sei();
}
}
ISR(PCINT0_vect){
cli();
valid_buffer <<= 1;
if(ten_us_counter > MIN_PULSELENGTH)
{
if(ten_us_counter < MAX_PULSELENGTH)
{
valid_buffer |= 0x01;
if (valid_buffer == 0xFF)
{
state ^= 1;
TOGGLE(V_PORT, V_PIN(PI_IN));
}
}
}
ten_us_counter = 0;
TCNT1 = 0;
sei();
}