static void DMA1_SPI1_ReConfig(u8 u8Direction, u8 *pBuffer, u32 u32Len)
{
DMA_InitTypeDef DMA_InitStructure;
if(u8Direction == SPI_Direction_Transmitter)
{
DMA_InitStructure.DMA_Channel = DMA_Channel_3;
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI1_DR_Addr; //硬件地址
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)pBuffer; //内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //DMA传输方向 外设作为数据传送的目的地
DMA_InitStructure.DMA_BufferSize = u32Len; //设置BUFFER缓存大小
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传输模式 循环模式
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //DMA的优先级
DMA_InitStructure.DMA_FIFOMode = 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(DMA2_Stream3, &DMA_InitStructure);
DMA_ITConfig(DMA2_Stream3, DMA_IT_TC, ENABLE);
}
else
{
DMA_InitStructure.DMA_Channel = DMA_Channel_3;
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI1_DR_Addr; //硬件地址
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)pBuffer; //内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //DMA传输方向 内存作为数据传送的目的地
DMA_InitStructure.DMA_BufferSize = u32Len; //设置BUFFER缓存大小
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传输模式 循环模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA的优先级
DMA_InitStructure.DMA_FIFOMode = 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(DMA2_Stream0, &DMA_InitStructure);
}
}
void DMA1_SPI1_WriteRead(u16 u16Len)
{
u32 TimeOut;
DMA_ClearFlag(DMA2_Stream0, DMA_FLAG_TCIF0); //清除接收的完成标志位
DMA_ClearFlag(DMA2_Stream3, DMA_FLAG_TCIF3); //清除发送的完成标志位
SPI1->DR; //接送前读一次SPI1->DR
while((SPI1->SR & 0x02) == 0); //等待发送缓存为空
DMA_SetCurrDataCounter(DMA2_Stream3, u16Len);
DMA_SetCurrDataCounter(DMA2_Stream0, u16Len);
DMA_Cmd(DMA2_Stream3, ENABLE); //使能DMA发送
if(l_u8Stat_WR == SPI_Direction_Receiver) //写的时候不去更新读的数据
DMA_Cmd(DMA2_Stream0, ENABLE); //使能DMA接收
TimeOut = 0xFFFF;
while(DMA_GetITStatus(DMA2_Stream3, DMA_FLAG_TCIF3) == RESET) //发送完成标志
{
if(TimeOut-- == 0)
break;
}
DMA_Cmd(DMA2_Stream3, DISABLE);
DMA_Cmd(DMA2_Stream0, DISABLE);
}
读可以正常读 但写的时候 前面有一个字节产生 单步调试会正常 |