2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
20 #include "drivers/io_types.h"
21 #include "drivers/rcc_types.h"
22 #include "drivers/dma.h"
25 #define SPI_IO_AF_CFG IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL)
26 #define SPI_IO_AF_SCK_CFG IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_DOWN)
27 #define SPI_IO_AF_MISO_CFG IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_UP)
28 #define SPI_IO_CS_CFG IO_CONFIG(GPIO_Mode_OUT, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL)
29 #elif defined(STM32F7) || defined(STM32H7)
30 #define SPI_IO_AF_CFG IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_NOPULL)
31 #define SPI_IO_AF_SCK_CFG_HIGH IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_PULLUP)
32 #define SPI_IO_AF_SCK_CFG_LOW IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_PULLDOWN)
33 #define SPI_IO_AF_MISO_CFG IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_PULLUP)
34 #define SPI_IO_CS_CFG IO_CONFIG(GPIO_MODE_OUTPUT_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_NOPULL)
35 #elif defined(AT32F43x)
36 #define SPI_IO_AF_CFG IO_CONFIG(GPIO_MODE_MUX, GPIO_DRIVE_STRENGTH_STRONGER, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE)
37 #define SPI_IO_AF_SCK_CFG IO_CONFIG(GPIO_MODE_MUX, GPIO_DRIVE_STRENGTH_STRONGER, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_DOWN)
38 #define SPI_IO_AF_MISO_CFG IO_CONFIG(GPIO_MODE_MUX, GPIO_DRIVE_STRENGTH_STRONGER, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_UP)
39 #define SPI_IO_CS_CFG IO_CONFIG(GPIO_MODE_OUTPUT, GPIO_DRIVE_STRENGTH_STRONGER, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE)
43 Flash M25p16 tolerates 20mhz, SPI_CLOCK_FAST should sit around 20 or less.
47 SPI_CLOCK_INITIALIZATON
= 0, // Lowest possible
48 SPI_CLOCK_SLOW
= 1, // ~1 MHz
49 SPI_CLOCK_STANDARD
= 2, // ~10MHz
50 SPI_CLOCK_FAST
= 3, // ~20MHz
51 SPI_CLOCK_ULTRAFAST
= 4 // Highest possible
54 typedef enum SPIDevice
{
63 #define SPIDEV_COUNT 3
64 #elif defined(STM32F7) || defined(STM32H7)|| defined(AT32F43x)
65 #define SPIDEV_COUNT 4
67 #define SPIDEV_COUNT 4
70 typedef struct SPIDevice_s
{
81 #if defined(STM32F7) || defined(STM32H7) || defined(AT32F43x)
88 const uint32_t * divisorMap
;
89 volatile uint16_t errorCount
;
93 bool spiInitDevice(SPIDevice device
, bool leadingEdge
);
97 bool spiIsBusBusy(spi_type
*instance
);
98 void spiSetSpeed(spi_type
*instance
, SPIClockSpeed_e speed
);
99 uint8_t spiTransferByte(spi_type
*instance
, uint8_t in
);
100 bool spiTransfer(spi_type
*instance
, uint8_t *rxData
, const uint8_t *txData
, int len
);
102 uint16_t spiGetErrorCounter(spi_type
*instance
);
103 void spiResetErrorCounter(spi_type
*instance
);
104 SPIDevice
spiDeviceByInstance(spi_type
*instance
);
105 spi_type
* spiInstanceByDevice(SPIDevice device
);
108 bool spiIsBusBusy(SPI_TypeDef
*instance
);
109 void spiSetSpeed(SPI_TypeDef
*instance
, SPIClockSpeed_e speed
);
110 uint8_t spiTransferByte(SPI_TypeDef
*instance
, uint8_t in
);
111 bool spiTransfer(SPI_TypeDef
*instance
, uint8_t *rxData
, const uint8_t *txData
, int len
);
113 uint16_t spiGetErrorCounter(SPI_TypeDef
*instance
);
114 void spiResetErrorCounter(SPI_TypeDef
*instance
);
115 SPIDevice
spiDeviceByInstance(SPI_TypeDef
*instance
);
116 SPI_TypeDef
* spiInstanceByDevice(SPIDevice device
);