air103#

This chapter describes how to use the uart library for LuatOS

Introduction#

UART(Universal Asynchronous Receiver/Transmitter)Universal asynchronous transceiver, UART as a kind of asynchronous serial communication protocol, the working principle is to transmit each character of the data one by one. Is the most frequently used data bus in the application development process.

UART The characteristic of the serial port is to transmit data bit by bit in sequence. Two-way communication can be realized as long as two transmission lines. One line sends data while receiving data with another line. There are several important parameters for UART serial communication, namely baud rate, start bit, data bit, stop bit and parity bit. For two ports using UART serial communication, these parameters must match, otherwise the communication will not be completed normally.

Looking up Air103_MCU Design Manual V1.2.pdf, we can see that Air103 has 6 UART channels, and UART0 is fixed as download and log port.

We use UART1 to connect with PC through TTL to USB module to realize data transfer between PC and Air103

Hardware preparation#

  • Air103 Development Board

  • USB TTL Module

Wiring Schematic

        U1_RX/GPIO23  ------  TX  
Air103  U1_TX/GPIO22  ------  RX      USB-TTL->PC
        GND           ------  GND

Software part#

Interface documentation can be found in:uart library

uart When sending and receiving data, you can directly pass string type data, or you can pass zbuff objects, the following will demonstrate the two methods respectively.

Create two zbuff arrays#

Skip this step if you don’t use zbuff to process the data

The code is as follows

-- Create a 1KB send buffer
sendBuff = zbuff.create(1024)
-- Create a 1KB receive buffer
receiveBuff = zbuff.create(1024)
-- Write data to send buffer
sendBuff:write("Hi,I am Air103\n")
-- Move the pointer to the beginning of the send buffer again
sendBuff:seek(0)

Initialization uart#

Initialize UART1, baud rate is 921600,8 data bits, 1 stop bit, no check

The code is as follows

uart.setup(1, 921600, 8, 1, uart.None)

Register Serial Port Event Callback#

Register a receive event callback for UART1 to read and print when Air103 UART1 receives data

Receive data of type string#

The code is as follows

uart.on(1, "receive", function(id, len)
    local data = uart.read(id, len)
    log.info(PROJECT .. ".receive-" .. id, data)
end)

Receiving data using zbuff#

The code is as follows

uart.on(1, "receive", function(id, len)
    -- Read data to receive buffer
    uart.read(id, len, receiveBuff)
    -- Move the receive buffer pointer back to the beginning
    receiveBuff:seek(0)
    -- Read data from the receive buffer
    local data = receiveBuff:read(len)
    -- Move the receive buffer pointer back to the beginning
    receiveBuff:seek(0)
    log.info(PROJECT .. ".receive-" .. id, data)
end)

Create a new cycle timer to send data to UART1#

Send string data#

The code is as follows

sys.timerLoopStart(function()
    uart.write(1, "Hi,I am Air103\n")
end, 2000)

Send data using zbuff#

The code is as follows

sys.timerLoopStart(function()
    uart.write(1, sendBuff, 15)
end, 2000)

Full Code#

PROJECT = "uart"
VERSION = "1.0.0"

sys = require("sys")

USE_ZBUFF = false

if USE_ZBUFF == true then
    -- Create a 1KB send buffer
    sendBuff = zbuff.create(1024)
    -- Create a 1KB receive buffer
    receiveBuff = zbuff.create(1024)
    -- Write data to send buffer
    sendBuff:write("Hi,I am Air103\n")
    -- Move the pointer to the beginning of the send buffer again
    sendBuff:seek(0)
end

uart.setup(1, 921600, 8, 1, uart.None)

if USE_ZBUFF == true then
    uart.on(1, "receive", function(id, len)
        -- Read data to receive buffer
        uart.read(id, len, receiveBuff)
        -- Move the receive buffer pointer back to the beginning
        receiveBuff:seek(0)
        -- Read data from the receive buffer
        local data = receiveBuff:read(len)
        -- Move the receive buffer pointer back to the beginning
        receiveBuff:seek(0)
        log.info(PROJECT .. ".receive-" .. id, data)
    end)

    sys.timerLoopStart(function()
        uart.write(1, sendBuff, 15)
    end, 2000)
else
    uart.on(1, "receive", function(id, len)
        local data = uart.read(id, len)
        log.info(PROJECT .. ".receive-" .. id, data)
    end)

    sys.timerLoopStart(function()
        uart.write(1, "Hi,I am Air103\n")
    end, 2000)
end

sys.run()