Moving data to host using USB

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
s_programmer
Posts: 2
Joined: Tue Feb 28, 2017 3:36 am

Moving data to host using USB

Post by s_programmer » Tue Feb 28, 2017 3:44 am

Hi,
I am new to microPython. I am collecting data from a peripheral using SPI and I want to sent it via USB to a pc on real time (~ 1000 elements @ 60 Hz). I am not sure how to orchestrate both communication at the same time without loosing data.

Any guidance will be appreciated :)

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

Re: Moving data to host using USB

Post by pythoncoder » Tue Feb 28, 2017 5:05 pm

What platform are you using? The Pyboard? And what data type are the elements? Bytes, integers, floats, strings?

USB offers two modes potentially offering solutions: CDC mode where it appears as a serial interface and MSC mode where the target's filesystem may be mounted on the PC. Using MSC mode would involve writing data to a file and having the PC access the file as a mounted drive. I think this is a non-starter because that USB mode is not designed for concurrent access.

So we're left with CDC mode. The data rate sounds high to me. A typical baudrate of 115200 corresponds to 240bytes at a 60Hz rate: clearly too slow for your purpose. I've seen four times that rate used but even that's too slow even if your elements are a single byte. The question is how high can the baudrate go, both on the Pyboard and on the PC. I'm afraid I don't know the answer to that one; perhaps someone will chip in with a figure ;)
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: Moving data to host using USB

Post by dhylands » Tue Feb 28, 2017 9:24 pm

The baud rate over USB is fixed at 12 Mbits/sec regardless of the baud rate chosen.

However, the way that USB works, the host has to poll the device to get data in the device to host direction.

So I think you'd need to try sending data and see how fast it is.

s_programmer
Posts: 2
Joined: Tue Feb 28, 2017 3:36 am

Re: Moving data to host using USB

Post by s_programmer » Wed Mar 01, 2017 12:03 am

Thanks Peter and Dave.

I am using pyboard 1.1 and my data is int16. I was calculating that I need a speed transfer of 1000 points x 16 bits x 60 Hz = 960kBits/s. I am a little confused now because Peter mentioned a limit (or typical baudrate of 115.2 kBits/s) while Dave mentioned the speed of USB 1.0 (12 MBits/s), that seems a 100x difference and I am wonder which one is close to what you can reach in practice.

Are there any libraries/tutorials for using the USB port in pyboard 1.1 different to the interactive prompt?

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

Re: Moving data to host using USB

Post by dhylands » Wed Mar 01, 2017 5:22 am

This is not really scientific, but I was able to use rshell to copy a 3966165 bytes file from the sd card on the pyboard to the host in 16 seconds.

That translates to 242 Kbytes/sec or roughly 2.4 Mbits/sec

rshell uses a flow control mechanism where the pyboard will wait for an ACK after every 512 bytes, so faster rates should definitely be possible. The way that rshell copies files from the sd card is to download a python snippet using the raw-repl and executes that python to send the file contents using stdin/stdout.

I wrote a simple JSON IPC mechanism which works using the usb serial link.
https://github.com/dhylands/json-ipc

It basically just uses the pyb.USB_VCP class to do reads & writes on the usb serial link, and the host side uses pyserial to do the same.

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

Re: Moving data to host using USB

Post by pythoncoder » Wed Mar 01, 2017 6:14 am

@dhylands Now I'm confused :? Your JSON IPC program (very nice, by the way) uses a default baudrate of 115200 in serial_port.py.

Earlier you said "The baud rate over USB is fixed at 12 Mbits/sec regardless of the baud rate chosen." That implies that, even with 115200 set, you can expect 12Mbps throughput. Is that correct?
Peter Hinch
Index to my micropython libraries.

User avatar
marfis
Posts: 215
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

Re: Moving data to host using USB

Post by marfis » Wed Mar 01, 2017 6:26 am

Some of the confusions come from different USB HW and FW implementations.

If a USB - serial converter chip is used it will limit the datarate to the value you specify over your terminal (by using CDC / VCP profile). That is the case for example on the huzzah boards or if you use your own usb-serial adapter and connect it to the UART of the uPy board.

If a direct USB connection is implemented (e.g. on the pyboard) the FW / USB stack defines how much net datarate you will get. On the pyboard the datarate submitted with the CDC profile is ignored so the datarate you configure on the terminal is meaningless. Since Micropython 1.6 up to 7-8 Mbauds are possible (which matches with Daves measurements since SD card access adds some overhead). You can easily measure this by simply printing e.g. help() in a for loop 1000 times.

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

Re: Moving data to host using USB

Post by dhylands » Wed Mar 01, 2017 7:01 am

pythoncoder wrote:@dhylands Now I'm confused :? Your JSON IPC program (very nice, by the way) uses a default baudrate of 115200 in serial_port.py.

Earlier you said "The baud rate over USB is fixed at 12 Mbits/sec regardless of the baud rate chosen." That implies that, even with 115200 set, you can expect 12Mbps throughput. Is that correct?
Yeah when the host is talking to a device directly using USB-serial, then the baud rate is totally ignored.

If you were to create a USB serial adapter using a pyboard, and have the data sent over USB then appear on a real UART, then the baud rate is intended to be passed over the USB link and used to initialize the baudrate of the UART. But if you're talking to an entity on the pyboard itself using USB then there is no UART involved, so the baudrate is irrelevant. You can pick 300 baud and you'll get exactly the same data transfer rate between the host and the pyboard over USB as you would using 115200.

And rshell uses 115200 baud by default, so the 2.4 Mbits/second was using a usb-serial link initialized with 115200 (which also shows that the baud rate it ignored).

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

Re: Moving data to host using USB

Post by pythoncoder » Wed Mar 01, 2017 7:17 am

You learn something every day round here :D Thanks for that, Dave.
Peter Hinch
Index to my micropython libraries.

Post Reply