SPI2我采用了16位DMA数据传输方式,收发均开DMA,stm32为从机,在测试过程中发现,接收通道数据一切正常,但是发送数据会有丢数据的情况,举例说,每一轮触发16次数据传输,发送1-16这几个数字;测试结果发现会出现丢掉比如说2或者5(该数字为随机值)的现象,但是如果我改为8位DMA数据传输方式的话,就不存在这个问题。不知道各位朋友有没有发现类似的现象
请各位高人指点迷津!!!不胜感激!!
我是发送没问题,接收有时候就收不到最后一个数据,楼上的有没有遇到?
没有,楼上也是发送16位吗?
我的接收没问题,但发送有问题
版主
谢谢您的回复
这个设置是没问题的,我反复检查过多次啦
void SPI_configuration()
{
SPI_InitTypeDef SPI_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
SPI_Cmd(SPI2, DISABLE);
SPI_I2S_DeInit(SPI2);
//----------SPI_SLAVE configuration-------
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //36M/xx......
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
DMA_DeInit(DMA1_Channel5); //Tx
DMA_InitStructure.DMA_PeripheralBaseAddr = SPI_SLAVE_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI_SLAVE_Buffer_Tx; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = BufferSize;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_Priority_Low;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_DeInit(DMA1_Channel4); //Rx
DMA_InitStructure.DMA_PeripheralBaseAddr = SPI_SLAVE_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI_SLAVE_Buffer_Rx;//Rx
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = BufferSize;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
SPI_Cmd(SPI2, ENABLE);
/* Enable SPI_SLAVE Tx request */
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE); //;;;;
DMA_Cmd(DMA1_Channel5, ENABLE);
DMA_Cmd(DMA1_Channel4, ENABLE);
DMA_ITConfig(DMA1_Channel5, DMA_IT_TE, ENABLE); //Tx
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); //Rx..need
}
代码如上,如果我以8位的方式传输,就没有问题
固定长度的收发应该不会出现问题,我用串口收发16位定长数据,一点问题都没有。