Fastest way to transfer file from Raspberry PI 4 to Teensy 4.1

Gio26

Member
Hi, I'm new to the forum and also to Teensy! I just bought some teensy 4.1 and I am trying to transfer a file from raspberry pi 4 to teensy 4.1 sd.
I develop in Visual studio / Visual micro and on raspberry in .net.
It seems to me that I could use USB but I use the primary port for development and debugging. Can I use the USB Host to connect with the raspberry and transfer the file?
Thank you.
 
Yes but you need code on both sides..

And we are working on MTP for Teensyduino. Don’t remember if RPI has MTP host code or not. Mtp lib support?

Could probably use Rawhid as protocol.

Could configure teensy for dual or triple serial mode and use one of the other serial to do transfers.
 
Yes of course you can use USB type of Serial. That is you can decide in your Sketch code to download a file from the SD card to the RPI. But as I mentioned, your app will need to do whatever it needs to do, to do this. Like Open the file on the SD card, and do reads and writes to Serial, and probably have some form of handshaking with some app running on RPI, that receives the data and writes it to disk...

I was trying to point out some alternatives to this. Like RawHID: https://www.pjrc.com/teensy/rawhid.html
which current release sends 64 byte packets... I have version I was playing with on T4.x which could send 512 byte packets.

Or as I was mentioning MTP. With usb type: MTP Disk or Serial + MTP Disk

You can have a simple sketch like:
Code:
#include <SD.h>
#include <MTP_Teensy.h>

#define CS_SD BUILTIN_SDCARD  // Works on T_3.6 and T_4.1
//#define CS_SD 10  // Works on SPI with this CS pin
void setup()
{
  Serial.begin(9600);
  while (!Serial && millis() < 5000) {
    // wait for serial port to connect.
  }

  // mandatory to begin the MTP session.
  MTP.begin();

  // Add SD Card
  if (SD.begin(CS_SD)) {
    MTP.addFilesystem(SD, "SD Card");
    Serial.println("Added SD card using built in SDIO, or given SPI CS");
  } else {
    Serial.println("No SD Card");
  }
  Serial.println("\nSetup done");
}


void loop() {
  MTP.loop();  //This is mandatory to be placed in the loop code.
}
And note this will get simpler soon.... But with this when you plug in your Teensy to a Windows machine or Ubuntu or with a little help MAC, You can open a logical Folder window, and you will see the
files that are on the SD. And you can drag and drop or the like, files to or from the SD on your Teensy. I have not tried this in awhile on RPI to know if they support this or not.
Also not sure if you are running your RPI headless or not, so may not have GUI. But the GUI is built on an MTP Library ... SO maybe command lines that can do this...
 
Many thanks you have given me a lot of useful information. Now I'm going to start doing some tests.
Could you tell me how to wire the usb Host port? I mean:
Teensy 4.1 -> Raspberry
5v to none correct?
D- to D- Or I need to swap?
D + to D +
GND to GND
GND to none or GND again?

Thanks.
 
This page or product might indicate the connections for using HOST mode: pjrc.com/store/cable_usb_host_t36.html

The Teensy needs to have enough 5V power onboard to run what the connected device requires. If powered by USB that has to power the Teensy first - then have enough for 'the device'. A powered HUB can make up the difference if required.

But if connecting to an rPi - it will be the host? And the Teensy device micro USB connector would be used for the p#4 solution.
 
Thanks defragster.
If I'm not mistaken I will not use USB Host on teensy having to connect the teensy to the USB port of the raspberry which already has a 5v power supply.
I am using Teensy's micro usb for development and debugging so I can't use it for data transfer.
This is why I asked if I can use the additional "USB Host" port to connect to the raspberry.
Thanks
 
If I understand correctly, you want to transfer data from the RPi to the Teensy. RPi and Teensy are connected to a PC for programming / debugging.

If so, I'd do something like this:

rpi.jpg

On the Teensy side I'd use one of the Hardware Serial ports. E.g. Serial1 which is on pin 0 (RX) and pin 1(TX). Connect it to the Serial port of the RPi (according to the pinout this should be GPIO14 / 15. Make sure that the RPI has 3V3 outputs. Don't forget to cross (TX->RX and RX -> TX) and don't forget to connect GND between RPi and Teensy as well.
 
Last edited:
Thanks luni.
you understood well except that the raspberry is not connected to the pc with usb and the debugging on rasperry takes place via LAN.
Yes I know the serial transmission but I was hoping to be able to have a faster connection via usb, maybe I'm wrong.
I am confused about how to use host usb port as non host usb.
 
I'd start with a simple serial connection and see if that is fast enough. Might be that the SD card is the bottle neck anyway. How large are the files you need to transmit?
Using the USB Host is kind of advanced and I'd only use this kind of stuff if you really need it. Using it in non host mode is even more advanced. I'm not aware of any library providing that feature.https://www.pjrc.com/store/ethernet_kit.html
 
Last edited:
i have already tried serial, i2c and spi (not on teensy because i need spi slave) and the results are not good for me.
The size of the file is not known but I am using as an example a text file of 30mb with more than a million lines. (the file is gcode)
Previously I was using an ESP32 and the only solution I had found was WiFi and packing about 50 lines for any trasmission.
Why can't I use KurtE's solution? It seems to me the best possible solution, but I didn't understand how to wire the host usb port.
 
30MB will probably take about 2-3 minutes at 2MBaud that is not very convenient indeed.

Why can't I use KurtE's solution? It seems to me the best possible solution, but I didn't understand how to wire the host usb port.
I don't think Kurts solution will reprogram the usb host as usb slave but maybe I misunderstood it. Here https://www.pjrc.com/store/cable_usb_host_t36.html a connector. Nothing to wire just plug it in (EDIT: just saw that defragster already linked that). Curious if that will work at the end of the day...

You might also think of Ethernet. That should run out of the box and there are nice libraries around.
 
I don't think Kurts solution will reprogram the usb host as usb slave but maybe I misunderstood it. Here https://www.pjrc.com/store/cable_usb_host_t36.html a connector. Nothing to wire just plug it in (EDIT: just saw that defragster already linked that). Curious if that will work at the end of the day...

I never said Kurts intended to use the USB host port as a slave. In fact I asked how to wire it and if I had not misunderstood I should have wired it as a slave. My interpretation for the 5v pin!
If it's a secret I'll buy the pre-wired cable.
I'm not even looking for a solution that should work by the end of the day.

Thanks for your help.
 
I never said Kurts intended to use the USB host port as a slave. In fact I asked how to wire it and if I had not misunderstood I should have wired it as a slave.
As I understand it the Receiving device on USB is a slave. You asked to move data from RPI to T4.1 (Master to Slave).
 
Again maybe would help to understand desired connections between:
PC and Teensy
PC and RPI 4

Teensy and RPI 4

For example is this something like 3D printer or CNC... and both the RPI and T4.1 are in some board/box... which could be remote from computer?

If Teensy is plugged into RPI - then solutions of different USB types to build the Teensy with, might apply.

If RPI is plugged into Teensy USB Host port, then other similar but different solutions might exist. But I don't know RPI well enough to know if it can show up to Host as something like a Mass Storage Device or MSC? or Serial device? Again been awhile since I tried that... But for example we do have support in USB Host for some different ways. Like if it is an MSC type device that support FAT, we could then talk directly to it... I see mention of Gadget mode, but not sure what that is.

As Luni mentioned Ethernet might be a solution.

Will have to play around a little.
 
this is what I asked:
"Can I use the USB Host (Teensy 4.1) to connect with the raspberry and transfer the file?"
If is not possibile ok but if is possibile I would like information on how to proceed in this direction.
Thanks.
 
For example is this something like 3D printer or CNC... and both the RPI and T4.1 are in some board/box... which could be remote from computer?
this is exactly what I am doing.

The physical connection between raspberry and Teensy is not yet defined as I am looking for a solution for the file transfer.
The rest of the data exchange can take place without problems with I2C or Serial.
During development I use the Teensy micro usb port for development / debugging on Visual studio, for this reason the usb host port would have been useful to connect to the raspberry.

As I asked you at the beginning it would be great for me to use https://github.com/TeensyUser/doc/wiki/Serial ?. I tried it with teensy micro usb and for my expectations it is perfect.

About Ethernet, yes can be a good possibile, but is really the last choice since it needs other layers for connection.

Thanks.
 
Since you are new here, it is quite difficult to judge your experience and knowledge with that stuff. This might lead to frustrating or unsatisfying answers. But believe me, usually the guys here are very friendly and want to help you. Maybe the confusion/frustration comes from a misunderstanding of how USB works?

USB is a quite complicated protocol where the Host and the devices (slaves or functions) have completely different tasks. The host is managing the complete bus, checks for new devices, enumerates them and is the only one on the bus which can talk to the devices. The devices have a much simpler task, they listen for commands on the bus and perform actions like providing data if requested from the host. Thus, you can not make a device behaving like a host by rewiring the cable or some other tricks.

In your setup the RPi will be the host and if you want to transmit data to this host you necessarily need to be a usb-device. The first USB Port (that with the micro USB jack) is programmed as device. Therefore you can use it easily to communicate to the RPi. It can be used in different modes. One of the modes is the CDC mode where the device (our Teensy) acts as a virtual serial interface. However, the second USB port is programmed as USB-Host. Thus, you can not connect it to the RPi however you wire it. There simply is no host to host communication on the USB bus. Of course you can reprogram the second USB Port to be a second USB device (slave) but this as mentioned is quite advanced.

This is why it seems to be much easier to use Serial (too slow) or Ethernet instead of the second USB port. But maybe (as usual :) ) Kurt comes up with some cunning idea.
 
Since you are new here, it is quite difficult to judge your experience and knowledge with that stuff. This might lead to frustrating or unsatisfying answers. But believe me, usually the guys here are very friendly and want to help you. Maybe the confusion/frustration comes from a misunderstanding of how USB works?
Thank you for your understanding and thank you for the explanation.
Undoubtedly I don't know anything about Teensy and itslibraries, I mistakenly thought I could use the usb host port similar to the micro usb port.
Thanks.
 
Sorry I have been playing with an RPI4 running Ubuntu 22.04 64 bits and a Teensy4.1
Note: I have the Teensy plugged into the RPI4

And was experimenting... Started this to also test out the new Beta build from Paul to see if it worked on this setup...

Now this this setup, I am using Arduino 1.8.19 and/or Arduino 2RC9 with latest beta of Teensyduino. posted today

I am using the not yet in builds mtp_teensy library: https://github.com/KurtE/MTP_Teensy

On the Teensy I am running the example sketch in MTP_Teensy Simplified Examples/Example_3_simple_SD
I built it using USB Type set to Serial + MTP Disk

Note: right now my RPI is not running headless, i.e. has display, mouse, keyboard.
In this state: The Teensy shows up in the Files app and you can browse to it and copy file to and from the SD card on the Teensy...

But I am assuming that you may not want to be setup with gui, so was experimenting:

If you do searches on net and find that the Teensy was mounted: https://askubuntu.com/questions/342319/where-are-mtp-mounted-devices-located-in-the-filesystem
at /run/user/1000/gvfs/mtp:host...

If you cd to that directory it appears like you can copy files from the SD to the RPI, but not from RPI to the Teensy...
It will give you an error something like: can not create ordinary file.

So took me a bit of time to find a command line that works:
So far my best luck is with the gio command: https://www.baeldung.com/linux/mounting-mtp-devices

So I was successful to copy a file to using the command: gio copy <source> <destination>

In my case, I tried it from command window, where I had cd down to the teensy and into sub-directory of sd card and tried:
gio copy ~/github/mtp_teensy/src/*.cpp .

And the files now are on the SD card...

There are other ways to connect up MTP drives to the RPI. which may work as well, but this gives at least one way to do it.
 
Many thanks for your help, that's great.
I would like to start testing your solution, unfortunately I haven't been able to get the USB Host Cable yet. It is not readily available but I have this available:

image0.jpeg

It's compatible?
Thanks
 
Back
Top