2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 /* Values from SmartRF® Studio for:
22 * Deviation: 20.507812 kHz
23 * Datarate: 38.360596 kBaud
25 * RF Freq: 434.549927 MHz
26 * Channel: 99.975586 kHz
28 * RX filter: 93.75 kHz
32 * For IF freq of 140.62kHz, the IF value is:
34 * 140.62e3 / (24e6 / 2**10) = 6
37 #define IF_FREQ_CONTROL 6
40 * For channel bandwidth of 93.75 kHz, the CHANBW_E and CHANBW_M values are
42 * BW = 24e6 / (8 * (4 + M) * 2 ** E)
51 * For a symbol rate of 38360kBaud, the DRATE_E and DRATE_M values are:
53 * R = (256 + M) * 2** E * 24e6 / 2**28
55 * So M is 163 and E is 10
62 * For a channel deviation of 20.5kHz, the DEVIATION_E and DEVIATION_M values are:
64 * F = 24e6/2**17 * (8 + DEVIATION_M) * 2**DEVIATION_E
66 * So M is 6 and E is 3
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:
78 * To make the tone last for 200ms, we need 2000 * .2 = 400 bits or 50 bytes
82 #define RDF_DRATE_M 94
83 #define RDF_PACKET_LEN 50
86 * RDF deviation should match the normal NFM value of 5kHz
92 #define RDF_DEVIATION_M 6
93 #define RDF_DEVIATION_E 1
95 /* This are from the table for 433MHz */
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
107 #define RF_POWER RF_POWER_10_DBM
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,
119 RF_FSCTRL1_OFF, (IF_FREQ_CONTROL << RF_FSCTRL1_FREQ_IF_SHIFT),
120 RF_FSCTRL0_OFF, (0 << RF_FSCTRL0_FREQOFF_SHIFT),
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),
136 RF_DEVIATN_OFF, ((DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
137 (DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
139 /* SmartRF says set LODIV_BUF_CURRENT_TX to 0
140 * And, we're not using power ramping, so use PA_POWER 0
142 RF_FREND0_OFF, ((1 << RF_FREND0_LODIV_BUF_CURRENT_TX_SHIFT) |
143 (0 << RF_FREND0_PA_POWER_SHIFT)),
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)),
159 /* default sync values */
163 /* max packet length */
164 RF_PKTLEN_OFF, sizeof (struct ao_telemetry),
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|
172 RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
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|
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,
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)),
210 RF_DEVIATN_OFF, ((RDF_DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
211 (RDF_DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
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),
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)),
232 RF_DEVIATN_OFF, ((DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
233 (DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
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|
243 RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
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;
253 ao_radio_general_isr(void) __interrupt 16
256 if (RFIF & RFIF_IM_TIMEOUT) {
257 ao_radio_recv_abort();
258 RFIF &= ~ RFIF_IM_TIMEOUT;
259 } else if (RFIF & RFIF_IM_DONE) {
261 ao_wakeup(&ao_radio_done);
262 RFIF &= ~RFIF_IM_DONE;
267 ao_radio_set_packet(void)
270 for (i = 0; i < sizeof (fixed_pkt_setup); i += 2)
271 RF[fixed_pkt_setup[i]] = fixed_pkt_setup[i+1];
277 if (RF_MARCSTATE != RF_MARCSTATE_IDLE)
282 } while (RF_MARCSTATE != RF_MARCSTATE_IDLE);
287 ao_radio_get(uint8_t len)
290 ao_mutex_get(&ao_radio_mutex);
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);
301 ao_radio_send(__xdata void *packet, uint8_t size) __reentrant
305 ao_dma_set_transfer(ao_radio_dma,
309 DMA_CFG0_WORDSIZE_8 |
310 DMA_CFG0_TMODE_SINGLE |
311 DMA_CFG0_TRIGGER_RADIO,
314 DMA_CFG1_PRIORITY_HIGH);
315 ao_dma_start(ao_radio_dma);
317 __critical while (!ao_radio_done)
318 ao_sleep(&ao_radio_done);
323 ao_radio_recv(__xdata void *packet, uint8_t size) __reentrant
326 ao_radio_get(size - 2);
327 ao_dma_set_transfer(ao_radio_dma,
331 DMA_CFG0_WORDSIZE_8 |
332 DMA_CFG0_TMODE_SINGLE |
333 DMA_CFG0_TRIGGER_RADIO,
336 DMA_CFG1_PRIORITY_HIGH);
337 ao_dma_start(ao_radio_dma);
340 /* Wait for DMA to be done, for the radio receive process to
341 * get aborted or for a receive timeout to fire
343 __critical while (!ao_radio_dma_done && !ao_radio_abort)
344 if (ao_sleep(&ao_radio_dma_done))
347 /* If recv was aborted, clean up by stopping the DMA engine
348 * and idling the radio
350 if (!ao_radio_dma_done) {
351 ao_dma_abort(ao_radio_dma);
355 return ao_radio_dma_done;
359 * Wake up a task waiting to receive a radio packet
360 * and tell them to abort the transfer
364 ao_radio_recv_abort(void)
367 ao_wakeup(&ao_radio_dma_done);
370 __xdata ao_radio_rdf_value = 0x55;
379 * Compute the packet length as follows:
381 * 2000 bps (for a 1kHz tone)
382 * so, for 'ms' milliseconds, we need
383 * 2 * ms bits, or ms / 4 bytes
390 ao_radio_get(pkt_len);
392 for (i = 0; i < sizeof (rdf_setup); i += 2)
393 RF[rdf_setup[i]] = rdf_setup[i+1];
395 ao_dma_set_transfer(ao_radio_dma,
399 DMA_CFG0_WORDSIZE_8 |
400 DMA_CFG0_TMODE_SINGLE |
401 DMA_CFG0_TRIGGER_RADIO,
404 DMA_CFG1_PRIORITY_HIGH);
405 ao_dma_start(ao_radio_dma);
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);
413 ao_radio_set_packet();
418 ao_radio_rdf_abort(void)
421 ao_wakeup(&ao_radio_done);
430 static __xdata radio_on;
432 if (ao_cmd_lex_c != '\n') {
434 mode = (uint8_t) ao_cmd_lex_u32;
437 if ((mode & 2) && !radio_on) {
439 ao_packet_slave_stop();
445 printf ("Hit a character to stop..."); flush();
449 if ((mode & 1) && radio_on) {
456 __code struct ao_cmds ao_radio_cmds[] = {
457 { ao_radio_test, "C <1 start, 0 stop, none both>\0Radio carrier test" },
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);
471 RFIM = RFIM_IM_TIMEOUT|RFIM_IM_DONE;
473 ao_cmd_register(&ao_radio_cmds[0]);