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
29 #include "build/build_config.h"
31 #include "drivers/bus_spi.h"
32 #include "bus_spi_soft.h"
33 #include "drivers/io.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));}
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
) {
62 if (spiType
== RX_SPI_SOFTSPI
) {
64 softSpiInit(&softSPIDevice
);
66 const SPIDevice rxSPIDevice
= SOFT_SPIDEV_1
;
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
75 #ifdef RX_SPI_INSTANCE
76 spiSetDivisor(RX_SPI_INSTANCE
, SPI_CLOCK_STANDARD
);
78 hardwareInitialised
= true;
81 uint8_t rxSpiTransferByte(uint8_t data
)
85 return softSpiTransferByte(&softSPIDevice
, data
);
89 #ifdef RX_SPI_INSTANCE
90 return spiTransferByte(RX_SPI_INSTANCE
, data
);
97 uint8_t rxSpiWriteByte(uint8_t data
)
100 const uint8_t ret
= rxSpiTransferByte(data
);
105 uint8_t rxSpiWriteCommand(uint8_t command
, uint8_t data
)
108 const uint8_t ret
= rxSpiTransferByte(command
);
109 rxSpiTransferByte(data
);
114 uint8_t rxSpiWriteCommandMulti(uint8_t command
, const uint8_t *data
, uint8_t length
)
117 const uint8_t ret
= rxSpiTransferByte(command
);
118 for (uint8_t i
= 0; i
< length
; i
++) {
119 rxSpiTransferByte(data
[i
]);
125 uint8_t rxSpiReadCommand(uint8_t command
, uint8_t data
)
128 rxSpiTransferByte(command
);
129 const uint8_t ret
= rxSpiTransferByte(data
);
134 uint8_t rxSpiReadCommandMulti(uint8_t command
, uint8_t commandData
, uint8_t *retData
, uint8_t length
)
137 const uint8_t ret
= rxSpiTransferByte(command
);
138 for (uint8_t i
= 0; i
< length
; i
++) {
139 retData
[i
] = rxSpiTransferByte(commandData
);