Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / bus_busdev_spi.c
blob51a30d849443ec5df3e72703afc84e9c96cb8613
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 #include <stdbool.h>
19 #include <stdint.h>
20 #include <string.h>
22 #include <platform.h>
23 #include <build/debug.h>
25 #if defined(USE_SPI)
27 #include "drivers/io.h"
28 #include "drivers/bus.h"
29 #include "drivers/bus_spi.h"
30 #include "drivers/time.h"
32 void spiChipSelectSetupDelay(void)
34 // CS->CLK delay, MPU6000 - 8ns
35 delayNanos(8);
38 void spiChipSelectHoldTime(void)
40 // CLK->CS delay, MPU6000 - 500ns
41 delayNanos(500);
44 bool spiBusInitHost(const busDevice_t * dev)
46 const bool spiLeadingEdge = (dev->flags & DEVFLAGS_SPI_MODE_0);
47 return spiInitDevice(dev->busdev.spi.spiBus, spiLeadingEdge);
50 void spiBusSelectDevice(const busDevice_t * dev)
52 IOLo(dev->busdev.spi.csnPin);
53 spiChipSelectSetupDelay();
56 void spiBusDeselectDevice(const busDevice_t * dev)
58 spiChipSelectHoldTime();
59 IOHi(dev->busdev.spi.csnPin);
62 void spiBusSetSpeed(const busDevice_t * dev, busSpeed_e speed)
64 const SPIClockSpeed_e spiClock[] = { SPI_CLOCK_INITIALIZATON, SPI_CLOCK_SLOW, SPI_CLOCK_STANDARD, SPI_CLOCK_FAST, SPI_CLOCK_ULTRAFAST };
66 #if defined(AT32F43x)
67 spi_type * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
68 #else
69 SPI_TypeDef * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
70 #endif
72 #ifdef BUS_SPI_SPEED_MAX
73 if (speed > BUS_SPI_SPEED_MAX)
74 speed = BUS_SPI_SPEED_MAX;
75 #endif
77 spiSetSpeed(instance, spiClock[speed]);
81 bool spiBusTransfer(const busDevice_t * dev, uint8_t * rxBuf, const uint8_t * txBuf, int length)
83 #if defined(AT32F43x)
84 spi_type * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
85 #else
86 SPI_TypeDef * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
87 #endif
89 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
90 spiBusSelectDevice(dev);
93 spiTransfer(instance, rxBuf, txBuf, length);
95 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
96 spiBusDeselectDevice(dev);
99 return true;
102 bool spiBusTransferMultiple(const busDevice_t * dev, busTransferDescriptor_t * dsc, int count)
104 #if defined(AT32F43x)
105 spi_type * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
106 #else
107 SPI_TypeDef * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
108 #endif
110 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
111 spiBusSelectDevice(dev);
114 for (int n = 0; n < count; n++) {
115 spiTransfer(instance, dsc[n].rxBuf, dsc[n].txBuf, dsc[n].length);
118 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
119 spiBusDeselectDevice(dev);
122 return true;
125 bool spiBusWriteRegister(const busDevice_t * dev, uint8_t reg, uint8_t data)
127 #if defined(AT32F43x)
128 spi_type * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
129 #else
130 SPI_TypeDef * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
131 #endif
133 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
134 spiBusSelectDevice(dev);
137 spiTransferByte(instance, reg);
138 spiTransferByte(instance, data);
140 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
141 spiBusDeselectDevice(dev);
144 return true;
147 bool spiBusWriteBuffer(const busDevice_t * dev, uint8_t reg, const uint8_t * data, uint8_t length)
149 #if defined(AT32F43x)
150 spi_type * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
151 #else
152 SPI_TypeDef * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
153 #endif
155 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
156 spiBusSelectDevice(dev);
159 spiTransferByte(instance, reg);
160 spiTransfer(instance, NULL, data, length);
162 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
163 spiBusDeselectDevice(dev);
166 return true;
169 bool spiBusReadBuffer(const busDevice_t * dev, uint8_t reg, uint8_t * data, uint8_t length)
171 #if defined(AT32F43x)
172 spi_type * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
173 #else
174 SPI_TypeDef * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
175 #endif
177 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
178 spiBusSelectDevice(dev);
181 spiTransferByte(instance, reg);
182 spiTransfer(instance, data, NULL, length);
184 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
185 spiBusDeselectDevice(dev);
188 return true;
191 bool spiBusReadRegister(const busDevice_t * dev, uint8_t reg, uint8_t * data)
193 #if defined(AT32F43x)
194 spi_type * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
195 #else
196 SPI_TypeDef * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
197 #endif
199 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
200 spiBusSelectDevice(dev);
203 spiTransferByte(instance, reg);
204 spiTransfer(instance, data, NULL, 1);
206 if (!(dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
207 spiBusDeselectDevice(dev);
210 return true;
213 bool spiBusIsBusy(const busDevice_t * dev)
215 #if defined(AT32F43x)
216 spi_type * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
217 #else
218 SPI_TypeDef * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
219 #endif
220 return spiIsBusBusy(instance);
222 #endif