IO Clean up and use of Low level for F7
[betaflight.git] / src / main / drivers / rx_spi.c
blobf8f8bfdbaa7ed689ae664d652fcd0d2cf7573c58
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 // This file is copied with modifications from project Deviation,
19 // see http://deviationtx.com
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <stdlib.h>
25 #include <platform.h>
27 #ifdef USE_RX_SPI
29 #include "build/build_config.h"
31 #include "drivers/bus_spi.h"
32 #include "bus_spi_soft.h"
33 #include "drivers/io.h"
34 #include "io_impl.h"
35 #include "rcc.h"
36 #include "rx_spi.h"
37 #include "drivers/system.h"
39 #define DISABLE_RX() {IOHi(DEFIO_IO(RX_NSS_PIN));}
40 #define ENABLE_RX() {IOLo(DEFIO_IO(RX_NSS_PIN));}
42 #ifdef USE_RX_SOFTSPI
43 static const softSPIDevice_t softSPIDevice = {
44 .sckTag = IO_TAG(RX_SCK_PIN),
45 .mosiTag = IO_TAG(RX_MOSI_PIN),
46 .misoTag = IO_TAG(RX_MISO_PIN),
47 // Note: Nordic Semiconductor uses 'CSN', STM uses 'NSS'
48 .nssTag = IO_TAG(RX_NSS_PIN),
50 static bool useSoftSPI = false;
51 #endif // USE_RX_SOFTSPI
53 void rxSpiDeviceInit(rx_spi_type_e spiType)
55 static bool hardwareInitialised = false;
57 if (hardwareInitialised) {
58 return;
61 #ifdef USE_RX_SOFTSPI
62 if (spiType == RX_SPI_SOFTSPI) {
63 useSoftSPI = true;
64 softSpiInit(&softSPIDevice);
66 const SPIDevice rxSPIDevice = SOFT_SPIDEV_1;
67 #else
68 UNUSED(spiType);
69 const SPIDevice rxSPIDevice = spiDeviceByInstance(RX_SPI_INSTANCE);
70 IOInit(DEFIO_IO(RX_NSS_PIN), OWNER_SPI_CS, rxSPIDevice + 1);
71 #endif // USE_RX_SOFTSPI
73 DISABLE_RX();
75 #ifdef RX_SPI_INSTANCE
76 spiSetDivisor(RX_SPI_INSTANCE, SPI_CLOCK_STANDARD);
77 #endif
78 hardwareInitialised = true;
81 uint8_t rxSpiTransferByte(uint8_t data)
83 #ifdef USE_RX_SOFTSPI
84 if (useSoftSPI) {
85 return softSpiTransferByte(&softSPIDevice, data);
86 } else
87 #endif
89 #ifdef RX_SPI_INSTANCE
90 return spiTransferByte(RX_SPI_INSTANCE, data);
91 #else
92 return 0;
93 #endif
97 uint8_t rxSpiWriteByte(uint8_t data)
99 ENABLE_RX();
100 const uint8_t ret = rxSpiTransferByte(data);
101 DISABLE_RX();
102 return ret;
105 uint8_t rxSpiWriteCommand(uint8_t command, uint8_t data)
107 ENABLE_RX();
108 const uint8_t ret = rxSpiTransferByte(command);
109 rxSpiTransferByte(data);
110 DISABLE_RX();
111 return ret;
114 uint8_t rxSpiWriteCommandMulti(uint8_t command, const uint8_t *data, uint8_t length)
116 ENABLE_RX();
117 const uint8_t ret = rxSpiTransferByte(command);
118 for (uint8_t i = 0; i < length; i++) {
119 rxSpiTransferByte(data[i]);
121 DISABLE_RX();
122 return ret;
125 uint8_t rxSpiReadCommand(uint8_t command, uint8_t data)
127 ENABLE_RX();
128 rxSpiTransferByte(command);
129 const uint8_t ret = rxSpiTransferByte(data);
130 DISABLE_RX();
131 return ret;
134 uint8_t rxSpiReadCommandMulti(uint8_t command, uint8_t commandData, uint8_t *retData, uint8_t length)
136 ENABLE_RX();
137 const uint8_t ret = rxSpiTransferByte(command);
138 for (uint8_t i = 0; i < length; i++) {
139 retData[i] = rxSpiTransferByte(commandData);
141 DISABLE_RX();
142 return ret;
144 #endif