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/>.
21 // This file is copied with modifications from project Deviation,
22 // see http://deviationtx.com
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"
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 void rxSpiDevicePreInit(const rxSpiConfig_t
*rxSpiConfig
)
63 spiPreinitRegister(rxSpiConfig
->csnTag
, IOCFG_IPU
, 1);
66 void rxSpiExtiHandler(extiCallbackRec_t
* callback
)
70 const timeUs_t extiTimeUs
= microsISR();
72 if (IORead(extiPin
) == extiLevel
) {
73 lastExtiTimeUs
= extiTimeUs
;
74 extiHasOccurred
= true;
78 void rxSpiNormalSpeed()
80 spiSetClkDivisor(dev
, spiCalculateDivider(RX_MAX_SPI_CLK_HZ
));
83 void rxSpiStartupSpeed()
85 spiSetClkDivisor(dev
, spiCalculateDivider(RX_STARTUP_MAX_SPI_CLK_HZ
));
88 bool rxSpiDeviceInit(const rxSpiConfig_t
*rxSpiConfig
)
90 if (!spiSetBusInstance(dev
, rxSpiConfig
->spibus
, OWNER_RX_SPI_CS
)) {
94 const IO_t rxCsPin
= IOGetByTag(rxSpiConfig
->csnTag
);
95 IOInit(rxCsPin
, OWNER_RX_SPI_CS
, 0);
96 IOConfigGPIO(rxCsPin
, SPI_IO_CS_CFG
);
97 dev
->busType_u
.spi
.csnPin
= rxCsPin
;
99 // Set the clock phase/polarity
100 spiSetClkPhasePolarity(dev
, true);
105 extiPin
= IOGetByTag(rxSpiConfig
->extiIoTag
);
108 IOInit(extiPin
, OWNER_RX_SPI_EXTI
, 0);
114 void rxSpiExtiInit(ioConfig_t rxSpiExtiPinConfig
, extiTrigger_t rxSpiExtiPinTrigger
)
117 if (rxSpiExtiPinTrigger
== BETAFLIGHT_EXTI_TRIGGER_FALLING
) {
120 EXTIHandlerInit(&rxSpiExtiCallbackRec
, rxSpiExtiHandler
);
121 EXTIConfig(extiPin
, &rxSpiExtiCallbackRec
, NVIC_PRIO_MPU_INT_EXTI
, rxSpiExtiPinConfig
, rxSpiExtiPinTrigger
);
122 EXTIEnable(extiPin
, true);
126 void rxSpiDmaEnable(bool enable
)
128 spiDmaEnable(dev
, enable
);
131 uint8_t rxSpiTransferByte(uint8_t data
)
133 return spiReadWrite(dev
, data
);
136 void rxSpiWriteByte(uint8_t data
)
141 void rxSpiWriteCommand(uint8_t command
, uint8_t data
)
143 spiWriteReg(dev
, command
, data
);
146 void rxSpiWriteCommandMulti(uint8_t command
, const uint8_t *data
, uint8_t length
)
148 spiWriteRegBuf(dev
, command
, (uint8_t *)data
, length
);
151 uint8_t rxSpiReadCommand(uint8_t command
, uint8_t data
)
154 return spiReadReg(dev
, command
);
157 void rxSpiReadCommandMulti(uint8_t command
, uint8_t commandData
, uint8_t *retData
, uint8_t length
)
160 spiReadRegBuf(dev
, command
, retData
, length
);
163 bool rxSpiExtiConfigured(void)
165 return extiPin
!= IO_NONE
;
168 bool rxSpiGetExtiState(void)
170 return IORead(extiPin
);
173 bool rxSpiPollExti(void)
175 return extiHasOccurred
;
178 void rxSpiResetExti(void)
180 extiHasOccurred
= false;
183 timeUs_t
rxSpiGetLastExtiTimeUs(void)
185 return lastExtiTimeUs
;