华大单片机HC32L176串口调试问题

近期做个项目用到了华大单片机HC32L176PATA,在做串口通讯的时候遇到收发数据错误的问题,用了2天的时间查遍了所有问题,最后尝试设置端口后问题得到解决,问题也反应给了华大的FAE,希望在后续的代码中得到更改。具体修改的地方如下图所示

华大单片机HC32L176串口调试问题_第1张图片

红色的框起来的是添加的代码。

完整的代码在下面

/******************************************************************************
* Copyright (C) 2017, Huada Semiconductor Co.,Ltd All rights reserved.
*
* This software is owned and published by:
* Huada Semiconductor Co.,Ltd ("HDSC").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with HDSC
* components. This software is licensed by HDSC to be adapted only
* for use in systems utilizing HDSC components. HDSC shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. HDSC is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* Disclaimer:
* HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
* WARRANTY OF NONINFRINGEMENT.
* HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
* SAVINGS OR PROFITS,
* EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
* FROM, THE SOFTWARE.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/
/******************************************************************************/
/** \file main.c
 **
 ** A detailed description is available at
 ** @link Sample Group Some description @endlink
 **
 **   - 2017-05-17  1.0  cj First version for Device Driver Library of Module.
 **
 ******************************************************************************/

/******************************************************************************
 * Include files
 ******************************************************************************/
#include "ddl.h"
#include "uart.h"
#include "gpio.h"
#include "sysctrl.h"

/******************************************************************************
 * Local pre-processor symbols/macros ('#define')                            
 ******************************************************************************/

/******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/******************************************************************************
 * Local type definitions ('typedef')                                         
 ******************************************************************************/

/******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/******************************************************************************
 * Local variable definitions ('static')                                      *
 ******************************************************************************/

/******************************************************************************
 * Local pre-processor symbols/macros ('#define')                             
 ******************************************************************************/
/*****************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
/**
 ******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 ** This sample
 **
 ******************************************************************************/
uint8_t u8TxData[2] = {0x00,0x55};

void Uart_PortInit(void)
{
    stc_gpio_config_t stcGpioCfg;
    DDL_ZERO_STRUCT(stcGpioCfg);
    stcGpioCfg.enDir = GpioDirOut;
    stcGpioCfg.enPu = GpioPuEnable;
    stcGpioCfg.enCtrlMode = GpioAHB;
    stcGpioCfg.enDrv = GpioDrvH;
    stcGpioCfg.enOD = GpioOdDisable;
    stcGpioCfg.enPd = GpioPdDisable;
    Gpio_Init(GpioPortE,GpioPin15,&stcGpioCfg);
    Gpio_SetAfMode(GpioPortE,GpioPin15,GpioAf3); //配置为UART3 TX
    stcGpioCfg.enDir = GpioDirIn;
    stcGpioCfg.enPu = GpioPuEnable;
    stcGpioCfg.enCtrlMode = GpioAHB;
    stcGpioCfg.enDrv = GpioDrvL;
    stcGpioCfg.enOD = GpioOdDisable;
    stcGpioCfg.enPd = GpioPdDisable;
    Gpio_Init(GpioPortE,GpioPin14,&stcGpioCfg);
    Gpio_SetAfMode(GpioPortE,GpioPin14,GpioAf3);//配置为UART3 RX
}
int32_t main(void)
{  
    uint8_t i;
    stc_uart_config_t  stcConfig;
    //stc_uart_multimode_t stcMulti;
    //stc_uart_baud_t stcBaud;

    DDL_ZERO_STRUCT(stcConfig);
    //DDL_ZERO_STRUCT(stcMulti);
    //DDL_ZERO_STRUCT(stcBaud);
    
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); //GPIO外设模块时钟使能
    Sysctrl_SetPeripheralGate(SysctrlPeripheralUart3,TRUE);//UART0外设模块时钟使能
    Uart_PortInit();//端口初始化
	
	stcConfig.enRunMode = UartMskMode1;    //模式3
    stcConfig.enStopBit = UartMsk1bit;     //1位停止位
	stcConfig.enMmdorCk = UartMskDataOrAddr;     //偶校验
	stcConfig.stcBaud.u32Baud = 9600;      //波特率9600
	stcConfig.stcBaud.enClkDiv = UartMsk8Or16Div;      //通道采样分频配置
	stcConfig.stcBaud.u32Pclk = Sysctrl_GetPClkFreq(); //获得外设时钟(PCLK)频率值
	Uart_Init(M0P_UART3, &stcConfig);     //串口初始化

    Uart_ClrStatus(M0P_UART3,UartRC);    //清接收请求
	Uart_ClrStatus(M0P_UART3,UartTC);    //清发送请求
    Uart_DisableIrq(M0P_UART3,UartRxIrq); //使能串口接收中断
    Uart_DisableIrq(M0P_UART3,UartTxIrq); //使能串口发送中断
    
    while(1)
    {
        if((Uart_GetStatus(M0P_UART3, UartFE))||(Uart_GetStatus(M0P_UART3, UartPE)))//错误请求
        {
            Uart_ClrStatus(M0P_UART3, UartFE);  //清除帧错误标记
            Uart_ClrStatus(M0P_UART3, UartPE);  //清除奇偶校验错误标记
        }
        if(Uart_GetStatus(M0P_UART3,UartRC))    //接收到数据
        {
            Uart_ClrStatus(M0P_UART3,UartRC);
            u8TxData[0] = Uart_ReceiveData(M0P_UART3);
            for(i=0;i<2;i++)
            {
                Uart_SendDataPoll(M0P_UART3,u8TxData[i]);  //查询方式发送数据
            }
        }
    }
}

/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/


 

你可能感兴趣的:(华大单片机)