Acc g scaling with DSP
[inav.git] / src / main / drivers / bus_spi.h
blobe55119564c25719a6000238f1ee1e505a482181d
1 /*
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/>.
18 #pragma once
20 #include "drivers/io_types.h"
21 #include "drivers/rcc_types.h"
22 #include "drivers/dma.h"
24 #if defined(STM32F4)
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)
40 #endif
43 Flash M25p16 tolerates 20mhz, SPI_CLOCK_FAST should sit around 20 or less.
46 typedef enum {
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
52 } SPIClockSpeed_e;
54 typedef enum SPIDevice {
55 SPIINVALID = -1,
56 SPIDEV_1 = 0,
57 SPIDEV_2,
58 SPIDEV_3,
59 SPIDEV_4
60 } SPIDevice;
62 #if defined(STM32F4)
63 #define SPIDEV_COUNT 3
64 #elif defined(STM32F7) || defined(STM32H7)|| defined(AT32F43x)
65 #define SPIDEV_COUNT 4
66 #else
67 #define SPIDEV_COUNT 4
68 #endif
70 typedef struct SPIDevice_s {
71 #if defined(AT32F43x)
72 spi_type *dev;
73 #else
74 SPI_TypeDef *dev;
75 #endif
76 ioTag_t nss;
77 ioTag_t sck;
78 ioTag_t mosi;
79 ioTag_t miso;
80 rccPeriphTag_t rcc;
81 #if defined(STM32F7) || defined(STM32H7)
82 uint8_t sckAF;
83 uint8_t misoAF;
84 uint8_t mosiAF;
85 #else
86 uint8_t af;
87 #endif
88 const uint32_t * divisorMap;
89 volatile uint16_t errorCount;
90 bool initDone;
91 } spiDevice_t;
93 bool spiInitDevice(SPIDevice device, bool leadingEdge);
95 #if defined(AT32F43x)
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);
107 #else
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);
117 #endif