Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / drivers / bus_spi_config.c
blob72c9c16e3b084f45c480b8f135f294f74510ac82
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
26 #ifdef USE_SPI
28 #include "drivers/io.h"
29 #include "drivers/resource.h"
30 #include "drivers/system.h"
32 #include "drivers/flash.h"
33 #include "drivers/max7456.h"
34 #include "drivers/rx/rx_spi.h"
35 #include "drivers/sdcard.h"
37 #include "pg/flash.h"
38 #include "pg/max7456.h"
39 #include "pg/rx_spi.h"
40 #include "pg/sdcard.h"
42 typedef struct spiPreinit_s {
43 ioTag_t iotag;
44 uint8_t iocfg;
45 bool init;
46 } spiPreinit_t;
48 static spiPreinit_t spiPreinitArray[SPI_PREINIT_COUNT];
49 static int spiPreinitCount = 0;
51 void spiPreinitRegister(ioTag_t iotag, uint8_t iocfg, bool init)
53 if (!iotag) {
54 return;
57 if (spiPreinitCount == SPI_PREINIT_COUNT) {
58 indicateFailure(FAILURE_DEVELOPER, 5);
59 return;
62 spiPreinitArray[spiPreinitCount].iotag = iotag;
63 spiPreinitArray[spiPreinitCount].iocfg = iocfg;
64 spiPreinitArray[spiPreinitCount].init = init;
65 ++spiPreinitCount;
68 static void spiPreinitPin(spiPreinit_t *preinit, int index)
70 IO_t io = IOGetByTag(preinit->iotag);
71 IOInit(io, OWNER_PREINIT, RESOURCE_INDEX(index));
72 IOConfigGPIO(io, preinit->iocfg);
73 if (preinit->init) {
74 IOHi(io);
75 } else {
76 IOLo(io);
80 void spiPreinit(void)
82 #ifdef USE_SDCARD_SPI
83 sdcard_preInit(sdcardConfig());
84 #endif
86 #if defined(RTC6705_CS_PIN) && !defined(USE_VTX_RTC6705_SOFTSPI) // RTC6705 soft SPI initialisation handled elsewhere.
87 // XXX Waiting for "RTC6705 cleanup #7114" to be done
88 #endif
90 #ifdef USE_FLASH_CHIP
91 flashPreInit(flashConfig());
92 #endif
94 #if defined(USE_RX_SPI)
95 rxSpiDevicePreInit(rxSpiConfig());
96 #endif
98 #ifdef USE_MAX7456
99 max7456PreInit(max7456Config());
100 #endif
102 for (int i = 0; i < spiPreinitCount; i++) {
103 spiPreinitPin(&spiPreinitArray[i], i);
107 void spiPreinitByIO(IO_t io)
109 for (int i = 0; i < spiPreinitCount; i++) {
110 if (io == IOGetByTag(spiPreinitArray[i].iotag)) {
111 spiPreinitPin(&spiPreinitArray[i], i);
112 return;
117 void spiPreinitByTag(ioTag_t tag)
119 spiPreinitByIO(IOGetByTag(tag));
121 #endif