留着 以后用
为了提高传输效率我使用 SPI 的 DMA 模式进行传输,测试过程中将 MISO 和 MOSI 短接,直接发送和接收。当数据量为 256 个的时候发送和接收一致。当时数据量为300 的时候后面的20个数据就异常了。下面是我的代码烦请大家帮忙看看,或者提供一下思路,谢谢!
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
int ii =0;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/*!< Enable SPI2 clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
/*!< Enable GPIO clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
/*!< Connect SPI pins to AF5 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13,GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14,GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15,GPIO_AF_SPI2);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
Cdfinger_BspCs_HIGH();
//Cdfinger_Bspdelay_ms(1);
//Cdfinger_BspCs_LOW();
SPI_Cmd(SPI2,DISABLE);
SPI_DeInit(SPI2);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
memset(&cdfingerimgtxbuf[0],0x55,COMMUNICATIONLEN);
/* DMA1 Stream0 channel4 spi tx configuration **************************************/
DMA_DeInit(DMA1_Stream4);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(SPI2->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&cdfingerimgtxbuf[0];
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = COMMUNICATIONLEN;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//;
DMA_InitStructure.DMA_MemoryInc = 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_High;//DMA_Priority_Medium;//DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//;//DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream4, &DMA_InitStructure);
/* DMA1 Stream0 channel3 spi rx configuration **************************************/
DMA_DeInit(DMA1_Stream3);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(SPI2->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&cdfingerimgrxbuf[0];//(uint32_t)&cdfingerimgrxbuf[0];
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize =COMMUNICATIONLEN;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = 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_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;//DMA_Priority_Medium;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream3, &DMA_InitStructure);
//Cdfinger_BspCs_HIGH();
//发送中断
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//接收中断
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x04;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//SPI_TIModeCmd(SPI2,ENABLE);
SPI_Cmd(SPI2, ENABLE);
DMA_ITConfig(DMA1_Stream4,DMA_IT_TC,ENABLE);
DMA_ITConfig(DMA1_Stream3,DMA_IT_TC,ENABLE);
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx|SPI_I2S_DMAReq_Rx, ENABLE);
DMA_Cmd(DMA1_Stream4, ENABLE);
DMA_Cmd(DMA1_Stream3, ENABLE);
void DMA1_Stream4_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_Stream4,DMA_IT_TCIF4) != RESET)
{
DMA_ClearITPendingBit(DMA1_Stream4,DMA_IT_TCIF4);
DMA_ClearFlag(DMA1_Stream4,DMA_IT_TCIF4);
}
}
void DMA1_Stream3_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_Stream3,DMA_IT_TCIF3) != RESET)
{
DMA_ClearITPendingBit(DMA1_Stream3,DMA_IT_TCIF3);
DMA_ClearFlag(DMA1_Stream3,DMA_IT_TCIF3);
}
} |