本文章仅仅简单记录32单片机的SPI+DMA驱动显示屏的性能测试,这里不花费时间介绍SPI和DMA。
硬件材料:SPI显示屏一个,32单片机
软件材料:
1.LCD的SPI驱动显示程序(SPI / SPI+DMA):
(1)SPI的配置程序:
SPI_HandleTypeDef SPI3_Handler; //create a SPI handle
/**
* @brief SPI3 master mode
* @param void
* @return void
*/
void SPI3_Init(void)
{
SPI3_Handler.Instance=SPI3;
SPI3_Handler.Init.Mode=SPI_MODE_MASTER;
SPI3_Handler.Init.Direction=SPI_DIRECTION_2LINES;
SPI3_Handler.Init.DataSize=SPI_DATASIZE_8BIT;
SPI3_Handler.Init.CLKPolarity=SPI_POLARITY_HIGH; //Idle state clock is high
SPI3_Handler.Init.CLKPhase=SPI_PHASE_2EDGE;
SPI3_Handler.Init.NSS=SPI_NSS_SOFT;
SPI3_Handler.Init.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_2;//baudrate is highest
SPI3_Handler.Init.FirstBit=SPI_FIRSTBIT_MSB;
SPI3_Handler.Init.TIMode=SPI_TIMODE_DISABLE;
SPI3_Handler.Init.CRCCalculation=SPI_CRCCALCULATION_DISABLE;
SPI3_Handler.Init.CRCPolynomial=7; //omit
HAL_SPI_Init(&SPI3_Handler);
__HAL_SPI_ENABLE(&SPI3_Handler);
}
/**
* @brief SPI3 lower driver config:perip clock ,io
* @param hspi: the SPI handle pointer should be use
* @return void
*/
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_SPI3_CLK_ENABLE();
GPIO_Initure.Pin=GPIO_PIN_3|GPIO_PIN_5;
GPIO_Initure.Mode=GPIO_MODE_AF_PP;
GPIO_Initure.Pull=GPIO_PULLUP;
GPIO_Initure.Speed=GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_Initure.Alternate=GPIO_AF6_SPI3;
HAL_GPIO_Init(GPIOB,&GPIO_Initure);
}
(2)SPI的读写接口(非DMA情况下需要使用到)
/**
* @brief SPI3 R/W one octet
* @param TxData the u8 data which should be writed
* @return the u8 data received
*/
u8 SPI3_ReadWriteByte(u8 TxData)
{
u8 Rxdata;
HAL_SPI_TransmitReceive(&SPI3_Handler,&TxData,&Rxdata,1, 1000);
return Rxdata;
}
/**
* @brief SPI3 Write one octet
* @param TxData the tx payload
* @param size payload length
* @return u8 0:success,others:fail
*/
u8 SPI3_WriteByte(u8 *TxData,u16 size)
{
return HAL_SPI_Transmit(&SPI3_Handler,TxData,size,1000);
}
(3)SPI的DMA配置初始化
我使用的是Stm32L4做SPI3+DMA的测试,首先看芯片手册DMA相关的内容:
上面的表格罗列了DMA2每个通道的DMA请求,SPI3_Tx对应通道2,请求3(0011),那么我们可以这样配置SPI3的DMA通道:
void vidSPI3DMA_Config(void)
{
__HAL_RCC_DMA2_CLK_ENABLE();//DMA2 Clock Enable
__HAL_LINKDMA(&SPI3_Handler, hdmatx, SPI3TxDMA_Handler); //Link DMA to SPI3, here we use the dma tx function
//Tx Dma config
SPI3TxDMA_Handler.Instance = DMA2_Channel2; //select the channel
SPI3TxDMA_Handler.Init.Request = DMA_REQUEST_3; //request(CxS:0011)
SPI3TxDMA_Handler.Init.Direction = DMA_MEMORY_TO_PERIPH; //memory to peripheral
SPI3TxDMA_Handler.Init.PeriphInc = DMA_PINC_DISABLE; //peripheral not increment
SPI3TxDMA_Handler.Init.MemInc = DMA_MINC_ENABLE; //memory inc
SPI3TxDMA_Handler.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; //perip data length:8 bit
SPI3TxDMA_Handler.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; //mem data length:8 bit
SPI3TxDMA_Handler.Init.Mode = DMA_NORMAL;
SPI3TxDMA_Handler.Init.Priority = DMA_PRIORITY_HIGH;
SPI3TxDMA_Handler.State = HAL_DMA_STATE_READY;
HAL_DMA_DeInit(&SPI3TxDMA_Handler);
HAL_DMA_Init(&SPI3TxDMA_Handler);
HAL_NVIC_SetPriority(DMA2_Channel2_IRQn, 0, 3);
HAL_NVIC_EnableIRQ(DMA2_Channel2_IRQn);
}
(4)使用DMA中断需要调用DMA中断函数去清除相关中断标志位。
/*
* @brief: DMA2 Channel2 IRQ handler function
* mainly clear the interrupt flag
*/
void DMA2_Channel2_IRQHandler(void)
{
HAL_DMA_IRQHandler(&SPI3TxDMA_Handler);
}
/*
* @brief: After Tx is completed, the SPI DMA should be stopped for next transfer
*/
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{
HAL_SPI_DMAStop(hspi);
}
2.LCD显示驱动程序:
(1)LCD基于SPI的数据发送接口:
/**
* @brief LCD SPI send data API
* @return void
*/
#define NO_USING_DMA 1
static void LCD_SPI_Send(u8 *data, u32 size)
{
#if NO_USING_DMA
u32 i;
u32 delta = size/0xFFFF;
for(i = 0; i<=delta; i++)
{
if( i==delta ) /* Send the last data */
SPI3_WriteByte(&data[i*0xFFFF], size%0xFFFF);//cause this API 2nd param is u16 type
else /* send 0xFFFF octets */
SPI3_WriteByte(&data[i*0xFFFF], 0xFFFF);
}
#else
while(SPI3_Handler.State != HAL_SPI_STATE_READY);
HAL_SPI_Transmit_DMA(&SPI3_Handler, data, size);
#endif
return;
}
往LCD写任何数据都是用的这个接口LCD_SPI_Send。关于图片的绘制的函数这里不多余贴出来,相信网上一大把程序可以参考,这里直接使用定时器去了解不开DMA和开DMA显示同一张图片耗时差异,时间从串口打印出来:文章来源:https://www.toymoban.com/news/detail-698749.html
u32 t=0;
printf("Show a 240X82 picture,Start: %d | ", t);
vidEnableTIM3(1);//enable TIM3,and clear the counter
Display_240x82_pic(0, 0);
t = u32GetTim3Counter();
printf("End: %d\r\n", t);
这里我定时器计数频率分频到1Mhz,最大计数50000次,也是就计数频率1us,周期50ms。
第一次我是以SPI+DMA驱动LCD显示240X82的图片,可以看出显示该图片在有DMA(加DMA中断)的加持下,耗时156us。第二次是我单独用SPI驱动LCD显示240X82的图片,耗时8685us,所以区别还是蛮大的。文章来源地址https://www.toymoban.com/news/detail-698749.html
到了这里,关于SPI+DMA传输性能比较的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!