code in internal flash and mounting sd card as usb mass storage

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
AndreasB
Posts: 7
Joined: Thu Apr 18, 2019 12:37 pm

code in internal flash and mounting sd card as usb mass storage

Post by AndreasB » Thu Apr 18, 2019 1:12 pm

hi,
I am attempting to mount the sdcard (if present) so it can be used to store logging data.
but i want the code to be on the internal flash.
i want the sd card to be accessible through the usb connection as a mass storage device.
i have been able to put the code on the flash and execute it even with sd card present with the SKIPSD file
i also managed to mount the sd card file system with this inside boot.py.
>sd = pyb.SDCard()
>if sd.present():
> uos.mount(sd, '/sd')
> pyb.usb_mode('CDC+MSC')
>else:
> pyb.usb_mode('CDC')
my problem is that the sd card is not the one mounted as a mass storage device through the USB.
is there any way to set the mount point for the usb mass storage class?
I do not want any code to reside on the sd card to make this work

rhubarbdog
Posts: 168
Joined: Tue Nov 07, 2017 11:45 pm

Re: code in internal flash and mounting sd card as usb mass storage

Post by rhubarbdog » Thu Apr 18, 2019 2:15 pm

remove the SKIPSD file from your flash
add a boot.py to the SD Card with the lines

Code: Select all

import pyb
pyb.main('/flash/main.py') 
This will automatically mount the SD Card but execute main.py from flash

AndreasB
Posts: 7
Joined: Thu Apr 18, 2019 12:37 pm

Re: code in internal flash and mounting sd card as usb mass storage

Post by AndreasB » Thu Apr 18, 2019 3:07 pm

I have though about that.
that would indeed mount the sd card and run the main from /flash
but i would still have the boot code on the sd card.
i actually don't want to have any code on the sd card as i do not want the code to be end-user accessible.

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

Re: code in internal flash and mounting sd card as usb mass storage

Post by Roberthh » Thu Apr 18, 2019 6:13 pm

In that case you should consider to create your own firmware image and replace in the file main.c "boot.py" by "/flash/boot.py" and "main.py" by "/flash/main.py")

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

Re: code in internal flash and mounting sd card as usb mass storage

Post by dhylands » Thu Apr 18, 2019 6:22 pm

And you can use rshell to copy files off of the sdcard whether it shows as the USB mass storage device or not.

I may have spoken too soon. You can do that on the pyboard, but it may not work if the sdcard needs to be mounted by boot.py. I'll need to add a command to rshell to rescan the filesystem.

rhubarbdog
Posts: 168
Joined: Tue Nov 07, 2017 11:45 pm

Re: code in internal flash and mounting sd card as usb mass storage

Post by rhubarbdog » Fri Apr 19, 2019 2:41 am

Have you considered that the file system that mounts when you plug the pyboard in isn't the filesystem that exists on the pyboard after your program has run. consider the following program

Code: Select all

import pyb

pyb.LED(4).on()

exist = True
try:
    with open('output.txt') as _:
        pass
except:
    exist = False
    pyb.delay(30 * 1000)
else:
    pyb.delay(1000)

with open('output.txt', 'w') as file_:
    for i in range(20):
        file_.write("line %d %s\n" % (i, 'exists' if exist else 'is new'))
        
pyb.LED(4).off()
The first time it runs the file system has mounted before the file output.txt has been created so

Code: Select all

ls PYBFLASH
reveals nothing. Press reset the file system remounts, let wait until the program has run then typing

Code: Select all

cat PYBFLASH/output.txt
produces

Code: Select all

line 0 is new
line 1 is new
line 2 is new
line 3 is new etc...
where as the file contents actually are

Code: Select all

line 0 exists
line 1 exists
line 2 exists
line 3 exists etc...
To see the correct information I have to unmount and remount the pyboard manually. Where as using something like rshell on your pyboard to list it's contents or cat's a file as it is when the program has completed runnning.

At least this is how it is on a Linux/Unix system

AndreasB
Posts: 7
Joined: Thu Apr 18, 2019 12:37 pm

Re: code in internal flash and mounting sd card as usb mass storage

Post by AndreasB » Fri Apr 19, 2019 8:39 am

I was already looking into the c code to see if i could change it there.
I think i have got it working the way i wanted by editing the main.c
original at line 653:

Code: Select all

    #if MICROPY_HW_ENABLE_USB
    // if the SD card isn't used as the USB MSC medium then use the internal flash
    if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
        pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
    }
    #endif
what i made of it

Code: Select all

    #if MICROPY_HW_ENABLE_USB
    // if the SD card isn't used as the USB MSC medium then use the internal flash
    if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
        pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD;
    }
    #endif
it seems this way it mounts the sd card for the usb mass storage
i need to do some more testing to see if it completely behaves like i want

chrismas9
Posts: 152
Joined: Wed Jun 25, 2014 10:07 am

Re: code in internal flash and mounting sd card as usb mass storage

Post by chrismas9 » Sat Apr 20, 2019 7:03 am

Have you considered using frozen byte code? The source code won't be on the system at all and will be better protected than hiding it on an non visible drive. To protect code on a non visible drive you would also have to disable REPL to stop people running their own code to read yours.

User avatar
Dugite
Posts: 21
Joined: Thu Jan 18, 2018 1:29 pm

Re: code in internal flash and mounting sd card as usb mass storage

Post by Dugite » Tue Jul 09, 2019 7:04 am

I have also been trying to get "boot.py" and "main.py" to execute from flash along with all of my other modules.

I was running MicroPython 1.10 with my own hardware based on the PyBoard 1.1 and I tried calling "pyexec_frozen_module" for "boot.py" and "main.py" but it still kept expecting a "main.py" file on the SD card.

After some time of trying different ways to get this to work, I ended up upgrading to MicroPython 1.11 and then everything worked with no changes to the MicroPython source code. I have not added a "SKIPSD" file to the internal flash as it works without this file.

I no longer have a "main.py" file and instead, call a "main_data_logger.py" from my "boot.py" as "pyb.main('main_data_logger.py')". There are no ".py" files on my SD card now.

I have my modules in "/micropython-1.11/ports/stm32/modules".

I hope this helps others.

Post Reply