void ReadFlashDataByDMA(u32 addr, u8 buf[], u32 len)
{
if(len == 0)
{
return;
}
SPI_CS_LOW();
spiTranAndReceiveData(0x03);
spiTranAndReceiveData((uint8)(addr>>16));
spiTranAndReceiveData((uint8)(addr>>8));
spiTranAndReceiveData((uint8)addr);
LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_4);
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_4, len);
LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_4,(uint32_t)buf);
LL_DMA_SetPeriphAddress(DMA1, LL_DMA_CHANNEL_4,(uint32_t)(&SPI2->DR));
LL_DMA_ClearFlag_TC4(DMA1);
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_4);
LL_SPI_Disable(SPI2);
LL_SPI_SetTransferDirection(SPI2,LL_SPI_SIMPLEX_RX);
while(0 != LL_SPI_IsActiveFlag_RXNE(SPI2))
{
LL_SPI_ReceiveData8(SPI2);
}
SPI2->CR2 |= SPI_CR2_RXDMAEN;
SPI2->CR1 |= SPI_CR1_SPE;
while(0 == LL_DMA_IsActiveFlag_TC4(DMA1));
LL_SPI_DisableDMAReq_RX(SPI2);
LL_SPI_SetTransferDirection(SPI2,LL_SPI_FULL_DUPLEX);
LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_4);
LL_DMA_ClearFlag_TC4(DMA1);
SPI_CS_HIGH();
}
关键的操作在于失能SPI之后把 SPI 改成只读模式。 因为最近在学习单片机播语音,需要快速读Flash的内容,所以就研究了DMA读,关于写还没有学习到。毕竟不要求大量写数据。粗略测试 24M 读 256 Bytes 需要 106.6uS 左右,发送地址/指令、配置等耗费了大约 20uS 的时间。
|