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