Freezing / Compiling raw binary data in DFU

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Damien
Site Admin
Posts: 619
Joined: Mon Dec 09, 2013 5:02 pm

Re: Freezing / Compiling raw binary data in DFU

Post by Damien » Tue Jul 02, 2019 7:32 am

As mentioned above, you can use frozen scripts to store a bytes object in ROM.

An alternative: to load binary data to some external device (here SPI RAM) then you could simply store that binary data directly on the filesystem as a binary file, then copy it to the external SPI RAM device using a simple loop, eg:

Code: Select all

buf = bytearray(1024)
with open('/assets/data.bin', 'rb') as f:
    while True:
        n = f.readinto(buf)
        if n == 0:
            break
        spi.write(memoryview(buf)[:n])
But then the question is how to get the asset file easily to the device, using DFU. There are two ways to do this: 1) to build the filesystem image (I guess FAT FS) on your PC and pack it into a DFU file alongside the main firmware; 2) just pack the binary asset directly into the DFU at a known location in ROM. For the latter you can do:

Code: Select all

python3 tools/dfu.py -b 0x08080000:asset_data.bin asset.dfu
The deploy asset.dfu as usual (DFU files can contain multiple binary sections, so you could also put the firmware in the same DFU as the assets).

Then use uctypes.bytes_at(0x08080000, asset_len) to access the data directly.

User avatar
TravisT
Posts: 54
Joined: Sun Feb 23, 2014 2:31 pm
Location: Portland, OR
Contact:

Re: Freezing / Compiling raw binary data in DFU

Post by TravisT » Tue Jul 02, 2019 2:19 pm

@pythoncoder
Very fair point, and the wiki would make more sense. It is unfortunate that the documentation, forums, and wiki are not always the easiest to search, but it could easily be argued that eventually any body of knowledge gets hard to keep track of and fully utilize. I am also guilty of not thinking enough about the wiki, partly because google does not seem to search it much or at all.

@damien
Thanks for this alternative approach! It would make sense to utilize the filesystem and standard binary files to save size and complexity. I will have to play with flashing filesystem images along side the flash.
_______________
Travis Travelstead

Post Reply