06
2023
11

stm32串口打印(Usart_Printf)时使用DMA

int Usart_Printf(const char *fmt,...)
{
    while(transmission_flag==0); 
	
    va_list ap;
    int n;
    char send_buf[256];
    va_start(ap, fmt);
    n=vsprintf(send_buf, fmt, ap);
    va_end(ap);
    Dma_Enable(send_buf,n);
	transmission_flag=0;
    return n;
}
#include "dma.h"
extern uint8_t transmission_flag;

void DMA1_NVIC_Init(void)
{ 
    NVIC_InitTypeDef NVIC_InitStructure;
        //DMA1通道4 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel =  DMA1_Channel4_IRQn;    
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;        //子优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;            //IRQ通道使能
 
    NVIC_Init(&NVIC_InitStructure);    //根据指定的参数初始化VIC寄存器
}
void Dma_Init()
{
	DMA_InitTypeDef  DMA_InitStructure;
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
	DMA1_NVIC_Init();
	DMA_DeInit(DMA1_Channel4);
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)& (USART1->DR);
	//DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)NULL;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
	DMA_InitStructure.DMA_BufferSize = 0;
	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_VeryHigh;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel4, &DMA_InitStructure);
	DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
	
	
}

void Dma_Enable(uint8_t* buffer,uint32_t len)
{
	DMA_Cmd(DMA1_Channel4, DISABLE);
	DMA1_Channel4->CMAR =  (uint32_t)buffer;
	DMA1_Channel4->CNDTR =len ;
	DMA_Cmd(DMA1_Channel4, ENABLE);
}

void DMA1_Channel4_IRQHandler()
{
	if(DMA_GetFlagStatus(DMA1_FLAG_TC4)==SET)
	{	
		transmission_flag=1;
		DMA_ClearFlag(DMA1_FLAG_TC4);
	}
	
}

Snipaste_2023-11-06_23-04-03.png

gzh

微信扫码关注

更新实时通知

« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。