原创 MTK之Uart篇

2010-7-18 12:47 9864 6 6 分类: 软件与OS

注:个人收集和理解所书,有误请指出,转载请注明出处


在MMI层中设置如下钩子来响应UART的READY  TO  READ消息,这时AT指令无效,直接发往MMI层。


原因:(MSG_ID_UART_READY_TO_READ_IND代号①


① 是属于驱动层的消息。通过串口发送AT指令的过程为MOD_DRV_HISR->MOD_TST_READER,由于MMI层截获了①这个消息,因此先响应MMI层的钩子函数,如果在读取UART的buffer后将其清空,MOD_TST_READER自然得不到AT指令,因此无法做出任何的AT响应。值得注意的是,所有的串口在接到字符后响应的都是消息①,因此在读取的时候需要判断是否是来自所需要串口的信息。


如果串口设置正确,通讯正常,使能串口后将接收缓存清空,那么每次接收到6个字符后便会发出消息①。


打开串口:


⑴禁止休眠


⑵设置MSG_ID_UART_READY_TO_READ_IND消息的钩子函数(响应函数)


⑶将串口所属MOD切换至将要使用的MOD(UART_GetOwnerID,UART_SetOwner)


⑷设置串口参数,波特率等(UART_SetDCBConfig)


⑸清空对应串口的接收Buffer(UART_ClrRxBuffer)


 


读取串口信息


用到的函数,UART_GetBytesAvail,UART_GetBytes


读完之后记得将接收Buffer清空UART_ClrRxBuffer


 


向串口写信息


⑴清空设备输入、输出FIFO(UART_Purge)


⑵清空发送、接收Buffer(UART_ClrTxBuffer,UART_ClrRxBuffer)


⑶写入数据UART_PutBytes


 


关闭串口


将所使用的串口MOD设置为原来的MOD


使能睡眠


 


以下是摘自mtk 旋风001 中的示例程序


#define CARDPAY_DA_UART                uart_port1
#define CARDPAY_DA_MAX_LEN        255
#define MOD_CARDPAY                        MOD_MMI


U8 gDAbuffer[CARDPAY_DA_MAX_LEN];
U16 gDAbuflen;


extern kal_uint8 CardPaySmHandle;
extern void HuayuRFIDHardInit(void);
extern void HuayuRFIDHardOff(void);


void CardPayDAUartInit(void)
{
#ifdef MMI_ON_HARDWARE_P
        /* UART init */
        UART_SetOwner(CARDPAY_DA_UART, MOD_CARDPAY);
        UART_SetBaudRate(CARDPAY_DA_UART, UART_BAUD_57600, MOD_CARDPAY);
        L1SM_SleepDisable(CardPaySmHandle);
#endif
        SetProtocolEventHandler(CardPayDAUartHandler, MSG_ID_UART_READY_TO_READ_IND);
}


U16 CardPayDAUartRead(U8 *buf, U16 maxlen, UART_PORT port, module_type ownerid)
{
        U8 status = 0;
        U16 avail;
        U16 retlen = 0, readlen;


#ifdef MMI_ON_HARDWARE_P
        while ((avail = UART_GetBytesAvail(port) && retlen <= maxlen) > 0)
        {
                if (avail + retlen > maxlen)
                        avail = maxlen - retlen;


                readlen = UART_GetBytes(port, (kal_uint8 *)(buf+retlen),
                        (kal_uint16)avail, &status, ownerid);
                retlen +=  readlen;
        }


        //UART_ClrTxBuffer(port, ownerid);
        UART_ClrRxBuffer(port, ownerid);
#endif


        return retlen;
}


U8 CardPayDAUartWrite(U8 *buf, U16 buflen, UART_PORT port, module_type ownerid)
{
        U8 ret = FALSE;
        U16 sent;
        module_type UART_Owner = 0;


        sent = 0;


#ifdef MMI_ON_HARDWARE_P
        UART_Purge(port, RX_BUF, ownerid);
        UART_Purge(port, TX_BUF, ownerid);
        UART_ClrTxBuffer(port, ownerid);
        UART_ClrRxBuffer(port, ownerid);


        sent = UART_PutBytes(port, (kal_uint8 *)buf, (kal_uint16)buflen, ownerid);
#endif


        if (sent == buflen)
                ret = TRUE;


        return ret ;
}


void CardPayDAUartHandler(void *msg)
{
#ifdef MMI_ON_HARDWARE_P
        uart_ready_to_read_ind_struct *tmp = (uart_ready_to_read_ind_struct *)msg;


    if (UART_GetOwnerID(tmp->port) != MOD_CARDPAY)
                return ;


        if (tmp->port != CARDPAY_DA_UART)
                return ;


        gDAbuflen = CardPayDAUartRead(gDAbuffer, CARDPAY_DA_MAX_LEN, CARDPAY_DA_UART, MOD_CARDPAY);


        if (CardPayDAUartReadDone())
                CardPayDADataProcess();
#endif
}


U8 CardPayDAUartReadDone(void)
{
        return 1;
}


void CardPayDADataProcess(void)
{
        if (gDAbuflen > 0)


        {
                //CardPayDAUartWrite(gDAbuffer, gDAbuflen, CARDPAY_DA_UART, MOD_CARDPAY);
                // update display
                CardPayDAUpdateInfo(gDAbuffer, gDAbuflen);
               
        }
        CARDPAY_DEBUG_HEX(gDAbuffer, gDAbuflen);
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
6
×
广告
关闭 站长推荐上一条 /3 下一条