How to change default flash contents?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

How to change default flash contents?

Post by SpotlightKid » Wed May 20, 2015 9:54 pm

I have a STM32F4Discovery board where I unfortunately managed to break off the USB OTG socket.

Since it is still possible to flash the firmware via the ST-link interface on the top USB connector, I wonder if it is possible to change the default contents of the flash drive, so I can include my main.py in the firmware.elf file, so I can still use the board for applications where I don't need the USB interface? How would I proceed to do this?

Chris

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

Re: How to change default flash contents?

Post by dhylands » Wed May 20, 2015 11:24 pm

You can also program the flash over a serial link as well, although using the st-link is probably faster.
http://blog.davehylands.com/2014/02/ser ... m32f4.html

If you get the latest firmware and search main.c for the call to uart_init0(). Just after that you'll see:

Code: Select all

    // Change #if 0 to #if 1 if you want REPL on UART_6 (or another uart)
    // as well as on USB VCP
#if 0
    {
        mp_obj_t args[2] = {
            MP_OBJ_NEW_SMALL_INT(PYB_UART_6),
            MP_OBJ_NEW_SMALL_INT(115200),
        };
        MP_STATE_PORT(pyb_stdio_uart) = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
    }
#else
    MP_STATE_PORT(pyb_stdio_uart) = NULL;
#endif
If you change the #if 0 to #if 1, then you'll get a REPL on UART 6. You can then put stuff into the flash over the serial port.

I'm actually just in the process of writing a shell program which will run over a serial port or over the USB port and will allow you to copy files to/from the internal flash.

Until then (should be less than a week before I'm done), you can always write a python program, something like this:

Code: Select all

filename = '/flash/foo.py'
with open(filename, 'w') as file:
    file.write("""
print('This is a test')
print('to see what happens')
print('when you use python to write python scripts')
i = 4 + 5
print('i =', i)
""")

with open(filename, 'r') as file:
    print("===== Contents of '" + filename + "' =====")
    for line in file.readlines():
        print(line.strip())
    print("=====")
and run it like so:

Code: Select all

867 >python3 ../micropython/tools/pyboard.py write-file.py 
===== Contents of '/flash/foo.py' =====

print('This is a test')
print('to see what happens')
print('when you use python to write python scripts')
i = 4 + 5
print('i =', i)
=====
and then from the REPL:

Code: Select all

Micro Python v1.4.2-118-g88506dd on 2015-05-20; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> import foo
This is a test
to see what happens
when you use python to write python scripts
i = 9
>>> 

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: How to change default flash contents?

Post by SpotlightKid » Thu May 21, 2015 11:45 am

Ok, thanks using a serial console might be the way to go with this board, since the MicroPyton firmware is already on it. Need to find my usb-serial-adapter...

I'd be interested in your serial upload tool. Let us know, when you have a first version, please!

I'm still interested to know how to set the initial flash contents, though, might be useful if I ever want to make large quantities of a MicroPython based project.

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

Re: How to change default flash contents?

Post by dhylands » Thu May 21, 2015 2:45 pm

Probably the simplest way would be to populate the flash the way you want, then use dfu-util to grab a copy of the conttents.

Code: Select all

dfu-util --alt 0 --upload flash.bin -s:524288
will backup the first 512K of flash which will include the MicroPython firmware and the flash drive contents.

You can convert the flash.bin file into a flash.dfu file by using:

Code: Select all

tools/dfu.py -b 0x80000000:flash.bin flash.dfu
and then flash that back to the pyboard using:

Code: Select all

dfu-util --alt 0 --download flash.dfu
The fatfs contemts are inside flash.bin at a 16K (16384) offset for a size of 112K (114688) so you could extract just the fatfs contents and flash just that.

We don't currently have any tools for creating fatfs contents locally that can be uploaded, but it would be possible to create the contents that way as well.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: How to change default flash contents?

Post by SpotlightKid » Thu May 21, 2015 3:06 pm

Thanks, that's cool. I have another STM32F4 board which I can use to build the filesystem contents and copy them from there. :)

gradoj
Posts: 27
Joined: Mon Aug 11, 2014 5:47 pm

Re: How to change default flash contents?

Post by gradoj » Tue Jul 21, 2015 8:34 pm

I have a board with no USB as well and have been using the script here to change the filesystem contents. Just wondering, dhylands, if you ever finished that program to transfer files to/from internal flash? Thanks.

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

Re: How to change default flash contents?

Post by dhylands » Tue Jul 21, 2015 8:50 pm

rshell will allow you to copy files to/from internal flash using the raw repl:
https://github.com/dhylands/upy-shell/t ... ter/rshell

It still has some quirks that I'm slowly working on. The most important thing I'm working on right now is detecting when the pyboard goes away/comes back in a cross platform manner. It looks possible. It will need Windows, Mac and Linux implementations of the usb port detection, but it will make rshell much more robust.

You can also use rshell to do your REPL (i.e. no need to use a terminal program).

FYI: rshell also works using a HW UART repl, not just USB, although most of my current testing is with usb)

gradoj
Posts: 27
Joined: Mon Aug 11, 2014 5:47 pm

Re: How to change default flash contents?

Post by gradoj » Wed Jul 22, 2015 10:11 pm

There are going to be a few changes to make this work with Windows:
-wrong slashes in paths I think coming from resolve_path(). cp hangs.
-os.path.exists(com_port) isn't going to work with Windows. I'm using --nowait
-backspace in repl echoes as ←[K
-ls -l works but filenames begin and end in strange characters: 425 Jan 1 2014 ←[2;32mmain.py←[0m

Do you want a pull request as I fix these or how do you want to handle it? Thanks.

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

Re: How to change default flash contents?

Post by dhylands » Wed Jul 22, 2015 11:41 pm

So yeah PR's are good.

For windows, we need to clarify which windows. There is at least cygwin and Win32. For Win32 there is a python color library I was going to investigate (IIRC it was called colorama).

I'm also planning on porting some portions of https://www.npmjs.com/package/usb-detection for each of Windows, Mac, and Linux to deal with the com port existance problem (and to detect when the usb cable is unplugged so that rshell can close the usb serial port).

As far as paths are concerned, windows accepts both forward and backward slashses, so we should probably accept both.

I like using forward slashes for consistency across the OSes, but somebody else might really prefer using backslashes. So I'm fine with having a command line option to make backslash be the default. I'm also ok with having that behavior on non-Windows (so if you were a died in the wool Windows user, you could still use backslashes when running rshell under windows). I'd rather do this than have lots of conditional code everywhere.

I'd also like to support an environment variable (something like RSHELL_ARGS) which contains a "default" set of command line switches so you could set RSHELL_ARGS to '--nocolor' and that would then be the default when you launch rshell.

I'm certainly open to suggestions for any of this, so don't be afraid to argue with me (although we should do that over in the upy-shell github repository rather than here).

Post Reply