78d7d8ddaaa3e332c613595c18ff0ba7659d02eb
[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 /* This are from the table for 433MHz */
81
82 #define RF_POWER_M30_DBM        0x12
83 #define RF_POWER_M20_DBM        0x0e
84 #define RF_POWER_M15_DBM        0x1d
85 #define RF_POWER_M10_DBM        0x34
86 #define RF_POWER_M5_DBM         0x2c
87 #define RF_POWER_0_DBM          0x60
88 #define RF_POWER_5_DBM          0x84
89 #define RF_POWER_7_DBM          0xc8
90 #define RF_POWER_10_DBM         0xc0
91
92 #define RF_POWER                RF_POWER_10_DBM
93
94 static __code uint8_t radio_setup[] = {
95         RF_PA_TABLE7_OFF,       RF_POWER,
96         RF_PA_TABLE6_OFF,       RF_POWER,
97         RF_PA_TABLE5_OFF,       RF_POWER,
98         RF_PA_TABLE4_OFF,       RF_POWER,
99         RF_PA_TABLE3_OFF,       RF_POWER,
100         RF_PA_TABLE2_OFF,       RF_POWER,
101         RF_PA_TABLE1_OFF,       RF_POWER,
102         RF_PA_TABLE0_OFF,       RF_POWER,
103
104         RF_FREQ2_OFF,           (FREQ_CONTROL >> 16) & 0xff,
105         RF_FREQ1_OFF,           (FREQ_CONTROL >> 8) & 0xff,
106         RF_FREQ0_OFF,           (FREQ_CONTROL >> 0) & 0xff,
107         
108         RF_FSCTRL1_OFF,         (IF_FREQ_CONTROL << RF_FSCTRL1_FREQ_IF_SHIFT),
109         RF_FSCTRL0_OFF,         (0 << RF_FSCTRL0_FREQOFF_SHIFT),
110
111         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
112                                  (CHANBW_M << RF_MDMCFG4_CHANBW_M_SHIFT) |
113                                  (DRATE_E << RF_MDMCFG4_DRATE_E_SHIFT)),
114         RF_MDMCFG3_OFF,         (DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
115         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_OFF |
116                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
117                                  RF_MDMCFG2_SYNC_MODE_15_16_THRES),
118         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_EN |
119                                  RF_MDMCFG1_NUM_PREAMBLE_4 |
120                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
121         RF_MDMCFG0_OFF,         (17 << RF_MDMCFG0_CHANSPC_M_SHIFT),
122
123         RF_CHANNR_OFF,          0,
124
125         RF_DEVIATN_OFF,         ((DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
126                                  (DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
127
128         /* SmartRF says set LODIV_BUF_CURRENT_TX to 0
129          * And, we're not using power ramping, so use PA_POWER 0
130          */
131         RF_FREND0_OFF,          ((1 << RF_FREND0_LODIV_BUF_CURRENT_TX_SHIFT) |
132                                  (0 << RF_FREND0_PA_POWER_SHIFT)),
133
134         RF_FREND1_OFF,          ((1 << RF_FREND1_LNA_CURRENT_SHIFT) |
135                                  (1 << RF_FREND1_LNA2MIX_CURRENT_SHIFT) |
136                                  (1 << RF_FREND1_LODIV_BUF_CURRENT_RX_SHIFT) |
137                                  (2 << RF_FREND1_MIX_CURRENT_SHIFT)),
138
139         RF_FSCAL3_OFF,          0xE9,
140         RF_FSCAL2_OFF,          0x0A,
141         RF_FSCAL1_OFF,          0x00,
142         RF_FSCAL0_OFF,          0x1F,
143
144         RF_TEST2_OFF,           0x88,
145         RF_TEST1_OFF,           0x31,
146         RF_TEST0_OFF,           0x09,
147
148         /* default sync values */
149         RF_SYNC1_OFF,           0xD3,
150         RF_SYNC0_OFF,           0x91,
151         
152         /* max packet length */
153         RF_PKTLEN_OFF,          sizeof (struct ao_telemetry),
154
155         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
156                                  PKTCTRL1_APPEND_STATUS|
157                                  PKTCTRL1_ADR_CHK_NONE),
158         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_WHITE_DATA|
159                                  RF_PKTCTRL0_PKT_FORMAT_NORMAL|
160                                  RF_PKTCTRL0_CRC_EN|
161                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
162         RF_ADDR_OFF,            0x00,
163         RF_MCSM2_OFF,           (RF_MCSM2_RX_TIME_END_OF_PACKET),
164         RF_MCSM1_OFF,           (RF_MCSM1_CCA_MODE_RSSI_BELOW_UNLESS_RECEIVING|
165                                  RF_MCSM1_RXOFF_MODE_IDLE|
166                                  RF_MCSM1_TXOFF_MODE_IDLE),
167         RF_MCSM0_OFF,           (RF_MCSM0_FS_AUTOCAL_FROM_IDLE|
168                                  RF_MCSM0_MAGIC_3|
169                                  RF_MCSM0_CLOSE_IN_RX_0DB),
170         RF_FOCCFG_OFF,          (RF_FOCCFG_FOC_PRE_K_3K,
171                                  RF_FOCCFG_FOC_POST_K_PRE_K,
172                                  RF_FOCCFG_FOC_LIMIT_BW_OVER_4),
173         RF_BSCFG_OFF,           (RF_BSCFG_BS_PRE_K_2K|
174                                  RF_BSCFG_BS_PRE_KP_3KP|
175                                  RF_BSCFG_BS_POST_KI_PRE_KI|
176                                  RF_BSCFG_BS_POST_KP_PRE_KP|
177                                  RF_BSCFG_BS_LIMIT_0),
178         RF_AGCCTRL2_OFF,        0x43,
179         RF_AGCCTRL1_OFF,        0x40,
180         RF_AGCCTRL0_OFF,        0x91,
181
182         RF_IOCFG2_OFF,          0x00,
183         RF_IOCFG1_OFF,          0x00,
184         RF_IOCFG0_OFF,          0x00,
185 };
186
187 __xdata uint8_t ao_radio_dma;
188 __xdata uint8_t ao_radio_dma_done;
189 __xdata uint8_t ao_radio_mutex;
190
191 static void
192 ao_radio_idle(void)
193 {
194         if (RF_MARCSTATE != RF_MARCSTATE_IDLE)
195         {
196                 RFST = RFST_SIDLE;
197                 do {
198                         ao_yield();
199                 } while (RF_MARCSTATE != RF_MARCSTATE_IDLE);
200         }
201 }
202
203 void
204 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant
205 {
206         ao_mutex_get(&ao_radio_mutex);
207         ao_radio_idle();
208         ao_dma_set_transfer(ao_radio_dma,
209                             telemetry,
210                             &RFDXADDR,
211                             sizeof (struct ao_telemetry),
212                             DMA_CFG0_WORDSIZE_8 |
213                             DMA_CFG0_TMODE_SINGLE |
214                             DMA_CFG0_TRIGGER_RADIO,
215                             DMA_CFG1_SRCINC_1 |
216                             DMA_CFG1_DESTINC_0 |
217                             DMA_CFG1_PRIORITY_HIGH);
218         ao_dma_start(ao_radio_dma);
219         RFST = RFST_STX;
220         __critical while (!ao_radio_dma_done)
221                 ao_sleep(&ao_radio_dma_done);
222         ao_mutex_put(&ao_radio_mutex);
223 }
224
225 void
226 ao_radio_recv(__xdata struct ao_radio_recv *radio) __reentrant
227 {
228         ao_mutex_get(&ao_radio_mutex);
229         ao_radio_idle();
230         ao_dma_set_transfer(ao_radio_dma,
231                             &RFDXADDR,
232                             radio,
233                             sizeof (struct ao_radio_recv),
234                             DMA_CFG0_WORDSIZE_8 |
235                             DMA_CFG0_TMODE_SINGLE |
236                             DMA_CFG0_TRIGGER_RADIO,
237                             DMA_CFG1_SRCINC_0 |
238                             DMA_CFG1_DESTINC_1 |
239                             DMA_CFG1_PRIORITY_HIGH);
240         ao_dma_start(ao_radio_dma);
241         RFST = RFST_SRX;
242         __critical while (!ao_radio_dma_done)
243                 ao_sleep(&ao_radio_dma_done);
244         ao_mutex_put(&ao_radio_mutex);
245 }
246
247 void
248 ao_radio_init(void)
249 {
250         uint8_t i;
251         for (i = 0; i < sizeof (radio_setup); i += 2)
252                 RF[radio_setup[i]] = radio_setup[i+1];
253         ao_radio_dma_done = 1;
254         ao_radio_dma = ao_dma_alloc(&ao_radio_dma_done);
255 }
256