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/>.
27 #include "build/atomic.h"
31 #include "drivers/bus.h"
32 #include "drivers/bus_spi.h"
33 #include "drivers/bus_spi_impl.h"
34 #include "drivers/dma_reqmap.h"
35 #include "drivers/exti.h"
36 #include "drivers/io.h"
37 #include "drivers/motor.h"
38 #include "drivers/rcc.h"
40 #include "pg/bus_spi.h"
42 #define NUM_QUEUE_SEGS 5
44 static uint8_t spiRegisteredDeviceCount
= 0;
46 spiDevice_t spiDevice
[SPIDEV_COUNT
];
47 busDevice_t spiBusDevice
[SPIDEV_COUNT
];
49 SPIDevice
spiDeviceByInstance(SPI_TypeDef
*instance
)
51 #ifdef USE_SPI_DEVICE_1
52 if (instance
== SPI1
) {
57 #ifdef USE_SPI_DEVICE_2
58 if (instance
== SPI2
) {
63 #ifdef USE_SPI_DEVICE_3
64 if (instance
== SPI3
) {
69 #ifdef USE_SPI_DEVICE_4
70 if (instance
== SPI4
) {
75 #ifdef USE_SPI_DEVICE_5
76 if (instance
== SPI5
) {
81 #ifdef USE_SPI_DEVICE_6
82 if (instance
== SPI6
) {
90 SPI_TypeDef
*spiInstanceByDevice(SPIDevice device
)
92 if (device
== SPIINVALID
|| device
>= SPIDEV_COUNT
) {
96 return spiDevice
[device
].dev
;
99 bool spiInit(SPIDevice device
)
106 #ifdef USE_SPI_DEVICE_1
107 spiInitDevice(device
);
114 #ifdef USE_SPI_DEVICE_2
115 spiInitDevice(device
);
122 #if defined(USE_SPI_DEVICE_3) && !defined(STM32F1)
123 spiInitDevice(device
);
130 #if defined(USE_SPI_DEVICE_4)
131 spiInitDevice(device
);
138 #if defined(USE_SPI_DEVICE_5)
139 spiInitDevice(device
);
146 #if defined(USE_SPI_DEVICE_6)
147 spiInitDevice(device
);
156 // Return true if DMA engine is busy
157 bool spiIsBusy(const extDevice_t
*dev
)
159 return (dev
->bus
->curSegment
!= (busSegment_t
*)BUS_SPI_FREE
);
162 // Wait for DMA completion
163 void spiWait(const extDevice_t
*dev
)
165 // Wait for completion
166 while (dev
->bus
->curSegment
!= (busSegment_t
*)BUS_SPI_FREE
);
169 // Wait for bus to become free, then read/write block of data
170 void spiReadWriteBuf(const extDevice_t
*dev
, uint8_t *txData
, uint8_t *rxData
, int len
)
172 // This routine blocks so no need to use static data
173 busSegment_t segments
[] = {
174 {.u
.buffers
= {txData
, rxData
}, len
, true, NULL
},
175 {.u
.buffers
= {NULL
, NULL
}, 0, true, NULL
},
178 spiSequence(dev
, &segments
[0]);
183 // Read/Write a block of data, returning false if the bus is busy
184 bool spiReadWriteBufRB(const extDevice_t
*dev
, uint8_t *txData
, uint8_t *rxData
, int length
)
186 // Ensure any prior DMA has completed before continuing
187 if (spiIsBusy(dev
)) {
191 spiReadWriteBuf(dev
, txData
, rxData
, length
);
196 // Wait for bus to become free, then read/write a single byte
197 uint8_t spiReadWrite(const extDevice_t
*dev
, uint8_t data
)
201 // This routine blocks so no need to use static data
202 busSegment_t segments
[] = {
203 {.u
.buffers
= {&data
, &retval
}, sizeof(data
), true, NULL
},
204 {.u
.buffers
= {NULL
, NULL
}, 0, true, NULL
},
207 spiSequence(dev
, &segments
[0]);
214 // Wait for bus to become free, then read/write a single byte from a register
215 uint8_t spiReadWriteReg(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
)
219 // This routine blocks so no need to use static data
220 busSegment_t segments
[] = {
221 {.u
.buffers
= {®
, NULL
}, sizeof(reg
), false, NULL
},
222 {.u
.buffers
= {&data
, &retval
}, sizeof(data
), true, NULL
},
223 {.u
.buffers
= {NULL
, NULL
}, 0, true, NULL
},
226 spiSequence(dev
, &segments
[0]);
233 // Wait for bus to become free, then write a single byte
234 void spiWrite(const extDevice_t
*dev
, uint8_t data
)
236 // This routine blocks so no need to use static data
237 busSegment_t segments
[] = {
238 {.u
.buffers
= {&data
, NULL
}, sizeof(data
), true, NULL
},
239 {.u
.buffers
= {NULL
, NULL
}, 0, true, NULL
},
242 spiSequence(dev
, &segments
[0]);
247 // Write data to a register
248 void spiWriteReg(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
)
250 // This routine blocks so no need to use static data
251 busSegment_t segments
[] = {
252 {.u
.buffers
= {®
, NULL
}, sizeof(reg
), false, NULL
},
253 {.u
.buffers
= {&data
, NULL
}, sizeof(data
), true, NULL
},
254 {.u
.buffers
= {NULL
, NULL
}, 0, true, NULL
},
257 spiSequence(dev
, &segments
[0]);
262 // Write data to a register, returning false if the bus is busy
263 bool spiWriteRegRB(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
)
265 // Ensure any prior DMA has completed before continuing
266 if (spiIsBusy(dev
)) {
270 spiWriteReg(dev
, reg
, data
);
275 // Read a block of data from a register
276 void spiReadRegBuf(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
)
278 // This routine blocks so no need to use static data
279 busSegment_t segments
[] = {
280 {.u
.buffers
= {®
, NULL
}, sizeof(reg
), false, NULL
},
281 {.u
.buffers
= {NULL
, data
}, length
, true, NULL
},
282 {.u
.buffers
= {NULL
, NULL
}, 0, true, NULL
},
285 spiSequence(dev
, &segments
[0]);
290 // Read a block of data from a register, returning false if the bus is busy
291 bool spiReadRegBufRB(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
)
293 // Ensure any prior DMA has completed before continuing
294 if (spiIsBusy(dev
)) {
298 spiReadRegBuf(dev
, reg
, data
, length
);
303 // Read a block of data where the register is ORed with 0x80, returning false if the bus is busy
304 bool spiReadRegMskBufRB(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
)
306 return spiReadRegBufRB(dev
, reg
| 0x80, data
, length
);
309 // Wait for bus to become free, then write a block of data to a register
310 void spiWriteRegBuf(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint32_t length
)
312 // This routine blocks so no need to use static data
313 busSegment_t segments
[] = {
314 {.u
.buffers
= {®
, NULL
}, sizeof(reg
), false, NULL
},
315 {.u
.buffers
= {data
, NULL
}, length
, true, NULL
},
316 {.u
.buffers
= {NULL
, NULL
}, 0, true, NULL
},
319 spiSequence(dev
, &segments
[0]);
324 // Wait for bus to become free, then read a byte from a register
325 uint8_t spiReadReg(const extDevice_t
*dev
, uint8_t reg
)
328 // This routine blocks so no need to use static data
329 busSegment_t segments
[] = {
330 {.u
.buffers
= {®
, NULL
}, sizeof(reg
), false, NULL
},
331 {.u
.buffers
= {NULL
, &data
}, sizeof(data
), true, NULL
},
332 {.u
.buffers
= {NULL
, NULL
}, 0, true, NULL
},
335 spiSequence(dev
, &segments
[0]);
342 // Wait for bus to become free, then read a byte of data where the register is ORed with 0x80
343 uint8_t spiReadRegMsk(const extDevice_t
*dev
, uint8_t reg
)
345 return spiReadReg(dev
, reg
| 0x80);
348 uint16_t spiCalculateDivider(uint32_t freq
)
350 #if defined(STM32F4) || defined(STM32G4) || defined(STM32F7)
351 uint32_t spiClk
= SystemCoreClock
/ 2;
352 #elif defined(STM32H7)
353 uint32_t spiClk
= 100000000;
355 #error "Base SPI clock not defined for this architecture"
358 uint16_t divisor
= 2;
362 for (; (spiClk
> freq
) && (divisor
< 256); divisor
<<= 1, spiClk
>>= 1);
367 uint32_t spiCalculateClock(uint16_t spiClkDivisor
)
369 #if defined(STM32F4) || defined(STM32G4) || defined(STM32F7)
370 uint32_t spiClk
= SystemCoreClock
/ 2;
371 #elif defined(STM32H7)
372 uint32_t spiClk
= 100000000;
374 #error "Base SPI clock not defined for this architecture"
377 return spiClk
/ spiClkDivisor
;
380 // Interrupt handler for SPI receive DMA completion
381 static void spiIrqHandler(const extDevice_t
*dev
)
383 busDevice_t
*bus
= dev
->bus
;
384 busSegment_t
*nextSegment
;
386 if (bus
->curSegment
->callback
) {
387 switch(bus
->curSegment
->callback(dev
->callbackArg
)) {
389 // Repeat the last DMA segment
391 // Reinitialise the cached init values as segment is not progressing
392 spiInternalInitStream(dev
, true);
396 bus
->curSegment
= (busSegment_t
*)BUS_SPI_FREE
;
401 // Advance to the next DMA segment
406 // Advance through the segment list
407 nextSegment
= bus
->curSegment
+ 1;
409 if (nextSegment
->len
== 0) {
410 // If a following transaction has been linked, start it
411 if (nextSegment
->u
.link
.dev
) {
412 const extDevice_t
*nextDev
= nextSegment
->u
.link
.dev
;
413 busSegment_t
*nextSegments
= nextSegment
->u
.link
.segments
;
414 // The end of the segment list has been reached
415 bus
->curSegment
= nextSegments
;
416 nextSegment
->u
.link
.dev
= NULL
;
417 spiSequenceStart(nextDev
);
419 // The end of the segment list has been reached, so mark transactions as complete
420 bus
->curSegment
= (busSegment_t
*)BUS_SPI_FREE
;
423 bus
->curSegment
= nextSegment
;
425 // After the completion of the first segment setup the init structure for the subsequent segment
426 if (bus
->initSegment
) {
427 spiInternalInitStream(dev
, false);
428 bus
->initSegment
= false;
431 // Launch the next transfer
432 spiInternalStartDMA(dev
);
434 // Prepare the init structures ready for the next segment to reduce inter-segment time
435 spiInternalInitStream(dev
, true);
439 // Interrupt handler for SPI receive DMA completion
440 static void spiRxIrqHandler(dmaChannelDescriptor_t
* descriptor
)
442 const extDevice_t
*dev
= (const extDevice_t
*)descriptor
->userParam
;
448 busDevice_t
*bus
= dev
->bus
;
450 if (bus
->curSegment
->negateCS
) {
451 // Negate Chip Select
452 IOHi(dev
->busType_u
.spi
.csnPin
);
455 spiInternalStopDMA(dev
);
457 #ifdef __DCACHE_PRESENT
459 if (bus
->curSegment
->u
.buffers
.rxData
&&
460 ((bus
->curSegment
->u
.buffers
.rxData
< &_dmaram_start__
) || (bus
->curSegment
->u
.buffers
.rxData
>= &_dmaram_end__
))) {
462 if (bus
->curSegment
->u
.buffers
.rxData
) {
464 // Invalidate the D cache covering the area into which data has been read
465 SCB_InvalidateDCache_by_Addr(
466 (uint32_t *)((uint32_t)bus
->curSegment
->u
.buffers
.rxData
& ~CACHE_LINE_MASK
),
467 (((uint32_t)bus
->curSegment
->u
.buffers
.rxData
& CACHE_LINE_MASK
) +
468 bus
->curSegment
->len
- 1 + CACHE_LINE_SIZE
) & ~CACHE_LINE_MASK
);
470 #endif // __DCACHE_PRESENT
475 #if !defined(STM32G4) && !defined(STM32H7)
476 // Interrupt handler for SPI transmit DMA completion
477 static void spiTxIrqHandler(dmaChannelDescriptor_t
* descriptor
)
479 const extDevice_t
*dev
= (const extDevice_t
*)descriptor
->userParam
;
485 busDevice_t
*bus
= dev
->bus
;
487 spiInternalStopDMA(dev
);
489 if (bus
->curSegment
->negateCS
) {
490 // Negate Chip Select
491 IOHi(dev
->busType_u
.spi
.csnPin
);
498 // Mark this bus as being SPI and record the first owner to use it
499 bool spiSetBusInstance(extDevice_t
*dev
, uint32_t device
)
501 if ((device
== 0) || (device
> SPIDEV_COUNT
)) {
505 dev
->bus
= &spiBusDevice
[SPI_CFG_TO_DEV(device
)];
507 // By default each device should use SPI DMA if the bus supports it
510 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
511 // This bus has already been initialised
512 dev
->bus
->deviceCount
++;
516 busDevice_t
*bus
= dev
->bus
;
518 bus
->busType_u
.spi
.instance
= spiInstanceByDevice(SPI_CFG_TO_DEV(device
));
520 if (bus
->busType_u
.spi
.instance
== NULL
) {
524 bus
->busType
= BUS_TYPE_SPI
;
526 bus
->deviceCount
= 1;
527 bus
->initTx
= &dev
->initTx
;
528 bus
->initRx
= &dev
->initRx
;
536 #if defined(STM32F4) && defined(USE_DSHOT_BITBANG)
537 /* Check https://www.st.com/resource/en/errata_sheet/dm00037591-stm32f405407xx-and-stm32f415417xx-device-limitations-stmicroelectronics.pdf
538 * section 2.1.10 which reports an errata that corruption may occurs on DMA2 if AHB peripherals (eg GPIO ports) are
539 * access concurrently with APB peripherals (eg SPI busses). Bitbang DSHOT uses DMA2 to write to GPIO ports. If this
540 * is enabled, then don't enable DMA on an SPI bus using DMA2
542 const bool dshotBitbangActive
= isDshotBitbangActive(&motorConfig()->dev
);
545 for (device
= 0; device
< SPIDEV_COUNT
; device
++) {
546 busDevice_t
*bus
= &spiBusDevice
[device
];
548 if (bus
->busType
!= BUS_TYPE_SPI
) {
549 // This bus is not in use
553 dmaIdentifier_e dmaTxIdentifier
= DMA_NONE
;
554 dmaIdentifier_e dmaRxIdentifier
= DMA_NONE
;
556 int8_t txDmaopt
= spiPinConfig(device
)->txDmaopt
;
557 uint8_t txDmaoptMin
= 0;
558 uint8_t txDmaoptMax
= MAX_PERIPHERAL_DMA_OPTIONS
- 1;
560 if (txDmaopt
!= -1) {
561 txDmaoptMin
= txDmaopt
;
562 txDmaoptMax
= txDmaopt
;
565 for (uint8_t opt
= txDmaoptMin
; opt
<= txDmaoptMax
; opt
++) {
566 const dmaChannelSpec_t
*dmaTxChannelSpec
= dmaGetChannelSpecByPeripheral(DMA_PERIPH_SPI_MOSI
, device
, opt
);
568 if (dmaTxChannelSpec
) {
569 dmaTxIdentifier
= dmaGetIdentifier(dmaTxChannelSpec
->ref
);
570 if (!dmaAllocate(dmaTxIdentifier
, OWNER_SPI_MOSI
, device
+ 1)) {
571 dmaTxIdentifier
= DMA_NONE
;
574 #if defined(STM32F4) && defined(USE_DSHOT_BITBANG)
575 if (dshotBitbangActive
&& (DMA_DEVICE_NO(dmaTxIdentifier
) == 2)) {
576 dmaTxIdentifier
= DMA_NONE
;
580 bus
->dmaTx
= dmaGetDescriptorByIdentifier(dmaTxIdentifier
);
581 bus
->dmaTx
->stream
= DMA_DEVICE_INDEX(dmaTxIdentifier
);
582 bus
->dmaTx
->channel
= dmaTxChannelSpec
->channel
;
584 dmaEnable(dmaTxIdentifier
);
590 int8_t rxDmaopt
= spiPinConfig(device
)->rxDmaopt
;
591 uint8_t rxDmaoptMin
= 0;
592 uint8_t rxDmaoptMax
= MAX_PERIPHERAL_DMA_OPTIONS
- 1;
594 if (rxDmaopt
!= -1) {
595 rxDmaoptMin
= rxDmaopt
;
596 rxDmaoptMax
= rxDmaopt
;
599 for (uint8_t opt
= rxDmaoptMin
; opt
<= rxDmaoptMax
; opt
++) {
600 const dmaChannelSpec_t
*dmaRxChannelSpec
= dmaGetChannelSpecByPeripheral(DMA_PERIPH_SPI_MISO
, device
, opt
);
602 if (dmaRxChannelSpec
) {
603 dmaRxIdentifier
= dmaGetIdentifier(dmaRxChannelSpec
->ref
);
604 if (!dmaAllocate(dmaRxIdentifier
, OWNER_SPI_MISO
, device
+ 1)) {
605 dmaRxIdentifier
= DMA_NONE
;
608 #if defined(STM32F4) && defined(USE_DSHOT_BITBANG)
609 if (dshotBitbangActive
&& (DMA_DEVICE_NO(dmaRxIdentifier
) == 2)) {
610 dmaRxIdentifier
= DMA_NONE
;
614 bus
->dmaRx
= dmaGetDescriptorByIdentifier(dmaRxIdentifier
);
615 bus
->dmaRx
->stream
= DMA_DEVICE_INDEX(dmaRxIdentifier
);
616 bus
->dmaRx
->channel
= dmaRxChannelSpec
->channel
;
618 dmaEnable(dmaRxIdentifier
);
624 if (dmaTxIdentifier
&& dmaRxIdentifier
) {
625 // Ensure streams are disabled
626 spiInternalResetStream(bus
->dmaRx
);
627 spiInternalResetStream(bus
->dmaTx
);
629 spiInternalResetDescriptors(bus
);
631 /* Note that this driver may be called both from the normal thread of execution, or from USB interrupt
632 * handlers, so the DMA completion interrupt must be at a higher priority
634 dmaSetHandler(dmaRxIdentifier
, spiRxIrqHandler
, NVIC_PRIO_SPI_DMA
, 0);
637 #if !defined(STM32G4) && !defined(STM32H7)
638 } else if (dmaTxIdentifier
) {
639 // Transmit on DMA is adequate for OSD so worth having
640 bus
->dmaTx
= dmaGetDescriptorByIdentifier(dmaTxIdentifier
);
641 bus
->dmaRx
= (dmaChannelDescriptor_t
*)NULL
;
643 // Ensure streams are disabled
644 spiInternalResetStream(bus
->dmaTx
);
646 spiInternalResetDescriptors(bus
);
648 dmaSetHandler(dmaTxIdentifier
, spiTxIrqHandler
, NVIC_PRIO_SPI_DMA
, 0);
653 // Disassociate channels from bus
654 bus
->dmaRx
= (dmaChannelDescriptor_t
*)NULL
;
655 bus
->dmaTx
= (dmaChannelDescriptor_t
*)NULL
;
660 void spiSetClkDivisor(const extDevice_t
*dev
, uint16_t divisor
)
662 ((extDevice_t
*)dev
)->busType_u
.spi
.speed
= divisor
;
665 // Set the clock phase/polarity to be used for accesses by the given device
666 void spiSetClkPhasePolarity(const extDevice_t
*dev
, bool leadingEdge
)
668 ((extDevice_t
*)dev
)->busType_u
.spi
.leadingEdge
= leadingEdge
;
671 // Enable/disable DMA on a specific device. Enabled by default.
672 void spiDmaEnable(const extDevice_t
*dev
, bool enable
)
674 ((extDevice_t
*)dev
)->useDMA
= enable
;
677 bool spiUseDMA(const extDevice_t
*dev
)
679 // Full DMA only requires both transmit and receive}
680 return dev
->bus
->useDMA
&& dev
->bus
->dmaRx
&& dev
->useDMA
;
683 bool spiUseMOSI_DMA(const extDevice_t
*dev
)
685 return dev
->bus
->useDMA
&& dev
->useDMA
;
688 void spiBusDeviceRegister(const extDevice_t
*dev
)
692 spiRegisteredDeviceCount
++;
695 uint8_t spiGetRegisteredDeviceCount(void)
697 return spiRegisteredDeviceCount
;
700 uint8_t spiGetExtDeviceCount(const extDevice_t
*dev
)
702 return dev
->bus
->deviceCount
;
705 // DMA transfer setup and start
706 void spiSequence(const extDevice_t
*dev
, busSegment_t
*segments
)
708 busDevice_t
*bus
= dev
->bus
;
710 ATOMIC_BLOCK(NVIC_PRIO_MAX
) {
711 if (spiIsBusy(dev
)) {
712 busSegment_t
*endSegment
;
714 // Defer this transfer to be triggered upon completion of the current transfer
716 // Find the last segment of the current transfer
717 for (endSegment
= segments
; endSegment
->len
; endSegment
++);
719 busSegment_t
*endCmpSegment
= bus
->curSegment
;
723 // Find the last segment of the current transfer
724 for (; endCmpSegment
->len
; endCmpSegment
++);
726 if (endCmpSegment
== endSegment
) {
727 /* Attempt to use the new segment list twice in the same queue. Abort.
728 * Note that this can only happen with non-blocking transfers so drivers must take
729 * care to avoid this.
734 if (endCmpSegment
->u
.link
.dev
== NULL
) {
735 // End of the segment list queue reached
738 // Follow the link to the next queued segment list
739 endCmpSegment
= endCmpSegment
->u
.link
.segments
;
743 // Record the dev and segments parameters in the terminating segment entry
744 endCmpSegment
->u
.link
.dev
= dev
;
745 endCmpSegment
->u
.link
.segments
= segments
;
750 // Claim the bus with this list of segments
751 bus
->curSegment
= segments
;
755 spiSequenceStart(dev
);