Sipeed Maix Bit – adding a boot script

I will present a way to add a python script that can be executed at boot time.

For this we need a micro SD card and we need to install Ampy.

Sipeed doc says that the SD card needs to be formatted FAT but I believe that the latest firmware supports FAT32 too.

Ampy is a command line tool that can be used to send commands to the device through the serial line.

Setup

If you use a SD card, the Maix bit (and most MicroPython devices) will try to execute a script called boot.py from the root of the SD card. If no script with that name is there it will create one that displays one message. Here is an example:

from fpioa_manager import *
import os, Maix, lcd, image
from Maix import FPIOA, GPIO

test_pin=16
fpioa = FPIOA()
fpioa.set_function(test_pin,FPIOA.GPIO7)
test_gpio=GPIO(GPIO.GPIO7,GPIO.IN)
lcd.init(color=(255,0,0))
lcd.draw_string(100,120, "Welcome to MaixPy", lcd.WHITE, lcd.GREEN)
if test_gpio.value() == 0:
    print('PIN 16 pulled down, enter test mode')
    import sensor
    import image
    sensor.reset()
    sensor.set_pixformat(sensor.RGB565)
    sensor.set_framesize(sensor.QVGA)
    sensor.run(1)
    lcd.freq(16000000)
    while True:
        img=sensor.snapshot()
        lcd.display(img)

In theory MicroPython devices are supposed to look first for a boot.py and then for a main.py files on the /flash folder and only then for the same files on the /sd folder. I was not able to make any main.py files to execute and I did not want to mess up with the boot.py file on the /flash folder. The idea was to override the boot.py file on the SD card with a different one.

Copying another file over the boot.py with the SD card inserted in my Windows machine did not work so I tried using Ampy.

Install Ampy by running: pip install adafruit-ampy

I recommend using Anaconda to manage the python environment. More installation instructions are here: https://github.com/pycampers/ampy .

Then pop the SD card into the Maix Bit, connect the device to the USB of the computer and you can run Ampy commands.

For example to list the content of the SD card run (use your own port):

ampy -p COM4 -d 0.5 ls /sd

Now it is time to copy our custom boot file.

I created this file (camera_test.py) on my Windows machine:

#image test
import sensor,image,lcd,time
import KPU as kpu

lcd.init(freq=15000000)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_vflip(1)
sensor.run(1)
sensor.set_vflip(True)
sensor.set_hmirror(False)

clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    print(clock.fps())
    a = lcd.display(img)
a = kpu.deinit(task)

then I copied it to the SD card:

ampy -p COM4 -d 0.5 put camera_test.py /sd/boot.py

To test it you can either reboot using the RESET button or try to read the file using Ampy:

ampy -p COM4 -d 0.5 get /sd/boot.py

Now you can disconnect the device from your computer and connect it to a power supply. It should display the video feed of the camera.

WARNING!! If your boot.py doesn’t exit like in the script above you won’t be able to use Ampy to make changes. The solution is to pop the SD card out, plug it into your computer and delete the boot.py

More Ampy commands are described here: https://learn.adafruit.com/sino-bit-micropython/running-code-with-ampy .

You can try to replace the boot.py on the /flash directory in the same way if the SD solution doesn’t work or you don’t have an SD card.

Leave a Reply