Rename tools to ao-<foo>
[fw/altos] / ao-tools / target / 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 "radio.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 #define PACKET_LEN      128
81
82 /* This are from the table for 433MHz */
83
84 #define RF_POWER_M30_DBM        0x12
85 #define RF_POWER_M20_DBM        0x0e
86 #define RF_POWER_M15_DBM        0x1d
87 #define RF_POWER_M10_DBM        0x34
88 #define RF_POWER_M5_DBM         0x2c
89 #define RF_POWER_0_DBM          0x60
90 #define RF_POWER_5_DBM          0x84
91 #define RF_POWER_7_DBM          0xc8
92 #define RF_POWER_10_DBM         0xc0
93
94 #define RF_POWER                RF_POWER_0_DBM
95
96 static __code uint8_t radio_setup[] = {
97         RF_PA_TABLE7_OFF,       RF_POWER,
98         RF_PA_TABLE6_OFF,       RF_POWER,
99         RF_PA_TABLE5_OFF,       RF_POWER,
100         RF_PA_TABLE4_OFF,       RF_POWER,
101         RF_PA_TABLE3_OFF,       RF_POWER,
102         RF_PA_TABLE2_OFF,       RF_POWER,
103         RF_PA_TABLE1_OFF,       RF_POWER,
104         RF_PA_TABLE0_OFF,       RF_POWER,
105
106         RF_FREQ2_OFF,           FREQ_CONTROL >> 16,
107         RF_FREQ1_OFF,           FREQ_CONTROL >> 8,
108         RF_FREQ0_OFF,           FREQ_CONTROL >> 0,
109
110         RF_FSCTRL1_OFF,         (IF_FREQ_CONTROL << RF_FSCTRL1_FREQ_IF_SHIFT),
111         RF_FSCTRL0_OFF,         (0 << RF_FSCTRL0_FREQOFF_SHIFT),
112
113         RF_MDMCFG4_OFF,         ((CHANBW_E << RF_MDMCFG4_CHANBW_E_SHIFT) |
114                                  (CHANBW_M << RF_MDMCFG4_CHANBW_M_SHIFT) |
115                                  (DRATE_E << RF_MDMCFG4_DRATE_E_SHIFT)),
116         RF_MDMCFG3_OFF,         (DRATE_M << RF_MDMCFG3_DRATE_M_SHIFT),
117         RF_MDMCFG2_OFF,         (RF_MDMCFG2_DEM_DCFILT_OFF |
118                                  RF_MDMCFG2_MOD_FORMAT_GFSK |
119                                  RF_MDMCFG2_SYNC_MODE_15_16_THRES),
120         RF_MDMCFG1_OFF,         (RF_MDMCFG1_FEC_EN |
121                                  RF_MDMCFG1_NUM_PREAMBLE_4 |
122                                  (2 << RF_MDMCFG1_CHANSPC_E_SHIFT)),
123         RF_MDMCFG0_OFF,         (17 << RF_MDMCFG0_CHANSPC_M_SHIFT),
124
125         RF_CHANNR_OFF,          0,
126
127         RF_DEVIATN_OFF,         ((DEVIATION_E << RF_DEVIATN_DEVIATION_E_SHIFT) |
128                                  (DEVIATION_M << RF_DEVIATN_DEVIATION_M_SHIFT)),
129
130         /* SmartRF says set LODIV_BUF_CURRENT_TX to 0
131          * And, we're not using power ramping, so use PA_POWER 0
132          */
133         RF_FREND0_OFF,          ((1 << RF_FREND0_LODIV_BUF_CURRENT_TX_SHIFT) |
134                                  (0 << RF_FREND0_PA_POWER_SHIFT)),
135
136         RF_FREND1_OFF,          ((1 << RF_FREND1_LNA_CURRENT_SHIFT) |
137                                  (1 << RF_FREND1_LNA2MIX_CURRENT_SHIFT) |
138                                  (1 << RF_FREND1_LODIV_BUF_CURRENT_RX_SHIFT) |
139                                  (2 << RF_FREND1_MIX_CURRENT_SHIFT)),
140
141         RF_FSCAL3_OFF,          0xE9,
142         RF_FSCAL2_OFF,          0x0A,
143         RF_FSCAL1_OFF,          0x00,
144         RF_FSCAL0_OFF,          0x1F,
145
146         RF_TEST2_OFF,           0x88,
147         RF_TEST1_OFF,           0x31,
148         RF_TEST0_OFF,           0x09,
149
150         /* default sync values */
151         RF_SYNC1_OFF,           0xD3,
152         RF_SYNC0_OFF,           0x91,
153
154         /* max packet length */
155         RF_PKTLEN_OFF,          PACKET_LEN,
156
157         RF_PKTCTRL1_OFF,        ((1 << PKTCTRL1_PQT_SHIFT)|
158                                  PKTCTRL1_APPEND_STATUS|
159                                  PKTCTRL1_ADR_CHK_NONE),
160         RF_PKTCTRL0_OFF,        (RF_PKTCTRL0_WHITE_DATA|
161                                  RF_PKTCTRL0_PKT_FORMAT_NORMAL|
162                                  RF_PKTCTRL0_CRC_EN|
163                                  RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
164         RF_ADDR_OFF,            0x00,
165         RF_MCSM2_OFF,           (RF_MCSM2_RX_TIME_END_OF_PACKET),
166         RF_MCSM1_OFF,           (RF_MCSM1_CCA_MODE_RSSI_BELOW_UNLESS_RECEIVING|
167                                  RF_MCSM1_RXOFF_MODE_IDLE|
168                                  RF_MCSM1_TXOFF_MODE_IDLE),
169         RF_MCSM0_OFF,           (RF_MCSM0_FS_AUTOCAL_FROM_IDLE|
170                                  RF_MCSM0_MAGIC_3|
171                                  RF_MCSM0_CLOSE_IN_RX_18DB),
172         RF_FOCCFG_OFF,          (RF_FOCCFG_FOC_PRE_K_3K,
173                                  RF_FOCCFG_FOC_POST_K_PRE_K,
174                                  RF_FOCCFG_FOC_LIMIT_BW_OVER_4),
175         RF_BSCFG_OFF,           (RF_BSCFG_BS_PRE_K_2K|
176                                  RF_BSCFG_BS_PRE_KP_3KP|
177                                  RF_BSCFG_BS_POST_KI_PRE_KI|
178                                  RF_BSCFG_BS_POST_KP_PRE_KP|
179                                  RF_BSCFG_BS_LIMIT_0),
180         RF_AGCCTRL2_OFF,        0x43,
181         RF_AGCCTRL1_OFF,        0x40,
182         RF_AGCCTRL0_OFF,        0x91,
183
184         RF_IOCFG2_OFF,          0x00,
185         RF_IOCFG1_OFF,          0x00,
186         RF_IOCFG0_OFF,          0x00,
187 };
188
189 void
190 radio_init(void) {
191         uint8_t i;
192         for (i = 0; i < sizeof (radio_setup); i += 2)
193                 RF[radio_setup[i]] = radio_setup[i+1];
194 }
195
196 #define nop()   _asm nop _endasm;
197
198 void
199 delay (unsigned char n)
200 {
201         unsigned char i = 0;
202
203         n <<= 1;
204         while (--n != 0)
205                 while (--i != 0)
206                         nop();
207 }