Ameba MicroPython: [RTL8722CSM] [RTL8722DM] UART

class UART – Universal Asynchronous Receiver/Transmitter
Examples
UART send and recv:
• Materials: Ameba x 1, TTL USB to Serial module x 1
• Steps:
UART is a very versatile communication protocol and almost an essential part of a microcontroller. A TTL USB to Serial module is an IC that helps to translate UART signal to USB signal so that we can see uart log printed on our PC. This module is often found on many development boards, including ameba. However, the module on Ameba is reserved for LOG UART and Firmware uploading, that is why we need a separate module to communicate between ameba and PC.
There are currently 2 sets of UART available to MicroPython users and they are,
Unit TX RX
0
PA_21
PA_22
3
PA_26
PA_25
Here we are using unit 0 to demonstrate how uart works on ameba.
Connect TTL module to PA_21 and PA_22 as shown below,
get-start-7
Then, copy and paste the following code line by line into REPL to see its effect.
from machine import UART
uart = UART(tx="PA_21", rx= "PA_22")
uart.init()
uart.write('hello')
uart.read(5) # read up to 5 bytes
API Documents
Constructors
UART(unit [optional], baudrate [optional], databits [optional], stopbit [optional], paritybit [optional], timeout [optional], tx_pin [required], rx_pin [required])
Create a UART object associated with the given tx and rx pins and configure it using other parameters. This allows you to then read/write uart signal on the pins.
• unit: The unit ID, either 0 or 3
• baudrate: 115200 is the recommended baudrate on ameba
• databits: the number of bits for data bits, usually 7 or 8 bits
• stopbits: the number of stop bits, usually 1 or 1.5 or 2 bits
• paritybit: for parity check, usually none, odd or even
• timeout: how long uart wait before its timeout (in milliseconds)
• tx_pin: the transmitter pin, connect the rx pin of the receiver
• rx_pin: the receiver pin, connect to tx pin of the transmitter
Note: Not all parameters are required, thus MicroPython will assume its default value once left blank, here are the default values for each optional parameter,
Parameter Default Value
Unit
0
Baudrate
115200
Databits
8
Stopbits
1
Paritybit
0
Timeout
10 (ms)
Methods
UART.init()
This method initializes and configures the UART.
 
UART.read(length [optional])
This method reads the data received in UART buffer.
• length: the length of the data to receive
 
UART.readline()
This method is similar to read(), but read a line ending with a newline character.
 
UART.write(buffer [require])
This method works in 2 way:
• Check the status of uart irq when NOT passing any argument, and it will return True if irq is enabled, False if disabled
• Enable/disable uart irq handler by passing True or False as bool
 
UART.irq_enable(bool [optional])
This method will return the current timer tick in Gtimer clock(0~32768).
 
UART.irq_handler(function [required])
Passing the python handler to uart irq so that it will be triggered when an UART event occurs.
• function: a function defined in python or a lambda function
Please confirm that QQ communication software is installed