ai.rs blog

Artificial Intelligence – Technology Blog

M5StickV How to flash MicroPython (on Sipeed Maix series K210)

Ok, your M5StickV have arrived, what to do next? Initial demo shows squares around detected faces. But how to change firmware or use it for MicroPython?

Good news, there is support for Linux, MacOS, Windows and even for ARM bases computers (I will cover part for Nvidia Jetson Nano).

Only thing missing is IDE environment for ARM computers. Sipeed, please compile it!

Useful links:

M5Stick V community forum
MaixPy source code
Flash utility (GUI) source and binaries
IDE enviroment download
Precompiled binaries

Before flashing please note, that If you update the firmware, the demo originally stored in Flash memory may not work. If you want to restore it, you can restore m5stickV_Firmware_0630Fixed.kfpkg from M5Stack.com with Kflash_gui. (The firmware will also be restored.)


#make directory, clone source and install requirements
mkdir m5stickv
git clone https://github.com/sipeed/MaixPy.git
cd ./MaixPy
git submodule update --init
pip3 install -r requirements.txt
cd projects/maixpy_m5stickv/
wget http://dl.cdn.sipeed.com/kendryte-toolchain-ubuntu-amd64-8.2.0-20190409.tar.xz
sudo cp ./kendryte-toolchain-ubuntu-amd64-8.2.0-20190409.tar.xz /opt
cd /opt
sudo tar -xvf kendryte-toolchain-ubuntu-amd64-8.2.0-20190409.tar.xz
cd /home/jetson/m5stickv/MaixPy/projects/maixpy_m5stickv/
python3 project.py --toolchain /opt/kendryte-toolchain/bin --toolchain-prefix riscv64-unknown-elf- config
python3 project.py clean_conf
python3 project.py menuconfig
python3 project.py build

if you are using ARM computer you will get error:
/bin/sh: 1: /opt/kendryte-toolchain/bin/riscv64-unknown-elf-gcc: Exec format error
And you need to use different toolchain, compiled for ARM architecture, it is available here, just unpack it to your /opt

After successful compliation new binary will be available in build folder


-- Generating .bin firmware at /home/jetson/m5stickv/MaixPy/projects/maixpy_m5stickv/build/maixpy.bin
============= firmware =============
text data bss dec hex filename
1455595 11328 634232 2101155 200fa3 /home/jetson/m5stickv/MaixPy/projects/maixpy_m5stickv/build/maixpy.elf
[100%] Built target maixpy
==================================
build end, time last:98.36s
==================================

Now let’s install flash utility


sudo pip3 install kflash

Optionally lets install GUI for flash utility


cd /home/jetson/m5stickv
wget https://github.com/sipeed/kflash_gui/releases/download/v1.5.2/kflash_gui_v1.5.2_linux.tar.xz
tar -xvf kflash_gui_v1.5.2_linux.tar.xz
cd kflash_gui/
./kflash_gui

If you are on ARM (Jetson Nano or Raspberry, you need to compile it)


git clone https://github.com/sipeed/kflash_gui
cd kflash_gui/
cd kflash_py/
git clone https://github.com/sipeed/kflash.py .
cd ..
sudo python3 /home/jetson/m5stickv/kflash_gui/kflash_gui.py


For flashing use root account or add your user to dialout group in order to get access to ttyUSBx.


#find serial port
dmesg | grep ttyUSB
#[2148283.025290] usb 1-2.4: FTDI USB Serial Device converter now attached to ttyUSB0
sudo kflash -p /dev/ttyUSB0 ./build/maixpy.bin

in order yo connect via serial port (and see Micropython prompt) you can use screen as terminal.


apt-get install screen
screen /dev/ttyUSB0 115200
#type help()

If you want to paste longer micropython code, use CTRL+E to enter paste mode and CTRL+D to finish.
If you want to exit screen (serial console) press CTR+A and type :quit

Now lets install IDE from: http://dl.sipeed.com/MAIX/MaixPy/ide/


/home/jetson/m5stickv
wget maixpy-ide-linux-x86_64-0.2.3-installer-archive.7z
sudo apt-get install p7zip-full
7za x maixpy-ide-linux-x86_64-0.2.3-installer-archive.7z
cd bin
./maixpyide.sh

REMARK there is no IDE now for Jetson (ARM architecture CPU)
but it works very nice on Ubuntu and MacOS K210 IDE.

Tip: python code could be uploaded using uPyLoader or install “pip3 install pyserial”

Additional scripts for K210 are available here

If you prefer video (on chinese but with english subtitles) check this.

Kflash also supports saving code to SRAM or Flash (if you want to have it after reset). If you use name boot.py it will automatically run. You can also execute user code directly in SRAM and view it in serial terminal.


# Using pip
# For `.elf` file
kflash -b 115200 -B goE -s -t hello_world
# For `.bin` file
kflash -b 115200 -B goE -s -t hello_world.bin

# Using source code
# For `.elf` file
python3 kflash.py -b 115200 -B goE -s -t hello_world
# For `.bin` file
python3 kflash.py -b 115200 -B goE -s -t hello_world.bin


Ok, now we are ready to write some python code. It is based on one of the samples, but with few changes. Display orientation is fixed. We are drawing decoded string on display above QR code. And also QR code is surrounded with square.

import sensor
import image
import lcd
import time

clock = time.clock()
lcd.direction(lcd.YX_RLUD)
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_vflip(1)
sensor.set_hmirror(1)
sensor.run(1)
sensor.skip_frames(30)
sensor.set_brightness(17)
while True:
—-clock.tick()
—-img = sensor.snapshot()
—-res = img.find_qrcodes()
—-fps =clock.fps()
—-if len(res) > 0:
——–#Get coordinates of the first QR
——–tupleboxa = res[0].rect()
——–img.draw_string(40,50, res[0].payload(), (236,36,36),scale=1.5)
——–img.draw_rectangle(tupleboxa,(236,36,36))
——–#lcd.draw_string(10, 10, res[0].payload(), lcd.RED, lcd.BLACK)
——–print(res[0].payload())
—-lcd.display(img)

Happy experimenting !

Read more about M5StickV specs


Scan to read
Scan the QR Code
QR Code Generator
Be Sociable, Share!