first 2016/02 SDFileSystemDMA inherited from Official SDFileSystem.
Dependents: SDFileSystemDMA-test DmdFullRGB_0_1
Fork of SDFileSystemDMA by
SDFileSystemDMA is enhanced SDFileSystem library for STM32 micros by using DMA functionality.
Max read transfer rate reaches over 2MByte/sec at 24MHz SPI clock if enough read buffer size is set.
Even though minimum read buffer size (512Byte) is set, read transfer rate will reach over 1MByte/sec at 24MHz SPI Clock.
( but depends on the ability of each SD card)
Test program is here.
https://developer.mbed.org/users/mimi3/code/SDFileSystemDMA-test/
Supported SPI port is shown below table.
(v): Verified. It works well.
(w): Probably it will work well. (not tested)
(c): Only compiled. (not tested)
(f): Over flash.
(r): Only read mode. (when _FS_READONLY==1)
(u) Under construction
(z): Dose not work.
Caution
If your board has SRAM less than or equal to 8KB, the buffer size must be set to 512 Bytes.
Supported Boards:
Cortex-M0
Board | SRAM | SPI1 | SPI2 | SPI3 |
---|---|---|---|---|
NUCLEO-F030R8 | 8KB | (v) | ||
DISCO-F051R8 | 8KB | (w) | ||
4KB | (f) | |||
NUCLEO-F042K6 | 6KB | (r) | ||
NUCLEO-F070RB | 16KB | (w) | ||
NUCLEO-F072RB | 16KB | (w) | ||
NUCLEO-F091RC | 32KB | (c) |
Cortex-L0
Board | SRAM | SPI1 | SPI2 | SPI3 |
---|---|---|---|---|
DISCO-L053C8 | 8KB | (c) | ||
NUCLEO-L053R8 | 8KB | (c) | ||
NUCLEO-L073RZ | 20KB | (c) |
Cortex-M3
Board | SRAM | SPI1 | SPI2 | SPI3 |
---|---|---|---|---|
DISCO-F100RB | 8KB | (v) | (v) | - |
BLUEPILL-F103CB | 20KB | (w) | (w) | - |
NUCLEO-F103RB | 20KB | (v) | (v) | - |
NUCLEO-L152RE | 80KB | (v) | (w) | - |
MOTE-L152RC | 32KB | (w) | (w) | - |
Cortex-M4
F3
Board | SRAM | SPI1 | SPI2 | SPI3 |
---|---|---|---|---|
DISCO-F303VC | 40KB | - | (v) | (v) |
NUCLEO-F303RE | 64KB | (w) | (w) | (w) |
NUCLEO-F302R8 | 16KB | - | - | (c) |
NUCLEO-F303K8 | 12KB | (c) | - | - |
DISCO-F334C8 | 12KB | (c) | - | - |
NUCLEO-F334R8 | 12KB | (c) | - | - |
F4
Board | SPI1 | SPI2 | SPI3 |
---|---|---|---|
ELMO-F411RE | (w) | - | (w) |
MTS-MDOT-F411RE | (u) | - | (u) |
MTS-DRAGONFLY-F411RE | (w) | - | (w) |
NUCLEO-F411RE | (v) | - | (v) |
NUCLEO-F401RE | (w) | - | (w) |
MTS-MDOT-F405RG | (u) | - | (u) |
NUCLEO-F410RB | (c) | - | (c) |
NUCLEO-F446RE | (c) | - | (c) |
NUCLEO-F429ZI | (c) | - | (c) |
B96B-F446VE | (c) | - | (c) |
NUCLEO-F446ZE | (c) | - | (c) |
DISCO-F429ZI | (u) | - | (u) |
DISCO-F469NI | (c) | - | (c) |
Information
This library is set to use "short file name" in SDFileSystemDMA/FATFileSystem/ChaN/ffconf.h . ( _USE_LFN=0)
You can change this option to _USE_LFN=1 .
spi_dma/spi_dma_stm32f4.c@20:c6a6e019922e, 2016-02-15 (annotated)
- Committer:
- mimi3
- Date:
- Mon Feb 15 22:40:41 2016 +0900
- Revision:
- 20:c6a6e019922e
- Parent:
- 18:1b1a0e68008a
- Child:
- 21:41129109d6ab
Refactoring: F4 Part2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mimi3 | 2:0e871408d51b | 1 | #if defined(TARGET_STM32F4) |
mimi3 | 2:0e871408d51b | 2 | /* |
mimi3 | 2:0e871408d51b | 3 | |
mimi3 | 2:0e871408d51b | 4 | This file is licensed under Apache 2.0 license. |
mimi3 | 2:0e871408d51b | 5 | (C) 2016 dinau |
mimi3 | 2:0e871408d51b | 6 | |
mimi3 | 2:0e871408d51b | 7 | */ |
mimi3 | 2:0e871408d51b | 8 | #include "spi_dma.h" |
mimi3 | 2:0e871408d51b | 9 | |
mimi3 | 2:0e871408d51b | 10 | #define DMAx_CLK_ENABLE() __DMA2_CLK_ENABLE() |
mimi3 | 2:0e871408d51b | 11 | #define SPIx_TX_DMA_STREAM DMA2_Stream3 |
mimi3 | 2:0e871408d51b | 12 | #define SPIx_RX_DMA_STREAM DMA2_Stream2 |
mimi3 | 2:0e871408d51b | 13 | #define SPIx_TX_DMA_CHANNEL DMA_CHANNEL_3 |
mimi3 | 2:0e871408d51b | 14 | #define SPIx_RX_DMA_CHANNEL DMA_CHANNEL_3 |
mimi3 | 20:c6a6e019922e | 15 | #define SPIx_DMA_TX_IRQn DMA2_Stream3_IRQn |
mimi3 | 20:c6a6e019922e | 16 | #define SPIx_DMA_RX_IRQn DMA2_Stream2_IRQn |
mimi3 | 18:1b1a0e68008a | 17 | #define DMAx_TX_IRQHandler DMA2_Stream3_IRQHandler |
mimi3 | 18:1b1a0e68008a | 18 | #define DMAx_RX_IRQHandler DMA2_Stream2_IRQHandler |
mimi3 | 2:0e871408d51b | 19 | |
mimi3 | 2:0e871408d51b | 20 | #define readReg( reg, mask) ( (reg) & (mask) ) |
mimi3 | 2:0e871408d51b | 21 | |
mimi3 | 2:0e871408d51b | 22 | |
mimi3 | 2:0e871408d51b | 23 | void spi_dma_get_info( SPI_TypeDef *spi ) |
mimi3 | 2:0e871408d51b | 24 | { |
mimi3 | 2:0e871408d51b | 25 | SPI_HandleTypeDef *hspi; |
mimi3 | 2:0e871408d51b | 26 | if( spi == SPI1 ){ |
mimi3 | 2:0e871408d51b | 27 | hspi = &Spi1Handle; |
mimi3 | 2:0e871408d51b | 28 | } |
mimi3 | 2:0e871408d51b | 29 | #if 0 |
mimi3 | 2:0e871408d51b | 30 | else { |
mimi3 | 2:0e871408d51b | 31 | hspi = &Spi2Handle; |
mimi3 | 2:0e871408d51b | 32 | } |
mimi3 | 2:0e871408d51b | 33 | #endif |
mimi3 | 20:c6a6e019922e | 34 | hspi->Instance = spi; |
mimi3 | 2:0e871408d51b | 35 | hspi->Init.Mode = readReg(spi->CR1, SPI_MODE_MASTER); |
mimi3 | 2:0e871408d51b | 36 | hspi->Init.BaudRatePrescaler = readReg(spi->CR1, SPI_CR1_BR); |
mimi3 | 2:0e871408d51b | 37 | hspi->Init.Direction = readReg(spi->CR1, SPI_CR1_BIDIMODE); |
mimi3 | 2:0e871408d51b | 38 | hspi->Init.CLKPhase = readReg(spi->CR1, SPI_CR1_CPHA); |
mimi3 | 2:0e871408d51b | 39 | hspi->Init.CLKPolarity = readReg(spi->CR1, SPI_CR1_CPOL); |
mimi3 | 2:0e871408d51b | 40 | hspi->Init.CRCCalculation = readReg(spi->CR1, SPI_CR1_CRCEN); |
mimi3 | 2:0e871408d51b | 41 | hspi->Init.CRCPolynomial = spi->CRCPR & 0xFFFF; |
mimi3 | 2:0e871408d51b | 42 | hspi->Init.DataSize = readReg(spi->CR1, SPI_CR1_DFF); |
mimi3 | 2:0e871408d51b | 43 | hspi->Init.FirstBit = SPI_FIRSTBIT_MSB; |
mimi3 | 2:0e871408d51b | 44 | hspi->Init.NSS = readReg(spi->CR1, SPI_CR1_SSM); |
mimi3 | 2:0e871408d51b | 45 | hspi->Init.TIMode = SPI_TIMODE_DISABLED; |
mimi3 | 20:c6a6e019922e | 46 | hspi->State = HAL_SPI_STATE_READY; |
mimi3 | 2:0e871408d51b | 47 | } |
mimi3 | 2:0e871408d51b | 48 | |
mimi3 | 18:1b1a0e68008a | 49 | void spi_dma_handle_setup( SPI_TypeDef *spi, uint8_t mode ) |
mimi3 | 18:1b1a0e68008a | 50 | { |
mimi3 | 18:1b1a0e68008a | 51 | static uint8_t dma_handle_inited = 0; |
mimi3 | 20:c6a6e019922e | 52 | DMA_HandleTypeDef *hdma_tx, *hdma_rx; |
mimi3 | 2:0e871408d51b | 53 | /* Peripheral DMA init*/ |
mimi3 | 2:0e871408d51b | 54 | if( spi == SPI1 ){ |
mimi3 | 20:c6a6e019922e | 55 | hdma_tx = &hdma_spi1_tx; |
mimi3 | 20:c6a6e019922e | 56 | hdma_rx = &hdma_spi1_rx; |
mimi3 | 2:0e871408d51b | 57 | /* TX: */ |
mimi3 | 20:c6a6e019922e | 58 | if( !dma_handle_inited ){ |
mimi3 | 20:c6a6e019922e | 59 | hdma_tx->Instance = SPIx_TX_DMA_STREAM; |
mimi3 | 20:c6a6e019922e | 60 | hdma_tx->Init.Channel = SPIx_TX_DMA_CHANNEL; |
mimi3 | 20:c6a6e019922e | 61 | hdma_tx->Init.Direction = DMA_MEMORY_TO_PERIPH; |
mimi3 | 20:c6a6e019922e | 62 | hdma_tx->Init.PeriphInc = DMA_PINC_DISABLE; |
mimi3 | 20:c6a6e019922e | 63 | hdma_tx->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; |
mimi3 | 20:c6a6e019922e | 64 | hdma_tx->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; |
mimi3 | 20:c6a6e019922e | 65 | hdma_tx->Init.Mode = DMA_NORMAL; |
mimi3 | 20:c6a6e019922e | 66 | hdma_tx->Init.Priority = DMA_PRIORITY_HIGH; |
mimi3 | 20:c6a6e019922e | 67 | /**/ |
mimi3 | 20:c6a6e019922e | 68 | hdma_tx->Init.FIFOMode = DMA_FIFOMODE_DISABLE; |
mimi3 | 20:c6a6e019922e | 69 | hdma_tx->Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; |
mimi3 | 20:c6a6e019922e | 70 | hdma_tx->Init.MemBurst = DMA_MBURST_SINGLE; |
mimi3 | 20:c6a6e019922e | 71 | hdma_tx->Init.PeriphBurst = DMA_PBURST_SINGLE; |
mimi3 | 20:c6a6e019922e | 72 | /**/ |
mimi3 | 20:c6a6e019922e | 73 | } |
mimi3 | 20:c6a6e019922e | 74 | hdma_tx->Init.MemInc = ( mode == DMA_SPI_READ) ? DMA_MINC_DISABLE : DMA_MINC_ENABLE; |
mimi3 | 2:0e871408d51b | 75 | HAL_DMA_Init(&hdma_spi1_tx); |
mimi3 | 2:0e871408d51b | 76 | __HAL_LINKDMA( &Spi1Handle,hdmatx,hdma_spi1_tx); |
mimi3 | 2:0e871408d51b | 77 | |
mimi3 | 2:0e871408d51b | 78 | /* RX: */ |
mimi3 | 20:c6a6e019922e | 79 | if( !dma_handle_inited ){ |
mimi3 | 20:c6a6e019922e | 80 | hdma_rx->Instance = SPIx_RX_DMA_STREAM; |
mimi3 | 20:c6a6e019922e | 81 | hdma_rx->Init.Channel = SPIx_RX_DMA_CHANNEL; |
mimi3 | 20:c6a6e019922e | 82 | hdma_rx->Init.Direction = DMA_PERIPH_TO_MEMORY; |
mimi3 | 20:c6a6e019922e | 83 | hdma_rx->Init.PeriphInc = DMA_PINC_DISABLE; |
mimi3 | 20:c6a6e019922e | 84 | hdma_rx->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; |
mimi3 | 20:c6a6e019922e | 85 | hdma_rx->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; |
mimi3 | 20:c6a6e019922e | 86 | hdma_rx->Init.Mode = DMA_NORMAL; |
mimi3 | 20:c6a6e019922e | 87 | hdma_rx->Init.Priority = DMA_PRIORITY_LOW; |
mimi3 | 20:c6a6e019922e | 88 | /**/ |
mimi3 | 20:c6a6e019922e | 89 | hdma_rx->Init.FIFOMode = DMA_FIFOMODE_DISABLE; |
mimi3 | 20:c6a6e019922e | 90 | hdma_rx->Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; |
mimi3 | 20:c6a6e019922e | 91 | hdma_rx->Init.MemBurst = DMA_MBURST_SINGLE; |
mimi3 | 20:c6a6e019922e | 92 | hdma_rx->Init.PeriphBurst = DMA_PBURST_SINGLE; |
mimi3 | 20:c6a6e019922e | 93 | /**/ |
mimi3 | 20:c6a6e019922e | 94 | } |
mimi3 | 20:c6a6e019922e | 95 | hdma_rx->Init.MemInc = (mode == DMA_SPI_READ) ? DMA_MINC_ENABLE : DMA_MINC_DISABLE; |
mimi3 | 2:0e871408d51b | 96 | HAL_DMA_Init(&hdma_spi1_rx); |
mimi3 | 2:0e871408d51b | 97 | __HAL_LINKDMA( &Spi1Handle,hdmarx,hdma_spi1_rx); |
mimi3 | 2:0e871408d51b | 98 | } |
mimi3 | 2:0e871408d51b | 99 | #if 0 |
mimi3 | 2:0e871408d51b | 100 | else if( spi == SPI2 ) { |
mimi3 | 2:0e871408d51b | 101 | |
mimi3 | 2:0e871408d51b | 102 | } |
mimi3 | 2:0e871408d51b | 103 | #endif |
mimi3 | 18:1b1a0e68008a | 104 | dma_handle_inited = 1; |
mimi3 | 2:0e871408d51b | 105 | } |
mimi3 | 2:0e871408d51b | 106 | |
mimi3 | 2:0e871408d51b | 107 | void spi_dma_irq_setup( SPI_TypeDef *spi) |
mimi3 | 2:0e871408d51b | 108 | { |
mimi3 | 2:0e871408d51b | 109 | if( spi == SPI1 ) { |
mimi3 | 2:0e871408d51b | 110 | /* DMA controller clock enable */ |
mimi3 | 2:0e871408d51b | 111 | DMAx_CLK_ENABLE(); |
mimi3 | 2:0e871408d51b | 112 | |
mimi3 | 2:0e871408d51b | 113 | /* DMA interrupt init */ |
mimi3 | 18:1b1a0e68008a | 114 | HAL_NVIC_SetPriority(SPIx_DMA_TX_IRQn, 0, 1); |
mimi3 | 18:1b1a0e68008a | 115 | HAL_NVIC_EnableIRQ( SPIx_DMA_TX_IRQn); |
mimi3 | 18:1b1a0e68008a | 116 | HAL_NVIC_SetPriority(SPIx_DMA_RX_IRQn, 0, 0); |
mimi3 | 18:1b1a0e68008a | 117 | HAL_NVIC_EnableIRQ( SPIx_DMA_RX_IRQn); |
mimi3 | 2:0e871408d51b | 118 | } |
mimi3 | 2:0e871408d51b | 119 | #if 0 |
mimi3 | 2:0e871408d51b | 120 | else if ( spi == SPI2 ){ |
mimi3 | 2:0e871408d51b | 121 | |
mimi3 | 2:0e871408d51b | 122 | } |
mimi3 | 2:0e871408d51b | 123 | #endif |
mimi3 | 2:0e871408d51b | 124 | } |
mimi3 | 2:0e871408d51b | 125 | |
mimi3 | 18:1b1a0e68008a | 126 | void DMAx_TX_IRQHandler(void) |
mimi3 | 18:1b1a0e68008a | 127 | { |
mimi3 | 18:1b1a0e68008a | 128 | HAL_DMA_IRQHandler(&hdma_spi1_tx); |
mimi3 | 18:1b1a0e68008a | 129 | } |
mimi3 | 2:0e871408d51b | 130 | |
mimi3 | 18:1b1a0e68008a | 131 | void DMAx_RX_IRQHandler(void) |
mimi3 | 2:0e871408d51b | 132 | { |
mimi3 | 2:0e871408d51b | 133 | HAL_DMA_IRQHandler(&hdma_spi1_rx); |
mimi3 | 2:0e871408d51b | 134 | } |
mimi3 | 2:0e871408d51b | 135 | |
mimi3 | 2:0e871408d51b | 136 | #endif /* TARGET_STM32F4 */ |
mimi3 | 2:0e871408d51b | 137 |