Forum Rule: Always post complete source code & details to reproduce any issue!
Page 1 of 2 1 2 LastLast
Results 1 to 25 of 37

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

  1. #1
    Junior Member
    Join Date
    Jul 2022
    Posts
    17

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

    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.

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,949
    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.

  3. #3
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    Thanks a lot for your answer.
    I can't use someyhing like: https://github.com/TeensyUser/doc/wiki/Serial?
    I will be happy even if I will be able to receive the data and then I will write them in the sd.

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,949
    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...

  5. #5
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    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.

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,358
    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.

  7. #7
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    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

  8. #8
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,892
    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:

    Click image for larger version. 

Name:	rpi.jpg 
Views:	5 
Size:	99.1 KB 
ID:	29007

    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 by luni; 07-25-2022 at 11:59 AM.

  9. #9
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    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.

  10. #10
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,892
    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 by luni; 07-25-2022 at 01:47 PM.

  11. #11
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    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.

  12. #12
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,892
    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.

  13. #13
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    Quote Originally Posted by luni View Post
    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.

  14. #14
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    865
    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).

  15. #15
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,949
    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.

  16. #16
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    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.

  17. #17
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    Quote Originally Posted by KurtE View Post
    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.

  18. #18
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,892
    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.

  19. #19
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    Quote Originally Posted by luni View Post
    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.

  20. #20
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,949
    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/3423...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.

  21. #21
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    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:

    Click image for larger version. 

Name:	image0.jpeg 
Views:	3 
Size:	60.9 KB 
ID:	29022

    It's compatible?
    Thanks

  22. #22
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    865
    I would like to start testing your solution, unfortunately I haven't been able to get the USB Host Cable
    Pimoroni have them, see here.

  23. #23
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    Thanks,
    just bought but it will take a long time for it to come to me.

  24. #24
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    865
    Where are you located?

  25. #25
    Junior Member
    Join Date
    Jul 2022
    Posts
    17
    I am in Italy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •