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)
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/>.
29 #include "build/debug.h"
31 #include "drivers/bus_spi.h"
32 #include "drivers/bus_spi_impl.h"
33 #include "drivers/dma.h"
34 #include "drivers/exti.h"
35 #include "drivers/io.h"
36 #include "drivers/rcc.h"
38 #include "pg/bus_spi.h"
40 const spiHardware_t spiHardware
[] = {
58 .rcc
= RCC_APB2(SPI1
),
64 { DEFIO_TAG_E(PB10
) },
65 { DEFIO_TAG_E(PB13
) },
68 { DEFIO_TAG_E(PB14
) },
72 { DEFIO_TAG_E(PB15
) },
76 .rcc
= RCC_APB1(SPI2
),
83 { DEFIO_TAG_E(PC10
) },
87 { DEFIO_TAG_E(PC11
) },
91 { DEFIO_TAG_E(PC12
) },
94 .rcc
= RCC_APB1(SPI3
),
102 { DEFIO_TAG_E(PA5
), GPIO_AF5_SPI1
},
103 { DEFIO_TAG_E(PB3
), GPIO_AF5_SPI1
},
106 { DEFIO_TAG_E(PA6
), GPIO_AF5_SPI1
},
107 { DEFIO_TAG_E(PB4
), GPIO_AF5_SPI1
},
110 { DEFIO_TAG_E(PA7
), GPIO_AF5_SPI1
},
111 { DEFIO_TAG_E(PB5
), GPIO_AF5_SPI1
},
113 .rcc
= RCC_APB2(SPI1
),
114 .dmaIrqHandler
= DMA2_ST3_HANDLER
,
120 { DEFIO_TAG_E(PA9
), GPIO_AF5_SPI2
},
121 { DEFIO_TAG_E(PB10
), GPIO_AF5_SPI2
},
122 { DEFIO_TAG_E(PB13
), GPIO_AF5_SPI2
},
123 { DEFIO_TAG_E(PD3
), GPIO_AF5_SPI2
},
126 { DEFIO_TAG_E(PB14
), GPIO_AF5_SPI2
},
127 { DEFIO_TAG_E(PC2
), GPIO_AF5_SPI2
},
130 { DEFIO_TAG_E(PB15
), GPIO_AF5_SPI2
},
131 { DEFIO_TAG_E(PC1
), GPIO_AF5_SPI2
},
132 { DEFIO_TAG_E(PC3
), GPIO_AF5_SPI2
},
134 .rcc
= RCC_APB1(SPI2
),
135 .dmaIrqHandler
= DMA1_ST4_HANDLER
,
141 { DEFIO_TAG_E(PB3
), GPIO_AF6_SPI3
},
142 { DEFIO_TAG_E(PC10
), GPIO_AF6_SPI3
},
145 { DEFIO_TAG_E(PB4
), GPIO_AF6_SPI3
},
146 { DEFIO_TAG_E(PC11
), GPIO_AF6_SPI3
},
149 { DEFIO_TAG_E(PB2
), GPIO_AF7_SPI3
},
150 { DEFIO_TAG_E(PB5
), GPIO_AF6_SPI3
},
151 { DEFIO_TAG_E(PC12
), GPIO_AF6_SPI3
},
152 { DEFIO_TAG_E(PD6
), GPIO_AF5_SPI3
},
154 .rcc
= RCC_APB1(SPI3
),
155 .dmaIrqHandler
= DMA1_ST7_HANDLER
,
161 { DEFIO_TAG_E(PE2
), GPIO_AF5_SPI4
},
162 { DEFIO_TAG_E(PE12
), GPIO_AF5_SPI4
},
165 { DEFIO_TAG_E(PE5
), GPIO_AF5_SPI4
},
166 { DEFIO_TAG_E(PE13
), GPIO_AF5_SPI4
},
169 { DEFIO_TAG_E(PE6
), GPIO_AF5_SPI4
},
170 { DEFIO_TAG_E(PE14
), GPIO_AF5_SPI4
},
172 .rcc
= RCC_APB2(SPI4
),
173 .dmaIrqHandler
= DMA2_ST1_HANDLER
,
181 { DEFIO_TAG_E(PA5
), GPIO_AF5_SPI1
},
182 { DEFIO_TAG_E(PB3
), GPIO_AF5_SPI1
},
185 { DEFIO_TAG_E(PA6
), GPIO_AF5_SPI1
},
186 { DEFIO_TAG_E(PB4
), GPIO_AF5_SPI1
},
189 { DEFIO_TAG_E(PA7
), GPIO_AF5_SPI1
},
190 { DEFIO_TAG_E(PB5
), GPIO_AF5_SPI1
},
191 { DEFIO_TAG_E(PD7
), GPIO_AF5_SPI1
},
193 .rcc
= RCC_APB2(SPI1
),
194 //.dmaIrqHandler = DMA2_ST3_HANDLER,
200 { DEFIO_TAG_E(PA9
), GPIO_AF5_SPI2
},
201 { DEFIO_TAG_E(PA12
), GPIO_AF5_SPI2
},
202 { DEFIO_TAG_E(PB10
), GPIO_AF5_SPI2
},
203 { DEFIO_TAG_E(PB13
), GPIO_AF5_SPI2
},
204 { DEFIO_TAG_E(PD3
), GPIO_AF5_SPI2
},
207 { DEFIO_TAG_E(PB14
), GPIO_AF5_SPI2
},
208 { DEFIO_TAG_E(PC2
), GPIO_AF5_SPI2
},
211 { DEFIO_TAG_E(PB15
), GPIO_AF5_SPI2
},
212 { DEFIO_TAG_E(PC1
), GPIO_AF5_SPI2
},
213 { DEFIO_TAG_E(PC3
), GPIO_AF5_SPI2
},
215 .rcc
= RCC_APB1L(SPI2
),
216 //.dmaIrqHandler = DMA1_ST4_HANDLER,
222 { DEFIO_TAG_E(PB3
), GPIO_AF6_SPI3
},
223 { DEFIO_TAG_E(PC10
), GPIO_AF6_SPI3
},
226 { DEFIO_TAG_E(PB4
), GPIO_AF6_SPI3
},
227 { DEFIO_TAG_E(PC11
), GPIO_AF6_SPI3
},
230 { DEFIO_TAG_E(PB2
), GPIO_AF7_SPI3
},
231 { DEFIO_TAG_E(PB5
), GPIO_AF7_SPI3
},
232 { DEFIO_TAG_E(PC12
), GPIO_AF6_SPI3
},
233 { DEFIO_TAG_E(PD6
), GPIO_AF5_SPI3
},
235 .rcc
= RCC_APB1L(SPI3
),
236 //.dmaIrqHandler = DMA1_ST7_HANDLER,
242 { DEFIO_TAG_E(PE2
), GPIO_AF5_SPI4
},
243 { DEFIO_TAG_E(PE12
), GPIO_AF5_SPI4
},
246 { DEFIO_TAG_E(PE5
), GPIO_AF5_SPI4
},
247 { DEFIO_TAG_E(PE13
), GPIO_AF5_SPI4
},
250 { DEFIO_TAG_E(PE6
), GPIO_AF5_SPI4
},
251 { DEFIO_TAG_E(PE14
), GPIO_AF5_SPI4
},
253 .rcc
= RCC_APB2(SPI4
),
254 //.dmaIrqHandler = DMA2_ST1_HANDLER,
260 { DEFIO_TAG_E(PF7
), GPIO_AF5_SPI5
},
263 { DEFIO_TAG_E(PF8
), GPIO_AF5_SPI5
},
266 { DEFIO_TAG_E(PF11
), GPIO_AF5_SPI5
},
268 .rcc
= RCC_APB2(SPI5
),
269 //.dmaIrqHandler = DMA2_ST1_HANDLER,
275 { DEFIO_TAG_E(PA5
), GPIO_AF8_SPI6
},
276 { DEFIO_TAG_E(PB3
), GPIO_AF8_SPI6
},
279 { DEFIO_TAG_E(PA6
), GPIO_AF8_SPI6
},
280 { DEFIO_TAG_E(PB4
), GPIO_AF8_SPI6
},
283 { DEFIO_TAG_E(PA7
), GPIO_AF8_SPI6
},
284 { DEFIO_TAG_E(PB5
), GPIO_AF8_SPI6
},
286 .rcc
= RCC_APB4(SPI6
),
287 //.dmaIrqHandler = DMA2_ST1_HANDLER,
295 { DEFIO_TAG_E(PA5
), GPIO_AF5_SPI1
},
296 { DEFIO_TAG_E(PB3
), GPIO_AF5_SPI1
},
299 { DEFIO_TAG_E(PA6
), GPIO_AF5_SPI1
},
300 { DEFIO_TAG_E(PB4
), GPIO_AF5_SPI1
},
303 { DEFIO_TAG_E(PA7
), GPIO_AF5_SPI1
},
304 { DEFIO_TAG_E(PB5
), GPIO_AF5_SPI1
},
306 .rcc
= RCC_APB2(SPI1
),
307 //.dmaIrqHandler = DMA2_ST3_HANDLER,
313 { DEFIO_TAG_E(PB13
), GPIO_AF5_SPI2
},
316 { DEFIO_TAG_E(PA10
), GPIO_AF5_SPI2
},
317 { DEFIO_TAG_E(PB14
), GPIO_AF5_SPI2
},
320 { DEFIO_TAG_E(PA11
), GPIO_AF5_SPI2
},
321 { DEFIO_TAG_E(PB15
), GPIO_AF5_SPI2
},
323 .rcc
= RCC_APB11(SPI2
),
324 //.dmaIrqHandler = DMA1_ST4_HANDLER,
330 { DEFIO_TAG_E(PB3
), GPIO_AF6_SPI3
},
331 { DEFIO_TAG_E(PC10
), GPIO_AF6_SPI3
},
334 { DEFIO_TAG_E(PB4
), GPIO_AF6_SPI3
},
335 { DEFIO_TAG_E(PC11
), GPIO_AF6_SPI3
},
338 { DEFIO_TAG_E(PB5
), GPIO_AF6_SPI3
},
339 { DEFIO_TAG_E(PC12
), GPIO_AF6_SPI3
},
341 .rcc
= RCC_APB11(SPI3
),
342 //.dmaIrqHandler = DMA1_ST7_HANDLER,
347 void spiPinConfigure(const spiPinConfig_t
*pConfig
)
349 for (size_t hwindex
= 0 ; hwindex
< ARRAYLEN(spiHardware
) ; hwindex
++) {
350 const spiHardware_t
*hw
= &spiHardware
[hwindex
];
356 SPIDevice device
= hw
->device
;
357 spiDevice_t
*pDev
= &spiDevice
[device
];
359 for (int pindex
= 0 ; pindex
< MAX_SPI_PIN_SEL
; pindex
++) {
360 if (pConfig
[device
].ioTagSck
== hw
->sckPins
[pindex
].pin
) {
361 pDev
->sck
= hw
->sckPins
[pindex
].pin
;
362 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
363 pDev
->sckAF
= hw
->sckPins
[pindex
].af
;
366 if (pConfig
[device
].ioTagMiso
== hw
->misoPins
[pindex
].pin
) {
367 pDev
->miso
= hw
->misoPins
[pindex
].pin
;
368 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
369 pDev
->misoAF
= hw
->misoPins
[pindex
].af
;
372 if (pConfig
[device
].ioTagMosi
== hw
->mosiPins
[pindex
].pin
) {
373 pDev
->mosi
= hw
->mosiPins
[pindex
].pin
;
374 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
375 pDev
->mosiAF
= hw
->mosiPins
[pindex
].af
;
380 if (pDev
->sck
&& pDev
->miso
&& pDev
->mosi
) {
382 #if !(defined(STM32F7) || defined(STM32H7) || defined(STM32G4))
386 pDev
->leadingEdge
= false; // XXX Should be part of transfer context
387 #if defined(USE_DMA) && defined(USE_HAL_DRIVER)
388 pDev
->dmaIrqHandler
= hw
->dmaIrqHandler
;