altosui: Report error message back from libaltos
[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; 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_bringup.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) & 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,          64,
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         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
210                                  (CHANBW_M << RF_MDMCFG4_CHANBW_M_SHIFT) |
211                                  (DRATE_E << RF_MDMCFG4_DRATE_E_SHIFT)),
212         RF_MDMCFG3_OFF,         (DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
213         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_OFF |
214                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
215                                  RF_MDMCFG2_SYNC_MODE_15_16_THRES),
216         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_EN |
217                                  RF_MDMCFG1_NUM_PREAMBLE_4 |
218                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
219
220         RF_DEVIATN_OFF,         ((DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
221                                  (DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
222
223         /* max packet length */
224         RF_PKTLEN_OFF,          0xff,
225         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
226                                  PKTCTRL1_APPEND_STATUS|
227                                  PKTCTRL1_ADR_CHK_NONE),
228         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_WHITE_DATA|
229                                  RF_PKTCTRL0_PKT_FORMAT_NORMAL|
230                                  RF_PKTCTRL0_CRC_EN|
231                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
232 };
233
234 void
235 ao_radio_idle(void)
236 {
237         if (RF_MARCSTATE != RF_MARCSTATE_IDLE)
238         {
239                 RFST = RFST_SIDLE;
240                 do {
241                         ;
242                 } while (RF_MARCSTATE != RF_MARCSTATE_IDLE);
243         }
244 }
245
246 void
247 ao_radio_init(void) {
248         uint8_t i;
249         for (i = 0; i < sizeof (radio_setup); i += 2)
250                 RF[radio_setup[i]] = radio_setup[i+1];
251 }