Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / rx / rx_spi.c
blob58d11953cd932e3d7c038a07a8c89c24767549dc
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 // 13.5 MHz max SPI frequency
47 #define RX_MAX_SPI_CLK_HZ 13500000
48 // 6.5 MHz max SPI frequency during startup
49 #define RX_STARTUP_MAX_SPI_CLK_HZ 6500000
51 static extDevice_t rxSpiDevice;
52 static extDevice_t *dev = &rxSpiDevice;
54 static IO_t extiPin = IO_NONE;
55 static extiCallbackRec_t rxSpiExtiCallbackRec;
56 static bool extiLevel = true;
58 static volatile bool extiHasOccurred = false;
59 static volatile timeUs_t lastExtiTimeUs = 0;
61 static uint32_t spiNormalSpeedMhz = RX_MAX_SPI_CLK_HZ;
63 void rxSpiDevicePreInit(const rxSpiConfig_t *rxSpiConfig)
65 spiPreinitRegister(rxSpiConfig->csnTag, IOCFG_IPU, 1);
68 void rxSpiExtiHandler(extiCallbackRec_t* callback)
70 UNUSED(callback);
72 const timeUs_t extiTimeUs = microsISR();
74 if (IORead(extiPin) == extiLevel) {
75 lastExtiTimeUs = extiTimeUs;
76 extiHasOccurred = true;
80 void rxSpiSetNormalSpeedMhz(uint32_t mhz)
82 spiNormalSpeedMhz = mhz;
85 void rxSpiNormalSpeed()
87 spiSetClkDivisor(dev, spiCalculateDivider(spiNormalSpeedMhz));
90 void rxSpiStartupSpeed()
92 spiSetClkDivisor(dev, spiCalculateDivider(RX_STARTUP_MAX_SPI_CLK_HZ));
95 bool rxSpiDeviceInit(const rxSpiConfig_t *rxSpiConfig)
97 if (!spiSetBusInstance(dev, rxSpiConfig->spibus)) {
98 return false;
101 const IO_t rxCsPin = IOGetByTag(rxSpiConfig->csnTag);
102 IOInit(rxCsPin, OWNER_RX_SPI_CS, 0);
103 IOConfigGPIO(rxCsPin, SPI_IO_CS_CFG);
104 dev->busType_u.spi.csnPin = rxCsPin;
106 // Set the clock phase/polarity
107 spiSetClkPhasePolarity(dev, true);
108 rxSpiNormalSpeed();
110 IOHi(rxCsPin);
112 extiPin = IOGetByTag(rxSpiConfig->extiIoTag);
114 if (extiPin) {
115 IOInit(extiPin, OWNER_RX_SPI_EXTI, 0);
118 return true;
121 void rxSpiExtiInit(ioConfig_t rxSpiExtiPinConfig, extiTrigger_t rxSpiExtiPinTrigger)
123 if (extiPin) {
124 if (rxSpiExtiPinTrigger == BETAFLIGHT_EXTI_TRIGGER_FALLING) {
125 extiLevel = false;
127 EXTIHandlerInit(&rxSpiExtiCallbackRec, rxSpiExtiHandler);
128 EXTIConfig(extiPin, &rxSpiExtiCallbackRec, NVIC_PRIO_MPU_INT_EXTI, rxSpiExtiPinConfig, rxSpiExtiPinTrigger);
129 EXTIEnable(extiPin, true);
133 void rxSpiDmaEnable(bool enable)
135 spiDmaEnable(dev, enable);
138 uint8_t rxSpiTransferByte(uint8_t data)
140 return spiReadWrite(dev, data);
143 void rxSpiWriteByte(uint8_t data)
145 spiWrite(dev, data);
148 void rxSpiWriteCommand(uint8_t command, uint8_t data)
150 spiWriteReg(dev, command, data);
153 void rxSpiWriteCommandMulti(uint8_t command, const uint8_t *data, uint8_t length)
155 spiWriteRegBuf(dev, command, (uint8_t *)data, length);
158 uint8_t rxSpiReadCommand(uint8_t command, uint8_t data)
160 UNUSED(data);
161 return spiReadReg(dev, command);
164 void rxSpiReadCommandMulti(uint8_t command, uint8_t commandData, uint8_t *retData, uint8_t length)
166 UNUSED(commandData);
167 spiReadRegBuf(dev, command, retData, length);
170 bool rxSpiExtiConfigured(void)
172 return extiPin != IO_NONE;
175 bool rxSpiGetExtiState(void)
177 return IORead(extiPin);
180 bool rxSpiPollExti(void)
182 return extiHasOccurred;
185 void rxSpiResetExti(void)
187 extiHasOccurred = false;
190 timeUs_t rxSpiGetLastExtiTimeUs(void)
192 return lastExtiTimeUs;
195 bool rxSpiIsBusy(void)
197 return spiIsBusy(dev);
200 void rxSpiTransferCommandMulti(uint8_t *data, uint8_t length)
202 spiReadWriteBuf(dev, data, data, length);
205 #endif