Merge branch 'buttonbox' of git://git.gag.com/fw/altos into buttonbox
[fw/altos] / src / ao_radio.c
1 /*
2  * Copyright Â© 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 /* Values from SmartRF® Studio for:
21  *
22  * Deviation:   20.507812 kHz
23  * Datarate:    38.360596 kBaud
24  * Modulation:  GFSK
25  * RF Freq:     434.549927 MHz
26  * Channel:     99.975586 kHz
27  * Channel:     0
28  * RX filter:   93.75 kHz
29  */
30
31 /*
32  * For IF freq of 140.62kHz, the IF value is:
33  *
34  * 140.62e3 / (24e6 / 2**10) = 6
35  */
36
37 #define IF_FREQ_CONTROL 6
38
39 /*
40  * For channel bandwidth of 93.75 kHz, the CHANBW_E and CHANBW_M values are
41  *
42  * BW = 24e6 / (8 * (4 + M) * 2 ** E)
43  *
44  * So, M = 0 and E = 3
45  */
46
47 #define CHANBW_M        0
48 #define CHANBW_E        3
49
50 /*
51  * For a symbol rate of 38360kBaud, the DRATE_E and DRATE_M values are:
52  *
53  * R = (256 + M) * 2** E * 24e6 / 2**28
54  *
55  * So M is 163 and E is 10
56  */
57
58 #define DRATE_E         10
59 #define DRATE_M         163
60
61 /*
62  * For a channel deviation of 20.5kHz, the DEVIATION_E and DEVIATION_M values are:
63  *
64  * F = 24e6/2**17 * (8 + DEVIATION_M) * 2**DEVIATION_E
65  *
66  * So M is 6 and E is 3
67  */
68
69 #define DEVIATION_M     6
70 #define DEVIATION_E     3
71
72 /*
73  * For our RDF beacon, set the symbol rate to 2kBaud (for a 1kHz tone),
74  * so the DRATE_E and DRATE_M values are:
75  *
76  * M is 94 and E is 6
77  *
78  * To make the tone last for 200ms, we need 2000 * .2 = 400 bits or 50 bytes
79  */
80
81 #define RDF_DRATE_E     6
82 #define RDF_DRATE_M     94
83 #define RDF_PACKET_LEN  50
84
85 /*
86  * RDF deviation should match the normal NFM value of 5kHz
87  *
88  * M is 6 and E is 1
89  *
90  */
91
92 #define RDF_DEVIATION_M 6
93 #define RDF_DEVIATION_E 1
94
95 /* This are from the table for 433MHz */
96
97 #define RF_POWER_M30_DBM        0x12
98 #define RF_POWER_M20_DBM        0x0e
99 #define RF_POWER_M15_DBM        0x1d
100 #define RF_POWER_M10_DBM        0x34
101 #define RF_POWER_M5_DBM         0x2c
102 #define RF_POWER_0_DBM          0x60
103 #define RF_POWER_5_DBM          0x84
104 #define RF_POWER_7_DBM          0xc8
105 #define RF_POWER_10_DBM         0xc0
106
107 #define RF_POWER                RF_POWER_10_DBM
108
109 static __code uint8_t radio_setup[] = {
110         RF_PA_TABLE7_OFF,       RF_POWER,
111         RF_PA_TABLE6_OFF,       RF_POWER,
112         RF_PA_TABLE5_OFF,       RF_POWER,
113         RF_PA_TABLE4_OFF,       RF_POWER,
114         RF_PA_TABLE3_OFF,       RF_POWER,
115         RF_PA_TABLE2_OFF,       RF_POWER,
116         RF_PA_TABLE1_OFF,       RF_POWER,
117         RF_PA_TABLE0_OFF,       RF_POWER,
118
119         RF_FSCTRL1_OFF,         (IF_FREQ_CONTROL << RF_FSCTRL1_FREQ_IF_SHIFT),
120         RF_FSCTRL0_OFF,         (0 << RF_FSCTRL0_FREQOFF_SHIFT),
121
122         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
123                                  (CHANBW_M << RF_MDMCFG4_CHANBW_M_SHIFT) |
124                                  (DRATE_E << RF_MDMCFG4_DRATE_E_SHIFT)),
125         RF_MDMCFG3_OFF,         (DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
126         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_OFF |
127                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
128                                  RF_MDMCFG2_SYNC_MODE_15_16_THRES),
129         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_EN |
130                                  RF_MDMCFG1_NUM_PREAMBLE_4 |
131                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
132         RF_MDMCFG0_OFF,         (17 << RF_MDMCFG0_CHANSPC_M_SHIFT),
133
134         RF_CHANNR_OFF,          0,
135
136         RF_DEVIATN_OFF,         ((DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
137                                  (DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
138
139         /* SmartRF says set LODIV_BUF_CURRENT_TX to 0
140          * And, we're not using power ramping, so use PA_POWER 0
141          */
142         RF_FREND0_OFF,          ((1 << RF_FREND0_LODIV_BUF_CURRENT_TX_SHIFT) |
143                                  (0 << RF_FREND0_PA_POWER_SHIFT)),
144
145         RF_FREND1_OFF,          ((1 << RF_FREND1_LNA_CURRENT_SHIFT) |
146                                  (1 << RF_FREND1_LNA2MIX_CURRENT_SHIFT) |
147                                  (1 << RF_FREND1_LODIV_BUF_CURRENT_RX_SHIFT) |
148                                  (2 << RF_FREND1_MIX_CURRENT_SHIFT)),
149
150         RF_FSCAL3_OFF,          0xE9,
151         RF_FSCAL2_OFF,          0x0A,
152         RF_FSCAL1_OFF,          0x00,
153         RF_FSCAL0_OFF,          0x1F,
154
155         RF_TEST2_OFF,           0x88,
156         RF_TEST1_OFF,           0x31,
157         RF_TEST0_OFF,           0x09,
158
159         /* default sync values */
160         RF_SYNC1_OFF,           0xD3,
161         RF_SYNC0_OFF,           0x91,
162
163         /* max packet length */
164         RF_PKTLEN_OFF,          sizeof (struct ao_telemetry),
165
166         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
167                                  PKTCTRL1_APPEND_STATUS|
168                                  PKTCTRL1_ADR_CHK_NONE),
169         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_WHITE_DATA|
170                                  RF_PKTCTRL0_PKT_FORMAT_NORMAL|
171                                  RF_PKTCTRL0_CRC_EN|
172                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
173         RF_ADDR_OFF,            0x00,
174         RF_MCSM2_OFF,           (RF_MCSM2_RX_TIME_END_OF_PACKET),
175         RF_MCSM1_OFF,           (RF_MCSM1_CCA_MODE_RSSI_BELOW_UNLESS_RECEIVING|
176                                  RF_MCSM1_RXOFF_MODE_IDLE|
177                                  RF_MCSM1_TXOFF_MODE_IDLE),
178         RF_MCSM0_OFF,           (RF_MCSM0_FS_AUTOCAL_FROM_IDLE|
179                                  RF_MCSM0_MAGIC_3|
180                                  RF_MCSM0_CLOSE_IN_RX_0DB),
181         RF_FOCCFG_OFF,          (RF_FOCCFG_FOC_PRE_K_3K,
182                                  RF_FOCCFG_FOC_POST_K_PRE_K,
183                                  RF_FOCCFG_FOC_LIMIT_BW_OVER_4),
184         RF_BSCFG_OFF,           (RF_BSCFG_BS_PRE_K_2K|
185                                  RF_BSCFG_BS_PRE_KP_3KP|
186                                  RF_BSCFG_BS_POST_KI_PRE_KI|
187                                  RF_BSCFG_BS_POST_KP_PRE_KP|
188                                  RF_BSCFG_BS_LIMIT_0),
189         RF_AGCCTRL2_OFF,        0x43,
190         RF_AGCCTRL1_OFF,        0x40,
191         RF_AGCCTRL0_OFF,        0x91,
192
193         RF_IOCFG2_OFF,          0x00,
194         RF_IOCFG1_OFF,          0x00,
195         RF_IOCFG0_OFF,          0x00,
196 };
197
198 static __code uint8_t rdf_setup[] = {
199         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
200                                  (CHANBW_M << RF_MDMCFG4_CHANBW_M_SHIFT) |
201                                  (RDF_DRATE_E << RF_MDMCFG4_DRATE_E_SHIFT)),
202         RF_MDMCFG3_OFF,         (RDF_DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
203         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_OFF |
204                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
205                                  RF_MDMCFG2_SYNC_MODE_15_16_THRES),
206         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_DIS |
207                                  RF_MDMCFG1_NUM_PREAMBLE_2 |
208                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
209
210         RF_DEVIATN_OFF,         ((RDF_DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
211                                  (RDF_DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
212
213         /* packet length is set in-line */
214         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
215                                  PKTCTRL1_ADR_CHK_NONE),
216         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_PKT_FORMAT_NORMAL|
217                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
218 };
219
220 static __code uint8_t telemetry_setup[] = {
221         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
222                                  (CHANBW_M << RF_MDMCFG4_CHANBW_M_SHIFT) |
223                                  (DRATE_E << RF_MDMCFG4_DRATE_E_SHIFT)),
224         RF_MDMCFG3_OFF,         (DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
225         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_OFF |
226                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
227                                  RF_MDMCFG2_SYNC_MODE_15_16_THRES),
228         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_EN |
229                                  RF_MDMCFG1_NUM_PREAMBLE_4 |
230                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
231
232         RF_DEVIATN_OFF,         ((DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
233                                  (DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
234
235         /* max packet length */
236         RF_PKTLEN_OFF,          sizeof (struct ao_telemetry),
237         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
238                                  PKTCTRL1_APPEND_STATUS|
239                                  PKTCTRL1_ADR_CHK_NONE),
240         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_WHITE_DATA|
241                                  RF_PKTCTRL0_PKT_FORMAT_NORMAL|
242                                  RF_PKTCTRL0_CRC_EN|
243                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
244 };
245
246 static __code uint8_t packet_setup[] = {
247         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
248                                  (CHANBW_M << RF_MDMCFG4_CHANBW_M_SHIFT) |
249                                  (DRATE_E << RF_MDMCFG4_DRATE_E_SHIFT)),
250         RF_MDMCFG3_OFF,         (DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
251         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_OFF |
252                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
253                                  RF_MDMCFG2_SYNC_MODE_15_16_THRES),
254         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_EN |
255                                  RF_MDMCFG1_NUM_PREAMBLE_4 |
256                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
257
258         RF_DEVIATN_OFF,         ((DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
259                                  (DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
260
261         /* max packet length */
262         RF_PKTLEN_OFF,          sizeof (struct ao_packet),
263         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
264                                  PKTCTRL1_APPEND_STATUS|
265                                  PKTCTRL1_ADR_CHK_NONE),
266         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_WHITE_DATA|
267                                  RF_PKTCTRL0_PKT_FORMAT_NORMAL|
268                                  RF_PKTCTRL0_CRC_EN|
269                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
270 };
271
272 __xdata uint8_t ao_radio_dma;
273 __xdata uint8_t ao_radio_dma_done;
274 __xdata uint8_t ao_radio_done;
275 __xdata uint8_t ao_radio_mutex;
276
277 void
278 ao_radio_general_isr(void) __interrupt 16
279 {
280         S1CON &= ~0x03;
281         if (RFIF & RFIF_IM_TIMEOUT) {
282                 ao_dma_abort(ao_radio_dma);
283                 RFIF &= ~ RFIF_IM_TIMEOUT;
284         } else if (RFIF & RFIF_IM_DONE) {
285                 ao_radio_done = 1;
286                 ao_wakeup(&ao_radio_done);
287                 RFIF &= ~RFIF_IM_DONE;
288         }
289 }
290
291 void
292 ao_radio_set_telemetry(void)
293 {
294         uint8_t i;
295         for (i = 0; i < sizeof (telemetry_setup); i += 2)
296                 RF[telemetry_setup[i]] = telemetry_setup[i+1];
297 }
298
299 void
300 ao_radio_set_packet(void)
301 {
302         uint8_t i;
303         for (i = 0; i < sizeof (packet_setup); i += 2)
304                 RF[packet_setup[i]] = packet_setup[i+1];
305 }
306
307 void
308 ao_radio_set_rdf(void)
309 {
310         uint8_t i;
311         for (i = 0; i < sizeof (rdf_setup); i += 2)
312                 RF[rdf_setup[i]] = rdf_setup[i+1];
313 }
314
315 void
316 ao_radio_idle(void)
317 {
318         if (RF_MARCSTATE != RF_MARCSTATE_IDLE)
319         {
320                 do {
321                         RFST = RFST_SIDLE;
322                         ao_yield();
323                 } while (RF_MARCSTATE != RF_MARCSTATE_IDLE);
324         }
325 }
326
327 void
328 ao_radio_get(void)
329 {
330         ao_config_get();
331         ao_mutex_get(&ao_radio_mutex);
332         ao_radio_idle();
333         RF_CHANNR = ao_config.radio_channel;
334         RF_FREQ2 = (uint8_t) (ao_config.radio_cal >> 16);
335         RF_FREQ1 = (uint8_t) (ao_config.radio_cal >> 8);
336         RF_FREQ0 = (uint8_t) (ao_config.radio_cal);
337 }
338
339
340 void
341 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant
342 {
343         ao_radio_get();
344         ao_radio_done = 0;
345         ao_dma_set_transfer(ao_radio_dma,
346                             telemetry,
347                             &RFDXADDR,
348                             sizeof (struct ao_telemetry),
349                             DMA_CFG0_WORDSIZE_8 |
350                             DMA_CFG0_TMODE_SINGLE |
351                             DMA_CFG0_TRIGGER_RADIO,
352                             DMA_CFG1_SRCINC_1 |
353                             DMA_CFG1_DESTINC_0 |
354                             DMA_CFG1_PRIORITY_HIGH);
355         ao_dma_start(ao_radio_dma);
356         RFST = RFST_STX;
357         __critical while (!ao_radio_done)
358                 ao_sleep(&ao_radio_done);
359         ao_radio_put();
360 }
361
362 uint8_t
363 ao_radio_recv(__xdata struct ao_radio_recv *radio) __reentrant
364 {
365         ao_radio_get();
366         ao_dma_set_transfer(ao_radio_dma,
367                             &RFDXADDR,
368                             radio,
369                             sizeof (struct ao_radio_recv),
370                             DMA_CFG0_WORDSIZE_8 |
371                             DMA_CFG0_TMODE_SINGLE |
372                             DMA_CFG0_TRIGGER_RADIO,
373                             DMA_CFG1_SRCINC_0 |
374                             DMA_CFG1_DESTINC_1 |
375                             DMA_CFG1_PRIORITY_HIGH);
376         ao_dma_start(ao_radio_dma);
377         RFST = RFST_SRX;
378         __critical while (!ao_radio_dma_done)
379                 ao_sleep(&ao_radio_dma_done);
380         ao_radio_put();
381         return (ao_radio_dma_done & AO_DMA_DONE);
382 }
383
384 __xdata ao_radio_rdf_running;
385 __xdata ao_radio_rdf_value = 0x55;
386
387 void
388 ao_radio_rdf(int ms)
389 {
390         uint8_t i;
391         uint8_t pkt_len;
392
393         ao_radio_get();
394         ao_radio_rdf_running = 1;
395         for (i = 0; i < sizeof (rdf_setup); i += 2)
396                 RF[rdf_setup[i]] = rdf_setup[i+1];
397
398         /*
399          * Compute the packet length as follows:
400          *
401          * 2000 bps (for a 1kHz tone)
402          * so, for 'ms' milliseconds, we need
403          * 2 * ms bits, or ms / 4 bytes
404          */
405         if (ms > (255 * 4))
406                 ms = 255 * 4;
407         pkt_len = ms >> 2;
408         RF[RF_PKTLEN_OFF] = pkt_len;
409
410         ao_dma_set_transfer(ao_radio_dma,
411                             &ao_radio_rdf_value,
412                             &RFDXADDR,
413                             pkt_len,
414                             DMA_CFG0_WORDSIZE_8 |
415                             DMA_CFG0_TMODE_SINGLE |
416                             DMA_CFG0_TRIGGER_RADIO,
417                             DMA_CFG1_SRCINC_0 |
418                             DMA_CFG1_DESTINC_0 |
419                             DMA_CFG1_PRIORITY_HIGH);
420         ao_dma_start(ao_radio_dma);
421         RFST = RFST_STX;
422
423         __critical while (!ao_radio_dma_done)
424                 ao_sleep(&ao_radio_dma_done);
425         ao_radio_rdf_running = 0;
426         ao_radio_idle();
427         for (i = 0; i < sizeof (telemetry_setup); i += 2)
428                 RF[telemetry_setup[i]] = telemetry_setup[i+1];
429         ao_radio_put();
430 }
431
432 void
433 ao_radio_abort(void)
434 {
435         ao_dma_abort(ao_radio_dma);
436         ao_radio_idle();
437 }
438
439 void
440 ao_radio_rdf_abort(void)
441 {
442         if (ao_radio_rdf_running)
443                 ao_radio_abort();
444 }
445
446
447 /* Output carrier */
448 void
449 ao_radio_test(void)
450 {
451         ao_set_monitor(0);
452         ao_packet_slave_stop();
453         ao_radio_get();
454         printf ("Hit a character to stop..."); flush();
455         RFST = RFST_STX;
456         getchar();
457         ao_radio_idle();
458         ao_radio_put();
459         putchar('\n');
460 }
461
462 __code struct ao_cmds ao_radio_cmds[] = {
463         { 'C',  ao_radio_test,  "C                                  Radio carrier test" },
464         { 0,    ao_radio_test,  NULL },
465 };
466
467 void
468 ao_radio_init(void)
469 {
470         uint8_t i;
471         for (i = 0; i < sizeof (radio_setup); i += 2)
472                 RF[radio_setup[i]] = radio_setup[i+1];
473         ao_radio_set_telemetry();
474         ao_radio_dma_done = 1;
475         ao_radio_dma = ao_dma_alloc(&ao_radio_dma_done);
476         RFIF = 0;
477         RFIM = RFIM_IM_TIMEOUT|RFIM_IM_DONE;
478         IEN2 |= IEN2_RFIE;
479         ao_cmd_register(&ao_radio_cmds[0]);
480 }