UartDma工作方式

一、初始化

1.初始化串口,时钟

MX_USART1_UART_Init();

串口时钟初始化为内部时钟

PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_HSI;

PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_HSI;

2.初始化dma,端口复用

HAL_UART_MspInit()

打开空闲帧中断

__HAL_UART_ENABLE_IT(uartHandle, UART_IT_IDLE);

二、启动接收传送

1.启动数据接收

/*启动DMA接收传送*/
void    Mx_Uart_DebugRxStart(UART_E uart)
{
    if(uart>= UART_MAX){return ;}
    if(!g_stMyUart[uart].bDebugRxStop){return ;}

    for(uint8 i=0;i<UART_DEBUG_BUFF_LEVER;i++)
    {
        if(!g_stMyUart[uart].bDebugRxAvail[i])
        {
            g_stMyUart[uart].bDebugRxStop=false;
            HAL_UART_Receive_DMA(g_stMyUart[uart].huart
              ,g_stMyUart[uart].cDebugBuffRx[i]
              ,UART_RX_BUFF_SIZE-1);
            g_stMyUart[uart].cCurDebugRxBuff=i;
            g_stMyUart[uart].uDebugRxCount[i]=0;
            break;
        }
    }
}

2.如果接收完数据则触发DMA中断,触 发DMa中断后,调用传送完成中断,如果收到空闲帧,也会调用传送完成中断

uint32_t temp;
    temp = huart->Instance->ISR;
    temp = huart->Instance->RDR;
    if(HAL_OK !=HAL_UART_DMAStopRx(huart))
    {
        Error_Handler();
    }
    g_stMyUart[uart].bDebugRxStop=true;
    if(NULL != huart->hdmarx)
    {
        temp  = huart->hdmarx->Instance->CNDTR;
        MX_UART_DebugGetData(uart,temp);
        Mx_Uart_DebugRxStart(uart);
    }
    else{Error_Handler();}

停止DMA传送,置接收停止标志,读取数据,重新开始数据接收

三、发送DMA

1.启动传送

if(HAL_UART_Transmit_DMA(g_stMyUart[UART_DEBUG].huart
                      , g_stMyUart[UART_DEBUG].cDebugBuffTx[i]
                      , g_stMyUart[UART_DEBUG].uDebugTxCount[i]) == HAL_OK)
                    {
                        g_stMyUart[UART_DEBUG].cCurDebugTxBuff=i;
                        g_stMyUart[UART_DEBUG].uDebugTxCount[i]=0;
                    }

2.DMA传送完成时,打开串口发送完成中断,

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
    UART_E uart;
    for(uart=UART_DEBUG;uart<UART_MAX;uart++)
    {
        if(g_stMyUart[uart].huart == huart){break;}
    }
    if(UART_MAX == uart){return ;}

    HAL_UART_DMAStopTx(huart);
    if(g_stMyUart[uart].cCurDebugTxBuff <UART_DEBUG_BUFF_LEVER)
    {
        g_stMyUart[uart].bDebugTxEn[g_stMyUart[uart].cCurDebugTxBuff]=false;
    }
}

转载于:https://www.cnblogs.com/jiangbo1980/p/5567850.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值