Jump to content
Electronics-Lab.com Community

Run MicroPython on Realtek's RTL8722 development board


Recommended Posts

Realtek's RTL8722DM development board now supports running Python code through the MicroPython firmware. You can now control the GPIO and other peripherals using the Python syntax through the REPL interface in MicroPython, or by writing it as a script on the device.

A basic example using WiFi to control a LED can be seen at : 

 

Join in the community discussions at:
https://www.facebook.com/groups/AmebaIoT/
https://forum.amebaiot.com/

Purchase links for the various Realtek development boards can be found at:
https://www.amebaiot.com/en/where-to-buy-link/

Link to post
Share on other sites
  • 1 month later...

Microcontrollers are known for its low power usage and limited resources thus often deemed unable to understand Python script as Python need interpretor to translate script into machine langauge and intepretor are usaully quite resource-consuming. However, MicroPython has arisen as a lean and efficiant Python 3 interpretor that can be run on ARM Cortex-M series microcontrollers.

Ameba RTL8722 is an ARM Cortex-M33 microcontroller that has dual-band WiFi and BLE 5.0, other than that it is fully capable of running MicroPython and controls WiFi using Python script. In addition, its requirement for developing platform is also quite minimal-- only needs a serial port terminal like Tera term. Here is an example of RTL8722 controlling WiFi using just a few lines of Python code on Tera Term,

from wireless import WLAN
wifi = WLAN(mode = WLAN.STA)
wifi.scan()

Right after the last line, you will be able to see the result printed on the Tera term almost immediately,

micropython-started-4.png.af7c108059ba58d66d90b8d48c08208e.png

 

More details on getting started with MicroPython on the RTL8722DM can be found at:
https://www.amebaiot.com/en/amebad-micropython-getting-started/

Join in the community discussions at:
https://www.facebook.com/groups/AmebaIoT/
https://forum.amebaiot.com/

Purchase links for the various Realtek development boards can be found at:
https://www.amebaiot.com/en/where-to-buy-link/

Link to post
Share on other sites

For developers interested in MicroPython, the process of building a MicroPython firmware image and adding new features is also easy.

To build a MicroPython firmware successfully, the first step is to get the software and set them up correctly.

Here is a list of the most important software that are used in the firmware building process,

Cygwin: This is the first software you need to install, and we recommand using 32-bit version even for a 64-bit machine. During cygwin installation, there are 2 additional packeges to install as well, GNU make under Devel category and bc under Math category

Python3 : We need python3 to process some python script invoked during the making process, so this is needed. When installing, make sure to choose to add python3 to your system’s evironmental variable PATH so that cygwin knows where to look for the python3 executable file

Toolchain : This is the most important part of the SDK and luckily toolchain is provided in the SDK and will be automatically installed during the first time compilation

Teraterm : Teraterm is an free serial COM software that allow users to interact with MicroPython through REPL (Read Evaluate Print and Loop) prompt. Of course there are other options for serial COM, you may choose the ones you like, but make sure to set baud rate to 115200 in order to talk to REPL

Text editor : To develop efficiently, you need a powerful text editor to help you better understand the code and provide necessary help using its built-in code hinting and deifinition checking features. There are too many options out there, the most common ones are Visual Studio Code, Sublime, Notepad++ and etc.

For more information on developing MicroPython for the RTL8722, you can find an introductory series of post in the AmebaIoT forum:
https://forum.amebaiot.com/

Purchase links for the various Realtek development boards can be found at:
https://www.amebaiot.com/en/where-to-buy-link/

Link to post
Share on other sites
  • 2 weeks later...
UART is a versatile communication protocol commonly used to transmit and receive data with other microcontrollers. To use the UART protocol with a PC, a TTL USB to Serial module is required to translate UART signals to USB signals.
Here is an example of RTL8722 using the UART protocol with just a few lines of MicroPython code.
Connect the USB to UART module to PA_21 and PA_22 as shown below:
1.jpg
Then, copy and paste the following code line by line into REPL.
 
from machine import UART
uart = UART(tx="PA_21", rx= "PA_22")
uart.init()
uart.write('hello')
uart.read(5)

More details on the MicroPython UART API can be found at: https://www.amebaiot.com/en/amebad-micropython-uart/

Join in the community discussions at:
https://www.facebook.com/groups/AmebaIoT/
https://forum.amebaiot.com/

Purchase links for the various Realtek development boards can be found at:
https://www.amebaiot.com/en/where-to-buy-link/

Link to post
Share on other sites
  • 4 weeks later...

Introduction to developing for MicroPython — MicroPython API design and methods

Introduction
MicroPython is a scripting language whereas C/C++ is not, so there are a lot of rules we have to follow to make the Python compiler understand our C code.

API Design
Let’s take ADC module as an example,

STATIC mp_obj_t adc_read(mp_obj_t self_in) {
    adc_obj_t *self = self_in;
    uint16_t value = analogin_read_u16(&(self->obj));
    return mp_obj_new_int(value);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);

There are a few things to take note:

1. Almost all MicroPython API has to be declared as “STATIC mp_obj_t” return type

This is so to make sure the return type is a void pointer and thus can be easily utilized by MicroPython core and casted to other data type if needed

2. Always return using built-in return function

In the 4th line of the code above, we see the unsigned integer 16-bit variable value was returned using “mp_obj_new_int” API, this API can be found in “obj.h” file.

Since the return type is void pointer, so we have to use these API found in obj.h to type cast it to the correct format and then pass back to user.

3. This API adc_read takes in an mp_obj_t parameter — self_in

This step is not compulsory and is essential if you want a stable structure, as self_in was previsouly pointed to an instance of a structure whose first element is of type of mp_obj_base_t, this is an essential element while MicroPython’s compiler is working.

However, the API’s parameter can take many different forms to cater to various situation, this is a huge topic and will be discussed in the future.

4. The API was linked to its dedicated object using MP_DEFINE_CONST_FUN_OBJ_1

In the last line of the above code, we can see adc_read was linked to adc_read_obj using MP_DEFINE_CONST_FUN_OBJ_1( ) API, this MicroPython built-in API not only do the linking but also tell the compiler that this API only takes in 1 parameter, thus if your API needs 2 paramenter, you need to use MP_DEFINE_CONST_FUN_OBJ_2 instead, so on and so forth.

5. The body of the API follows the same rules as normal C function

Step 3 and Step 4 is a huge topic

As Python can handle same API with different types, numbers and names of parameter, handling this in MicroPython is also required. 

Link to post
Share on other sites
  • 4 weeks later...
I2C is a communication protocol commonly used for with interfacing with external sensors. A key advantage of the I2C bus is the low pin count required, only two wires are needed to achieve a data rate up to 3.4Mbps. It works in master-slave model and a master can simultaneously connect to up to 128 slaves, making it a very versatile communication protocol between microcontroller and sensor.
Here we are going to use RTL8722DM as an I2C master and Arduino UNO as a slave to achieve I2C send and receive.
Before connection, make sure to upload the “Examples -> Wire -> Slave_receiver” example code to Arduino UNO.
Connection is shown as follows, here we are using PA_26 as SDA pin and PA_25 as SCL.
 
1.png
Then copy and paste the following code line by line into REPL to see their effects.
from machine import Pin, I2C
i2c = I2C(scl = "PA_25", sda = "PA_26", freq=100000) # configure I2C with pins and freq. of 100KHz
i2c.scan()
i2c.writeto(8, 123) # send 1 byte to slave with address 8
i2c.readfrom(8, 6) # receive 6 bytes from slave

 

 
Link to post
Share on other sites
  • 2 weeks later...

SPI is a fast and robust communication protocol that are commonly found on many microcontrollers and is often used to retrieve sensor data or output image signal to a display. RTL8722DM supports SPI in both master and slave mode. Here we are going to see an example demonstrating how to receive data in slave mode on MicroPython on the RTL8722DM board.

Before connection, make sure to upload the following code to your Arduino UNO.

#include "SPI.h"
#define SS 10

void setup (void) {
   Serial.begin(115200); //set baud rate to 115200 for usart
   digitalWrite(SS, HIGH); // disable Slave Select
   SPI.begin ();
}
void loop (void) {
   char c;
   digitalWrite(SS, LOW); // enable Slave Select
   // send test string
   for (const char * p = "Hello, world!\r" ; c = *p; p++) {
      SPI.transfer(c);
      Serial.print(c);
   }
   Serial.println();
   digitalWrite(SS, HIGH); // disable Slave Select
   delay(2000);
}

Connection is shown as follows, here we are using RTTL8722 as SPI slave, and Arduino UNO as SPI master,

1.jpg

Then copy and paste the following code into REPL under paste mode to see their effects.

from machine import SPI
s1= SPI(0 , mode = SPI.SLAVE)
for i in range(14):
chr(s1.read())

 

Link to post
Share on other sites
  • 2 weeks later...

With BLEUART, it is easy to expand the provided example code to accept custom UART commands to achieve your desired application. Here is an example of using BLEUART to achieve remote control of a robot car from a smartphone app.

More details on the project can be found at:
Project: Ameba BLE Robot Car

Join in the community discussions at:
Ameba IoT Forum[RTL8722/RTL8195/RTL8710/...]
Realtek Ameba IOT Developers Forum (RTL8722, RTL8195, RTL8710, RTL8720) - IOT / MCU Solutions

Purchase links for the various Realtek development boards can be found at:
Buy Ameba Boards – Realtek IoT/Wi-Fi MCU Solutions

Link to post
Share on other sites
  • 2 weeks later...

Realtek's RTL8722DM_mini development board is now supported in the Arduino IDE.

In addition to features found on the regular RTL8722DM such as GPIO, UART, SPI, I2C, ADC, WiFi and BLE 5.0, the new mini development board integrates a microphone, audio output jack and SD card slot for audio related projects, while also reducing the PCB size to better fit into your projects.

8722mini-p5fz59kj4dug9lw04iupwj6rift778h

More details on the new development board can be found at:
Ameba ARDUINO: Getting Started with RTL8722DM mini

Join in the community discussions at:
Facebook - Ameba IoT Forum
Realtek Ameba IOT Developers Forum

Purchase links for the various Realtek development boards can be found at:
Buy Ameba Boards – Realtek IoT/Wi-Fi MCU Solutions

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
  • Create New...