Add lots more aoview UI bits
[fw/altos] / 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 */
226         RF_PKTLEN_OFF,          RDF_PACKET_LEN,
227         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
228                                  PKTCTRL1_ADR_CHK_NONE),
229         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_PKT_FORMAT_NORMAL|
230                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
231 };
232
233 static __code uint8_t telemetry_setup[] = {
234         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
235                                  (CHANBW_M << RF_MDMCFG4_CHANBW_M_SHIFT) |
236                                  (DRATE_E << RF_MDMCFG4_DRATE_E_SHIFT)),
237         RF_MDMCFG3_OFF,         (DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
238         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_OFF |
239                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
240                                  RF_MDMCFG2_SYNC_MODE_15_16_THRES),
241         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_EN |
242                                  RF_MDMCFG1_NUM_PREAMBLE_4 |
243                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
244
245         RF_DEVIATN_OFF,         ((DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
246                                  (DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
247
248         /* max packet length */
249         RF_PKTLEN_OFF,          sizeof (struct ao_telemetry),
250         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
251                                  PKTCTRL1_APPEND_STATUS|
252                                  PKTCTRL1_ADR_CHK_NONE),
253         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_WHITE_DATA|
254                                  RF_PKTCTRL0_PKT_FORMAT_NORMAL|
255                                  RF_PKTCTRL0_CRC_EN|
256                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
257 };
258
259 __xdata uint8_t ao_radio_dma;
260 __xdata uint8_t ao_radio_dma_done;
261 __xdata uint8_t ao_radio_mutex;
262
263 static void
264 ao_radio_idle(void)
265 {
266         if (RF_MARCSTATE != RF_MARCSTATE_IDLE)
267         {
268                 RFST = RFST_SIDLE;
269                 do {
270                         ao_yield();
271                 } while (RF_MARCSTATE != RF_MARCSTATE_IDLE);
272         }
273 }
274
275 void
276 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant
277 {
278         ao_config_get();
279         ao_mutex_get(&ao_radio_mutex);
280         ao_radio_idle();
281         RF_CHANNR = ao_config.radio_channel;
282         ao_dma_set_transfer(ao_radio_dma,
283                             telemetry,
284                             &RFDXADDR,
285                             sizeof (struct ao_telemetry),
286                             DMA_CFG0_WORDSIZE_8 |
287                             DMA_CFG0_TMODE_SINGLE |
288                             DMA_CFG0_TRIGGER_RADIO,
289                             DMA_CFG1_SRCINC_1 |
290                             DMA_CFG1_DESTINC_0 |
291                             DMA_CFG1_PRIORITY_HIGH);
292         ao_dma_start(ao_radio_dma);
293         RFST = RFST_STX;
294         __critical while (!ao_radio_dma_done)
295                 ao_sleep(&ao_radio_dma_done);
296         ao_mutex_put(&ao_radio_mutex);
297 }
298
299 void
300 ao_radio_recv(__xdata struct ao_radio_recv *radio) __reentrant
301 {
302         ao_config_get();
303         ao_mutex_get(&ao_radio_mutex);
304         ao_radio_idle();
305         RF_CHANNR = ao_config.radio_channel;
306         ao_dma_set_transfer(ao_radio_dma,
307                             &RFDXADDR,
308                             radio,
309                             sizeof (struct ao_radio_recv),
310                             DMA_CFG0_WORDSIZE_8 |
311                             DMA_CFG0_TMODE_SINGLE |
312                             DMA_CFG0_TRIGGER_RADIO,
313                             DMA_CFG1_SRCINC_0 |
314                             DMA_CFG1_DESTINC_1 |
315                             DMA_CFG1_PRIORITY_HIGH);
316         ao_dma_start(ao_radio_dma);
317         RFST = RFST_SRX;
318         __critical while (!ao_radio_dma_done)
319                 ao_sleep(&ao_radio_dma_done);
320         ao_mutex_put(&ao_radio_mutex);
321 }
322
323 __xdata ao_radio_rdf_running;
324 __xdata ao_radio_rdf_value = 0x55;
325
326 void
327 ao_radio_rdf(void)
328 {
329         uint8_t i;
330         ao_mutex_get(&ao_radio_mutex);
331         ao_radio_idle();
332         ao_radio_rdf_running = 1;
333         for (i = 0; i < sizeof (rdf_setup); i += 2)
334                 RF[rdf_setup[i]] = rdf_setup[i+1];
335
336         ao_dma_set_transfer(ao_radio_dma,
337                             &ao_radio_rdf_value,
338                             &RFDXADDR,
339                             RDF_PACKET_LEN,
340                             DMA_CFG0_WORDSIZE_8 |
341                             DMA_CFG0_TMODE_SINGLE |
342                             DMA_CFG0_TRIGGER_RADIO,
343                             DMA_CFG1_SRCINC_0 |
344                             DMA_CFG1_DESTINC_0 |
345                             DMA_CFG1_PRIORITY_HIGH);
346         ao_dma_start(ao_radio_dma);
347         RFST = RFST_STX;
348
349         __critical while (!ao_radio_dma_done)
350                 ao_sleep(&ao_radio_dma_done);
351         ao_radio_rdf_running = 0;
352         ao_radio_idle();
353         for (i = 0; i < sizeof (rdf_setup); i += 2)
354                 RF[telemetry_setup[i]] = telemetry_setup[i+1];
355         ao_mutex_put(&ao_radio_mutex);
356 }
357
358 void
359 ao_radio_rdf_abort(void)
360 {
361         if (ao_radio_rdf_running) {
362                 ao_dma_abort(ao_radio_dma);
363                 ao_radio_idle();
364         }
365 }
366
367 void
368 ao_radio_init(void)
369 {
370         uint8_t i;
371         for (i = 0; i < sizeof (radio_setup); i += 2)
372                 RF[radio_setup[i]] = radio_setup[i+1];
373         ao_radio_dma_done = 1;
374         ao_radio_dma = ao_dma_alloc(&ao_radio_dma_done);
375 }
376