MTK串口配置及流程

MTK 专栏收录该内容
3 篇文章 0 订阅

端口配置

static port_setting_struct const NVRAM_EF_PORT_SETTING_DEFAULT[]=
{
    {
    /* USB转虚拟串口 */
        99,   
        4,   /* uart_port_usb */           
    /* 串口输出 */
        99,  /* tst-ps uses uart_port_null(value is 99) */
        0,   /* uart_port1 (value is 0) */
/* Catcher打印trace */
        4,   
        99,

        115200, /* tst default baud rate base = 115200 = 0x0001C200 */

流程

初始化注册各端口的handler

将uart_port1与Uart_Drv_Handler绑定,将uart_port_usb端口与USB2UART_Drv_Handler绑定。

// hal/peripheral/src/drv_comm.c
void Drv_Init_Phase1(void)
{
    DCL_HANDLE uart_handle;
    uart_handle =  DclSerialPort_Open(uart_port1, 0);
    DclSerialPort_RegisterCallback(uart_handle, &Uart_Drv_Handler);
}

void Drv_Init_Phase2(void)
{
    DCL_HANDLE uart_handle;

    #ifdef __USB_COM_PORT_ENABLE
    uart_handle = DclSerialPort_Open(uart_port_usb, 0);
    DclSerialPort_RegisterCallback(uart_handle, &USB2UART_Drv_Handler);
}

Uart_Drv_Handler的实现

// hal/peripheral/src/uart_handler.c
Seriport_HANDLER_T  Uart_Drv_Handler = {DCL_UART_TYPE, UART_Handler};

DCL_STATUS UART_Handler(DCL_DEV dev,DCL_CTRL_CMD cmd, DCL_CTRL_DATA_T *data)
{
    kal_bool return_flag = KAL_FALSE;
    kal_bool return_status = STATUS_OK;

    switch (cmd)
        {
        case SIO_CMD_INIT:
                {
                    UART_CTRL_INIT_T*    prCtrlInit;
                    prCtrlInit = &(data->rUARTCtrlInit);
                    UART_DriverInit(dev,prCtrlInit->u4Flag);
                    //UART_DriverInit(dev);         
                }
            break;

        case SIO_CMD_OPEN:
                {
                    UART_CTRL_OPEN_T* prCtrlOpen;
                    prCtrlOpen = &(data->rUARTCtrlOPEN);
                    return_flag = pUart_CMD_FUNC[dev]->Open(dev,prCtrlOpen->u4OwenrId);
                    if(return_flag == KAL_FALSE)
                        return_status = STATUS_FAIL;
                }
            break;

        case SIO_CMD_CLOSE:
                {
                    UART_CTRL_CLOSE_T* prCtrlClose;
                    prCtrlClose = &(data->rUARTCtrlCLOSE);
                    pUart_CMD_FUNC[dev]->Close(dev,prCtrlClose->u4OwenrId);
                }
            break;

        case SIO_CMD_GET_BYTES:
                {
                    UART_CTRL_GET_BYTES_T* prCtrlGetBytes;
                    prCtrlGetBytes = &(data->rUARTCtrlGETBYTES);
                prCtrlGetBytes->u2RetSize=pUart_CMD_FUNC[dev]->GetBytes(dev, prCtrlGetBytes->puBuffaddr, prCtrlGetBytes->u2Length, prCtrlGetBytes->pustatus, prCtrlGetBytes->u4OwenrId);
                }
            break;

        case SIO_CMD_PUT_BYTES:
                {
                    UART_CTRL_PUT_BYTES_T* prCtrlPutBytes;
                    prCtrlPutBytes = &(data->rUARTCtrlPUTBYTES);
                prCtrlPutBytes->u2RetSize=  pUart_CMD_FUNC[dev]->PutBytes(dev, prCtrlPutBytes->puBuffaddr, prCtrlPutBytes->u2Length, prCtrlPutBytes->u4OwenrId);
                }
            break;
            ...
    }
    return return_status;
}   

USB2UART_Drv_Handler的实现

// usb/src/usbacm_adp.c
Seriport_HANDLER_T  USB2Uart_Drv_Handler = {DCL_UART_USB_TYPE, USB2UartDriver};

DCL_STATUS USB2UartDriver(DCL_DEV dev,DCL_CTRL_CMD cmd, DCL_CTRL_DATA_T *data)
{
    DCL_STATUS return_flag = KAL_FALSE;
    DCL_STATUS return_status = STATUS_OK;

    switch (cmd)
    {
        case SIO_CMD_INIT:
            USB2UART_init();
            break;

        case SIO_CMD_OPEN:
            {
                UART_CTRL_OPEN_T* prCtrlOpen;
                prCtrlOpen = &(data->rUARTCtrlOPEN);
                return_flag = USB2UART_open(dev, (module_type)prCtrlOpen->u4OwenrId);
                if(return_flag == KAL_FALSE)
                    return_status = STATUS_FAIL;
            }
            break;

        case SIO_CMD_CLOSE:
            {
                UART_CTRL_CLOSE_T* prCtrlClose;
                prCtrlClose = &(data->rUARTCtrlCLOSE);
                USB2UART_close(dev, (module_type)prCtrlClose->u4OwenrId);
            }
            break;

        case SIO_CMD_GET_BYTES:
            {
                UART_CTRL_GET_BYTES_T* prCtrlGetBytes;
                prCtrlGetBytes = &(data->rUARTCtrlGETBYTES);
                prCtrlGetBytes->u2RetSize=USB2UART_GetBytes(dev, prCtrlGetBytes->puBuffaddr, prCtrlGetBytes->u2Length, prCtrlGetBytes->pustatus, (module_type)prCtrlGetBytes->u4OwenrId);
            }
            break;

        case SIO_CMD_PUT_BYTES:
            {
                UART_CTRL_PUT_BYTES_T* prCtrlPutBytes;
                prCtrlPutBytes = &(data->rUARTCtrlPUTBYTES);
                prCtrlPutBytes->u2RetSize=  USB2UART_PutBytes(dev, prCtrlPutBytes->puBuffaddr, prCtrlPutBytes->u2Length, (module_type)prCtrlPutBytes->u4OwenrId);
            }
            break;
        ...
    }
    return return_status;
}   

HAL层

然后可以通过DCL_STATUS DclSerialPort_Control(DCL_HANDLE handle, DCL_CTRL_CMD cmd, DCL_CTRL_DATA_T *data)来控制。

//ps/dt/src/dt_utility.c
void DT_HAL_UART_SetOwner(kal_uint16 port, module_type owner)
{
    DCL_HANDLE handle;
    UART_CTRL_OWNER_T data;
    data.u4OwenrId = (DCL_UINT32)owner;

    handle = DclSerialPort_Open((DCL_DEV)port, 0);
    DclSerialPort_Control(handle, SIO_CMD_SET_OWNER, (DCL_CTRL_DATA_T*)&data);
}

kal_uint16 DT_HAL_UART_GetBytes(kal_uint16 port, kal_uint8 *Buffaddr, kal_uint16 Length, kal_uint8 *status, module_type ownerid)
{
    DCL_HANDLE handle;
    UART_CTRL_GET_BYTES_T data;

    data.u4OwenrId = (DCL_UINT32)ownerid;
    data.u2Length = (DCL_UINT16)Length;
    data.puBuffaddr = (DCL_UINT8*)Buffaddr;
    data.pustatus = (DCL_UINT8*)status;

    handle = DclSerialPort_Open((DCL_DEV)port, 0);
    DclSerialPort_Control(handle, SIO_CMD_GET_BYTES, (DCL_CTRL_DATA_T*)&data);
    return (kal_uint16)data.u2RetSize;
}

kal_uint16 DT_HAL_UART_PutBytes(kal_uint16 port, kal_uint8 *Buffaddr, kal_uint16 Length, module_type ownerid)
{
    DCL_HANDLE handle;
    UART_CTRL_PUT_BYTES_T data;

    data.u4OwenrId = (DCL_UINT32)ownerid;
    data.u2Length = (DCL_UINT16)Length;
    data.puBuffaddr = (DCL_UINT8*)Buffaddr;

    handle = DclSerialPort_Open((DCL_DEV)port, 0);
    DclSerialPort_Control(handle, SIO_CMD_PUT_BYTES, (DCL_CTRL_DATA_T*)&data);
    return (kal_uint16)data.u2RetSize;
}

USB转虚拟串口

端口配置为99, 4

//初始化
static kal_uint8 ghSleepMode;
static void init_uart(void)
{
    //禁止休眠
    ghSleepMode = L1SM_GetHandle();  
    L1SM_SleepDisable(ghSleepMode);
    DT_HAL_UART_SetOwner(uart_port_usb, MOD_MMI);
}

kal_uint8 status;
kal_uint8 tmp;
kal_uint8 tmpLen;
//发送数据
DT_HAL_UART_PutBytes(uart_port_usb, tmp, strlen(tmp), MOD_MMI);
//接收数据
tmpLen = DT_HAL_UART_GetBytes(uart_port_usb, tmp, sizeof(tmp), &status, MOD_MMI);

串口输出

端口配置为99, 0

//初始化
static kal_uint8 ghSleepMode;
static void init_uart(void)
{
    //禁止休眠
    ghSleepMode = L1SM_GetHandle();  
    L1SM_SleepDisable(ghSleepMode);
    DT_HAL_UART_SetOwner(uart_port1, MOD_MMI);
}

kal_uint8 status;
kal_uint8 tmp;
kal_uint8 tmpLen;
//发送数据
DT_HAL_UART_PutBytes(uart_port1, tmp, strlen(tmp), MOD_MMI);
//接收数据
tmpLen = DT_HAL_UART_GetBytes(uart_port1, tmp, sizeof(tmp), &status, MOD_MMI);
  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值