使用
STM32f051r8 SPI_DMA,进行配置后,能进行SPI DMA的收发,本意是将数据收到一个固定的数组中(RfBuf),在进行测试时,在发送端发送一个固定格式的数据,在spi接收端使用DMA读取,在进行测试时,发现以下问题:
例如发送固定的数据01,02,03,04...
在进行接收的时候,一开始运行,RfBuf接收数据正确收到01,02,03,04..,但当运行大概一段时间后,接收数组RfBuf收到的数据会突然向右移动三个字节,变成00,00,00,01,02,03,04...突然在真实数据的前面加了三个字节的00,接收数据的后三个字节丢弃,再运行一段时间,收到的数据再左移一个字节,变为00,00,01,02,03,04...丢弃后两个字节,再运行一段时间后再次左移一个字节,变为00,01,02,03,04... 运行一段时间后变成01,02,03,04..(正确的数据格式).,之后重复此过程,感觉上这个变化的间隔时间没规律可言,但变化的方向是一致的,先右移动3字节,之后三次左移1个字节恢复正常,重复此过程。
为了测试是不是数据发送端的问题,我使用一般的SPI模式读取,得到的数据是正常的,这个表明不是发送端的问题,下面是我的SPI DMA 配置:
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// Enable DMA1 clock//
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//SPI2 RX DMA 将数据从外设读取到内部存储器中
SPI_Cmd(SPI2, DISABLE);
DMA_Cmd(DMA1_Channel4, DISABLE);
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 = 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_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);
// Tx DMA Config
DMA_Cmd(DMA1_Channel5, DISABLE);
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = SPI2_DR_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)dummy;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 64;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;//DMA_MemoryInc_Enable;
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,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);
SPI_Cmd(SPI2, ENABLE);
在主函数中启动发送过程:
SPI2->DR;
DMA_Cmd(DMA1_Channel5, DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel5,64);
DMA_Cmd(DMA1_Channel4, DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel4,64);
A7130_SPI2_DMA_RX(64,1);
A7130_SPI2_DMA_TX(64);
中断处理过程
void DMA1_Channel4_5_IRQHandler(void)
{
//rx
if(DMA_GetFlagStatus(DMA1_FLAG_TC4) == SET)
{
DMA_ClearITPendingBit(DMA1_FLAG_TC4 | DMA1_FLAG_HT4 |DMA1_FLAG_TE4); //DMA1_FLAG_TC2 | DMA1_FLAG_HT2 | DMA1_FLAG_TE2
DMA_Cmd(DMA1_Channel4, DISABLE);
}
//发送完成
if(DMA_Ge
tiTStatus(DMA1_IT_TC5) == SET)
{
DMA_ClearITPendingBit(DMA1_FLAG_TC5 | DMA1_FLAG_HT5 |DMA1_FLAG_TE5);
DMA_Cmd(DMA1_Channel5, DISABLE);
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, DISABLE);
SPI2->DR;
}
请遇到类似情况的兄弟指导,谢谢!
一周热门 更多>