Nucleo and adding code to main.py

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
User avatar
Guenther78
Posts: 25
Joined: Fri May 19, 2017 11:48 am

Nucleo and adding code to main.py

Post by Guenther78 » Thu Jun 08, 2017 3:54 pm

Hello Forum!

I have a Nucleo F446 Board. I have a REPL connection and after importing the uos module I can see with

Code: Select all

uos.listdir(uos.getcwd())

the content of the flash.

But if I want to add code to main.py, how do I do this?
There are at least three ways:

1) Sendiing CTRL + F to the Terminal:
http://www.carminenoviello.com/2015/06/ ... nucleo-f4/
Thsi doesn't work with my REPL. The file repl.c from Carmine looks similar, but maybe this function is added in runtime.c? Maybe I will have a closer look at this code.

2) Working with pyboard.py
This is also from Carmine Noviello, There is a python script named pyboard.py in the tools folder. I try to get a connection with my Nucleo board at this time.

3) The third way is to add an external USB:
viewtopic.php?f=12&t=2343&p=13429&hilit ... usb#p13429
This I will try tomorrow.

What are You using? Which way do You prefer? Or is there a fourth or fifth way?
Best regards.
Guenther

shaoziyang
Posts: 363
Joined: Sun Apr 17, 2016 1:55 pm

Re: Nucleo and adding code to main.py

Post by shaoziyang » Thu Jun 08, 2017 4:15 pm

external USB is good if you may using it.

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

Re: Nucleo and adding code to main.py

Post by dhylands » Thu Jun 08, 2017 5:54 pm

I wrote a utility called rshell which can be used over USB or serial for viewing/copying files to/from a pyboard.
https://github.com/dhylands/rshell

If using it over a serial link, then you should use --buffer-size=32

You can also use frozen files, which will get linked in with the micropython image. Files found in the stmhal/modules directory will be compiled to bytecode and included in the image. The directory can be overriden by passing FROZEN_MPY_DIR=path-to-some-directory to make when building the firmware.

You can also pass FROZEN_DIR=path-to-some-other-directory and the python files included in that directory will also be included (but not byte-compiled).

User avatar
Guenther78
Posts: 25
Joined: Fri May 19, 2017 11:48 am

Re: Nucleo and adding code to main.py

Post by Guenther78 » Fri Jun 09, 2017 1:46 pm

Great, first I wiill test it wirh Anaconda and then with Cygwin.

User avatar
Guenther78
Posts: 25
Joined: Fri May 19, 2017 11:48 am

Re: Nucleo and adding code to main.py

Post by Guenther78 » Mon Jun 19, 2017 4:06 pm

Great! It seems to work.
The test with Anaconda failed, but I got it to run with cygwin.

I tested it on a STM32F4 discovery board (I have no NUCLEO board yet) and I was able to copy files to the flash. After copying them I started repl and I could see them in the flash content.
But I was wondering (the NUCLEO board has a micro USB that appears as a flash device in the exploer), why I couldn't see the files in the windows explorer. But I could see them after a reboot.

I am going to test it with the NUCLEO board in the near future.

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

Re: Nucleo and adding code to main.py

Post by pythoncoder » Tue Jun 20, 2017 4:12 am

As a user of rshell it's generally best to disable the USB MSC mode so that the device does not appear as an external mass storage device. The reason for this is that the MSC mode used by devices such as external disk drives is not designed for multi user access. The PC assumes that any file written to the device will not be modified by it. Boards running MicroPython are naturally free to create and modify files on their local filesystem. Sooner or later this will bite you on the posterior and you'll end up with a corrupted filesystem.

If you read the docs you'll find that rshell offers such good file management that MSC mode can readily be disabled. I'm not familiar with your boards but on the Pyboard MSC mode is disabled by editing boot.py on the device so the usb_mode line reads:

Code: Select all

pyb.usb_mode('CDC')
Peter Hinch
Index to my micropython libraries.

User avatar
Guenther78
Posts: 25
Joined: Fri May 19, 2017 11:48 am

Re: Nucleo and adding code to main.py

Post by Guenther78 » Tue Jun 20, 2017 6:23 am

I have tested copying files to the NUCLEO board. And it works.

Back to the discovery board and the remote shelll. After connecting the board and starting repl I entered the command to disable the usb flash device.
But it was still visible in the explorer. I refreshed it with F5, but it didn't go away. And I am still able to open the files with an editor.
But it doesn't matter, I wiill not write to or read from the mass storage device when I'm working with the remote shell.

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

Re: Nucleo and adding code to main.py

Post by dhylands » Tue Jun 20, 2017 4:38 pm

The pyb.usb_mode command only works when used from within boot.py.

The order of initialization is:
- run boot.py
- initialize USB
- run main.py
- enter REPL

In order to actually affect the USB mode, pyb.usb_mode needs to be called before the USB is initialized.

User avatar
Guenther78
Posts: 25
Joined: Fri May 19, 2017 11:48 am

Re: Nucleo and adding code to main.py

Post by Guenther78 » Wed Jun 21, 2017 7:19 am

Okay, for the last time I opened the PYBFLASH with the explorer. I started editing boot.py with notepad, added the line

Code: Select all

pyb.usb_mode('CDC')
and PYBFLASH disappeared. I did'nt see it in the explorer anymore.

But what if I want to have it back? I could copy the old boot.py file to the flash again, assumed I have it...
(I have a backup copy from the initial content of the flash folder.)

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

Re: Nucleo and adding code to main.py

Post by dhylands » Wed Jun 21, 2017 7:30 pm

You have some options.

1 - Use a tool like rshell which works over the USB link and allows files to be manipulated
2 - Do a filesystem reset (which will wipe all of the files) http://docs.micropython.org/en/latest/p ... boot-modes
3 - Write a python script which creates a new boot.py and execute that script on the pyboard using pyboard.py.

Post Reply