基本步骤是:
1. 配置好SPI相应引脚功能
2. 配置和初始化SPI
3. 初始化DMA
4. 片选信号选择要通信的设备
5. 打开DMA
对应DMA通道:SPI1_RX---DMA1_Channel2;SPI1_TX---DMA1_Channel3
代码:
#define L3GD20_SPI_TDR (u32)(SPI1_BASE+0x0C)
#define L3GD20_SPI_RDR (u32)(SPI1_BASE+0x0C)
u8 SPI_TXBUFFER[7]={0xE8,0x00,0x00,0x00,0x00,0x00,0x00};
u8 SPI_RXBUFFER[7]={0};
void SPI_DMA_Test(void)
{
SPI_GPIO_Config();
SPI_Config();//
SPI_DMA_Config();
GPIO_ResetBit(GPIOE,GPIO_Pin_3);//片选信号
//打开DMA
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
DMA_Cmd(DMA1_Channel2, ENABLE);
DMA_Cmd(DMA1_Channel3, ENABLE);
}
void SPI_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_StructInit(&DMA_InitStruct);
DMA_DeInit(DMA1_Channel2);
DMA_DeInit(DMA1_Channel3);
DMA_InitStruct.DMA_PeripheralBaseAddr = L3GD20_SPI_TDR;
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t )SPI_TXBUFFER;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStruct.DMA_BufferSize =7;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
DMA_InitStruct.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStruct);
DMA_InitStruct.DMA_PeripheralBaseAddr = L3GD20_SPI_RDR;
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t )SPI_RXBUFFER;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_Init(DMA1_Channel2, &DMA_InitStruct);
}
处理数据时只需要读取缓冲区的数据即可,不需要额外的程序去读取,节省资源,用起来方便。