stm32f407 spi端口DMA模式驱动液晶屏
cpu=stm32f407zgt6
spi=spi3
批量传送方式开启DMA传送,提升刷屏速度
废话少说,上代码。开启DMA后刷屏速度爆表
开启SPI3
//-----------------------------------------------------
void spi3::SPI_RCC_Configuration(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);
}
//-----------------------------------------------------
void spi3::SPI_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 |GPIO_Pin_12;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//GPIO_PuPd_DOWN;
GPIO_Init(GPIOC, &GPIO_InitStruct);
}
//-----------------------------------------------------
void spi3::SPI_Configuration_8b(void)
{
SPI_InitTypeDef SPI_InitStruct;
SPI_RCC_Configuration();
SPI_GPIO_Configuration();
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
//SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI_Init(SPI3,&SPI_InitStruct);
SPI_Cmd(SPI3, ENABLE);
}
开启DMA并且发送
void spi3_DMA_tx::Configuration()
{
DMA_Stream_TypeDef* steram;
steram=DMA1_Stream5;
//steram=DMA1_Stream7;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); //外设基址为:ADC 数据寄存器地址
DMA_DeInit(steram);
DMA_InitTypeDef DMA_InitStructure;
DMA_StructInit(&DMA_InitStructure);
DMA_InitStructure.DMA_Channel = DMA_Channel_0; //通道号和流号码是两回事。 初始化DMA流,流相当于一个大的管道,管道里面有很多通道
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&SPI3->DR); //存储器地址,实际上就是一个内部SRAM的变量
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)this->memorySRC; //数据传输方向为外设到存储器
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //缓冲区大小为,指一次传输的数据量
DMA_InitStructure.DMA_BufferSize = this->data_length;//外设寄存器只有一个,地址不用递增
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//DMA_PeripheralInc_Enable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//DMA_MemoryInc_Disable;
#if 0
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//DMA_MemoryDataSize_Byte;
#else
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
#endif
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //禁止DMA FIFO,使用直连模式
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;//DMA_FIFOMode_Disable; //FIFO 大小,FIFO模式禁止时,这个不用配置
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;//DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//选择 DMA 通道,通道存在于流中
DMA_Init(steram, &DMA_InitStructure);//使能DMA流
SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, ENABLE);//去掉了这个就没有spi的发送波形
DMA_Cmd(steram, ENABLE);
bool finish=false;
do//等待发送结束
{
if(DMA_GetFlagStatus(steram, DMA_FLAG_TCIF5)!=RESET)
{
finish=true;
}
}
while(!finish);
//while (DMA_GetFlagStatus(steram, DMA_FLAG_TCIF5)==RESET); //等待发送结束
}