You are viewing an older revision! See the latest version

USBHostHub

Table of Contents

  1. Hello World
  2. Details
  3. Related

Mbed OS 2 and Mbed OS 5

This is the handbook for Mbed OS 2. If you’re working with Mbed OS 5, please see the Mbed OS 5 documentation.

Library in Beta!

This library is in Beta. If you have any problems using the USBHost library, please send a bug report at support@mbed.org

The USB Host connector should be attached to

  • p31 (D+), p32 (D-) and GND for the LPC1768
  • add two 15k resistors tied to GND on D+ and D-

Hello World

Import program

00001 #include "mbed.h"
00002 #include "USBHostKeyboard.h"
00003 #include "USBHostMouse.h"
00004 
00005 DigitalOut led(LED1);
00006 
00007 void onKey(uint8_t key) {
00008     printf("Key: %c\r\n", key);
00009 }
00010 
00011 void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
00012     printf("buttons: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
00013 }
00014 
00015 void keyboard_task(void const *) {
00016     
00017     USBHostKeyboard keyboard;
00018     
00019     while(1) {
00020         // try to connect a USB keyboard
00021         while(!keyboard.connect())
00022             Thread::wait(500);
00023     
00024         // when connected, attach handler called on keyboard event
00025         keyboard.attach(onKey);
00026         
00027         // wait until the keyboard is disconnected
00028         while(keyboard.connected())
00029             Thread::wait(500);
00030     }
00031 }
00032 
00033 void mouse_task(void const *) {
00034     
00035     USBHostMouse mouse;
00036     
00037     while(1) {
00038         // try to connect a USB mouse
00039         while(!mouse.connect())
00040             Thread::wait(500);
00041     
00042         // when connected, attach handler called on mouse event
00043         mouse.attachEvent(onMouseEvent);
00044         
00045         // wait until the mouse is disconnected
00046         while(mouse.connected())
00047             Thread::wait(500);
00048     }
00049 }
00050 
00051 int main() {
00052     Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4);
00053     Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4);
00054     while(1) {
00055         led=!led;
00056         Thread::wait(500);
00057     }
00058 }

Troobleshooting

If your mbed board is automatically resetted when you plug a USB device, you should consider to use an external power supply

Details

As you can see there is no instance of USBHostHub in the previous code. All the Hubs are automatically enumerated by the usb thread.


All wikipages