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 "common/utils.h"
30 #include "common/maths.h"
32 #include "drivers/bus.h"
33 #include "drivers/bus_spi.h"
34 #include "drivers/bus_spi_impl.h"
35 #include "drivers/dma.h"
36 #include "drivers/io.h"
37 #include "drivers/rcc.h"
40 #define SPI2_NSS_PIN PB12
41 #define SPI2_SCK_PIN PB13
42 #define SPI2_MISO_PIN PB14
43 #define SPI2_MOSI_PIN PB15
47 #define SPI3_NSS_PIN PA15
48 #define SPI3_SCK_PIN PB3
49 #define SPI3_MISO_PIN PB4
50 #define SPI3_MOSI_PIN PB5
54 #define SPI4_NSS_PIN PA15
55 #define SPI4_SCK_PIN PB3
56 #define SPI4_MISO_PIN PB4
57 #define SPI4_MOSI_PIN PB5
61 #define SPI1_NSS_PIN NONE
64 #define SPI2_NSS_PIN NONE
67 #define SPI3_NSS_PIN NONE
70 #define SPI4_NSS_PIN NONE
73 #define SPI_DEFAULT_TIMEOUT 10
76 #define IS_DTCM(p) (((uint32_t)p & 0xfffe0000) == 0x20000000)
77 #elif defined(STM32F7)
78 #define IS_DTCM(p) (((uint32_t)p & 0xffff0000) == 0x20000000)
79 #elif defined(STM32G4)
80 #define IS_CCM(p) ((((uint32_t)p & 0xffff8000) == 0x10000000) || (((uint32_t)p & 0xffff8000) == 0x20018000))
82 static LL_SPI_InitTypeDef defaultInit
=
84 .TransferDirection
= LL_SPI_FULL_DUPLEX
,
85 .Mode
= LL_SPI_MODE_MASTER
,
86 .DataWidth
= LL_SPI_DATAWIDTH_8BIT
,
87 .NSS
= LL_SPI_NSS_SOFT
,
88 .BaudRate
= LL_SPI_BAUDRATEPRESCALER_DIV8
,
89 .BitOrder
= LL_SPI_MSB_FIRST
,
90 .CRCCalculation
= LL_SPI_CRCCALCULATION_DISABLE
,
91 .ClockPolarity
= LL_SPI_POLARITY_HIGH
,
92 .ClockPhase
= LL_SPI_PHASE_2EDGE
,
95 static uint32_t spiDivisorToBRbits(SPI_TypeDef
*instance
, uint16_t divisor
)
98 // SPI2 and SPI3 are on APB1/AHB1 which PCLK is half that of APB2/AHB2.
100 if (instance
== SPI2
|| instance
== SPI3
) {
101 divisor
/= 2; // Safe for divisor == 0 or 1
107 divisor
= constrain(divisor
, 2, 256);
110 const uint32_t baudRatePrescaler
[8] = {
111 LL_SPI_BAUDRATEPRESCALER_DIV2
,
112 LL_SPI_BAUDRATEPRESCALER_DIV4
,
113 LL_SPI_BAUDRATEPRESCALER_DIV8
,
114 LL_SPI_BAUDRATEPRESCALER_DIV16
,
115 LL_SPI_BAUDRATEPRESCALER_DIV32
,
116 LL_SPI_BAUDRATEPRESCALER_DIV64
,
117 LL_SPI_BAUDRATEPRESCALER_DIV128
,
118 LL_SPI_BAUDRATEPRESCALER_DIV256
,
120 int prescalerIndex
= ffs(divisor
) - 2; // prescaler begins at "/2"
122 return baudRatePrescaler
[prescalerIndex
];
124 return (ffs(divisor
) - 2) << SPI_CR1_BR_Pos
;
128 void spiInitDevice(SPIDevice device
)
130 spiDevice_t
*spi
= &spiDevice
[device
];
137 RCC_ClockCmd(spi
->rcc
, ENABLE
);
138 RCC_ResetCmd(spi
->rcc
, ENABLE
);
140 IOInit(IOGetByTag(spi
->sck
), OWNER_SPI_SCK
, RESOURCE_INDEX(device
));
141 IOInit(IOGetByTag(spi
->miso
), OWNER_SPI_MISO
, RESOURCE_INDEX(device
));
142 IOInit(IOGetByTag(spi
->mosi
), OWNER_SPI_MOSI
, RESOURCE_INDEX(device
));
144 IOConfigGPIOAF(IOGetByTag(spi
->miso
), SPI_IO_AF_MISO_CFG
, spi
->misoAF
);
145 IOConfigGPIOAF(IOGetByTag(spi
->mosi
), SPI_IO_AF_CFG
, spi
->mosiAF
);
146 IOConfigGPIOAF(IOGetByTag(spi
->sck
), SPI_IO_AF_SCK_CFG_HIGH
, spi
->sckAF
);
148 LL_SPI_Disable(spi
->dev
);
149 LL_SPI_DeInit(spi
->dev
);
152 // Prevent glitching when SPI is disabled
153 LL_SPI_EnableGPIOControl(spi
->dev
);
155 LL_SPI_SetFIFOThreshold(spi
->dev
, LL_SPI_FIFO_TH_01DATA
);
156 LL_SPI_Init(spi
->dev
, &defaultInit
);
158 LL_SPI_SetRxFIFOThreshold(spi
->dev
, SPI_RXFIFO_THRESHOLD_QF
);
160 LL_SPI_Init(spi
->dev
, &defaultInit
);
161 LL_SPI_Enable(spi
->dev
);
165 void spiInternalResetDescriptors(busDevice_t
*bus
)
167 LL_DMA_InitTypeDef
*initTx
= bus
->initTx
;
169 LL_DMA_StructInit(initTx
);
170 #if defined(STM32G4) || defined(STM32H7)
171 initTx
->PeriphRequest
= bus
->dmaTx
->channel
;
173 initTx
->Channel
= bus
->dmaTx
->channel
;
175 initTx
->Mode
= LL_DMA_MODE_NORMAL
;
176 initTx
->Direction
= LL_DMA_DIRECTION_MEMORY_TO_PERIPH
;
178 initTx
->PeriphOrM2MSrcAddress
= (uint32_t)&bus
->busType_u
.spi
.instance
->TXDR
;
180 initTx
->PeriphOrM2MSrcAddress
= (uint32_t)&bus
->busType_u
.spi
.instance
->DR
;
182 initTx
->Priority
= LL_DMA_PRIORITY_LOW
;
183 initTx
->PeriphOrM2MSrcIncMode
= LL_DMA_PERIPH_NOINCREMENT
;
184 initTx
->PeriphOrM2MSrcDataSize
= LL_DMA_PDATAALIGN_BYTE
;
185 initTx
->MemoryOrM2MDstDataSize
= LL_DMA_MDATAALIGN_BYTE
;
188 LL_DMA_InitTypeDef
*initRx
= bus
->initRx
;
190 LL_DMA_StructInit(initRx
);
191 #if defined(STM32G4) || defined(STM32H7)
192 initRx
->PeriphRequest
= bus
->dmaRx
->channel
;
194 initRx
->Channel
= bus
->dmaRx
->channel
;
196 initRx
->Mode
= LL_DMA_MODE_NORMAL
;
197 initRx
->Direction
= LL_DMA_DIRECTION_PERIPH_TO_MEMORY
;
199 initRx
->PeriphOrM2MSrcAddress
= (uint32_t)&bus
->busType_u
.spi
.instance
->RXDR
;
201 initRx
->PeriphOrM2MSrcAddress
= (uint32_t)&bus
->busType_u
.spi
.instance
->DR
;
203 initRx
->Priority
= LL_DMA_PRIORITY_LOW
;
204 initRx
->PeriphOrM2MSrcIncMode
= LL_DMA_PERIPH_NOINCREMENT
;
205 initRx
->PeriphOrM2MSrcDataSize
= LL_DMA_PDATAALIGN_BYTE
;
209 void spiInternalResetStream(dmaChannelDescriptor_t
*descriptor
)
211 // Disable the stream
213 LL_DMA_DisableChannel(descriptor
->dma
, descriptor
->stream
);
214 while (LL_DMA_IsEnabledChannel(descriptor
->dma
, descriptor
->stream
));
216 LL_DMA_DisableStream(descriptor
->dma
, descriptor
->stream
);
217 while (LL_DMA_IsEnabledStream(descriptor
->dma
, descriptor
->stream
));
220 // Clear any pending interrupt flags
221 DMA_CLEAR_FLAG(descriptor
, DMA_IT_HTIF
| DMA_IT_TEIF
| DMA_IT_TCIF
);
225 static bool spiInternalReadWriteBufPolled(SPI_TypeDef
*instance
, const uint8_t *txData
, uint8_t *rxData
, int len
)
228 LL_SPI_SetTransferSize(instance
, len
);
229 LL_SPI_Enable(instance
);
230 LL_SPI_StartMasterTransfer(instance
);
232 while (!LL_SPI_IsActiveFlag_TXP(instance
));
233 uint8_t b
= txData
? *(txData
++) : 0xFF;
234 LL_SPI_TransmitData8(instance
, b
);
236 while (!LL_SPI_IsActiveFlag_RXP(instance
));
237 b
= LL_SPI_ReceiveData8(instance
);
243 while (!LL_SPI_IsActiveFlag_EOT(instance
));
244 LL_SPI_ClearFlag_TXTF(instance
);
245 LL_SPI_Disable(instance
);
247 // set 16-bit transfer
248 CLEAR_BIT(instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
250 while (!LL_SPI_IsActiveFlag_TXE(instance
));
253 w
= *((uint16_t *)txData
);
258 LL_SPI_TransmitData16(instance
, w
);
260 while (!LL_SPI_IsActiveFlag_RXNE(instance
));
261 w
= LL_SPI_ReceiveData16(instance
);
263 *((uint16_t *)rxData
) = w
;
268 // set 8-bit transfer
269 SET_BIT(instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
271 while (!LL_SPI_IsActiveFlag_TXE(instance
));
272 uint8_t b
= txData
? *(txData
++) : 0xFF;
273 LL_SPI_TransmitData8(instance
, b
);
275 while (!LL_SPI_IsActiveFlag_RXNE(instance
));
276 b
= LL_SPI_ReceiveData8(instance
);
287 void spiInternalInitStream(const extDevice_t
*dev
, bool preInit
)
289 STATIC_DMA_DATA_AUTO
uint8_t dummyTxByte
= 0xff;
290 STATIC_DMA_DATA_AUTO
uint8_t dummyRxByte
;
291 busDevice_t
*bus
= dev
->bus
;
293 busSegment_t
*segment
= bus
->curSegment
;
296 // Prepare the init structure for the next segment to reduce inter-segment interval
298 if(segment
->len
== 0) {
299 // There's no following segment
304 int len
= segment
->len
;
306 uint8_t *txData
= segment
->u
.buffers
.txData
;
307 LL_DMA_InitTypeDef
*initTx
= bus
->initTx
;
310 #ifdef __DCACHE_PRESENT
312 if ((txData
< &_dmaram_start__
) || (txData
>= &_dmaram_end__
)) {
314 // No need to flush DTCM memory
315 if (!IS_DTCM(txData
)) {
317 // Flush the D cache to ensure the data to be written is in main memory
318 SCB_CleanDCache_by_Addr(
319 (uint32_t *)((uint32_t)txData
& ~CACHE_LINE_MASK
),
320 (((uint32_t)txData
& CACHE_LINE_MASK
) + len
- 1 + CACHE_LINE_SIZE
) & ~CACHE_LINE_MASK
);
322 #endif // __DCACHE_PRESENT
323 initTx
->MemoryOrM2MDstAddress
= (uint32_t)txData
;
324 initTx
->MemoryOrM2MDstIncMode
= LL_DMA_MEMORY_INCREMENT
;
326 initTx
->MemoryOrM2MDstAddress
= (uint32_t)&dummyTxByte
;
327 initTx
->MemoryOrM2MDstIncMode
= LL_DMA_MEMORY_NOINCREMENT
;
329 initTx
->NbData
= len
;
331 #if !defined(STM32G4) && !defined(STM32H7)
332 if (dev
->bus
->dmaRx
) {
334 uint8_t *rxData
= segment
->u
.buffers
.rxData
;
335 LL_DMA_InitTypeDef
*initRx
= bus
->initRx
;
338 /* Flush the D cache for the start and end of the receive buffer as
339 * the cache will be invalidated after the transfer and any valid data
340 * just before/after must be in memory at that point
342 #ifdef __DCACHE_PRESENT
343 // No need to flush/invalidate DTCM memory
345 if ((rxData
< &_dmaram_start__
) || (rxData
>= &_dmaram_end__
)) {
347 // No need to flush DTCM memory
348 if (!IS_DTCM(rxData
)) {
350 SCB_CleanInvalidateDCache_by_Addr(
351 (uint32_t *)((uint32_t)rxData
& ~CACHE_LINE_MASK
),
352 (((uint32_t)rxData
& CACHE_LINE_MASK
) + len
- 1 + CACHE_LINE_SIZE
) & ~CACHE_LINE_MASK
);
354 #endif // __DCACHE_PRESENT
355 initRx
->MemoryOrM2MDstAddress
= (uint32_t)rxData
;
356 initRx
->MemoryOrM2MDstIncMode
= LL_DMA_MEMORY_INCREMENT
;
358 initRx
->MemoryOrM2MDstAddress
= (uint32_t)&dummyRxByte
;
359 initRx
->MemoryOrM2MDstIncMode
= LL_DMA_MEMORY_NOINCREMENT
;
361 initRx
->NbData
= len
;
362 #if !defined(STM32G4) && !defined(STM32H7)
367 void spiInternalStartDMA(const extDevice_t
*dev
)
369 busDevice_t
*bus
= dev
->bus
;
371 // Assert Chip Select
372 IOLo(dev
->busType_u
.spi
.csnPin
);
374 dmaChannelDescriptor_t
*dmaTx
= bus
->dmaTx
;
375 dmaChannelDescriptor_t
*dmaRx
= bus
->dmaRx
;
377 #if !defined(STM32G4) && !defined(STM32H7)
380 // Use the correct callback argument
381 dmaRx
->userParam
= (uint32_t)dev
;
383 // Clear transfer flags
384 DMA_CLEAR_FLAG(dmaTx
, DMA_IT_HTIF
| DMA_IT_TEIF
| DMA_IT_TCIF
);
385 DMA_CLEAR_FLAG(dmaRx
, DMA_IT_HTIF
| DMA_IT_TEIF
| DMA_IT_TCIF
);
388 // Disable channels to enable update
389 LL_DMA_DisableChannel(dmaTx
->dma
, dmaTx
->stream
);
390 LL_DMA_DisableChannel(dmaRx
->dma
, dmaRx
->stream
);
392 /* Use the Rx interrupt as this occurs once the SPI operation is complete whereas the Tx interrupt
393 * occurs earlier when the Tx FIFO is empty, but the SPI operation is still in progress
395 LL_DMA_EnableIT_TC(dmaRx
->dma
, dmaRx
->stream
);
398 LL_DMA_Init(dmaTx
->dma
, dmaTx
->stream
, bus
->initTx
);
399 LL_DMA_Init(dmaRx
->dma
, dmaRx
->stream
, bus
->initRx
);
401 LL_SPI_EnableDMAReq_RX(dev
->bus
->busType_u
.spi
.instance
);
404 LL_DMA_EnableChannel(dmaTx
->dma
, dmaTx
->stream
);
405 LL_DMA_EnableChannel(dmaRx
->dma
, dmaRx
->stream
);
407 LL_SPI_EnableDMAReq_TX(dev
->bus
->busType_u
.spi
.instance
);
409 DMA_Stream_TypeDef
*streamRegsTx
= (DMA_Stream_TypeDef
*)dmaTx
->ref
;
410 DMA_Stream_TypeDef
*streamRegsRx
= (DMA_Stream_TypeDef
*)dmaRx
->ref
;
412 // Disable streams to enable update
413 LL_DMA_WriteReg(streamRegsTx
, CR
, 0U);
414 LL_DMA_WriteReg(streamRegsRx
, CR
, 0U);
416 /* Use the Rx interrupt as this occurs once the SPI operation is complete whereas the Tx interrupt
417 * occurs earlier when the Tx FIFO is empty, but the SPI operation is still in progress
419 LL_EX_DMA_EnableIT_TC(streamRegsRx
);
422 LL_DMA_Init(dmaTx
->dma
, dmaTx
->stream
, bus
->initTx
);
423 LL_DMA_Init(dmaRx
->dma
, dmaRx
->stream
, bus
->initRx
);
427 * If the user enables the used peripheral before the corresponding DMA stream, a “FEIF”
428 * (FIFO Error Interrupt Flag) may be set due to the fact the DMA is not ready to provide
429 * the first required data to the peripheral (in case of memory-to-peripheral transfer).
432 // Enable the SPI DMA Tx & Rx requests
434 LL_SPI_SetTransferSize(dev
->bus
->busType_u
.spi
.instance
, dev
->bus
->curSegment
->len
);
435 LL_DMA_EnableStream(dmaTx
->dma
, dmaTx
->stream
);
436 LL_DMA_EnableStream(dmaRx
->dma
, dmaRx
->stream
);
437 SET_BIT(dev
->bus
->busType_u
.spi
.instance
->CFG1
, SPI_CFG1_RXDMAEN
| SPI_CFG1_TXDMAEN
);
438 LL_SPI_Enable(dev
->bus
->busType_u
.spi
.instance
);
439 LL_SPI_StartMasterTransfer(dev
->bus
->busType_u
.spi
.instance
);
442 LL_DMA_EnableStream(dmaTx
->dma
, dmaTx
->stream
);
443 LL_DMA_EnableStream(dmaRx
->dma
, dmaRx
->stream
);
445 SET_BIT(dev
->bus
->busType_u
.spi
.instance
->CR2
, SPI_CR2_TXDMAEN
| SPI_CR2_RXDMAEN
);
447 #if !defined(STM32G4) && !defined(STM32H7)
449 DMA_Stream_TypeDef
*streamRegsTx
= (DMA_Stream_TypeDef
*)dmaTx
->ref
;
451 // Use the correct callback argument
452 dmaTx
->userParam
= (uint32_t)dev
;
454 // Clear transfer flags
455 DMA_CLEAR_FLAG(dmaTx
, DMA_IT_HTIF
| DMA_IT_TEIF
| DMA_IT_TCIF
);
457 // Disable streams to enable update
458 LL_DMA_WriteReg(streamRegsTx
, CR
, 0U);
460 LL_EX_DMA_EnableIT_TC(streamRegsTx
);
463 LL_DMA_Init(dmaTx
->dma
, dmaTx
->stream
, bus
->initTx
);
467 * If the user enables the used peripheral before the corresponding DMA stream, a “FEIF”
468 * (FIFO Error Interrupt Flag) may be set due to the fact the DMA is not ready to provide
469 * the first required data to the peripheral (in case of memory-to-peripheral transfer).
472 // Enable the SPI DMA Tx request
474 LL_DMA_EnableStream(dmaTx
->dma
, dmaTx
->stream
);
476 SET_BIT(dev
->bus
->busType_u
.spi
.instance
->CR2
, SPI_CR2_TXDMAEN
);
482 void spiInternalStopDMA (const extDevice_t
*dev
)
484 busDevice_t
*bus
= dev
->bus
;
486 dmaChannelDescriptor_t
*dmaTx
= bus
->dmaTx
;
487 dmaChannelDescriptor_t
*dmaRx
= bus
->dmaRx
;
488 SPI_TypeDef
*instance
= bus
->busType_u
.spi
.instance
;
490 #if !defined(STM32G4) && !defined(STM32H7)
493 // Disable the DMA engine and SPI interface
495 LL_DMA_DisableChannel(dmaTx
->dma
, dmaTx
->stream
);
496 LL_DMA_DisableChannel(dmaRx
->dma
, dmaRx
->stream
);
498 LL_DMA_DisableStream(dmaRx
->dma
, dmaRx
->stream
);
499 LL_DMA_DisableStream(dmaTx
->dma
, dmaTx
->stream
);
502 // Clear transfer flags
503 DMA_CLEAR_FLAG(dmaRx
, DMA_IT_HTIF
| DMA_IT_TEIF
| DMA_IT_TCIF
);
505 LL_SPI_DisableDMAReq_TX(instance
);
506 LL_SPI_DisableDMAReq_RX(instance
);
508 LL_SPI_ClearFlag_TXTF(dev
->bus
->busType_u
.spi
.instance
);
509 LL_SPI_Disable(dev
->bus
->busType_u
.spi
.instance
);
511 #if !defined(STM32G4) && !defined(STM32H7)
513 SPI_TypeDef
*instance
= bus
->busType_u
.spi
.instance
;
515 // Ensure the current transmission is complete
516 while (LL_SPI_IsActiveFlag_BSY(instance
));
518 // Drain the RX buffer
519 while (LL_SPI_IsActiveFlag_RXNE(instance
)) {
523 // Disable the DMA engine and SPI interface
524 LL_DMA_DisableStream(dmaTx
->dma
, dmaTx
->stream
);
526 DMA_CLEAR_FLAG(dmaTx
, DMA_IT_HTIF
| DMA_IT_TEIF
| DMA_IT_TCIF
);
528 LL_SPI_DisableDMAReq_TX(instance
);
530 #if !defined(STM32G4) && !defined(STM32H7)
535 // DMA transfer setup and start
536 void spiSequenceStart(const extDevice_t
*dev
)
538 busDevice_t
*bus
= dev
->bus
;
539 SPI_TypeDef
*instance
= bus
->busType_u
.spi
.instance
;
540 spiDevice_t
*spi
= &spiDevice
[spiDeviceByInstance(instance
)];
541 bool dmaSafe
= dev
->useDMA
;
542 uint32_t xferLen
= 0;
543 uint32_t segmentCount
= 0;
545 bus
->initSegment
= true;
548 #if !defined(STM32H7)
549 LL_SPI_Disable(instance
);
552 if (dev
->busType_u
.spi
.speed
!= bus
->busType_u
.spi
.speed
) {
553 LL_SPI_SetBaudRatePrescaler(instance
, spiDivisorToBRbits(instance
, dev
->busType_u
.spi
.speed
));
554 bus
->busType_u
.spi
.speed
= dev
->busType_u
.spi
.speed
;
557 // Switch SPI clock polarity/phase if necessary
558 if (dev
->busType_u
.spi
.leadingEdge
!= bus
->busType_u
.spi
.leadingEdge
) {
559 if (dev
->busType_u
.spi
.leadingEdge
) {
560 IOConfigGPIOAF(IOGetByTag(spi
->sck
), SPI_IO_AF_SCK_CFG_LOW
, spi
->sckAF
);
561 LL_SPI_SetClockPhase(instance
, LL_SPI_PHASE_1EDGE
);
562 LL_SPI_SetClockPolarity(instance
, LL_SPI_POLARITY_LOW
);
565 IOConfigGPIOAF(IOGetByTag(spi
->sck
), SPI_IO_AF_SCK_CFG_HIGH
, spi
->sckAF
);
566 LL_SPI_SetClockPhase(instance
, LL_SPI_PHASE_2EDGE
);
567 LL_SPI_SetClockPolarity(instance
, LL_SPI_POLARITY_HIGH
);
570 bus
->busType_u
.spi
.leadingEdge
= dev
->busType_u
.spi
.leadingEdge
;
573 #if !defined(STM32H7)
574 LL_SPI_Enable(instance
);
577 /* Where data is being read into a buffer which is cached, where the start or end of that
578 * buffer is not cache aligned, there is a risk of corruption of other data in that cache line.
579 * After the read is complete, the cache lines covering the structure will be invalidated to ensure
580 * that the processor sees the read data, not what was in cache previously. Unfortunately if
581 * there is any other data in the area covered by those cache lines, at the start or end of the
582 * buffer, it too will be invalidated, so had the processor written to those locations during the DMA
583 * operation those written values will be lost.
586 // Check that any reads are cache aligned and of multiple cache lines in length
587 for (busSegment_t
*checkSegment
= bus
->curSegment
; checkSegment
->len
; checkSegment
++) {
588 // Check there is no receive data as only transmit DMA is available
589 if ((checkSegment
->u
.buffers
.rxData
) && (bus
->dmaRx
== (dmaChannelDescriptor_t
*)NULL
)) {
594 // Check if RX data can be DMAed
595 if ((checkSegment
->u
.buffers
.rxData
) &&
596 // DTCM can't be accessed by DMA1/2 on the H7
597 (IS_DTCM(checkSegment
->u
.buffers
.rxData
) ||
598 // Memory declared as DMA_RAM will have an address between &_dmaram_start__ and &_dmaram_end__
599 (((checkSegment
->u
.buffers
.rxData
< &_dmaram_start__
) || (checkSegment
->u
.buffers
.rxData
>= &_dmaram_end__
)) &&
600 (((uint32_t)checkSegment
->u
.buffers
.rxData
& (CACHE_LINE_SIZE
- 1)) || (checkSegment
->len
& (CACHE_LINE_SIZE
- 1)))))) {
604 // Check if TX data can be DMAed
605 else if ((checkSegment
->u
.buffers
.txData
) && IS_DTCM(checkSegment
->u
.buffers
.txData
)) {
609 #elif defined(STM32F7)
610 if ((checkSegment
->u
.buffers
.rxData
) &&
611 // DTCM is accessible and uncached on the F7
612 (!IS_DTCM(checkSegment
->u
.buffers
.rxData
) &&
613 (((uint32_t)checkSegment
->u
.buffers
.rxData
& (CACHE_LINE_SIZE
- 1)) || (checkSegment
->len
& (CACHE_LINE_SIZE
- 1))))) {
617 #elif defined(STM32G4)
618 // Check if RX data can be DMAed
619 if ((checkSegment
->u
.buffers
.rxData
) &&
620 // CCM can't be accessed by DMA1/2 on the G4
621 IS_CCM(checkSegment
->u
.buffers
.rxData
)) {
625 if ((checkSegment
->u
.buffers
.txData
) &&
626 // CCM can't be accessed by DMA1/2 on the G4
627 IS_CCM(checkSegment
->u
.buffers
.txData
)) {
632 // Note that these counts are only valid if dmaSafe is true
634 xferLen
+= checkSegment
->len
;
637 // Use DMA if possible
638 if (bus
->useDMA
&& dmaSafe
&& ((segmentCount
> 1) || (xferLen
> 8))) {
639 // Intialise the init structures for the first transfer
640 spiInternalInitStream(dev
, false);
642 // Start the transfers
643 spiInternalStartDMA(dev
);
645 // Manually work through the segment list performing a transfer for each
646 while (bus
->curSegment
->len
) {
647 // Assert Chip Select
648 IOLo(dev
->busType_u
.spi
.csnPin
);
650 spiInternalReadWriteBufPolled(
651 bus
->busType_u
.spi
.instance
,
652 bus
->curSegment
->u
.buffers
.txData
,
653 bus
->curSegment
->u
.buffers
.rxData
,
654 bus
->curSegment
->len
);
656 if (bus
->curSegment
->negateCS
) {
657 // Negate Chip Select
658 IOHi(dev
->busType_u
.spi
.csnPin
);
661 if (bus
->curSegment
->callback
) {
662 switch(bus
->curSegment
->callback(dev
->callbackArg
)) {
664 // Repeat the last DMA segment
669 bus
->curSegment
= (busSegment_t
*)BUS_SPI_FREE
;
674 // Advance to the next DMA segment
681 // If a following transaction has been linked, start it
682 if (bus
->curSegment
->u
.link
.dev
) {
683 const extDevice_t
*nextDev
= bus
->curSegment
->u
.link
.dev
;
684 busSegment_t
*nextSegments
= bus
->curSegment
->u
.link
.segments
;
685 busSegment_t
*endSegment
= bus
->curSegment
;
686 bus
->curSegment
= nextSegments
;
687 endSegment
->u
.link
.dev
= NULL
;
688 spiSequenceStart(nextDev
);
690 // The end of the segment list has been reached, so mark transactions as complete
691 bus
->curSegment
= (busSegment_t
*)BUS_SPI_FREE
;