stm32m0 使用SPI-DMA RX接收数据,配置如下:
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(A7130_SPI2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//SPI2 RX DMA 将数据从外设读取到内部存储器中
DMA_DeInit(DMA1_Channel4); //
DMA_InitStructure.DMA_PeripheralBaseAddr = SPI2_DR_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RfBuf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//外设为源地址
//DMA_InitStructure.DMA_BufferSize = 65;
DMA_InitStructure.DMA_BufferSize = 64;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //传输时外设地址不自增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存自增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设传输长度为8 bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //内存传输长度为8 bit
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //非循环采集
//DMA_InitStructure.DMA_Priority = DMA_Priority_High; //优先级为中
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //优先级为中
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //非存储器到存储器模式
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC | DMA_IT_TE ,ENABLE);
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, DISABLE);
DMA_Cmd(DMA1_Channel4, DISABLE);
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = SPI2_DR_ADDRESS;
// DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)A7130_READ_ADDR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&A7130_READ_ADDR;
// DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&dummy;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
//DMA_InitStructure.DMA_BufferSize = 65;
DMA_InitStructure.DMA_BufferSize = 64;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
// DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel5,DMA_IT_TC | DMA_IT_TE,ENABLE);
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, DISABLE);
DMA_Cmd(DMA1_Channel5, DISABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
准备将接收的数据放到RfBuf数组中,在主函数中使能发送和接收,主要如下:
DMA_Cmd(DMA1_Channel5, DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel5,66); //配置发送数据长度
DMA_Cmd(DMA1_Channel4, DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel4,64); //配置接收数据长度
DMA_Cmd(DMA1_Channel5, ENABLE);
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx , ENABLE);
DMA_Cmd(DMA1_Channel4, ENABLE);
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);
现在的情况如下:SPI DMA 能收到数据,能放入RfBuf中,当每运行一次,数据的位置都在变化,
例如原本RfBuf的第一次收到的数据为 01,02,03,04.....第二次运行的时候就为00,00,00,01,02,03...第三次运行的时候就为00,00,01,02,03...
第四次运行的时候就为00,01,02,03... 第5次的时候为 01,02,03,04....
还有另外一个问题就是按道理来说,要使用DMA-RX接收数据,需要使用DMA-TX发送时钟,但我发送的时钟已经比接收时钟多两个字节了,却还是先进入发送完成中断先,难道DMA接收和发送的时钟不是同步的吗?
请指教,谢谢! |
|
|