2020-08-26


前言

长城需要提供基于open的uart demo。

一、Ma510框架图

系统调用逻辑

此图演示了应用层如何调用到底层设备。

二、demo创建

1.内核修改

选择uart2进行串口验证。
需要修改内核相关配置使得串口2能够生效。
具体修改见下图:

内核修改

2.demo 代码介绍

打开串口代码

   qapi_UART_Open_Config_t open_properties;
   memset(&PAL_Context_D, 0, sizeof(PAL_Context_D));
   memset (&open_properties, 0, sizeof (open_properties));

   open_properties.parity_Mode = QAPI_UART_NO_PARITY_E;
   open_properties.num_Stop_Bits= QAPI_UART_1_0_STOP_BITS_E;
   open_properties.baud_Rate   = 115200;
  
   open_properties.bits_Per_Char= QAPI_UART_8_BITS_PER_CHAR_E;
   open_properties.rx_CB_ISR = dam_cli_rx_cb;
   open_properties.tx_CB_ISR = dam_cli_tx_cb;
   
   open_properties.enable_Flow_Ctrl = false;
   open_properties.enable_Loopback= false;
    LOG_INFO("qapi_UART_Open");
   if(qapi_UART_Open(&PAL_Context_D.uart_handle, QAPI_UART_PORT_002_E, &open_properties) != QAPI_OK){

串口接收回调

//----------------------------------------------------------------------------------------------------
/**
 *    @brief This function handles receive callbacks from the UART.
 *
 *       @param Num_Bytes[in] is the number of bytes received.
 *          @param CB_Data[in]   is the application defined callback data.  In this case
 *                                  it is the index of the buffer received on.
 *                                  */
static void dam_cli_rx_cb(uint32_t num_bytes, void *cb_data)
{
    int Length = num_bytes;
        if(PAL_Context_D.Rx_Buffers_Free != 0)
        {
            /* See how much data can be read.                                    */
            if(Length > PAL_Context_D.Rx_Buffers_Free)
            {
                Length = PAL_Context_D.Rx_Buffers_Free;
            }
            PAL_Context_D.Rx_In_Index += Length;
            if(PAL_Context_D.Rx_In_Index == PAL_RECIEVE_BUFFER_SIZE)
            {
                PAL_Context_D.Rx_In_Index = 0;
            }
            PAL_ENTER_CRITICAL();
            PAL_Context_D.Rx_Buffers_Free -= Length;
            PAL_EXIT_CRITICAL();
            if(Length > 0)
            {
                /* Signal the event that data was received.                       */
                tx_event_flags_set((PAL_Context_D.Rx_Event), PAL_RECEIVE_EVENT_MASK, TX_OR);
            }
        }
}

串口发送

/**
   @brief This function is used to write a buffer to the console. Note
          that when this function returns, all data from the buffer will
          be written to the console or buffered locally.

   @param Length is the length of the data to be written.
   @param Buffer is a pointer to the buffer to be written to the console.
*/
void PAL_Console_Write(uint32_t Length, const uint8_t *Buffer)
{
   memset (send_buf, 0, sizeof (send_buf));
   if((Length) && (Buffer))
   {
     if(send_buf != NULL)
     {
        memcpy(send_buf, (char*)Buffer, Length);
        qapi_UART_Transmit(PAL_Context_D.uart_handle,send_buf, Length, (void*)send_buf);       
     }
     qapi_Timer_Sleep(2,QAPI_TIMER_UNIT_MSEC,true);
   }
}

总结

主要实现了ma510的uart串口demo验证。为客户开发串口应用提供了基础参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值