首页 > 其他分享 >void usart_dma_init(void)

void usart_dma_init(void)

时间:2024-04-11 14:35:16浏览次数:20  
标签:dma USART DMA void NVIC init DMA1 InitStructure GPIO

void usart_dma_init(void)
{
    GPIO_InitTypeDef    GPIO_InitStructure;
    USART_InitTypeDef    USART_InitStructure;
    DMA_InitTypeDef        DMA_InitStructure;
    NVIC_InitTypeDef    NVIC_InitStructure;


    
    /*  配置GPIO的模式和IO口 */    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);  
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;                    //TX
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;            //复用推挽输出
    GPIO_Init(GPIOA,&GPIO_InitStructure);                    //初始化串口输入IO
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;                //RX
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;        //模拟输入
    GPIO_Init(GPIOA,&GPIO_InitStructure);     


    
    /*初始化串口接收和发送函数*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;  
    USART_InitStructure.USART_StopBits = USART_StopBits_1;  
    USART_InitStructure.USART_Parity = USART_Parity_No;  
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;    
    USART_InitStructure.USART_BaudRate = DEFAULT_BAUD; 
    
    /*初始化串口*/
    USART_Init(USART1,&USART_InitStructure);
    
    /*中断配置*/
    USART_ITConfig(USART1,USART_IT_TC,DISABLE);
    USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
    USART_ITConfig(USART1,USART_IT_IDLE,ENABLE); 
    
    //配置UART1中断  
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;              //通道设置为串口1中断  
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;      //中断占先等级0  
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;             //中断响应优先级0  
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                //打开中断  
    NVIC_Init(&NVIC_InitStructure);



    /*DMA发送中断设置*/
    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    
    /*DMA1通道4配置发送*/
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
    DMA_DeInit(DMA1_Channel4);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Send_Buffer;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = 100;
    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_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA1_Channel4,&DMA_InitStructure);
    DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
    //DMA_Cmd(DMA1_Channel4, ENABLE);//使能通道4,一般发送的时候再使能
 
 

    /*DMA1通道5配置接收*/
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
    DMA_DeInit(DMA1_Channel5);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Rx;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = UART_RX_LEN;
    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_Channel5,&DMA_InitStructure);
 
    /*使能通道5*/
    DMA_Cmd(DMA1_Channel5,ENABLE);

 
        
    //采用DMA方式发送
    USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
    
    //采用DMA方式接收
    USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
    
    //启动串口  
    USART_Cmd(USART1, ENABLE);
}
    
View Code
/**@ brief 使能发送数据
 *
 * 启动DMA数据发送功能
 * 
 * @param size表示需要发送的DMA中数据的个数
 */
void uart_dma_send_enable(uint16_t size)
{
    DMA1_Channel4->CNDTR = (uint16_t)size; 
    DMA_Cmd(DMA1_Channel4, ENABLE);       
}    
    

/**@ brief串口1接收中断
 *
 * 收到一帧数据进入一次,进行DMA的读取
 * 
 */
void USART1_IRQHandler(void)                               
{   
    uint32_t temp = 0;
    uint16_t i = 0;
    
    if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
    {
        //USART_ClearFlag(USART1,USART_IT_IDLE);
        temp = USART1->SR;
        temp = USART1->DR; //清USART_IT_IDLE标志
        
        DMA_Cmd(DMA1_Channel5,DISABLE);
 
        temp = UART_RX_LEN - DMA_GetCurrDataCounter(DMA1_Channel5);
        for (i = 0;i < temp;i++)
        {
            Data_Receive_Usart = Uart_Rx[i];
            
            //+++对收到的数据加一后回发出去        
            Uart_Send_Buffer[i]=Data_Receive_Usart+1;                        
            //+++            
        }
        //uart_dma_send_enable(temp);
        
        //设置传输数据长度
        DMA_SetCurrDataCounter(DMA1_Channel5,UART_RX_LEN);
        
        //打开DMA
        DMA_Cmd(DMA1_Channel5,ENABLE);
    } 
}     



/**@ brief DMA发送中断
 *
 * 发送数据,将DMA中的数据发送出去
 * 
 */
void DMA1_Channel4_IRQHandler(void)
{
    if(DMA_GetITStatus(DMA1_FLAG_TC4)==SET)
    {
            DMA_ClearFlag(DMA1_FLAG_GL4);        
            DMA_Cmd(DMA1_Channel4, DISABLE);  
    }
}
View Code
int main()
{    
    usart_dma_init();    
    while(1)
    {
        uart_dma_send_enable(5);
        delay_ms(100);
    }
}
View Code

 

标签:dma,USART,DMA,void,NVIC,init,DMA1,InitStructure,GPIO
From: https://www.cnblogs.com/zhouyuqing1024/p/18129094

相关文章

  • Rust Reference Cycles: Resolving and Avoiding them
    InRust,referencecyclesoccurwhentwoormoreobjectsmutuallyreferenceeachother,formingacircularchain.Inthissituation,thereferencecountbetweenobjectsneverbecomeszero,leadingtomemoryleaksandresourceleaks.Thisblogpostwilldi......
  • 52 Things: Number 9: What are Shannon's definitions of entropy and information?
    52Things:Number9:WhatareShannon'sdefinitionsofentropyandinformation?52件事:数字9:香农对熵和信息的定义是什么?Thisisthelatestinaseriesofblogpoststoaddressthelistof'52ThingsEveryPhDStudentShouldKnowToDoCryptography':a......
  • windows环境安装Podman
    打开虚拟设置选择启用或关闭Windows功能勾选“适用于Linux的Windows子系统”和“虚拟机平台”两个选项。 安装WSL管理员模式启动powershell设置wsl版本:wsl--set-default-version2-更新wsl核:wsl--update【若干分钟】安装wsl:wsl--install【若干分钟】完成后需要设置......
  • GD32F470II的UART+DMA方式的使用笔记
    GD32官方给的DEMO真的是屎一样的存在,仅展示最基本简单的应用案例,拿到实际工程中参考性非常低,也就基本的配置过程具有有限的参考性。在这种环境下,使用UART+DMA的方式完全是瞎用,感觉能用的函数都给用上。UART&DMA配置如下:1/*!2\briefconfigureUSARTDMA3......
  • CF1097H Mateusz and an Infinite Sequence
    这种模非常小的数意义下的递归结构的区间查询显然可以抽象为\(O(d\log_{d}V)\)次信息的合并,问题的关键在于如何快速的处理信息的合并。一个非常\(\text{trival}\)的想法是每次合并时直接计算跨过分界点的区间个数,但这个严格不弱于通配符匹配问题,直接使用卷积只能做到\(O(nm......
  • 3dmax渲染十几个小时怎么办?3dmax怎么多机渲染
    当使用3dsMax进行渲染作业时,如果发现单张图像的渲染时间长达十数小时,这可能是由于计算机硬件配置较低或渲染场景过于复杂所致。为了缩短渲染时间并提高效率,我们可以考虑采用多台计算机进行协同渲染。下面,让我们一起探讨如何通过这种方式优化渲染流程。3dmax渲染十几个小时正常......
  • 使用SPI+DMA控制算法驱动WS2812
    1、ws2812b是一款集控制电路与发光电路于一体的智能外控LED光源,采用单线归0码协议,每个像素点的三基色颜色可实现256级亮度显示。速率能达到1024pixel×30fps/s,故被广泛用于各种需要大量使用RGB灯的场合。2、不同厂商生产的ws2812存在不同的时序要求,下图是一款最常见的ws2812b......
  • a value of type "int" cannot be used to initialize an entity of type enum
    报错解释:这个报错信息表明你尝试使用一个整型(int)值去初始化一个枚举类型(enum)的实体,但是这样的操作是不允许的。在C++等编程语言中,枚举(enum)类型是一种用户定义的数据类型,它仅仅限定变量可以从一个预定义的常量值集合中取值。解决方法:要解决这个问题,你需要确保初始化枚举类型的实体......
  • DMA第四版代码
    描述:我发现并不是所有的任务都有同步信号,这一期版本使用参数来控制是否需要同步信号;Test_Flow_Mode参数为高的时候意味着需要同步,否则不需要,仅检测数据流。不需要同步的情况如果丢包会出现严重后果。参考这篇笔记:https://www.cnblogs.com/VerweileDoch/p/18111545代码:`tim......
  • [React] Using key prop to reset component to avoid useEffect hook
    ThecomponentusinguseEffectwhichisnotnecessary:functionTopicEditor({selectedTopicId}){const[enteredNote,setEnteredNote]=useState('');constselectedTopic=DUMMY_TOPICS.find(topic=>topic.id===selectedTopicId)......