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