2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
25 #if defined(USE_RX_FRSKY_SPI) || defined(USE_RX_SFHSS_SPI)
27 #include "common/maths.h"
29 #include "drivers/io.h"
30 #include "drivers/rx/rx_cc2500.h"
31 #include "drivers/rx/rx_spi.h"
32 #include "drivers/time.h"
34 #include "config/config.h"
37 #include "pg/pg_ids.h"
39 #include "pg/rx_spi.h"
40 #include "pg/rx_spi_cc2500.h"
43 #include "rx/rx_spi.h"
45 #include "cc2500_common.h"
47 #if defined(USE_RX_CC2500_SPI_PA_LNA)
49 static IO_t rxLnaEnPin
;
50 #if defined(USE_RX_CC2500_SPI_DIVERSITY)
51 static IO_t antSelPin
;
55 static int16_t rssiDbm
;
57 uint16_t cc2500getRssiDbm(void)
62 void cc2500setRssiDbm(uint8_t value
)
65 rssiDbm
= ((((uint16_t)value
) * 18) >> 5) - 82;
67 rssiDbm
= ((((uint16_t)value
) * 18) >> 5) + 65;
70 setRssi(rssiDbm
<< 3, RSSI_SOURCE_RX_PROTOCOL
);
73 #if defined(USE_RX_CC2500_SPI_PA_LNA) && defined(USE_RX_CC2500_SPI_DIVERSITY)
74 void cc2500switchAntennae(void)
76 static bool alternativeAntennaSelected
= true;
79 if (alternativeAntennaSelected
) {
84 alternativeAntennaSelected
= !alternativeAntennaSelected
;
88 #if defined(USE_RX_CC2500_SPI_PA_LNA)
89 void cc2500TxEnable(void)
96 void cc2500TxDisable(void)
104 static bool cc2500SpiDetect(void)
106 cc2500Reset(); // Reset the chip and give it time to wake up
108 const uint8_t chipPartNum
= cc2500ReadReg(CC2500_30_PARTNUM
| CC2500_READ_BURST
); //CC2500 read registers chip part num
109 const uint8_t chipVersion
= cc2500ReadReg(CC2500_31_VERSION
| CC2500_READ_BURST
); //CC2500 read registers chip version
111 if (chipPartNum
== 0x80 && chipVersion
== 0x03) {
118 bool cc2500SpiInit(void)
120 if (rxCc2500SpiConfig()->chipDetectEnabled
&& !cc2500SpiDetect()) {
124 if (!rxSpiExtiConfigured()) {
128 #if defined(USE_RX_CC2500_SPI_PA_LNA)
129 if (rxCc2500SpiConfig()->lnaEnIoTag
) {
130 rxLnaEnPin
= IOGetByTag(rxCc2500SpiConfig()->lnaEnIoTag
);
131 IOInit(rxLnaEnPin
, OWNER_RX_SPI_CC2500_LNA_EN
, 0);
132 IOConfigGPIO(rxLnaEnPin
, IOCFG_OUT_PP
);
134 IOHi(rxLnaEnPin
); // always on at the moment
136 if (rxCc2500SpiConfig()->txEnIoTag
) {
137 txEnPin
= IOGetByTag(rxCc2500SpiConfig()->txEnIoTag
);
138 IOInit(txEnPin
, OWNER_RX_SPI_CC2500_TX_EN
, 0);
139 IOConfigGPIO(txEnPin
, IOCFG_OUT_PP
);
143 #if defined(USE_RX_CC2500_SPI_DIVERSITY)
144 if (rxCc2500SpiConfig()->antSelIoTag
) {
145 antSelPin
= IOGetByTag(rxCc2500SpiConfig()->antSelIoTag
);
146 IOInit(antSelPin
, OWNER_RX_SPI_CC2500_ANT_SEL
, 0);
147 IOConfigGPIO(antSelPin
, IOCFG_OUT_PP
);
154 #endif // USE_RX_CC2500_SPI_PA_LNA
156 #if defined(USE_RX_CC2500_SPI_PA_LNA)
158 #endif // USE_RX_CC2500_SPI_PA_LNA
160 if (rssiSource
== RSSI_SOURCE_NONE
) {
161 rssiSource
= RSSI_SOURCE_RX_PROTOCOL
;
167 void cc2500ApplyRegisterConfig(const cc2500RegisterConfigElement_t
*configArrayPtr
, int configSize
)
169 const int entryCount
= configSize
/ sizeof(cc2500RegisterConfigElement_t
);
170 for (int i
= 0; i
< entryCount
; i
++) {
171 cc2500WriteReg(configArrayPtr
->registerID
, configArrayPtr
->registerValue
);