Switch from GPLv2 to GPLv2+
[fw/altos] / src / cc1111 / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20 #if HAS_PAD
21 #include <ao_pad.h>
22 #endif
23
24 /* Values from SmartRF® Studio for:
25  *
26  * Deviation:   20.507812 kHz
27  * Datarate:    38.360596 kBaud
28  * Modulation:  GFSK
29  * RF Freq:     434.549927 MHz
30  * Channel:     99.975586 kHz
31  * Channel:     0
32  * RX filter:   93.75 kHz
33  */
34
35 /*
36  * For IF freq of 140.62kHz, the IF value is:
37  *
38  * 140.62e3 / (24e6 / 2**10) = 6
39  */
40
41 #define IF_FREQ_CONTROL 6
42
43 /*
44  *  http://www.ntia.doc.gov/files/ntia/publications/84-168.pdf
45  *
46  * Necessary bandwidth for a FSK modulated signal:
47  *
48  *      bw = 2.6d + 0.55b       1.5 < m < 5.5
49  *      bw = 2.1d + 1.9b        5.5 < m < 20
50  *
51  *      b is the modulation rate in bps
52  *      d is the peak deviation (from the center)
53  *
54  *      m = 2d / b
55  *
56  * 20.5 kHz deviation 38.4kbps signal:
57  *
58  *      m = 41 / 38.4, which is < 5.5:
59  *
60  *      bw = 2.6 * 20.5 + 0.55 * 38.4 = 74.42kHz
61  *
62  *      M = 1, E = 3, bw = 75kHz
63  *
64  * 5.125 kHz deviation, 9.6kbps signal
65  *
66  *      m = 10.25 / 9.6, which is < 5.5:
67  *
68  *      bw = 2.6 * 5.125 + 0.55 * 9.6 = 18.6kHz
69  *
70  *      M = 2, E = 3, bw = 53.6kHz
71  *
72  * 1.28125kHz deviation, 2.4kbps signal
73  *
74  *      m = 2.565 / 2.4, which is < 5.5:
75  *
76  *      bw = 2.6 * 20.5 + 1.9 * 2.4 = 47.61kHz
77  *
78  *      M = 3, E = 3, bw = 53.6kHz
79  *
80  * For channel bandwidth of 93.75 kHz, the CHANBW_E and CHANBW_M values are
81  *
82  * BW = 24e6 / (8 * (4 + M) * 2 ** E)
83  *
84  * So, M = 0 and E = 3
85  */
86
87 #define CHANBW_M_384    1
88 #define CHANBW_M_96     3
89 #define CHANBW_M_24     3
90 #define CHANBW_E        3
91
92 /*
93  * For a symbol rate of 38360kBaud, the DRATE_E and DRATE_M values are:
94  *
95  * R = (256 + M) * 2** E * 24e6 / 2**28
96  *
97  * So for 38360kBaud, M is 163 and E is 10
98  */
99
100 #define DRATE_M         163
101
102 #define DRATE_E_384     10
103
104 /* For 9600 baud, M is 163 and E is 8
105  */
106
107 #define DRATE_E_96      8
108
109 /* For 2400 baud, M is 163 and E is 6
110  */
111
112 #define DRATE_E_24      6
113
114 /*
115  * For a channel deviation of 20.5kHz, the DEVIATION_E and DEVIATION_M values are:
116  *
117  * F = 24e6/2**17 * (8 + DEVIATION_M) * 2**DEVIATION_E
118  *
119  * For 20.5kHz deviation, M is 6 and E is 3
120  * For 5.125kHz deviation, M is 6 and E is 1
121  * For 1.28125kHz deviation, M is 0 and E is 0
122  */
123
124 #define DEVIATION_M_384 6
125 #define DEVIATION_E_384 3
126
127 #define DEVIATION_M_96  6
128 #define DEVIATION_E_96  1
129
130 #define DEVIATION_M_24  0
131 #define DEVIATION_E_24  0
132
133 /*
134  * For our RDF beacon, set the symbol rate to 2kBaud (for a 1kHz tone),
135  * so the DRATE_E and DRATE_M values are:
136  *
137  * M is 94 and E is 6
138  *
139  * To make the tone last for 200ms, we need 2000 * .2 = 400 bits or 50 bytes
140  */
141
142 #define RDF_DRATE_E     6
143 #define RDF_DRATE_M     94
144 #define RDF_PACKET_LEN  50
145
146 /*
147  * RDF deviation should match the normal NFM value of 5kHz
148  *
149  * M is 6 and E is 1
150  *
151  */
152
153 #define RDF_DEVIATION_M 6
154 #define RDF_DEVIATION_E 1
155
156 /* This are from the table for 433MHz */
157
158 #define RF_POWER_M30_DBM        0x12
159 #define RF_POWER_M20_DBM        0x0e
160 #define RF_POWER_M15_DBM        0x1d
161 #define RF_POWER_M10_DBM        0x34
162 #define RF_POWER_M5_DBM         0x2c
163 #define RF_POWER_0_DBM          0x60
164 #define RF_POWER_5_DBM          0x84
165 #define RF_POWER_7_DBM          0xc8
166 #define RF_POWER_10_DBM         0xc0
167
168 #define RF_POWER                RF_POWER_10_DBM
169
170 static __code uint8_t radio_setup[] = {
171         RF_PA_TABLE7_OFF,       RF_POWER,
172         RF_PA_TABLE6_OFF,       RF_POWER,
173         RF_PA_TABLE5_OFF,       RF_POWER,
174         RF_PA_TABLE4_OFF,       RF_POWER,
175         RF_PA_TABLE3_OFF,       RF_POWER,
176         RF_PA_TABLE2_OFF,       RF_POWER,
177         RF_PA_TABLE1_OFF,       RF_POWER,
178         RF_PA_TABLE0_OFF,       RF_POWER,
179
180         RF_FSCTRL1_OFF,         (IF_FREQ_CONTROL << RF_FSCTRL1_FREQ_IF_SHIFT),
181         RF_FSCTRL0_OFF,         (0 << RF_FSCTRL0_FREQOFF_SHIFT),
182
183         RF_MDMCFG3_OFF,         (DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
184         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_ON |
185                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
186                                  RF_MDMCFG2_SYNC_MODE_15_16),
187         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_EN |
188                                  RF_MDMCFG1_NUM_PREAMBLE_4 |
189                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
190         RF_MDMCFG0_OFF,         (17 << RF_MDMCFG0_CHANSPC_M_SHIFT),
191
192         RF_CHANNR_OFF,          0,
193
194         /* SmartRF says set LODIV_BUF_CURRENT_TX to 0
195          * And, we're not using power ramping, so use PA_POWER 0
196          */
197         RF_FREND0_OFF,          ((1 << RF_FREND0_LODIV_BUF_CURRENT_TX_SHIFT) |
198                                  (0 << RF_FREND0_PA_POWER_SHIFT)),
199
200         RF_FREND1_OFF,          ((1 << RF_FREND1_LNA_CURRENT_SHIFT) |
201                                  (1 << RF_FREND1_LNA2MIX_CURRENT_SHIFT) |
202                                  (1 << RF_FREND1_LODIV_BUF_CURRENT_RX_SHIFT) |
203                                  (2 << RF_FREND1_MIX_CURRENT_SHIFT)),
204
205         RF_FSCAL3_OFF,          0xE9,
206         RF_FSCAL2_OFF,          0x0A,
207         RF_FSCAL1_OFF,          0x00,
208         RF_FSCAL0_OFF,          0x1F,
209
210         RF_TEST2_OFF,           RF_TEST2_RX_LOW_DATA_RATE_MAGIC,
211         RF_TEST1_OFF,           RF_TEST1_RX_LOW_DATA_RATE_MAGIC,
212         RF_TEST0_OFF,           0x09,
213
214         /* default sync values */
215         RF_SYNC1_OFF,           0xD3,
216         RF_SYNC0_OFF,           0x91,
217
218         /* max packet length */
219         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
220                                  PKTCTRL1_APPEND_STATUS|
221                                  PKTCTRL1_ADR_CHK_NONE),
222         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_WHITE_DATA|
223                                  RF_PKTCTRL0_PKT_FORMAT_NORMAL|
224                                  RF_PKTCTRL0_CRC_EN|
225                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
226         RF_ADDR_OFF,            0x00,
227         RF_MCSM2_OFF,           (RF_MCSM2_RX_TIME_END_OF_PACKET),
228         RF_MCSM1_OFF,           (RF_MCSM1_CCA_MODE_RSSI_BELOW_UNLESS_RECEIVING|
229                                  RF_MCSM1_RXOFF_MODE_IDLE|
230                                  RF_MCSM1_TXOFF_MODE_IDLE),
231         RF_MCSM0_OFF,           (RF_MCSM0_FS_AUTOCAL_FROM_IDLE|
232                                  RF_MCSM0_MAGIC_3|
233                                  RF_MCSM0_CLOSE_IN_RX_0DB),
234         RF_FOCCFG_OFF,          (RF_FOCCFG_FOC_PRE_K_3K,
235                                  RF_FOCCFG_FOC_POST_K_PRE_K,
236                                  RF_FOCCFG_FOC_LIMIT_BW_OVER_4),
237         RF_BSCFG_OFF,           (RF_BSCFG_BS_PRE_K_2K|
238                                  RF_BSCFG_BS_PRE_KP_3KP|
239                                  RF_BSCFG_BS_POST_KI_PRE_KI|
240                                  RF_BSCFG_BS_POST_KP_PRE_KP|
241                                  RF_BSCFG_BS_LIMIT_0),
242         RF_AGCCTRL2_OFF,        (RF_AGCCTRL2_MAX_DVGA_GAIN_ALL|
243                                  RF_AGCCTRL2_MAX_LNA_GAIN_0|
244                                  RF_AGCCTRL2_MAGN_TARGET_33dB),
245         RF_AGCCTRL1_OFF,        (RF_AGCCTRL1_AGC_LNA_PRIORITY_0 |
246                                  RF_AGCCTRL1_CARRIER_SENSE_REL_THR_DISABLE |
247                                  RF_AGCCTRL1_CARRIER_SENSE_ABS_THR_0DB),
248         RF_AGCCTRL0_OFF,        (RF_AGCCTRL0_HYST_LEVEL_NONE |
249                                  RF_AGCCTRL0_WAIT_TIME_8 |
250                                  RF_AGCCTRL0_AGC_FREEZE_NORMAL |
251                                  RF_AGCCTRL0_FILTER_LENGTH_8),
252         RF_IOCFG2_OFF,          0x00,
253         RF_IOCFG1_OFF,          0x00,
254         RF_IOCFG0_OFF,          0x00,
255 };
256
257 static __code uint8_t rdf_setup[] = {
258         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
259                                  (CHANBW_M_384 << RF_MDMCFG4_CHANBW_M_SHIFT) |
260                                  (RDF_DRATE_E << RF_MDMCFG4_DRATE_E_SHIFT)),
261         RF_MDMCFG3_OFF,         (RDF_DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
262         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_OFF |
263                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
264                                  RF_MDMCFG2_SYNC_MODE_NONE),
265         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_DIS |
266                                  RF_MDMCFG1_NUM_PREAMBLE_2 |
267                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
268
269         RF_DEVIATN_OFF,         ((RDF_DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
270                                  (RDF_DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
271
272         /* packet length is set in-line */
273         RF_PKTCTRL1_OFF,        ((0 << PKTCTRL1_PQT_SHIFT)|
274                                  PKTCTRL1_ADR_CHK_NONE),
275         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_PKT_FORMAT_NORMAL|
276                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
277 };
278
279 static __code uint8_t fixed_pkt_setup[] = {
280 #if !HAS_RADIO_RATE
281         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
282                                  (CHANBW_M_384 << RF_MDMCFG4_CHANBW_M_SHIFT) |
283                                  (DRATE_E_384 << RF_MDMCFG4_DRATE_E_SHIFT)),
284 #endif
285         RF_MDMCFG3_OFF,         (DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
286         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_ON |
287                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
288                                  RF_MDMCFG2_SYNC_MODE_15_16),
289         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_EN |
290                                  RF_MDMCFG1_NUM_PREAMBLE_4 |
291                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
292
293 #if !HAS_RADIO_RATE
294         RF_DEVIATN_OFF,         ((DEVIATION_E_384 << RF_DEVIATN_DEVIATION_E_SHIFT) |
295                                  (DEVIATION_M_384 << RF_DEVIATN_DEVIATION_M_SHIFT)),
296 #endif
297
298         /* max packet length -- now set inline */
299         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
300                                  PKTCTRL1_APPEND_STATUS|
301                                  PKTCTRL1_ADR_CHK_NONE),
302         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_WHITE_DATA|
303                                  RF_PKTCTRL0_PKT_FORMAT_NORMAL|
304                                  RF_PKTCTRL0_CRC_EN|
305                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
306 };
307
308 #if HAS_RADIO_RATE
309 static __code struct {
310         uint8_t         mdmcfg4;
311         uint8_t         deviatn;
312 } packet_rate_setup[] = {
313         /* 38400 */
314         {
315                 ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
316                  (CHANBW_M_384 << RF_MDMCFG4_CHANBW_M_SHIFT) |
317                  (DRATE_E_384 << RF_MDMCFG4_DRATE_E_SHIFT)),
318                 ((DEVIATION_E_384 << RF_DEVIATN_DEVIATION_E_SHIFT) |
319                  (DEVIATION_M_384 << RF_DEVIATN_DEVIATION_M_SHIFT)),
320         },
321         /* 9600 */
322         {
323                 ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
324                  (CHANBW_M_96 << RF_MDMCFG4_CHANBW_M_SHIFT) |
325                  (DRATE_E_96 << RF_MDMCFG4_DRATE_E_SHIFT)),
326                 ((DEVIATION_E_96 << RF_DEVIATN_DEVIATION_E_SHIFT) |
327                  (DEVIATION_M_96 << RF_DEVIATN_DEVIATION_M_SHIFT)),
328         },
329         /* 2400 */
330         {
331                 ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
332                  (CHANBW_M_24 << RF_MDMCFG4_CHANBW_M_SHIFT) |
333                  (DRATE_E_24 << RF_MDMCFG4_DRATE_E_SHIFT)),
334                 ((DEVIATION_E_24 << RF_DEVIATN_DEVIATION_E_SHIFT) |
335                  (DEVIATION_M_24 << RF_DEVIATN_DEVIATION_M_SHIFT)),
336         },
337 };
338 #endif
339
340 __xdata uint8_t ao_radio_dma;
341 __xdata uint8_t ao_radio_dma_done;
342 __xdata uint8_t ao_radio_done;
343 __xdata uint8_t ao_radio_abort;
344 __xdata uint8_t ao_radio_mutex;
345
346 #if PACKET_HAS_MASTER || HAS_AES
347 #define NEED_RADIO_RSSI 1
348 #endif
349
350 #ifndef NEED_RADIO_RSSI
351 #define NEED_RADIO_RSSI 0
352 #endif
353
354 #if NEED_RADIO_RSSI
355 __xdata int8_t ao_radio_rssi;
356 #endif
357
358 void
359 ao_radio_general_isr(void) __interrupt 16
360 {
361         S1CON &= ~0x03;
362         if (RFIF & RFIF_IM_TIMEOUT) {
363                 ao_radio_recv_abort();
364                 RFIF &= ~ RFIF_IM_TIMEOUT;
365         } else if (RFIF & RFIF_IM_DONE) {
366                 ao_radio_done = 1;
367                 ao_wakeup(&ao_radio_done);
368                 RFIF &= ~RFIF_IM_DONE;
369         }
370 }
371
372 static void
373 ao_radio_set_packet(void)
374 {
375         uint8_t i;
376         for (i = 0; i < sizeof (fixed_pkt_setup); i += 2)
377                 RF[fixed_pkt_setup[i]] = fixed_pkt_setup[i+1];
378 }
379
380 static void
381 ao_radio_idle(void)
382 {
383         if (RF_MARCSTATE != RF_MARCSTATE_IDLE)
384         {
385                 do {
386                         RFST = RFST_SIDLE;
387                         ao_yield();
388                 } while (RF_MARCSTATE != RF_MARCSTATE_IDLE);
389         }
390 }
391
392 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
393
394 static void
395 ao_radio_get(uint8_t len)
396 {
397         ao_config_get();
398         ao_mutex_get(&ao_radio_mutex);
399         ao_radio_idle();
400         RF_CHANNR = 0;
401         RF_FREQ2 = (uint8_t) (ao_config.radio_setting >> 16);
402         RF_FREQ1 = (uint8_t) (ao_config.radio_setting >> 8);
403         RF_FREQ0 = (uint8_t) (ao_config.radio_setting);
404         RF_PKTLEN = len;
405 #if HAS_RADIO_RATE
406         RF_MDMCFG4 = packet_rate_setup[ao_config.radio_rate].mdmcfg4;
407         RF_DEVIATN = packet_rate_setup[ao_config.radio_rate].deviatn;
408 #endif
409 }
410
411
412 void
413 ao_radio_send(__xdata void *packet, uint8_t size) __reentrant
414 {
415         ao_radio_get(size);
416         ao_radio_done = 0;
417         ao_dma_set_transfer(ao_radio_dma,
418                             packet,
419                             &RFDXADDR,
420                             size,
421                             DMA_CFG0_WORDSIZE_8 |
422                             DMA_CFG0_TMODE_SINGLE |
423                             DMA_CFG0_TRIGGER_RADIO,
424                             DMA_CFG1_SRCINC_1 |
425                             DMA_CFG1_DESTINC_0 |
426                             DMA_CFG1_PRIORITY_HIGH);
427         ao_dma_start(ao_radio_dma);
428         RFST = RFST_STX;
429         __critical while (!ao_radio_done)
430                 ao_sleep(&ao_radio_done);
431         ao_radio_put();
432 }
433
434 uint8_t
435 ao_radio_recv(__xdata void *packet, uint8_t size, uint8_t timeout) __reentrant
436 {
437         ao_radio_abort = 0;
438         ao_radio_get(size - 2);
439         ao_dma_set_transfer(ao_radio_dma,
440                             &RFDXADDR,
441                             packet,
442                             size,
443                             DMA_CFG0_WORDSIZE_8 |
444                             DMA_CFG0_TMODE_SINGLE |
445                             DMA_CFG0_TRIGGER_RADIO,
446                             DMA_CFG1_SRCINC_0 |
447                             DMA_CFG1_DESTINC_1 |
448                             DMA_CFG1_PRIORITY_HIGH);
449         ao_dma_start(ao_radio_dma);
450         RFST = RFST_SRX;
451
452         /* Wait for DMA to be done, for the radio receive process to
453          * get aborted or for a receive timeout to fire
454          */
455         __critical while (!ao_radio_dma_done && !ao_radio_abort)
456                            if (ao_sleep_for(&ao_radio_dma_done, timeout))
457                                    break;
458
459         /* If recv was aborted, clean up by stopping the DMA engine
460          * and idling the radio
461          */
462         if (!ao_radio_dma_done) {
463                 ao_dma_abort(ao_radio_dma);
464                 ao_radio_idle();
465 #if NEED_RADIO_RSSI
466                 ao_radio_rssi = 0;
467 #endif
468         }
469 #if NEED_RADIO_RSSI
470         else
471                 ao_radio_rssi = AO_RSSI_FROM_RADIO(((uint8_t *)packet)[size - 2]);
472 #endif
473         ao_radio_put();
474         return ao_radio_dma_done;
475 }
476
477 /*
478  * Wake up a task waiting to receive a radio packet
479  * and tell them to abort the transfer
480  */
481
482 void
483 ao_radio_recv_abort(void)
484 {
485         ao_radio_abort = 1;
486         ao_wakeup(&ao_radio_dma_done);
487 }
488
489 __code ao_radio_rdf_value = 0x55;
490
491 static void
492 ao_radio_rdf_start(void)
493 {
494         uint8_t i;
495         ao_radio_abort = 0;
496         ao_radio_get(AO_RADIO_RDF_LEN);
497         ao_radio_done = 0;
498         for (i = 0; i < sizeof (rdf_setup); i += 2)
499                 RF[rdf_setup[i]] = rdf_setup[i+1];
500 }
501
502 static void
503 ao_radio_rdf_run(void)
504 {
505         ao_dma_start(ao_radio_dma);
506         RFST = RFST_STX;
507         __critical while (!ao_radio_done && !ao_radio_abort)
508                            ao_sleep(&ao_radio_done);
509         if (!ao_radio_done) {
510                 ao_dma_abort(ao_radio_dma);
511                 ao_radio_idle();
512         }
513         ao_radio_set_packet();
514         ao_radio_put();
515 }
516
517 void
518 ao_radio_rdf(void)
519 {
520         ao_radio_rdf_start();
521
522         ao_dma_set_transfer(ao_radio_dma,
523                             CODE_TO_XDATA(&ao_radio_rdf_value),
524                             &RFDXADDR,
525                             AO_RADIO_RDF_LEN,
526                             DMA_CFG0_WORDSIZE_8 |
527                             DMA_CFG0_TMODE_SINGLE |
528                             DMA_CFG0_TRIGGER_RADIO,
529                             DMA_CFG1_SRCINC_0 |
530                             DMA_CFG1_DESTINC_0 |
531                             DMA_CFG1_PRIORITY_HIGH);
532         ao_radio_rdf_run();
533 }
534
535 #define PA      0x00
536 #define BE      0x55
537
538 #define CONT_PAUSE_8    PA, PA, PA, PA, PA, PA, PA, PA
539 #define CONT_PAUSE_16   CONT_PAUSE_8, CONT_PAUSE_8
540 #define CONT_PAUSE_24   CONT_PAUSE_16, CONT_PAUSE_8
541
542 #define CONT_BEEP_8     BE, BE, BE, BE, BE, BE, BE, BE
543
544 #if AO_RADIO_CONT_PAUSE_LEN == 24
545 #define CONT_PAUSE      CONT_PAUSE_24
546 #endif
547
548 #if AO_RADIO_CONT_TONE_LEN == 8
549 #define CONT_BEEP               CONT_BEEP_8
550 #define CONT_PAUSE_SHORT        CONT_PAUSE_8
551 #endif
552
553 #define CONT_ADDR(c)    CODE_TO_XDATA(&ao_radio_cont[(3-(c)) * (AO_RADIO_CONT_PAUSE_LEN + AO_RADIO_CONT_TONE_LEN)])
554
555 __code uint8_t ao_radio_cont[] = {
556         CONT_PAUSE, CONT_BEEP,
557         CONT_PAUSE, CONT_BEEP,
558         CONT_PAUSE, CONT_BEEP,
559         CONT_PAUSE, CONT_PAUSE_SHORT,
560         CONT_PAUSE, CONT_PAUSE_SHORT,
561         CONT_PAUSE,
562 };
563
564 void
565 ao_radio_continuity(uint8_t c)
566 {
567         ao_radio_rdf_start();
568         ao_dma_set_transfer(ao_radio_dma,
569                             CONT_ADDR(c),
570                             &RFDXADDR,
571                             AO_RADIO_CONT_TOTAL_LEN,
572                             DMA_CFG0_WORDSIZE_8 |
573                             DMA_CFG0_TMODE_SINGLE |
574                             DMA_CFG0_TRIGGER_RADIO,
575                             DMA_CFG1_SRCINC_1 |
576                             DMA_CFG1_DESTINC_0 |
577                             DMA_CFG1_PRIORITY_HIGH);
578         ao_radio_rdf_run();
579 }
580
581 void
582 ao_radio_rdf_abort(void)
583 {
584         ao_radio_abort = 1;
585         ao_wakeup(&ao_radio_done);
586 }
587
588
589 /* Output carrier */
590
591 static __xdata  ao_radio_test_on;
592
593 void
594 ao_radio_test(uint8_t on)
595 {
596         if (on) {
597                 if (!ao_radio_test_on) {
598 #if HAS_MONITOR
599                         ao_monitor_disable();
600 #endif
601 #if PACKET_HAS_SLAVE
602                         ao_packet_slave_stop();
603 #endif
604 #if HAS_PAD
605                         ao_pad_disable();
606 #endif
607                         ao_radio_get(0xff);
608                         RFST = RFST_STX;
609                         ao_radio_test_on = 1;
610                 }
611         } else  {
612                 if (ao_radio_test_on) {
613                         ao_radio_idle();
614                         ao_radio_put();
615                         ao_radio_test_on = 0;
616 #if HAS_MONITOR
617                         ao_monitor_enable();
618 #endif
619 #if HAS_PAD
620                         ao_pad_enable();
621 #endif
622                 }
623         }
624 }
625
626 static void
627 ao_radio_test_cmd(void)
628 {
629         uint8_t mode = 2;
630         static __xdata radio_on;
631         ao_cmd_white();
632         if (ao_cmd_lex_c != '\n') {
633                 ao_cmd_decimal();
634                 mode = (uint8_t) ao_cmd_lex_u32;
635         }
636         mode++;
637         if ((mode & 2))
638                 ao_radio_test(1);
639         if (mode == 3) {
640                 printf ("Hit a character to stop..."); flush();
641                 getchar();
642                 putchar('\n');
643         }
644         if ((mode & 1))
645                 ao_radio_test(0);
646 }
647
648 #if AO_RADIO_REG_TEST
649 static void
650 ao_radio_set_reg(void)
651 {
652         uint8_t offset;
653         ao_cmd_hex();
654         offset = ao_cmd_lex_i;
655         if (ao_cmd_status != ao_cmd_success)
656                 return;
657         ao_cmd_hex();
658         printf("RF[%x] %x", offset, RF[offset]);
659         if (ao_cmd_status == ao_cmd_success) {
660                 RF[offset] = ao_cmd_lex_i;
661                 printf (" -> %x", RF[offset]);
662         }
663         ao_cmd_status = ao_cmd_success;
664         printf("\n");
665 }
666 #endif
667
668 __code struct ao_cmds ao_radio_cmds[] = {
669         { ao_radio_test_cmd,    "C <1 start, 0 stop, none both>\0Radio carrier test" },
670 #if AO_RADIO_REG_TEST
671         { ao_radio_set_reg,     "V <offset> <value>\0Set radio register" },
672 #endif
673         { 0,    NULL },
674 };
675
676 void
677 ao_radio_init(void)
678 {
679         uint8_t i;
680         for (i = 0; i < sizeof (radio_setup); i += 2)
681                 RF[radio_setup[i]] = radio_setup[i+1];
682         ao_radio_set_packet();
683         ao_radio_dma_done = 1;
684         ao_radio_dma = ao_dma_alloc(&ao_radio_dma_done);
685         RFIF = 0;
686         RFIM = RFIM_IM_TIMEOUT|RFIM_IM_DONE;
687         IEN2 |= IEN2_RFIE;
688         ao_cmd_register(&ao_radio_cmds[0]);
689 }