4. Use of M601 UART

1 UART related data structure and API


For the data structure and API introduced in this article, please refer to the zyf_uart.h file in the SDK.


1.1 Overview


In OpenCPU, serial ports include physical serial ports and virtual serial ports. The physical serial port can be connected to external devices, and the virtual serial port is used for communication between the application program and the underlying operating system. One of the physical serial ports has a hardware handshake function, and the others are three-wire interfaces. OpenCPU supports two virtual serial ports for communication between App and Core. These serial ports are designed with RI and DCD information according to the characteristics of physical serial ports. The level of DCD can indicate whether the virtual serial port is in data mode or AT command mode.


1.2 Usage


The initialization and use of the physical serial port and virtual serial port can be completed through the following steps.
Step 1: Use ZYF_UartRegister to register the callback function of the serial port.
Step 2: Use ZYF_UartOpen to open the serial port.
Step 3: Use ZYF_UartWrite to write data to the serial port. When the actual number of bytes sent is less than the number of bytes to be sent, the application should stop sending data, and the application will receive the EVENT_UART_READY_TO_WRITE event from the callback function. After receiving the event, the application can continue to send data, and data that has not been sent before is also sent.
Step 4: In the callback function, handle the notification of the serial port. If the notification type is EVENT_UART_READY_TO_WRITE, the developer must read all data from the UART RX buffer; otherwise, when new data enters the UART RX buffer, this message will not notify the application.


1.3 UART related API


1.3.1 ZYF_UartRegister serial port registration
 Function prototype
int32_t ZYF_UartRegister(Enum_SerialPort port, PsFuncPtrcallback_uart)
 Parameters

port:
[input] The name of the serial port. Value is in Enum_SerialPort
callback_uart:
[input] Serial port registration callback function
 Return result
ZYF_RET_OK success
Other values ​​fail

1.3.2 ZYF_UartOpen Open the serial port
 Function prototype
int32_t ZYF_UartOpen(Enum_SerialPort port, uint32_t baudrate,Enum_FlowCtrl flowCtrl)
 Parameter
port:
[input] The name of the serial port. Value is in Enum_SerialPort
baudrate:
[input] Serial port baud rate
flowCtrl:
[input] Flow control, value is in Enum_FlowCtrl
 Return result
ZYF_RET_OK success
Other values ​​fail

1.3.3 ZYF_UartWrite Serial port to send data
 Function prototype
int32_t ZYF_UartWrite(Enum_SerialPort port, uint8_t* data, uint32_t writeLen)
 Parameter
port:
[input] The name of the serial port. Value is in Enum_SerialPort
data:
[input] The data to be sent
writeLen:
[input] The length of the sent data
 The return result
If >= 0, the transmission is successful, the data length of the successful transmission is returned
Other values ​​fail
If <0, the transmission fails

1.3.4 ZYF_UartRead serial port read data
 Function prototype
int32_t ZYF_UartRead(Enum_SerialPort port, uint8_t* data, uint32_t readLen)
 Parameter
port:
[input] The name of the serial port. Value is in Enum_SerialPort
data:
[output] read the data of the serial port
writeLen:
[input] the length of the read data
 Return result
If >= 0, read the object successfully, return the length of the read data
Other values ​​fail
If <0, read failure

1.3.5 ZYF_UartClose Close the serial port
 Function prototype
int32_t ZYF_UartClose(Enum_SerialPort port)
 Parameter
port:
[input] The name of the serial port. Value is in Enum_SerialPort
 Return result
NONE
1.3.6 ZYF_UartOpenEx Configure serial port properties and open serial port
 Function prototype
int32_tZYF_UartOpenEx (Enum_SerialPort port, ST_UARTDCB *dcb)
 Parameter
port:
[input] The name of the serial port. Value is in Enum_SerialPort dcb
:
[input] serial port attribute parameter. Value is in ST_UARTDCB
 Return result

ZYF_RET_OK succeeded. Other values ​​fail

1.3.7 ZYF_UartGetDCBConfig Get serial port properties
 Function prototype
int32_t ZYF_UartGetDCBConfig(Enum_SerialPort port, ST_UARTDCB *dcb)
 Parameter
port:
[input] The name of the serial port. Value is in Enum_SerialPort dcb
:
[Output] Serial port attribute parameter
 The return result
ZYF_RET_OK is successful. Other values ​​fail
1.3.8 ZYF_UartSetDCBConfig Set serial port properties
 Function prototype
int32_t ZYF_UartSetDCBConfig(Enum_SerialPort port, ST_UARTDCB *dcb)
 Parameter
port: [input] The name of the serial port. Value is in Enum_SerialPort dcb
:[input] Set serial port property parameters
 The return result
ZYF_RET_OK is successful. Other values ​​fail

1.3.9 ZYF_UartWriteCbRegister register callback function
 Function prototype
int32_t ZYF_UartWriteCbRegister(Enum_SerialPort port, PsFuncPtr
write_callback,void*Param)
 Parameters

port:[input] The name of the serial port. Value is in Enum_SerialPort
write_callback:[output] Point to the callback function of completion of serial port writing
 The return result
ZYF_RET_OK is successful. Other values ​​fail

1.3.10 ZYF_UartWriteCallbackSwitch Serial port write completion callback notification switch
 Function prototype
int32_t ZYF_UartWriteCallbackSwitch(Enum_SerialPort port, uint8_t on_off)
 Parameter
port: [input] The name of the serial port. Value is in Enum_SerialPort
on_off:[input] Turn on/off the function
 The return result
ZYF_RET_OK is successful. Other values ​​fail

2 UART routine introduction


This chapter mainly introduces how to use app_uart.c in SDK to test UART function separately.
Compiling method: .\examples\build\ corresponds to the .bat file by double-clicking to execute or opening it to compile.
Generated file: .\out\corresponding directory\hex\M601_example_**.pac


1. Register the serial port and receive data read callback function through ZYF_UartRegister().
2. Register the send data write callback function of the serial port through ZYF_UartWriteCbRegister().
3. Use ZYF_UartOpen() to configure the baud rate and flow control, and open the serial port.
4. The received data is processed in UartReadCallBack().
5. The result of sending data is processed in UartWriteCallBack().


#include <string.h>
#include <stdlib.h>

#include "zyf_trace.h"
#include "zyf_uart.h"
#include "zyf_error.h"
#include "zyf_thread.h"
#include "zyf_timer.h"
#include "zyf_app.h"

static Uart_Param_t g_uart1param;
static Uart_Param_t g_uart2param;
static Uart_Param_t g_uart3param;



void UartWriteCallBack(void* Param) // general com
{
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 
    if(Param == NULL)
    {
        return;
    }    

    ZYF_UartWrite(uartparam->port,(uint8_t *)"UartWrite succeed\r\n",strlen("UartWrite succeed\r\n"));
    ZYF_UartWriteCallbackSwitch(uartparam->port,false);

}

void UartReadCallBack(void* Param) // 
{
    uint32_t recvlen = 0;
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 

    /*
    UART_PORT1 = 0,
    UART_PORT2 = 1,
    UART_PORT3 = 2,
    */
    ZYF_LOG("Uart%d recv",uartparam->port);

    while(ZYF_UartRead(uartparam->port, &(uartparam->uartbuf[recvlen]), 1))
    {
        ZYF_LOG("recv :%02x",uartparam->uartbuf[recvlen]);
        recvlen++;
    }
    ZYF_UartWrite(uartparam->port,uartparam->uartbuf,recvlen);
    ZYF_UartWriteCallbackSwitch(uartparam->port,true);
}


static void _AppUart1Init(void)
{
    int32_t ret;
    g_uart1param.port = UART_PORT1;
    ZYF_UartRegister(g_uart1param.port, UartReadCallBack,&g_uart1param);
    ZYF_UartWriteCbRegister(g_uart1param.port,UartWriteCallBack,&g_uart1param);
    ZYF_UartOpen(g_uart1param.port, 115200, ZYF_FC_NONE);
//    ST_UARTDCB uartDcb = {0};
//    uartDcb.baudrate = 115200;
//    uartDcb.dataBits = DB_8BIT;
//    uartDcb.flowCtrl = ZYF_FC_NONE;
//    uartDcb.parity = PB_NONE;
//    uartDcb.stopBits = SB_ONE;
    
//    ret = ZYF_UartOpenEx(g_uart1param.port,&uartDcb);

    ZYF_LOG("_AppUart1Init");
    return;
}


static void _AppUart2Init(void)
{  
    g_uart2param.port = UART_PORT2;
    ZYF_UartRegister(UART_PORT2, UartReadCallBack,&g_uart2param);
    ZYF_UartWriteCbRegister(UART_PORT2,UartWriteCallBack,&g_uart2param);
    ZYF_UartOpen(UART_PORT2, 115200, ZYF_FC_NONE);
    ZYF_LOG("_AppUart2Init");

    return;
}


static void _AppUart3Init(void)
{   
    g_uart3param.port = UART_PORT3;
    ZYF_UartRegister(UART_PORT3, UartReadCallBack,&g_uart3param);
    ZYF_UartWriteCbRegister(UART_PORT3,UartWriteCallBack,&g_uart3param);
    ZYF_UartOpen(UART_PORT3, 115200, ZYF_FC_NONE);
    ZYF_LOG("_AppUart3Init");

    return;
}



void ZYF_AppUartInit(void)
{
    _AppUart1Init();
    _AppUart2Init();
    _AppUart3Init();
}

void UartThread_Example(void * Param)
{
    ZYF_MsgQ_t *ptMsg;
    ZYF_AppMsg_t tMsg;
    int iRet = -1;
    ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
    ZYF_LOG("thread enter!");
    
    while (1) {
        ZYF_LOG("in while.");
        iRet = ZYF_MsgQGet(ptMsg, (void *)&tMsg);
        if (iRet < 0) {
            ZYF_LOG("Failed to get msg");
            ZYF_ThreadSleep(1000);
        }
    }

}

static void prvInvokeGlobalCtors(void)
{
    extern void (*__init_array_start[])();
    extern void (*__init_array_end[])();

    size_t count = __init_array_end - __init_array_start;
    for (size_t i = 0; i < count; ++i)
        __init_array_start[i]();
}


int appimg_enter(void *param)
{
    ZYF_AppUartInit();
    ZYF_LOG("application image enter, param 0x%x", param);

    prvInvokeGlobalCtors();

    ZYF_ThreadCreate("UartThread_Example", UartThread_Example, NULL, ZYF_PRIORITY_HIGH, 10*1024);
    return 0;
}

void appimg_exit(void)
{
    OSI_LOGI(0, "application image exit");
}


 

Guess you like

Origin blog.csdn.net/w_hizyf_m/article/details/107083189