Adding libraries to the binary image

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
preiter
Posts: 7
Joined: Tue Aug 09, 2016 5:14 pm

Adding libraries to the binary image

Post by preiter » Sat Aug 20, 2016 5:59 pm

I'm trying to add some libraries to my firmware-combined.bin so that I can access them from my code.

I'm not sure of the proper procedure. I've been copying files from micropython-lib into the scripts directory. I'm not sure what to copy, just the libraryname.py file? The whole directory?

Some libraries have sublibraries like urllib. If I just want the functions in urllib.parse, do I need urllib as well?

I also got this error:

xtensa-lx106-elf-ld: build/firmware.elf section `.irom0.text' will not fit in region `irom0_0_seg'
xtensa-lx106-elf-ld: region `irom0_0_seg' overflowed by 34020 bytes
make: *** [build/firmware.elf] Error 1

Is the space available for libraries really limited?

Is there a tutorial on this that I could read somewhere?

preiter
Posts: 7
Joined: Tue Aug 09, 2016 5:14 pm

Re: Adding libraries to the binary image

Post by preiter » Sun Aug 21, 2016 1:48 am

I think I have it figured out now.

Modules consisting of a single .py file I just put in the scripts directory. Modules with submodules, like urllib.urequest I put in a subdirectory like urllib/urequest.py.

I still can't get urllib.parse to work, but I suspect that library is not working yet.

Thanks for reading.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Adding libraries to the binary image

Post by Roberthh » Sun Aug 21, 2016 7:03 am

Instead of the scripts directory, you can also create a modules directory and place your files there. The difference is, that Python scripts in the modules directory will be pre-compiled at image build time and then executed from flash, saving RAM during both loading and execution. I do that with scripts which are otherwise too large for parsing or execution. Whether this also works for scripts in subdirectories has to be tested.

Beta_Ravener
Posts: 35
Joined: Tue Aug 09, 2016 6:56 pm

Re: Adding libraries to the binary image

Post by Beta_Ravener » Sun Aug 21, 2016 11:29 am

I have just tested out of curiosity and sub-directories in modules seems to work too.
More info:
I have created sub-folder inside modules with following files:

Code: Select all

modules/sub/__init__.py
modules/sub/test.py
The init file is empty and I didn't test omitting it, but compiled it only has 48 bytes. The test file has following code:

Code: Select all

class Test:
	def __init__(self):
		print("Test from subfolder")
The code compiled successfully so I flashed the new firmware like usual and connected to the device. Below is the output from UART:

Code: Select all

>>>from sub.test import Test
>>> t = Test()
Test from subfolder
>>>
Edit: The __init__.py file can be omitted without problems. Saved 48 bytes per module, yay!

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Adding libraries to the binary image

Post by Roberthh » Sun Aug 21, 2016 3:24 pm

If you have your files in the modules directory, you do not need them in the scripts directory. Otherwise they would be stored twice in flash.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Adding libraries to the binary image

Post by zaord » Thu Apr 09, 2020 5:38 pm

Question : in the modules directory, do I need to put all the files for the micropython-lin I wants to freezes ?

For exemple I wants to freeze the upip in my pyboardv11 firmware, should I pout all the upip forlder in the submodules folder I create in ports/stm32 folder ?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Adding libraries to the binary image

Post by jimmo » Tue Apr 14, 2020 4:38 am

This thread (from 2016) is a bit out of date. The way freezing works has been updated since then to use the new "frozen manifest" feature, which allows you to extend from the default configuration in your own board configuration.

Probably the best example is to look at ports/esp32/boards/TINYPICO which adds additional modules over the default ones (this is for esp32, but the same process applies to esp8266).

Code: Select all

cd ports/esp32
make BOARD=TINYPICO
So you could do the same thing for your board. Note on esp8266 you might want to extend from manifest_release.py.

There's been a few forum threads discussing this, search for "manifest.py" for more info.

Post Reply