在SPI通讯中,能够一起敞开发送和接纳DMA恳求,主动数据的发送和接纳,完结数据的交流
根本过程是:
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);
}
处理数据时只需要读取缓冲区的数据即可,不需要额定的程序去读取,节约资源,用起来便利。