PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 1 PV198 – One-chip Controllers USB PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 2 Content 1. What is USB 2. What is it used for 3. How does it work 4. FRDM-K66F USB 5. Application PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 3 What is USB ▪ USB – Universal Serial Bus ▪ Serial interface bus PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 4 What is it used for ▪ Communication between computer and peripherals ▪ Human Interface Devices (mouse, keyboard) ▪ Mass Storage ▪ Printers ▪ Personal Healthcare How does it work – High-level PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 5 ▪ Tiered star topology ▪ Host scheduled, token based ▪ Up to 127 devices on USB host controller ▪ Speed: Low, Full, High, Super ▪ 1.5 Mbit/s – 40 Gbit/s https://www.totalphase.com/support/articles/200349256-USB-Background How does it work PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 6 ▪ 7-bit device address ▪ Host starts communication ▪ Device can have multiple: ▪ Configurations ▪ Interfaces ▪ Interface settings (Alt. Settings) ▪ Endpoints ▪ Endpoint defines: ▪ Transfer direction ▪ Transfer type (control, interrupt, bulk, isochronous) https://www.totalphase.com/support/articles/200349256-USB-Background How does it work – Signaling PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 7 ▪ 4-24 signals (OTG – 5, Super Speed – 9, USB-C – 24) ▪ 2 data (D+, D-), GND, Vcc ▪ Asynchronous (sync sequence at the beginning of the frame) ▪ Half-duplex ▪ NRZI line code (bit stuffing after 6 ones) FRDM-K66F USB ▪ USB Full Speed OTG Controller ▪ USB High Speed OTG Controller ▪ Complies with USB specification rev 2.0 ▪ Host / Device mode ▪ DMA ▪ Interrupts PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 8 Application – Overview PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 9 ▪ The application behaves as an USB mouse, when the board is connected to a PC ▪ The cursor is moved by roll and pitch angles of the board ▪ Buttons SW2 and SW3 are used as mouse buttons Open SDA USB High Speed Application PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 10 ▪ Peripherals used: ▪ GPIO (GPIO_A and GPIO_D): buttons ▪ I2C: accelerometer → used to move cursor ▪ Middleware used: ▪ USB Application – USB configuration PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 11 ▪ Standard USB configurations are predefined in peripherals tool ▪ Check USB configuration in the tool Application – USB implementation PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 12 ▪ Endpoint defined with direction “in” (host in, device out) and “Interrupt” transfer type ▪ Host asks for data from device every “interval” ms → we have to prepare data to send Application – Input Report for USB HID mouse PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 13 Usage Bits Description Button 1 1 1 = pressed, 0 = not pressed Button 2 1 1 = pressed, 0 = not pressed Button 3 1 1 = pressed, 0 = not pressed Not Used 5 5 bits in first Byte are not used X 8 X movement – 8 bit signed integer (negative = left) Y 8 Y movement – 8 bit signed integer (negative = up) Wheel 8 Movement of the mouse wheel (negative = scroll down) Application – Source Code PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 14 ▪ Main: ▪ Configure (pins, clocks, peripherals – I2C, USB, accelerometer) ▪ Loop: Read data from accelerometer Update USB buffer ▪ Interrupts: ▪ GPIO: update USB buffer ▪ USB Callback: ▪ Send USB buffer Application ▪ Update USB buffer on every change (button pressed, board tilted) PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 15 Homework Implement one of these tasks: ▪ Easy: Use SW3 button to scroll down (scroll only once per button press) ▪ Medium: Create “trackball” by using Joystick from lecture 5 to control cursor, instead of accelerometer ▪ Hard: Implement medium Homework + choose USB configuration to act as Joystick, instead of Mouse PV198 – One-chip Controllers, USB / Dávid Danaj, Tomáš Klír 16