Update actions to ubuntu-latest (#14114)
[betaflight.git] / src / main / rx / cc2500_common.c
blob05a143c5b59f3c699bed943a4e07578783b6c1ca
1 /*
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)
8 * any later version.
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/>.
21 #include <stdbool.h>
23 #include "platform.h"
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"
36 #include "pg/pg.h"
37 #include "pg/pg_ids.h"
38 #include "pg/rx.h"
39 #include "pg/rx_spi.h"
40 #include "pg/rx_spi_cc2500.h"
42 #include "rx/rx.h"
43 #include "rx/rx_spi.h"
45 #include "cc2500_common.h"
47 #if defined(USE_RX_CC2500_SPI_PA_LNA)
48 static IO_t txEnPin;
49 static IO_t rxLnaEnPin;
50 #if defined(USE_RX_CC2500_SPI_DIVERSITY)
51 static IO_t antSelPin;
52 #endif
53 #endif
55 static int16_t rssiDbm;
57 uint16_t cc2500getRssiDbm(void)
59 return rssiDbm;
62 void cc2500setRssiDbm(uint8_t value)
64 if (value >= 128) {
65 rssiDbm = ((((uint16_t)value) * 18) >> 5) - 82;
66 } else {
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;
78 if (antSelPin) {
79 if (alternativeAntennaSelected) {
80 IOLo(antSelPin);
81 } else {
82 IOHi(antSelPin);
84 alternativeAntennaSelected = !alternativeAntennaSelected;
87 #endif
88 #if defined(USE_RX_CC2500_SPI_PA_LNA)
89 void cc2500TxEnable(void)
91 if (txEnPin) {
92 IOHi(txEnPin);
96 void cc2500TxDisable(void)
98 if (txEnPin) {
99 IOLo(txEnPin);
102 #endif
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) {
112 return true;
115 return false;
118 bool cc2500SpiInit(void)
120 if (rxCc2500SpiConfig()->chipDetectEnabled && !cc2500SpiDetect()) {
121 return false;
124 if (!rxSpiExtiConfigured()) {
125 return false;
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);
140 } else {
141 txEnPin = IO_NONE;
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);
149 IOHi(antSelPin);
150 } else {
151 antSelPin = IO_NONE;
153 #endif
154 #endif // USE_RX_CC2500_SPI_PA_LNA
156 #if defined(USE_RX_CC2500_SPI_PA_LNA)
157 cc2500TxDisable();
158 #endif // USE_RX_CC2500_SPI_PA_LNA
160 if (rssiSource == RSSI_SOURCE_NONE) {
161 rssiSource = RSSI_SOURCE_RX_PROTOCOL;
164 return true;
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);
172 configArrayPtr++;
175 #endif