[4.4.2] Remove 15 m/s limit on estimated vario (#12788)
[betaflight.git] / src / main / drivers / rx / rx_spi.c
blob1755a06bb7137444f7c28f9e92d9427541d99d9c
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 // This file is copied with modifications from project Deviation,
22 // see http://deviationtx.com
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include <stdlib.h>
28 #include "platform.h"
30 #ifdef USE_RX_SPI
32 #include "build/build_config.h"
34 #include "drivers/bus_spi.h"
35 #include "drivers/io.h"
36 #include "drivers/io_impl.h"
37 #include "drivers/nvic.h"
38 #include "drivers/rcc.h"
39 #include "drivers/system.h"
40 #include "drivers/time.h"
42 #include "pg/rx_spi.h"
44 #include "rx_spi.h"
46 #ifdef USE_RX_EXPRESSLRS
47 #include "rx/rx_spi.h"
48 #include "rx/expresslrs.h"
49 #endif
51 // 13.5 MHz max SPI frequency
52 #define RX_MAX_SPI_CLK_HZ 13500000
53 // 6.5 MHz max SPI frequency during startup
54 #define RX_STARTUP_MAX_SPI_CLK_HZ 6500000
56 static extDevice_t rxSpiDevice;
57 static extDevice_t *dev = &rxSpiDevice;
59 static IO_t extiPin = IO_NONE;
60 static bool extiLevel = true;
61 static extiCallbackRec_t rxSpiExtiCallbackRec;
63 static volatile bool extiHasOccurred = false;
64 static volatile timeUs_t lastExtiTimeUs = 0;
66 static uint32_t spiNormalSpeedMhz = RX_MAX_SPI_CLK_HZ;
68 extDevice_t *rxSpiGetDevice(void)
70 return dev;
73 void rxSpiDevicePreInit(const rxSpiConfig_t *rxSpiConfig)
75 spiPreinitRegister(rxSpiConfig->csnTag, IOCFG_IPU, 1);
78 void rxSpiExtiHandler(extiCallbackRec_t* callback)
80 UNUSED(callback);
82 lastExtiTimeUs = microsISR();
83 extiHasOccurred = true;
85 #ifdef USE_RX_EXPRESSLRS
86 expressLrsISR(true);
87 #endif
90 void rxSpiSetNormalSpeedMhz(uint32_t mhz)
92 spiNormalSpeedMhz = mhz;
95 void rxSpiNormalSpeed(void)
97 spiSetClkDivisor(dev, spiCalculateDivider(spiNormalSpeedMhz));
100 void rxSpiStartupSpeed(void)
102 spiSetClkDivisor(dev, spiCalculateDivider(RX_STARTUP_MAX_SPI_CLK_HZ));
105 bool rxSpiDeviceInit(const rxSpiConfig_t *rxSpiConfig)
107 if (!spiSetBusInstance(dev, rxSpiConfig->spibus)) {
108 return false;
111 const IO_t rxCsPin = IOGetByTag(rxSpiConfig->csnTag);
112 IOInit(rxCsPin, OWNER_RX_SPI_CS, 0);
113 IOConfigGPIO(rxCsPin, SPI_IO_CS_CFG);
114 dev->busType_u.spi.csnPin = rxCsPin;
116 // Set the clock phase/polarity
117 spiSetClkPhasePolarity(dev, true);
118 rxSpiNormalSpeed();
120 IOHi(rxCsPin);
122 extiPin = IOGetByTag(rxSpiConfig->extiIoTag);
124 if (extiPin) {
125 IOInit(extiPin, OWNER_RX_SPI_EXTI, 0);
128 return true;
131 void rxSpiExtiInit(ioConfig_t rxSpiExtiPinConfig, extiTrigger_t rxSpiExtiPinTrigger)
133 if (extiPin) {
134 // Use interrupts on the EXTI pin
135 if (rxSpiExtiPinTrigger == BETAFLIGHT_EXTI_TRIGGER_FALLING) {
136 extiLevel = false;
139 EXTIHandlerInit(&rxSpiExtiCallbackRec, rxSpiExtiHandler);
140 EXTIConfig(extiPin, &rxSpiExtiCallbackRec, NVIC_PRIO_RX_INT_EXTI, rxSpiExtiPinConfig, rxSpiExtiPinTrigger);
141 EXTIEnable(extiPin);
143 // Check that we've not missed the rising edge on the interrupt line
144 if (rxSpiGetExtiState()) {
145 rxSpiExtiHandler(NULL);
150 void rxSpiDmaEnable(bool enable)
152 spiDmaEnable(dev, enable);
155 uint8_t rxSpiTransferByte(uint8_t data)
157 return spiReadWrite(dev, data);
160 void rxSpiWriteByte(uint8_t data)
162 spiWrite(dev, data);
165 void rxSpiWriteCommand(uint8_t command, uint8_t data)
167 spiWriteReg(dev, command, data);
170 void rxSpiWriteCommandMulti(uint8_t command, const uint8_t *data, uint8_t length)
172 spiWriteRegBuf(dev, command, (uint8_t *)data, length);
175 uint8_t rxSpiReadCommand(uint8_t command, uint8_t data)
177 UNUSED(data);
178 return spiReadReg(dev, command);
181 void rxSpiReadCommandMulti(uint8_t command, uint8_t commandData, uint8_t *retData, uint8_t length)
183 UNUSED(commandData);
184 spiReadRegBuf(dev, command, retData, length);
187 bool rxSpiExtiConfigured(void)
189 return extiPin != IO_NONE;
192 bool rxSpiGetExtiState(void)
194 return IORead(extiPin);
197 bool rxSpiPollExti(void)
199 return extiHasOccurred;
202 void rxSpiResetExti(void)
204 extiHasOccurred = false;
207 timeUs_t rxSpiGetLastExtiTimeUs(void)
209 return lastExtiTimeUs;
212 bool rxSpiIsBusy(void)
214 return spiIsBusy(dev);
217 void rxSpiTransferCommandMulti(uint8_t *data, uint8_t length)
219 spiReadWriteBuf(dev, data, data, length);
222 #endif