SD Card Questions

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.
JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

SD Card Questions

Post by JimTal001 » Tue Aug 25, 2015 8:35 pm

I have to say managing this SD card is a challenge at best.

What I simply need is the following:
1. Use the SD Card as a data logger
2. Be able to use REPL for debugging purpose.
3. Be able to examine the results of my data log between program execution.
4. Be able to edit my code on the SD card.

I did install dhylands upy-shell on the SD card as many have suggested but have not been able to test it yet.

Can someone suggest a boot.py setup for this scenario?

As per my understanding, with the SD card installed boot.py is always read from the SD card, not the Flash.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: SD Card Questions

Post by JimTal001 » Tue Aug 25, 2015 10:07 pm

I don't know if I have the a decent configuration but I have something working now and for others new to pyboard and needing to log data on SD, edit code on the SD using windows text editor and use REPL for debug purpose. Here is a simple setup:

On SD Card:

1. boot.py with only two lines of code:
a. import pyb
b. pyb.usb_mode('CDC'+'MSC')

2. Setup dhyland's upy-shell programon the SD (2 files: shell.py and cmd.py)
* note that when a data logging program is run you will not be able to see the log file so upy-shell helps resolve this issue.

3. Create a sub-folder on the SD card named log using windows (right click on SD, make folder, etc..).

4. Create a file on the SD Card named test.py and put the following code in it (modified from prior post)

Code: Select all

import pyb
import os

logPath = 'log/data.txt'

def deleteFile(filePath=""):
	try:
		os.remove(filePath)
	except OSError:
		print('Failed to Delete File')

def funct1():
	print("start log test")	
	deleteFile(logPath)
	pyb.delay(500)
	
	accel = pyb.Accel()
	l = pyb.LED(3)
	l.on()
	f = open(logPath, 'w')		# open the file for writing

	for i in range(10):			# loop 5 times only for testing
		time = pyb.millis()		# get the current time
		x, y, z = accel.filtered_xyz()	# get the accelerometer data

		# write time and x,y,z values to the file
		f.write('{} {} {} {} {}\n'.format(i, time, x, y, z))
		pyb.delay(500)			# wait 1000 ms = 1 second
		l.toggle()

	f.close()				# close the file
	l.off()					# LIGHT OFF
	print("end log test")	
	
funct1()
5. Open your serial terminal an connect to pyboard
6. Run the program test.py by using: import test
7. When the program is complete, "end log test" printed in the terminal go to the log folder on the SD card and you will notice there is no file, this is normal.
8. in the terminal type: import shell
9. At the sd> prompt navigate to the log folder: cd log
10. type ls to see the file created: data.txt
11. type: cat data.txt to see the data in the log file.

You can edit and save your program using normal text editor.
To repeat the process, get out of the shell: I use Ctrl C twice, but may be something better. This returns you to the >>> prompt. Press Ctrl D to soft reboot, then finally import test to run the program again.

Hopefully this will help others get started faster.

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

Re: SD Card Questions

Post by dhylands » Tue Aug 25, 2015 11:54 pm

Just to clarify, there are 2 shell programs, which are different and can be used independently.

There is shell.py and cmd.py which runs on the pyboard.

Since then I created rshell.py (which is in the rshell subdirectory). It runs on the host and allows you to do most of the same things as shell, but it also allows you to copy files into and out of the pyboard. All rshell needs is a serial interface,
JimTal001 wrote:On SD Card:

1. boot.py with only two lines of code:
a. import pyb
b. pyb.usb_mode('CDC'+'MSC')
That should be pyb.usb_mode('CDC+MSC')

Note that what you coded was essentially: pyb.usb_mode('CDCMSC') which should raise an exception.

To switch modes between running your logging program and running the REPL, you might want to do something like use the USR switch.

So you might code something like this as you main.py:

Code: Select all

import pyb

sw = pyb.Switch()
led = pyb.LED(4)
sw_was_pressed = False

for i in range(3):
    led.on()
    pyb.delay(100)
    led.off()
    for i in range(9):
        if sw():
            sw_was_pressed = True
            break
        pyb.delay(100)
    if sw_was_pressed:
        break

if not sw_was_pressed:
    print("Switch wasn't pressed - running main program")
    import main_program
else:
    print("Switch was pressed - dropping into REPL")
When the pyboard boots up, you'll start to see the blue LED flashing. If you press the USR button within 3 seconds then it will go into the REPL, otherwise it will import main_program which would be where you put your data logger.

For a data logger, I'd recommend that you use pyb.usb_mode('CDC') which disable USB Mass Storage, I've found that USB Mass Storage will often mess with the files especially if the pyboard is also writing to files.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: SD Card Questions

Post by JimTal001 » Wed Aug 26, 2015 12:32 pm

hello dhylands,
All rshell needs is a serial interface,
Does it not require python 3.0 or better installed on the pc?
Note that what you coded was essentially: pyb.usb_mode('CDCMSC') which should raise an exception.
My typo error.
For a data logger, I'd recommend that you use pyb.usb_mode('CDC') which disable USB Mass Storage
I tried pyb.usb_mode('CDC') in the boot.py of my SD card and I was not able to connect via serial until I removed it. I tried this on two computer with the same result. The pyboard usb/serial device driver on the pc changes to:
Pyboard Virtual Comm Port in FS mode

Questions:
1. Is there an additional line in the boot.py needed to activate the serial port when in pyb.usb_mode('CDC') mode?
2. Is there a way to switch back and forth between the two mode within the body of a program?

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

Re: SD Card Questions

Post by dhylands » Wed Aug 26, 2015 4:02 pm

What version of firmware are you using?

This is the boot.py that I often use:
https://github.com/dhylands/upy-example ... er/boot.py

And that seems to work for me on sdcard or internal flash.

I suspect that you're either running into an error in the boot.py script or your firmware is too old. My current version shows as:

Code: Select all

Micro Python v1.4.4-180-g44e3df4 on 2015-08-06; PYBv1.0 with STM32F405RG
I'd recommend that you rename boot.py to something else and try importing it to see if any other errors are reported.

pyb.usb_mode can be called anywhere, but only takes effect when called from within boot.py. This is because the usb module is initialized after boot.py is called and before main.py is called. And once usb is initialized, further changes to the usb_mode are essentially ignored.

Now you could take that same logic I put up about using the switch and put that in boot.py.

And yes - it looks like rshell.py needs python 3.0 or better (I just tried running it using python2 and it fails).

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: SD Card Questions

Post by JimTal001 » Wed Aug 26, 2015 6:17 pm

What version of firmware are you using?
The latest version I believe. When I issue Ctrl-D I get:
Micro Python v1.4.5-27-g5ab0a4a on 2015-08-24; PYBv1.0 with STM32F405RG
I suspect that you're either running into an error in the boot.py script
my boot.py script (the one that fails) is too simple, see here:

Code: Select all

import pyb
pyb.usb_mode('CDC') 

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

Re: SD Card Questions

Post by dhylands » Wed Aug 26, 2015 7:29 pm

Another possibility is if you're running linux then there could be permissions problems.

The udev rules refer to a PID of 9800 (which is the PID used for CDC+MSC. When using just CDC, the PID changes to 9802.

So I just dropped the PID altogether from my udev rules and use:

Code: Select all

ATTRS{idVendor}=="f055", ENV{ID_MM_DEVICE_IGNORE}="1"
ATTRS{idVendor}=="f055", ENV{MTP_NO_PROBE}="1"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="f055", MODE:="0666"
KERNEL=="ttyACM*", ATTRS{idVendor}=="f055", MODE:="0666"

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: SD Card Questions

Post by JimTal001 » Wed Aug 26, 2015 8:04 pm

I'm running Windows 7

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

Re: SD Card Questions

Post by dhylands » Wed Aug 26, 2015 8:11 pm

Under windows, the COM port will change for CDC versus CDC+MSC mode.

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

Re: SD Card Questions

Post by dhylands » Wed Aug 26, 2015 8:15 pm

Oh - maybe you need to make a copy of the pybcdc.inf file and then do a search and replace of 9800 with 9802 and install that driver.

Post Reply