preloading the FLASH with files

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
duane
Posts: 1
Joined: Mon Jan 28, 2019 7:52 am

preloading the FLASH with files

Post by duane » Mon Jan 28, 2019 8:07 am

For the STM32 ports, the file main.c - creates several files as part of boot.

What I want to know is this:
Is there a way to 'preload' the on-chip flash disk with files when flashing?

The idea and concept i want is this:
I have a directory with various .py and (data) files, this represents my application
and various data files that make up my application.

It would be nice if I could designate a directory that holds the various files
When I build the image for the device (ie: the DFU binary for STM32 chips)
I would like to have this data already preloaded in the DFU image.

Right now - The only possible thing seems to be:
Step 1 - set jumpers, power cycle device, using DFU-UTIL download new image
Step 2 - re-set jumpers, power cycle device (back to non-DFU mode)
Step 3 - using Mass Storage copy/load the images
Step 4 - Power cycle
Step 5 - run my app

Is this possible?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: preloading the FLASH with files

Post by pythoncoder » Wed Jan 30, 2019 5:35 am

No. You can precompile Python source files as frozen bytecode where they become incorporated into the firmware. In this case they don't appear in the filesystem but you can import and run them in the usual way. But you can't do this for data files which need to be copied using mass storage mode or a program such as rshell.
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: preloading the FLASH with files

Post by dhylands » Wed Jan 30, 2019 6:51 pm

I was able to copy a populated filesystem from my pyboard v1.1 by using dfu-util. Put the board in DFU mode and then execute the following commands:

Code: Select all

dfu-util --upload-size 0xc000 --upload fs1.bin -s 0x8004000 -a 0
dfu-util --upload-size 0x10000 --upload fs2.bin -s 0x8010000 -a 0
I then reset the filesystem (which puts it back to the basic 4 files) and with the board back in DFU mode I was able to then restore the filesystem by using:

Code: Select all

dfu-util -a 0 -D fs1.bin -s 0x8004000
dfu-util -a 0 -D fs2.bin -s 0x8010000
dfu-util wouldn't let me read across a segment boundary (not sure why not) which is why I needed to split this into 2 reads.

I was able to combine fs1.bin and fs2.bin and flash it back using a single invocation of dfu-util:

Code: Select all

cat fs1.bin fs2.bin > fs3.bin
dfu-util -a 0 -D fs3.bin -s 0x8004000
NOTE: These addresses and sizes are VERY board specific. The ones above work with the PYBV10/PYBV11 boards. Different boards may have different addresses/sizes.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: preloading the FLASH with files

Post by dhylands » Wed Jan 30, 2019 7:06 pm

I also don't use jumpers to put my board into DFU mode. You can run the following command:

Code: Select all

~/micropython/micropython/tools/pyboard.py --device /dev/ttyACM2 -c 'import pyb; pyb.bootloader()'
This will cause an error since the board reboots into bootloader mode and /dev/ttyACM2 disappears, so I see a traceback followed by an error:

Code: Select all

serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
I wind up redirecting the output to /dev/null. I also utilize a little script I wrote call find_port.py which you can find here: https://github.com/dhylands/usb-ser-mon ... nd_port.py I have a bootloader script that I use from the command line which contains the following:

Code: Select all

#!/bin/bash
# Puts a micropython board in booloader mode.

PORT=$(find_port.py -n MicroPython)
if [ -z "${PORT}" ]; then
  echo "No MicroPython device found"
  echo "Available Devices:"
  find_port.py -l
  exit 1
fi
~/micropython/micropython/tools/pyboard.py --device "${PORT}" -c 'import pyb; pyb.bootloader()' >& /dev/null
Also, when flashing, the board using dfu-util, you can pass in -s :leave to cause it to reboot the board after flashing, so it's possible to get into and out of DFU mode without having to touch the board.

Post Reply