[4.4.2] Remove 15 m/s limit on estimated vario (#12788)
[betaflight.git] / src / main / drivers / bus_spi.h
blob019378b19b396e2964c45584634b06b475d25ff8
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 #pragma once
23 #include "drivers/bus.h"
24 #include "drivers/io_types.h"
25 #include "drivers/bus.h"
26 #include "drivers/rcc_types.h"
28 #include "pg/pg.h"
29 #include "pg/pg_ids.h"
31 #if defined(STM32F4)
32 #define SPI_IO_AF_CFG IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL)
33 #define SPI_IO_AF_SCK_CFG IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_DOWN)
34 #define SPI_IO_AF_MISO_CFG IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_UP)
35 #define SPI_IO_CS_CFG IO_CONFIG(GPIO_Mode_OUT, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL)
36 #elif defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
37 #define SPI_IO_AF_CFG IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_NOPULL)
38 #define SPI_IO_AF_SCK_CFG_HIGH IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_PULLUP)
39 #define SPI_IO_AF_SCK_CFG_LOW IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_PULLDOWN)
40 #define SPI_IO_AF_MISO_CFG IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_PULLUP)
41 #define SPI_IO_CS_CFG IO_CONFIG(GPIO_MODE_OUTPUT_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_NOPULL)
42 #endif
44 // De facto standard mode
45 // See https://en.wikipedia.org/wiki/Serial_Peripheral_Interface
47 // Mode CPOL CPHA
48 // 0 0 0
49 // 1 0 1
50 // 2 1 0
51 // 3 1 1
52 typedef enum {
53 SPI_MODE0_POL_LOW_EDGE_1ST = 0,
54 SPI_MODE1_POL_LOW_EDGE_2ND,
55 SPI_MODE2_POL_HIGH_EDGE_1ST,
56 SPI_MODE3_POL_HIGH_EDGE_2ND
57 } SPIMode_e;
59 typedef enum SPIDevice {
60 SPIINVALID = -1,
61 SPIDEV_1 = 0,
62 SPIDEV_2,
63 SPIDEV_3,
64 SPIDEV_4,
65 SPIDEV_5,
66 SPIDEV_6
67 } SPIDevice;
69 #if defined(STM32F4)
70 #define SPIDEV_COUNT 3
71 #elif defined(STM32F7)
72 #define SPIDEV_COUNT 4
73 #elif defined(STM32H7)
74 #define SPIDEV_COUNT 6
75 #else
76 #define SPIDEV_COUNT 4
77 #endif
79 // Macros to convert between CLI bus number and SPIDevice.
80 #define SPI_CFG_TO_DEV(x) ((x) - 1)
81 #define SPI_DEV_TO_CFG(x) ((x) + 1)
83 // Work around different check routines in the libraries for different MCU types
84 #if defined(STM32H7)
85 #define CHECK_SPI_RX_DATA_AVAILABLE(instance) LL_SPI_IsActiveFlag_RXWNE(instance)
86 #define SPI_RX_DATA_REGISTER(base) ((base)->RXDR)
87 #else
88 #define CHECK_SPI_RX_DATA_AVAILABLE(instance) LL_SPI_IsActiveFlag_RXNE(instance)
89 #define SPI_RX_DATA_REGISTER(base) ((base)->DR)
90 #endif
92 void spiPreinit(void);
93 void spiPreinitRegister(ioTag_t iotag, uint8_t iocfg, uint8_t init);
94 void spiPreinitByIO(IO_t io);
95 void spiPreinitByTag(ioTag_t tag);
97 bool spiInit(SPIDevice device);
99 // Called after all devices are initialised to enable SPI DMA where streams are available.
100 void spiInitBusDMA();
103 SPIDevice spiDeviceByInstance(SPI_TypeDef *instance);
104 SPI_TypeDef *spiInstanceByDevice(SPIDevice device);
106 // BusDevice API
108 // Mark a device's associated bus as being SPI
109 bool spiSetBusInstance(extDevice_t *dev, uint32_t device);
110 // Determine the divisor to use for a given bus frequency
111 uint16_t spiCalculateDivider(uint32_t freq);
112 // Return the SPI clock based on the given divisor
113 uint32_t spiCalculateClock(uint16_t spiClkDivisor);
114 // Set the clock divisor to be used for accesses by the given device
115 void spiSetClkDivisor(const extDevice_t *dev, uint16_t divider);
116 // Set the clock phase/polarity to be used for accesses by the given device
117 void spiSetClkPhasePolarity(const extDevice_t *dev, bool leadingEdge);
118 // Enable/disable DMA on a specific device. Enabled by default.
119 void spiDmaEnable(const extDevice_t *dev, bool enable);
121 // DMA transfer setup and start
122 void spiSequence(const extDevice_t *dev, busSegment_t *segments);
123 // Wait for DMA completion
124 void spiWait(const extDevice_t *dev);
125 // Return true if DMA engine is busy
126 bool spiIsBusy(const extDevice_t *dev);
129 * Routine naming convention is:
130 * spi[Read][Write][Reg][Msk][Buf][RB]
132 * Read: Perform a read, returning the value read unless 'Buf' is specified
133 * Write Perform a write
134 * ReadWrite: Perform both a read and write, returning the value read unless 'Buf' is specified
135 * Reg: Register number 'reg' is written prior to the read being performed
136 * Msk: Register number is logically ORed with 0x80 as some devices indicate a read by accessing a register with bit 7 set
137 * Buf: Pass data of given length by reference
138 * RB: Return false immediately if the bus is busy, otherwise complete the access and return true
140 uint8_t spiReadReg(const extDevice_t *dev, uint8_t reg);
141 uint8_t spiReadRegMsk(const extDevice_t *dev, uint8_t reg);
142 void spiReadRegBuf(const extDevice_t *dev, uint8_t reg, uint8_t *data, uint8_t length);
143 bool spiReadRegBufRB(const extDevice_t *dev, uint8_t reg, uint8_t *data, uint8_t length);
144 bool spiReadRegMskBufRB(const extDevice_t *dev, uint8_t reg, uint8_t *data, uint8_t length);
146 void spiWrite(const extDevice_t *dev, uint8_t data);
147 void spiWriteReg(const extDevice_t *dev, uint8_t reg, uint8_t data);
148 bool spiWriteRegRB(const extDevice_t *dev, uint8_t reg, uint8_t data);
150 uint8_t spiReadWrite(const extDevice_t *dev, uint8_t data);
152 void spiWriteRegBuf(const extDevice_t *dev, uint8_t reg, uint8_t *data, uint32_t length);
153 uint8_t spiReadWriteReg(const extDevice_t *dev, uint8_t reg, uint8_t data);
154 void spiReadWriteBuf(const extDevice_t *dev, uint8_t *txData, uint8_t *rxData, int len);
155 bool spiReadWriteBufRB(const extDevice_t *dev, uint8_t *txData, uint8_t *rxData, int length);
158 // Config
161 struct spiPinConfig_s;
162 void spiPinConfigure(const struct spiPinConfig_s *pConfig);
163 bool spiUseDMA(const extDevice_t *dev);
164 bool spiUseMOSI_DMA(const extDevice_t *dev);
165 void spiBusDeviceRegister(const extDevice_t *dev);
166 uint8_t spiGetRegisteredDeviceCount(void);
167 uint8_t spiGetExtDeviceCount(const extDevice_t *dev);