ao-bringup: Allow dfu_util to "fail" while flashing chaoskey
[fw/altos] / ao-bringup / ao_radio_init.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao_bringup.h"
20
21 /* Values from SmartRF® Studio for:
22  *
23  * Deviation:   20.507812 kHz
24  * Datarate:    38.360596 kBaud
25  * Modulation:  GFSK
26  * RF Freq:     434.549927 MHz
27  * Channel:     99.975586 kHz
28  * Channel:     0
29  * RX filter:   93.75 kHz
30  */
31
32 /*
33  * For 434.550MHz, the frequency value is:
34  *
35  * 434.550e6 / (24e6 / 2**16) = 1186611.2
36  */
37
38 #define FREQ_CONTROL    1186611
39
40 /*
41  * For IF freq of 140.62kHz, the IF value is:
42  *
43  * 140.62e3 / (24e6 / 2**10) = 6
44  */
45
46 #define IF_FREQ_CONTROL 6
47
48 /*
49  * For channel bandwidth of 93.75 kHz, the CHANBW_E and CHANBW_M values are
50  *
51  * BW = 24e6 / (8 * (4 + M) * 2 ** E)
52  *
53  * So, M = 0 and E = 3
54  */
55
56 #define CHANBW_M        0
57 #define CHANBW_E        3
58
59 /*
60  * For a symbol rate of 38360kBaud, the DRATE_E and DRATE_M values are:
61  *
62  * R = (256 + M) * 2** E * 24e6 / 2**28
63  *
64  * So M is 163 and E is 10
65  */
66
67 #define DRATE_E         10
68 #define DRATE_M         163
69
70 /*
71  * For a channel deviation of 20.5kHz, the DEVIATION_E and DEVIATION_M values are:
72  *
73  * F = 24e6/2**17 * (8 + DEVIATION_M) * 2**DEVIATION_E
74  *
75  * So M is 6 and E is 3
76  */
77
78 #define DEVIATION_M     6
79 #define DEVIATION_E     3
80
81 /*
82  * For our RDF beacon, set the symbol rate to 2kBaud (for a 1kHz tone),
83  * so the DRATE_E and DRATE_M values are:
84  *
85  * M is 94 and E is 6
86  *
87  * To make the tone last for 200ms, we need 2000 * .2 = 400 bits or 50 bytes
88  */
89
90 #define RDF_DRATE_E     6
91 #define RDF_DRATE_M     94
92 #define RDF_PACKET_LEN  50
93
94 /*
95  * RDF deviation should match the normal NFM value of 5kHz
96  *
97  * M is 6 and E is 1
98  *
99  */
100
101 #define RDF_DEVIATION_M 6
102 #define RDF_DEVIATION_E 1
103
104 /* This are from the table for 433MHz */
105
106 #define RF_POWER_M30_DBM        0x12
107 #define RF_POWER_M20_DBM        0x0e
108 #define RF_POWER_M15_DBM        0x1d
109 #define RF_POWER_M10_DBM        0x34
110 #define RF_POWER_M5_DBM         0x2c
111 #define RF_POWER_0_DBM          0x60
112 #define RF_POWER_5_DBM          0x84
113 #define RF_POWER_7_DBM          0xc8
114 #define RF_POWER_10_DBM         0xc0
115
116 #define RF_POWER                RF_POWER_10_DBM
117
118 static __code uint8_t radio_setup[] = {
119         RF_PA_TABLE7_OFF,       RF_POWER,
120         RF_PA_TABLE6_OFF,       RF_POWER,
121         RF_PA_TABLE5_OFF,       RF_POWER,
122         RF_PA_TABLE4_OFF,       RF_POWER,
123         RF_PA_TABLE3_OFF,       RF_POWER,
124         RF_PA_TABLE2_OFF,       RF_POWER,
125         RF_PA_TABLE1_OFF,       RF_POWER,
126         RF_PA_TABLE0_OFF,       RF_POWER,
127
128         RF_FREQ2_OFF,           (FREQ_CONTROL >> 16) & 0xff,
129         RF_FREQ1_OFF,           (FREQ_CONTROL >> 8) & 0xff,
130         RF_FREQ0_OFF,           (FREQ_CONTROL) & 0xff,
131
132         RF_FSCTRL1_OFF,         (IF_FREQ_CONTROL << RF_FSCTRL1_FREQ_IF_SHIFT),
133         RF_FSCTRL0_OFF,         (0 << RF_FSCTRL0_FREQOFF_SHIFT),
134
135         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
136                                  (CHANBW_M << RF_MDMCFG4_CHANBW_M_SHIFT) |
137                                  (DRATE_E << RF_MDMCFG4_DRATE_E_SHIFT)),
138         RF_MDMCFG3_OFF,         (DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
139         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_OFF |
140                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
141                                  RF_MDMCFG2_SYNC_MODE_15_16_THRES),
142         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_EN |
143                                  RF_MDMCFG1_NUM_PREAMBLE_4 |
144                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
145         RF_MDMCFG0_OFF,         (17 << RF_MDMCFG0_CHANSPC_M_SHIFT),
146
147         RF_CHANNR_OFF,          0,
148
149         RF_DEVIATN_OFF,         ((DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
150                                  (DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
151
152         /* SmartRF says set LODIV_BUF_CURRENT_TX to 0
153          * And, we're not using power ramping, so use PA_POWER 0
154          */
155         RF_FREND0_OFF,          ((1 << RF_FREND0_LODIV_BUF_CURRENT_TX_SHIFT) |
156                                  (0 << RF_FREND0_PA_POWER_SHIFT)),
157
158         RF_FREND1_OFF,          ((1 << RF_FREND1_LNA_CURRENT_SHIFT) |
159                                  (1 << RF_FREND1_LNA2MIX_CURRENT_SHIFT) |
160                                  (1 << RF_FREND1_LODIV_BUF_CURRENT_RX_SHIFT) |
161                                  (2 << RF_FREND1_MIX_CURRENT_SHIFT)),
162
163         RF_FSCAL3_OFF,          0xE9,
164         RF_FSCAL2_OFF,          0x0A,
165         RF_FSCAL1_OFF,          0x00,
166         RF_FSCAL0_OFF,          0x1F,
167
168         RF_TEST2_OFF,           0x88,
169         RF_TEST1_OFF,           0x31,
170         RF_TEST0_OFF,           0x09,
171
172         /* default sync values */
173         RF_SYNC1_OFF,           0xD3,
174         RF_SYNC0_OFF,           0x91,
175
176         /* max packet length */
177         RF_PKTLEN_OFF,          64,
178
179         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
180                                  PKTCTRL1_APPEND_STATUS|
181                                  PKTCTRL1_ADR_CHK_NONE),
182         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_WHITE_DATA|
183                                  RF_PKTCTRL0_PKT_FORMAT_NORMAL|
184                                  RF_PKTCTRL0_CRC_EN|
185                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
186         RF_ADDR_OFF,            0x00,
187         RF_MCSM2_OFF,           (RF_MCSM2_RX_TIME_END_OF_PACKET),
188         RF_MCSM1_OFF,           (RF_MCSM1_CCA_MODE_RSSI_BELOW_UNLESS_RECEIVING|
189                                  RF_MCSM1_RXOFF_MODE_IDLE|
190                                  RF_MCSM1_TXOFF_MODE_IDLE),
191         RF_MCSM0_OFF,           (RF_MCSM0_FS_AUTOCAL_FROM_IDLE|
192                                  RF_MCSM0_MAGIC_3|
193                                  RF_MCSM0_CLOSE_IN_RX_0DB),
194         RF_FOCCFG_OFF,          (RF_FOCCFG_FOC_PRE_K_3K,
195                                  RF_FOCCFG_FOC_POST_K_PRE_K,
196                                  RF_FOCCFG_FOC_LIMIT_BW_OVER_4),
197         RF_BSCFG_OFF,           (RF_BSCFG_BS_PRE_K_2K|
198                                  RF_BSCFG_BS_PRE_KP_3KP|
199                                  RF_BSCFG_BS_POST_KI_PRE_KI|
200                                  RF_BSCFG_BS_POST_KP_PRE_KP|
201                                  RF_BSCFG_BS_LIMIT_0),
202         RF_AGCCTRL2_OFF,        0x43,
203         RF_AGCCTRL1_OFF,        0x40,
204         RF_AGCCTRL0_OFF,        0x91,
205
206         RF_IOCFG2_OFF,          0x00,
207         RF_IOCFG1_OFF,          0x00,
208         RF_IOCFG0_OFF,          0x00,
209
210         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
211                                  (CHANBW_M << RF_MDMCFG4_CHANBW_M_SHIFT) |
212                                  (DRATE_E << RF_MDMCFG4_DRATE_E_SHIFT)),
213         RF_MDMCFG3_OFF,         (DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
214         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_OFF |
215                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
216                                  RF_MDMCFG2_SYNC_MODE_15_16_THRES),
217         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_EN |
218                                  RF_MDMCFG1_NUM_PREAMBLE_4 |
219                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
220
221         RF_DEVIATN_OFF,         ((DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
222                                  (DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
223
224         /* max packet length */
225         RF_PKTLEN_OFF,          0xff,
226         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
227                                  PKTCTRL1_APPEND_STATUS|
228                                  PKTCTRL1_ADR_CHK_NONE),
229         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_WHITE_DATA|
230                                  RF_PKTCTRL0_PKT_FORMAT_NORMAL|
231                                  RF_PKTCTRL0_CRC_EN|
232                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
233 };
234
235 void
236 ao_radio_idle(void)
237 {
238         if (RF_MARCSTATE != RF_MARCSTATE_IDLE)
239         {
240                 RFST = RFST_SIDLE;
241                 do {
242                         ;
243                 } while (RF_MARCSTATE != RF_MARCSTATE_IDLE);
244         }
245 }
246
247 void
248 ao_radio_init(void) {
249         uint8_t i;
250         for (i = 0; i < sizeof (radio_setup); i += 2)
251                 RF[radio_setup[i]] = radio_setup[i+1];
252 }