🛠 Deux ports USB simultanés : périphérique 480 Mbit/s ET hôte USB sur les pastilles

Teensy 4.0 · avance · Schéma : IMG-teensy-40-avance-7-teensy40-double-usb-host-esclave.svg — Teensy 4.0 retourné montrant les pastilles inférieures : deux pastilles du port USB host (3,048 × 1,016 mm), pads de coupure VIN/VUSB, connecteur SD 4 bits ; à gauche le câble micro-USB vers le PC (device), à droite un clavier USB branché sur le port host via un adaptateur avec interrupteur de limitation de courant.

Objectives

The Teensy 4.0 features TWO fully independent 480 Mbit/s USB ports: the standard micro-USB port in device mode (Serial, MIDI/MIDIx4/MIDIx16, Audio, Keyboard, Mouse, Joystick, Touchscreen, MTP, Flight Sim, RawHID…) and a host port on two SMD pads on the back of the board, driven by the USBHost_t36 library. You will read a USB keyboard on the host side while communicating with the Serial Monitor on the device side — and learn about the two electrical pitfalls of this port.

Hardware

Complete code

// Hôte USB (pastilles du bas) + périphérique série (micro-USB) en parallèle.
#include <USBHost_t36.h>

USBHost myusb;
USBHIDParser hid1(myusb);
KeyboardController clavier(myusb);

void setup {
  Serial.begin(9600);      // port DEVICE : le Moniteur passe par là
  myusb.begin;           // port HOST : les pastilles du dessous
  Serial.println(F("branchez un clavier USB"));
}

void loop {
  myusb.Task;            // machine à états de l'hôte — À NE PAS OUBLIER
  if (clavier) {
    // getKeys renvoie les touches actives ; voir les exemples
    // Keyboard de USBHost_t36 pour le mapping complet.
    static uint8_t touches[6];
    if (memcmp(touches, clavier.getKeys, 6)) {
      memcpy(touches, clavier.getKeys, 6);
      Serial.print(F("touches actives : "));
      for (int i = 0; i < 6; i++)
        Serial.print(touches[i], HEX), Serial.print(' ');
      Serial.println;
    }
  }
}

Expected verification

The Serial Monitor (via micro-USB) displays key codes as you type on the keyboard connected to the pads: the two USB worlds coexist on the same board. Note that the 4.0's host port is data-only: it has no onboard current management circuit (unlike the 4.1). Workflow bonus: the same device port supports CircuitPython — flash the official .hex (https://circuitpython.org/board/teensy40/) using Teensy Loader, and the board mounts as a USB drive; PJRC warns that CircuitPython does not cover all of the 4.0's hardware.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials