Saturday 17 November 2018

Develop Micropython Firmware -- 01: Pure Python Based Driver

Micropython is almost like a mini operating system on a MCU and I'd like to build my own project standing on its shoulder.

Step 1: Setting up the environment
Just follow:
or

The awesome thing is that all the environment is in a virtual machine. Your host is safe. So finish configure the vagrant virtual box first and then compile ESP-toochain and Micropython source code in it.

pitfall 1: 
Depending on what kind of error you might run into, the following commands might help:

- vagrant box update
- vagrant box add ubuntu/trusty32 https://atlas.hashicorp.com/ubuntu/boxes/
vagrant provision

Step 2: coding the hello driver
Going into ~/micropython/drivers/, I found there are modules with C codes based and pure python based. In this tutorial, we focus on the pure python based drivers. For example, onewire and display.

step 2.1
mkdir hello_mpy

step 2.2
code hello_mpy.py:

def my_hello_mpy():
  print('This is my first micropython module.')

step 2.3
link it to esp8266 port.

cd ~/micropython/ports/esp8266/modules
ln -s ../../../drivers/hello_mpy/hello_mpy.py hello_mpy.py
ls -l
total 40
-rw-rw-r-- 1 vagrant vagrant  470 Nov 15 04:12 apa102.py
-rw-rw-r-- 1 vagrant vagrant  219 Nov 15 04:12 _boot.py
lrwxrwxrwx 1 vagrant vagrant   27 Nov 15 04:12 dht.py -> ../../../drivers/dht/dht.py
lrwxrwxrwx 1 vagrant vagrant   35 Nov 15 04:12 ds18x20.py -> ../../../drivers/onewire/ds18x20.py
-rw-rw-r-- 1 vagrant vagrant 1110 Nov 15 04:12 flashbdev.py
lrwxrwxrwx 1 vagrant vagrant   39 Nov 17 04:03 hello_mpy.py -> ../../../drivers/hello_mpy/hello_mpy.py
-rw-rw-r-- 1 vagrant vagrant 1425 Nov 15 04:12 inisetup.py
-rw-rw-r-- 1 vagrant vagrant  836 Nov 15 04:12 neopixel.py
-rw-rw-r-- 1 vagrant vagrant  850 Nov 15 04:12 ntptime.py
lrwxrwxrwx 1 vagrant vagrant   35 Nov 15 04:12 onewire.py -> ../../../drivers/onewire/onewire.py
-rw-rw-r-- 1 vagrant vagrant 1094 Nov 15 04:12 port_diag.py
lrwxrwxrwx 1 vagrant vagrant   22 Nov 15 04:12 upip.py -> ../../../tools/upip.py
lrwxrwxrwx 1 vagrant vagrant   31 Nov 15 04:12 upip_utarfile.py -> ../../../tools/upip_utarfile.py
-rw-rw-r-- 1 vagrant vagrant 2118 Nov 15 04:12 webrepl.py
-rw-rw-r-- 1 vagrant vagrant 2748 Nov 15 04:12 webrepl_setup.py
-rw-rw-r-- 1 vagrant vagrant 1624 Nov 15 04:12 websocket_helper.py

step 2.4 compile again
make axtls
make

step 2.5 
Get the .bin file out of vm.

pitfall 2:
I wasn't able to get my compiled firmware-combined.bin out from vm to host.
I eventually used vagrant-scp tool and it worked.
            https://github.com/invernizzi/vagrant-scp
It works both way, host to vm or vm to host.


Step 3: flash the new firmware
esptool.py -p /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 firmware-combined.bin

Step 4: use it
>>>import hello_mpy
>>>hello_mpy.my_hello_mpy()
This is my first micropython module.









No comments:

Post a Comment