]> git.gag.com Git - fw/altos/blob - src/ao_radio.c
altos: Write a few pre-launch samples for Tm/Tn devices
[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 fixed_pkt_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 -- now set inline */
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 __xdata uint8_t ao_radio_dma;
247 __xdata uint8_t ao_radio_dma_done;
248 __xdata uint8_t ao_radio_done;
249 __xdata uint8_t ao_radio_abort;
250 __xdata uint8_t ao_radio_mutex;
251
252 void
253 ao_radio_general_isr(void) __interrupt 16
254 {
255         S1CON &= ~0x03;
256         if (RFIF & RFIF_IM_TIMEOUT) {
257                 ao_radio_recv_abort();
258                 RFIF &= ~ RFIF_IM_TIMEOUT;
259         } else if (RFIF & RFIF_IM_DONE) {
260                 ao_radio_done = 1;
261                 ao_wakeup(&ao_radio_done);
262                 RFIF &= ~RFIF_IM_DONE;
263         }
264 }
265
266 void
267 ao_radio_set_packet(void)
268 {
269         uint8_t i;
270         for (i = 0; i < sizeof (fixed_pkt_setup); i += 2)
271                 RF[fixed_pkt_setup[i]] = fixed_pkt_setup[i+1];
272 }
273
274 void
275 ao_radio_idle(void)
276 {
277         if (RF_MARCSTATE != RF_MARCSTATE_IDLE)
278         {
279                 do {
280                         RFST = RFST_SIDLE;
281                         ao_yield();
282                 } while (RF_MARCSTATE != RF_MARCSTATE_IDLE);
283         }
284 }
285
286 void
287 ao_radio_get(uint8_t len)
288 {
289         ao_config_get();
290         ao_mutex_get(&ao_radio_mutex);
291         ao_radio_idle();
292         RF_CHANNR = ao_config.radio_channel;
293         RF_FREQ2 = (uint8_t) (ao_config.radio_cal >> 16);
294         RF_FREQ1 = (uint8_t) (ao_config.radio_cal >> 8);
295         RF_FREQ0 = (uint8_t) (ao_config.radio_cal);
296         RF_PKTLEN = len;
297 }
298
299
300 void
301 ao_radio_send(__xdata void *packet, uint8_t size) __reentrant
302 {
303         ao_radio_get(size);
304         ao_radio_done = 0;
305         ao_dma_set_transfer(ao_radio_dma,
306                             packet,
307                             &RFDXADDR,
308                             size,
309                             DMA_CFG0_WORDSIZE_8 |
310                             DMA_CFG0_TMODE_SINGLE |
311                             DMA_CFG0_TRIGGER_RADIO,
312                             DMA_CFG1_SRCINC_1 |
313                             DMA_CFG1_DESTINC_0 |
314                             DMA_CFG1_PRIORITY_HIGH);
315         ao_dma_start(ao_radio_dma);
316         RFST = RFST_STX;
317         __critical while (!ao_radio_done)
318                 ao_sleep(&ao_radio_done);
319         ao_radio_put();
320 }
321
322 uint8_t
323 ao_radio_recv(__xdata void *packet, uint8_t size) __reentrant
324 {
325         ao_radio_abort = 0;
326         ao_radio_get(size - 2);
327         ao_dma_set_transfer(ao_radio_dma,
328                             &RFDXADDR,
329                             packet,
330                             size,
331                             DMA_CFG0_WORDSIZE_8 |
332                             DMA_CFG0_TMODE_SINGLE |
333                             DMA_CFG0_TRIGGER_RADIO,
334                             DMA_CFG1_SRCINC_0 |
335                             DMA_CFG1_DESTINC_1 |
336                             DMA_CFG1_PRIORITY_HIGH);
337         ao_dma_start(ao_radio_dma);
338         RFST = RFST_SRX;
339
340         /* Wait for DMA to be done, for the radio receive process to
341          * get aborted or for a receive timeout to fire
342          */
343         __critical while (!ao_radio_dma_done && !ao_radio_abort)
344                            if (ao_sleep(&ao_radio_dma_done))
345                                    break;
346
347         /* If recv was aborted, clean up by stopping the DMA engine
348          * and idling the radio
349          */
350         if (!ao_radio_dma_done) {
351                 ao_dma_abort(ao_radio_dma);
352                 ao_radio_idle();
353         }
354         ao_radio_put();
355         return ao_radio_dma_done;
356 }
357
358 /*
359  * Wake up a task waiting to receive a radio packet
360  * and tell them to abort the transfer
361  */
362
363 void
364 ao_radio_recv_abort(void)
365 {
366         ao_radio_abort = 1;
367         ao_wakeup(&ao_radio_dma_done);
368 }
369
370 __xdata ao_radio_rdf_value = 0x55;
371
372 void
373 ao_radio_rdf(int ms)
374 {
375         uint8_t i;
376         uint8_t pkt_len;
377
378         /*
379          * Compute the packet length as follows:
380          *
381          * 2000 bps (for a 1kHz tone)
382          * so, for 'ms' milliseconds, we need
383          * 2 * ms bits, or ms / 4 bytes
384          */
385         if (ms > (255 * 4))
386                 ms = 255 * 4;
387         pkt_len = ms >> 2;
388
389         ao_radio_abort = 0;
390         ao_radio_get(pkt_len);
391         ao_radio_done = 0;
392         for (i = 0; i < sizeof (rdf_setup); i += 2)
393                 RF[rdf_setup[i]] = rdf_setup[i+1];
394
395         ao_dma_set_transfer(ao_radio_dma,
396                             &ao_radio_rdf_value,
397                             &RFDXADDR,
398                             pkt_len,
399                             DMA_CFG0_WORDSIZE_8 |
400                             DMA_CFG0_TMODE_SINGLE |
401                             DMA_CFG0_TRIGGER_RADIO,
402                             DMA_CFG1_SRCINC_0 |
403                             DMA_CFG1_DESTINC_0 |
404                             DMA_CFG1_PRIORITY_HIGH);
405         ao_dma_start(ao_radio_dma);
406         RFST = RFST_STX;
407         __critical while (!ao_radio_done && !ao_radio_abort)
408                            ao_sleep(&ao_radio_done);
409         if (!ao_radio_done) {
410                 ao_dma_abort(ao_radio_dma);
411                 ao_radio_idle();
412         }
413         ao_radio_set_packet();
414         ao_radio_put();
415 }
416
417 void
418 ao_radio_rdf_abort(void)
419 {
420         ao_radio_abort = 1;
421         ao_wakeup(&ao_radio_done);
422 }
423
424
425 /* Output carrier */
426 void
427 ao_radio_test(void)
428 {
429         uint8_t mode = 2;
430         static __xdata radio_on;
431         ao_cmd_white();
432         if (ao_cmd_lex_c != '\n') {
433                 ao_cmd_decimal();
434                 mode = (uint8_t) ao_cmd_lex_u32;
435         }
436         mode++;
437         if ((mode & 2) && !radio_on) {
438                 ao_set_monitor(0);
439                 ao_packet_slave_stop();
440                 ao_radio_get(0xff);
441                 RFST = RFST_STX;
442                 radio_on = 1;
443         }
444         if (mode == 3) {
445                 printf ("Hit a character to stop..."); flush();
446                 getchar();
447                 putchar('\n');
448         }
449         if ((mode & 1) && radio_on) {
450                 ao_radio_idle();
451                 ao_radio_put();
452                 radio_on = 0;
453         }
454 }
455
456 __code struct ao_cmds ao_radio_cmds[] = {
457         { ao_radio_test,        "C <1 start, 0 stop, none both>\0Radio carrier test" },
458         { 0,    NULL },
459 };
460
461 void
462 ao_radio_init(void)
463 {
464         uint8_t i;
465         for (i = 0; i < sizeof (radio_setup); i += 2)
466                 RF[radio_setup[i]] = radio_setup[i+1];
467         ao_radio_set_packet();
468         ao_radio_dma_done = 1;
469         ao_radio_dma = ao_dma_alloc(&ao_radio_dma_done);
470         RFIF = 0;
471         RFIM = RFIM_IM_TIMEOUT|RFIM_IM_DONE;
472         IEN2 |= IEN2_RFIE;
473         ao_cmd_register(&ao_radio_cmds[0]);
474 }