-
Notifications
You must be signed in to change notification settings - Fork 38
/
hardware.c
314 lines (301 loc) · 5.5 KB
/
hardware.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
/* Control a cc1110 for sub-ghz RF comms over uart. */
#include <stdint.h>
#include "hardware.h"
#include "serial.h"
#include "radio.h"
#include "timer.h"
#include "commands.h"
static LEDMode green_mode = 0;
static LEDMode blue_mode = 0;
mode_registers __xdata tx_registers;
mode_registers __xdata rx_registers;
void init_leds() {
// init LEDS
HARDWARE_LED_INIT; // see hardware.h
GREEN_LED_PIN = 0;
BLUE_LED_PIN = 0;
led_set_mode(GreenLED, LEDModeOff);
led_set_mode(BlueLED, LEDModeOff);
}
void led_set_mode(LEDNumber led, LEDMode new_mode)
{
if(led == GreenLED){
green_mode = new_mode;
if(new_mode == LEDModeOn) {
GREEN_LED_PIN = LEDStateOn;
} else if (new_mode == LEDModeOff) {
GREEN_LED_PIN = LEDStateOff;
}
}
else if(led == BlueLED){
blue_mode = new_mode;
if(new_mode == LEDModeOn) {
BLUE_LED_PIN = LEDStateOn;
} else if (new_mode == LEDModeOff) {
BLUE_LED_PIN = LEDStateOff;
}
}
}
void led_set_diagnostic(LEDNumber led, LEDState state)
{
if(led == GreenLED){
if(green_mode == LEDModeDiagnostic){
GREEN_LED_PIN = state;
}
}
else if(led == BlueLED){
if(blue_mode == LEDModeDiagnostic){
BLUE_LED_PIN = state;
}
}
}
uint8_t get_register(uint8_t addr) {
uint8_t value;
addr = serial_rx_byte();
switch(addr) {
case 0x00:
value = SYNC1;
break;
case 0x01:
value = SYNC0;
break;
case 0x02:
value = PKTLEN;
break;
case 0x03:
value = PKTCTRL1;
break;
case 0x04:
value = PKTCTRL0;
break;
case 0x05:
value = ADDR;
break;
case 0x06:
value = CHANNR;
break;
case 0x07:
value = FSCTRL1;
break;
case 0x08:
value = FSCTRL0;
break;
case 0x09:
value = FREQ2;
break;
case 0x0A:
value = FREQ1;
break;
case 0x0B:
value = FREQ0;
break;
case 0x0C:
value = MDMCFG4;
break;
case 0x0D:
value = MDMCFG3;
break;
case 0x0E:
value = MDMCFG2;
break;
case 0x0F:
value = MDMCFG1;
break;
case 0x10:
value = MDMCFG0;
break;
case 0x11:
value = DEVIATN;
break;
case 0x12:
value = MCSM2;
break;
case 0x13:
value = MCSM1;
break;
case 0x14:
value = MCSM0;
break;
case 0x15:
value = FOCCFG;
break;
case 0x16:
value = BSCFG;
break;
case 0x17:
value = AGCCTRL2;
break;
case 0x18:
value = AGCCTRL1;
break;
case 0x19:
value = AGCCTRL0;
break;
case 0x1A:
value = FREND1;
break;
case 0x1B:
value = FREND0;
break;
case 0x1C:
value = FSCAL3;
break;
case 0x1D:
value = FSCAL2;
break;
case 0x1E:
value = FSCAL1;
break;
case 0x1F:
value = FSCAL0;
break;
case 0x24:
value = TEST1;
break;
case 0x25:
value = TEST0;
break;
case 0x2D:
value = PA_TABLE1;
break;
case 0x2E:
value = PA_TABLE0;
break;
default:
value = 0x5A;
}
return value;
}
uint8_t set_register(uint8_t addr, uint8_t value) {
uint8_t rval;
rval = RESPONSE_CODE_SUCCESS;
switch(addr) {
case 0x00:
SYNC1 = value;
break;
case 0x01:
SYNC0 = value;
break;
case 0x02:
PKTLEN = value;
break;
case 0x03:
PKTCTRL1 = value;
break;
case 0x04:
PKTCTRL0 = value;
break;
case 0x05:
ADDR = value;
break;
case 0x06:
CHANNR = value;
break;
case 0x07:
FSCTRL1 = value;
break;
case 0x08:
FSCTRL0 = value;
break;
case 0x09:
FREQ2 = value;
break;
case 0x0A:
FREQ1 = value;
break;
case 0x0B:
FREQ0 = value;
break;
case 0x0C:
MDMCFG4 = value;
break;
case 0x0D:
MDMCFG3 = value;
break;
case 0x0E:
MDMCFG2 = value;
break;
case 0x0F:
MDMCFG1 = value;
break;
case 0x10:
MDMCFG0 = value;
break;
case 0x11:
DEVIATN = value;
break;
case 0x12:
MCSM2 = value;
break;
case 0x13:
MCSM1 = value;
break;
case 0x14:
MCSM0 = value;
break;
case 0x15:
FOCCFG = value;
break;
case 0x16:
BSCFG = value;
break;
case 0x17:
AGCCTRL2 = value;
break;
case 0x18:
AGCCTRL1= value;
break;
case 0x19:
AGCCTRL0 = value;
break;
case 0x1A:
FREND1 = value;
break;
case 0x1B:
FREND0 = value;
break;
case 0x1C:
FSCAL3 = value;
break;
case 0x1D:
FSCAL2 = value;
break;
case 0x1E:
FSCAL1 = value;
break;
case 0x1F:
FSCAL0 = value;
break;
case 0x24:
TEST1 = value;
break;
case 0x25:
TEST0 = value;
break;
case 0x2D:
PA_TABLE1 = value;
break;
case 0x2E:
PA_TABLE0 = value;
break;
default:
rval = RESPONSE_CODE_PARAM_ERROR;
}
return rval;
}
void mode_registers_clear(mode_registers *mode) {
mode->count = 0;
}
void mode_registers_add(mode_registers *mode, uint8_t addr, uint8_t value) {
if (mode->count < MAX_MODE_REGISTERS - 1) {
mode->registers[mode->count].addr = addr;
mode->registers[mode->count].value = value;
mode->count++;
}
}
void mode_registers_enact(mode_registers const *mode) {
int i;
for (i=0; i<mode->count; i++) {
set_register(mode->registers[i].addr, mode->registers[i].value);
}
}