Raspberry Pi Pico + Arduino IDE + USB HOST - To read serial data

Hello,

I have a SAMD21J18A and RPI Pico 2020 ( RP2040 IC ) and I would like to read serial data which is sent through the controller ( SAMD21J18A ) through USB from RPI Pico by using Pico in USB Host Mode.

Since the peripheral is not a USB-HID device, I am not sure which library to start with.

Has anyone else tried to do so? Any hint will be appreciated. Thank you.

There is one USB host example for RP2040. It supports CDC in addition to MSC (Mass Storage) and HID. It works with the Pi Pico SDK.

Thanks for the reply.

But the link that you've shared doesn't have anything related to poll data from CDC devices or it does but I am not understanding it. In the main.c file, how are we going to receive data, they have used tuh_cdc_receive() function but I am still not able to understand the code. Please let me know if you do.

I suggest running the example and examining the printf output to see the execution flow.

  • tuh_mount_cb() is called when a USB CDC is plugged in or detected on start up.
  • tuh_cdc_receive() schedules a USB transfer to receive data.
  • tuh_cdc_xfer_isr() is called when data is received. The data is printed with a call to printf(). It then calls tuh_cdc_receive() to schedule another transfer for more data.

Warning 1: Calling printf inside a function called from an Interrupt Service Routine is OK for demo and example code but is bad idea for production. Replace the printf without your own code to process the incoming data. Since interrupts are disabled inside in ISR context, slow code (such as printf) may result in dropping incoming data.

Warning 2: There is another good reason not to call printf except in demo code. There is no guarantee the data is null terminated ASCII. The buffer could contain 0-64 bytes of binary data. The xferred_bytes parameter should be the amount of valid data in the buffer.

1 Like

Thanks a lot!

I am debugging the code by blinking the default led and not by printing it. I am currently able to detect that a device is mounted but the blink function in the tuh_cdc_xfer_isr() doesn't execute meaning it's not able to receive the data.

I am currently taking serial data from Adruino Mega 2560 by: serial.print("MyData"); ( or serial.write(); )
by setting the baud rate to 115200 but still not able to receive it from pico.

Any hint for this?

My main interest is USB HID keyboard. My experimental setup is

  • Raspberry Pi Pico board
  • USB OTG micro to USB host cable
  • Various USB keyboards, USB barcode reader
  • USB to UART TTL 3.3V serial cable
Pi Pico - USB OTG to USB host cable - USB keyboard/barcode reader
   |
USB to TTL serial cable
   |
Ubuntu Linux PC

The wire colors may vary depending on the manufacturer.

Pi Pico USB to TTL cable
GPO,U0Tx Rx, White
GP1,U0Rx Tx, Green
GND GND, Black
VBUS VBUS, 5V, Red

The printf output appears on the PC in a terminal program (minicom).

The TinyUSB HID host is picky about keyboards so I am trying to fix the driver for my keyboards but USB host code is hard to understand.

It is possible the host CDC ACM code is broken. USB host does not get the same attention as USB device.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.