采用SPI DMA方式读写SD卡,适用于STM32F4。FatFs版本为最新的R0.11a。
一次读写512、1024、2048或4096字节,连续读写10s,然后计算读写速度。
测试的是一张8G的三星TF卡,以下是从串口获取的读写速度情况。
start reading ARTOGARD.WAV
fr: 0 num: 19522 br: 512
speed: 976.1kB/s
start reading ARTOGARD.WAV
fr: 0 num: 10866 br: 1024
speed: 1086.6kB/s
start reading ARTOGARD.WAV
fr: 0 num: 7660 br: 2048
speed: 1532.0kB/s
start reading ARTOGARD.WAV
fr: 0 num: 4827 br: 4096
speed: 1930.8kB/s
start writing 512.BIN
fr: 0 num: 6610 br: 512
speed: 330.5kB/s
start writing 1024.BIN
fr: 0 num: 3837 br: 1024
speed: 383.7kB/s
start writing 2048.BIN
fr: 0 num: 4037 br: 2048
speed: 807.4kB/s
start writing 4096.BIN
fr: 0 num: 3366 br: 4096
speed: 1346.4kB/s
SPI DMA方式核心部分代码如下,详见源码。
[mw_shl_code=c,true]void SD_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(SD_DMA_RCC, ENABLE);
DMA_DeInit(SD_DMA_RX_STREAM);
DMA_DeInit(SD_DMA_TX_STREAM);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(SD_SPI->DR));
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //!!!
DMA_InitStructure.DMA_BufferSize = 0; //!!!
DMA_InitStructure.DMA_Channel = SD_DMA_RX_CHANNEL;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0; //!!!
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_Init(SD_DMA_RX_STREAM, &DMA_InitStructure);
DMA_InitStructure.DMA_Channel = SD_DMA_TX_CHANNEL;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0; //!!!
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_Init(SD_DMA_TX_STREAM, &DMA_InitStructure);
SPI_I2S_DMACmd(SD_SPI, SPI_I2S_DMAReq_Rx, ENABLE);
SPI_I2S_DMACmd(SD_SPI, SPI_I2S_DMAReq_Tx, ENABLE);
}[/mw_shl_code] |