USB device stack, with KL25Z fixes for USB 3.0 hosts and sleep/resume interrupt handling

Dependents:   frdm_Slider_Keyboard idd_hw2_figlax_PanType idd_hw2_appachu_finger_chording idd_hw3_AngieWangAntonioDeLimaFernandesDanielLim_BladeSymphony ... more

Fork of USBDevice by mbed official

This is an overhauled version of the standard mbed USB device-side driver library, with bug fixes for KL25Z devices. It greatly improves reliability and stability of USB on the KL25Z, especially with devices using multiple endpoints concurrently.

I've had some nagging problems with the base mbed implementation for a long time, manifesting as occasional random disconnects that required rebooting the device. Recently (late 2015), I started implementing a USB device on the KL25Z that used multiple endpoints, and suddenly the nagging, occasional problems turned into frequent and predictable crashes. This forced me to delve into the USB stack and figure out what was really going on. Happily, the frequent crashes made it possible to track down and fix the problems. This new version is working very reliably in my testing - the random disconnects seem completely eradicated, even under very stressful conditions for the device.

Summary

  • Overall stability improvements
  • USB 3.0 host support
  • Stalled endpoint fixes
  • Sleep/resume notifications
  • Smaller memory footprint
  • General code cleanup

Update - 2/15/2016

My recent fixes introduced a new problem that made the initial connection fail most of the time on certain hosts. It's not clear if the common thread was a particular type of motherboard or USB chip set, or a specific version of Windows, or what, but several people ran into it. We tracked the problem down to the "stall" fixes in the earlier updates, which we now know weren't quite the right fixes after all. The latest update (2/15/2016) fixes this. It has new and improved "unstall" handling that so far works well with diverse hosts.

Race conditions and overall stability

The base mbed KL25Z implementation has a lot of problems with "race conditions" - timing problems that can happen when hardware interrupts occur at inopportune moments. The library shares a bunch of static variable data between interrupt handler context and regular application context. This isn't automatically a bad thing, but it does require careful coordination to make sure that the interrupt handler doesn't corrupt data that the other code was in the middle of updating when an interrupt occurs. The base mbed code, though, doesn't do any of the necessary coordination. This makes it kind of amazing that the base code worked at all for anyone, but I guess the interrupt rate is low enough in most applications that the glitch rate was below anyone's threshold to seriously investigate.

This overhaul adds the necessary coordination for the interrupt handlers to protect against these data corruptions. I think it's very solid now, and hopefully entirely free of the numerous race conditions in the old code. It's always hard to be certain that you've fixed every possible bug like this because they strike (effectively) at random, but I'm pretty confident: my test application was reliably able to trigger glitches in the base code in a matter of minutes, but the same application (with the overhauled library) now runs for days on end without dropping the connection.

Stalled endpoint fixes

USB has a standard way of handling communications errors called a "stall", which basically puts the connection into an error mode to let both sides know that they need to reset their internal states and sync up again. The original mbed version of the USB device library doesn't seem to have the necessary code to recover from this condition properly. The KL25Z hardware does some of the work, but it also seems to require the software to take some steps to "un-stall" the connection. (I keep saying "seems to" because the hardware reference material is very sketchy about all of this. Most of what I've figured out is from observing the device in action with a Windows host.) This new version adds code to do the necessary re-syncing and get the connection going again, automatically, and transparently to the user.

USB 3.0 Hosts

The original mbed code sometimes didn't work when connecting to hosts with USB 3.0 ports. This didn't affect every host, but it affected many of them. The common element seemed to be the Intel Haswell chip set on the host, but there may be other chip sets affected as well. In any case, the problem affected many PCs from the Windows 7 and 8 generation, as well as many Macs. It was possible to work around the problem by avoiding USB 3.0 ports - you could use a USB 2 port on the host, or plug a USB 2 hub between the host and device. But I wanted to just fix the problem and eliminate the need for such workarounds. This modified version of the library has such a fix, which so far has worked for everyone who's tried.

Sleep/resume notifications

This modified version also contains an innocuous change to the KL25Z USB HAL code to handle sleep and resume interrupts with calls to suspendStateChanged(). The original KL25Z code omitted these calls (and in fact didn't even enable the interrupts), but I think this was an unintentional oversight - the notifier function is part of the generic API, and other supported boards all implement it. I use this feature in my own application so that I can distinguish sleep mode from actual disconnects and handle the two conditions correctly.

Smaller memory footprint

The base mbed version of the code allocates twice as much memory for USB buffers as it really needed to. It looks like the original developers intended to implement the KL25Z USB hardware's built-in double-buffering mechanism, but they ultimately abandoned that effort. But they left in the double memory allocation. This version removes that and allocates only what's actually needed. The USB buffers aren't that big (128 bytes per endpoint), so this doesn't save a ton of memory, but even a little memory is pretty precious on this machine given that it only has 16K.

(I did look into adding the double-buffering support that the original developers abandoned, but after some experimentation I decided they were right to skip it. It just doesn't seem to mesh well with the design of the rest of the mbed USB code. I think it would take a major rewrite to make it work, and it doesn't seem worth the effort given that most applications don't need it - it would only benefit applications that are moving so much data through USB that they're pushing the limits of the CPU. And even for those, I think it would be a lot simpler to build a purely software-based buffer rotation mechanism.)

General code cleanup

The KL25Z HAL code in this version has greatly expanded commentary and a lot of general cleanup. Some of the hardware constants were given the wrong symbolic names (e.g., EVEN and ODD were reversed), and many were just missing (written as hard-coded numbers without explanation). I fixed the misnomers and added symbolic names for formerly anonymous numbers. Hopefully the next person who has to overhaul this code will at least have an easier time understanding what I thought I was doing!

Committer:
mjr
Date:
Fri Mar 17 22:01:47 2017 +0000
Revision:
54:2e181d51495a
Parent:
50:946bc763c068
Comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:80ab0d068708 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 1:80ab0d068708 2 *
samux 1:80ab0d068708 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:80ab0d068708 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 1:80ab0d068708 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 1:80ab0d068708 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 1:80ab0d068708 7 * Software is furnished to do so, subject to the following conditions:
samux 1:80ab0d068708 8 *
samux 1:80ab0d068708 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:80ab0d068708 10 * substantial portions of the Software.
samux 1:80ab0d068708 11 *
samux 1:80ab0d068708 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:80ab0d068708 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:80ab0d068708 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:80ab0d068708 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:80ab0d068708 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:80ab0d068708 17 */
samux 1:80ab0d068708 18
samux 1:80ab0d068708 19 #include "stdint.h"
samux 1:80ab0d068708 20
samux 1:80ab0d068708 21 #include "USBKeyboard.h"
samux 1:80ab0d068708 22
samux 1:80ab0d068708 23 #define REPORT_ID_KEYBOARD 1
samux 1:80ab0d068708 24 #define REPORT_ID_VOLUME 3
samux 1:80ab0d068708 25
samux 1:80ab0d068708 26
samux 1:80ab0d068708 27 typedef struct {
samux 1:80ab0d068708 28 unsigned char usage;
samux 1:80ab0d068708 29 unsigned char modifier;
samux 1:80ab0d068708 30 } KEYMAP;
samux 1:80ab0d068708 31
samux 1:80ab0d068708 32 #ifdef US_KEYBOARD
samux 1:80ab0d068708 33 /* US keyboard (as HID standard) */
samux 1:80ab0d068708 34 #define KEYMAP_SIZE (152)
samux 1:80ab0d068708 35 const KEYMAP keymap[KEYMAP_SIZE] = {
samux 1:80ab0d068708 36 {0, 0}, /* NUL */
samux 1:80ab0d068708 37 {0, 0}, /* SOH */
samux 1:80ab0d068708 38 {0, 0}, /* STX */
samux 1:80ab0d068708 39 {0, 0}, /* ETX */
samux 1:80ab0d068708 40 {0, 0}, /* EOT */
samux 1:80ab0d068708 41 {0, 0}, /* ENQ */
samux 1:80ab0d068708 42 {0, 0}, /* ACK */
samux 1:80ab0d068708 43 {0, 0}, /* BEL */
samux 1:80ab0d068708 44 {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */
samux 1:80ab0d068708 45 {0x2b, 0}, /* TAB */ /* Keyboard Tab */
samux 1:80ab0d068708 46 {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */
samux 1:80ab0d068708 47 {0, 0}, /* VT */
samux 1:80ab0d068708 48 {0, 0}, /* FF */
samux 1:80ab0d068708 49 {0, 0}, /* CR */
samux 1:80ab0d068708 50 {0, 0}, /* SO */
samux 1:80ab0d068708 51 {0, 0}, /* SI */
samux 1:80ab0d068708 52 {0, 0}, /* DEL */
samux 1:80ab0d068708 53 {0, 0}, /* DC1 */
samux 1:80ab0d068708 54 {0, 0}, /* DC2 */
samux 1:80ab0d068708 55 {0, 0}, /* DC3 */
samux 1:80ab0d068708 56 {0, 0}, /* DC4 */
samux 1:80ab0d068708 57 {0, 0}, /* NAK */
samux 1:80ab0d068708 58 {0, 0}, /* SYN */
samux 1:80ab0d068708 59 {0, 0}, /* ETB */
samux 1:80ab0d068708 60 {0, 0}, /* CAN */
samux 1:80ab0d068708 61 {0, 0}, /* EM */
samux 1:80ab0d068708 62 {0, 0}, /* SUB */
samux 1:80ab0d068708 63 {0, 0}, /* ESC */
samux 1:80ab0d068708 64 {0, 0}, /* FS */
samux 1:80ab0d068708 65 {0, 0}, /* GS */
samux 1:80ab0d068708 66 {0, 0}, /* RS */
samux 1:80ab0d068708 67 {0, 0}, /* US */
samux 1:80ab0d068708 68 {0x2c, 0}, /* */
samux 1:80ab0d068708 69 {0x1e, KEY_SHIFT}, /* ! */
samux 1:80ab0d068708 70 {0x34, KEY_SHIFT}, /* " */
samux 1:80ab0d068708 71 {0x20, KEY_SHIFT}, /* # */
samux 1:80ab0d068708 72 {0x21, KEY_SHIFT}, /* $ */
samux 1:80ab0d068708 73 {0x22, KEY_SHIFT}, /* % */
samux 1:80ab0d068708 74 {0x24, KEY_SHIFT}, /* & */
samux 1:80ab0d068708 75 {0x34, 0}, /* ' */
samux 1:80ab0d068708 76 {0x26, KEY_SHIFT}, /* ( */
samux 1:80ab0d068708 77 {0x27, KEY_SHIFT}, /* ) */
samux 1:80ab0d068708 78 {0x25, KEY_SHIFT}, /* * */
samux 1:80ab0d068708 79 {0x2e, KEY_SHIFT}, /* + */
samux 1:80ab0d068708 80 {0x36, 0}, /* , */
samux 1:80ab0d068708 81 {0x2d, 0}, /* - */
samux 1:80ab0d068708 82 {0x37, 0}, /* . */
samux 1:80ab0d068708 83 {0x38, 0}, /* / */
samux 1:80ab0d068708 84 {0x27, 0}, /* 0 */
samux 1:80ab0d068708 85 {0x1e, 0}, /* 1 */
samux 1:80ab0d068708 86 {0x1f, 0}, /* 2 */
samux 1:80ab0d068708 87 {0x20, 0}, /* 3 */
samux 1:80ab0d068708 88 {0x21, 0}, /* 4 */
samux 1:80ab0d068708 89 {0x22, 0}, /* 5 */
samux 1:80ab0d068708 90 {0x23, 0}, /* 6 */
samux 1:80ab0d068708 91 {0x24, 0}, /* 7 */
samux 1:80ab0d068708 92 {0x25, 0}, /* 8 */
samux 1:80ab0d068708 93 {0x26, 0}, /* 9 */
samux 1:80ab0d068708 94 {0x33, KEY_SHIFT}, /* : */
samux 1:80ab0d068708 95 {0x33, 0}, /* ; */
samux 1:80ab0d068708 96 {0x36, KEY_SHIFT}, /* < */
samux 1:80ab0d068708 97 {0x2e, 0}, /* = */
samux 1:80ab0d068708 98 {0x37, KEY_SHIFT}, /* > */
samux 1:80ab0d068708 99 {0x38, KEY_SHIFT}, /* ? */
samux 1:80ab0d068708 100 {0x1f, KEY_SHIFT}, /* @ */
samux 1:80ab0d068708 101 {0x04, KEY_SHIFT}, /* A */
samux 1:80ab0d068708 102 {0x05, KEY_SHIFT}, /* B */
samux 1:80ab0d068708 103 {0x06, KEY_SHIFT}, /* C */
samux 1:80ab0d068708 104 {0x07, KEY_SHIFT}, /* D */
samux 1:80ab0d068708 105 {0x08, KEY_SHIFT}, /* E */
samux 1:80ab0d068708 106 {0x09, KEY_SHIFT}, /* F */
samux 1:80ab0d068708 107 {0x0a, KEY_SHIFT}, /* G */
samux 1:80ab0d068708 108 {0x0b, KEY_SHIFT}, /* H */
samux 1:80ab0d068708 109 {0x0c, KEY_SHIFT}, /* I */
samux 1:80ab0d068708 110 {0x0d, KEY_SHIFT}, /* J */
samux 1:80ab0d068708 111 {0x0e, KEY_SHIFT}, /* K */
samux 1:80ab0d068708 112 {0x0f, KEY_SHIFT}, /* L */
samux 1:80ab0d068708 113 {0x10, KEY_SHIFT}, /* M */
samux 1:80ab0d068708 114 {0x11, KEY_SHIFT}, /* N */
samux 1:80ab0d068708 115 {0x12, KEY_SHIFT}, /* O */
samux 1:80ab0d068708 116 {0x13, KEY_SHIFT}, /* P */
samux 1:80ab0d068708 117 {0x14, KEY_SHIFT}, /* Q */
samux 1:80ab0d068708 118 {0x15, KEY_SHIFT}, /* R */
samux 1:80ab0d068708 119 {0x16, KEY_SHIFT}, /* S */
samux 1:80ab0d068708 120 {0x17, KEY_SHIFT}, /* T */
samux 1:80ab0d068708 121 {0x18, KEY_SHIFT}, /* U */
samux 1:80ab0d068708 122 {0x19, KEY_SHIFT}, /* V */
samux 1:80ab0d068708 123 {0x1a, KEY_SHIFT}, /* W */
samux 1:80ab0d068708 124 {0x1b, KEY_SHIFT}, /* X */
samux 1:80ab0d068708 125 {0x1c, KEY_SHIFT}, /* Y */
samux 1:80ab0d068708 126 {0x1d, KEY_SHIFT}, /* Z */
samux 1:80ab0d068708 127 {0x2f, 0}, /* [ */
samux 1:80ab0d068708 128 {0x31, 0}, /* \ */
samux 1:80ab0d068708 129 {0x30, 0}, /* ] */
samux 1:80ab0d068708 130 {0x23, KEY_SHIFT}, /* ^ */
samux 1:80ab0d068708 131 {0x2d, KEY_SHIFT}, /* _ */
samux 1:80ab0d068708 132 {0x35, 0}, /* ` */
samux 1:80ab0d068708 133 {0x04, 0}, /* a */
samux 1:80ab0d068708 134 {0x05, 0}, /* b */
samux 1:80ab0d068708 135 {0x06, 0}, /* c */
samux 1:80ab0d068708 136 {0x07, 0}, /* d */
samux 1:80ab0d068708 137 {0x08, 0}, /* e */
samux 1:80ab0d068708 138 {0x09, 0}, /* f */
samux 1:80ab0d068708 139 {0x0a, 0}, /* g */
samux 1:80ab0d068708 140 {0x0b, 0}, /* h */
samux 1:80ab0d068708 141 {0x0c, 0}, /* i */
samux 1:80ab0d068708 142 {0x0d, 0}, /* j */
samux 1:80ab0d068708 143 {0x0e, 0}, /* k */
samux 1:80ab0d068708 144 {0x0f, 0}, /* l */
samux 1:80ab0d068708 145 {0x10, 0}, /* m */
samux 1:80ab0d068708 146 {0x11, 0}, /* n */
samux 1:80ab0d068708 147 {0x12, 0}, /* o */
samux 1:80ab0d068708 148 {0x13, 0}, /* p */
samux 1:80ab0d068708 149 {0x14, 0}, /* q */
samux 1:80ab0d068708 150 {0x15, 0}, /* r */
samux 1:80ab0d068708 151 {0x16, 0}, /* s */
samux 1:80ab0d068708 152 {0x17, 0}, /* t */
samux 1:80ab0d068708 153 {0x18, 0}, /* u */
samux 1:80ab0d068708 154 {0x19, 0}, /* v */
samux 1:80ab0d068708 155 {0x1a, 0}, /* w */
samux 1:80ab0d068708 156 {0x1b, 0}, /* x */
samux 1:80ab0d068708 157 {0x1c, 0}, /* y */
samux 1:80ab0d068708 158 {0x1d, 0}, /* z */
samux 1:80ab0d068708 159 {0x2f, KEY_SHIFT}, /* { */
samux 1:80ab0d068708 160 {0x31, KEY_SHIFT}, /* | */
samux 1:80ab0d068708 161 {0x30, KEY_SHIFT}, /* } */
samux 1:80ab0d068708 162 {0x35, KEY_SHIFT}, /* ~ */
samux 1:80ab0d068708 163 {0,0}, /* DEL */
samux 1:80ab0d068708 164
samux 1:80ab0d068708 165 {0x3a, 0}, /* F1 */
samux 1:80ab0d068708 166 {0x3b, 0}, /* F2 */
samux 1:80ab0d068708 167 {0x3c, 0}, /* F3 */
samux 1:80ab0d068708 168 {0x3d, 0}, /* F4 */
samux 1:80ab0d068708 169 {0x3e, 0}, /* F5 */
samux 1:80ab0d068708 170 {0x3f, 0}, /* F6 */
samux 1:80ab0d068708 171 {0x40, 0}, /* F7 */
samux 1:80ab0d068708 172 {0x41, 0}, /* F8 */
samux 1:80ab0d068708 173 {0x42, 0}, /* F9 */
samux 1:80ab0d068708 174 {0x43, 0}, /* F10 */
samux 1:80ab0d068708 175 {0x44, 0}, /* F11 */
samux 1:80ab0d068708 176 {0x45, 0}, /* F12 */
samux 1:80ab0d068708 177
samux 1:80ab0d068708 178 {0x46, 0}, /* PRINT_SCREEN */
samux 1:80ab0d068708 179 {0x47, 0}, /* SCROLL_LOCK */
samux 1:80ab0d068708 180 {0x39, 0}, /* CAPS_LOCK */
samux 1:80ab0d068708 181 {0x53, 0}, /* NUM_LOCK */
samux 1:80ab0d068708 182 {0x49, 0}, /* INSERT */
samux 1:80ab0d068708 183 {0x4a, 0}, /* HOME */
samux 1:80ab0d068708 184 {0x4b, 0}, /* PAGE_UP */
samux 1:80ab0d068708 185 {0x4e, 0}, /* PAGE_DOWN */
mbed_official 25:7c72828865f3 186
samux 1:80ab0d068708 187 {0x4f, 0}, /* RIGHT_ARROW */
samux 1:80ab0d068708 188 {0x50, 0}, /* LEFT_ARROW */
samux 1:80ab0d068708 189 {0x51, 0}, /* DOWN_ARROW */
samux 1:80ab0d068708 190 {0x52, 0}, /* UP_ARROW */
samux 1:80ab0d068708 191 };
samux 1:80ab0d068708 192
samux 1:80ab0d068708 193 #else
samux 1:80ab0d068708 194 /* UK keyboard */
samux 1:80ab0d068708 195 #define KEYMAP_SIZE (152)
samux 1:80ab0d068708 196 const KEYMAP keymap[KEYMAP_SIZE] = {
samux 1:80ab0d068708 197 {0, 0}, /* NUL */
samux 1:80ab0d068708 198 {0, 0}, /* SOH */
samux 1:80ab0d068708 199 {0, 0}, /* STX */
samux 1:80ab0d068708 200 {0, 0}, /* ETX */
samux 1:80ab0d068708 201 {0, 0}, /* EOT */
samux 1:80ab0d068708 202 {0, 0}, /* ENQ */
samux 1:80ab0d068708 203 {0, 0}, /* ACK */
samux 1:80ab0d068708 204 {0, 0}, /* BEL */
samux 1:80ab0d068708 205 {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */
samux 1:80ab0d068708 206 {0x2b, 0}, /* TAB */ /* Keyboard Tab */
samux 1:80ab0d068708 207 {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */
samux 1:80ab0d068708 208 {0, 0}, /* VT */
samux 1:80ab0d068708 209 {0, 0}, /* FF */
samux 1:80ab0d068708 210 {0, 0}, /* CR */
samux 1:80ab0d068708 211 {0, 0}, /* SO */
samux 1:80ab0d068708 212 {0, 0}, /* SI */
samux 1:80ab0d068708 213 {0, 0}, /* DEL */
samux 1:80ab0d068708 214 {0, 0}, /* DC1 */
samux 1:80ab0d068708 215 {0, 0}, /* DC2 */
samux 1:80ab0d068708 216 {0, 0}, /* DC3 */
samux 1:80ab0d068708 217 {0, 0}, /* DC4 */
samux 1:80ab0d068708 218 {0, 0}, /* NAK */
samux 1:80ab0d068708 219 {0, 0}, /* SYN */
samux 1:80ab0d068708 220 {0, 0}, /* ETB */
samux 1:80ab0d068708 221 {0, 0}, /* CAN */
samux 1:80ab0d068708 222 {0, 0}, /* EM */
samux 1:80ab0d068708 223 {0, 0}, /* SUB */
samux 1:80ab0d068708 224 {0, 0}, /* ESC */
samux 1:80ab0d068708 225 {0, 0}, /* FS */
samux 1:80ab0d068708 226 {0, 0}, /* GS */
samux 1:80ab0d068708 227 {0, 0}, /* RS */
samux 1:80ab0d068708 228 {0, 0}, /* US */
samux 1:80ab0d068708 229 {0x2c, 0}, /* */
samux 1:80ab0d068708 230 {0x1e, KEY_SHIFT}, /* ! */
samux 1:80ab0d068708 231 {0x1f, KEY_SHIFT}, /* " */
samux 1:80ab0d068708 232 {0x32, 0}, /* # */
samux 1:80ab0d068708 233 {0x21, KEY_SHIFT}, /* $ */
samux 1:80ab0d068708 234 {0x22, KEY_SHIFT}, /* % */
samux 1:80ab0d068708 235 {0x24, KEY_SHIFT}, /* & */
samux 1:80ab0d068708 236 {0x34, 0}, /* ' */
samux 1:80ab0d068708 237 {0x26, KEY_SHIFT}, /* ( */
samux 1:80ab0d068708 238 {0x27, KEY_SHIFT}, /* ) */
samux 1:80ab0d068708 239 {0x25, KEY_SHIFT}, /* * */
samux 1:80ab0d068708 240 {0x2e, KEY_SHIFT}, /* + */
samux 1:80ab0d068708 241 {0x36, 0}, /* , */
samux 1:80ab0d068708 242 {0x2d, 0}, /* - */
samux 1:80ab0d068708 243 {0x37, 0}, /* . */
samux 1:80ab0d068708 244 {0x38, 0}, /* / */
samux 1:80ab0d068708 245 {0x27, 0}, /* 0 */
samux 1:80ab0d068708 246 {0x1e, 0}, /* 1 */
samux 1:80ab0d068708 247 {0x1f, 0}, /* 2 */
samux 1:80ab0d068708 248 {0x20, 0}, /* 3 */
samux 1:80ab0d068708 249 {0x21, 0}, /* 4 */
samux 1:80ab0d068708 250 {0x22, 0}, /* 5 */
samux 1:80ab0d068708 251 {0x23, 0}, /* 6 */
samux 1:80ab0d068708 252 {0x24, 0}, /* 7 */
samux 1:80ab0d068708 253 {0x25, 0}, /* 8 */
samux 1:80ab0d068708 254 {0x26, 0}, /* 9 */
samux 1:80ab0d068708 255 {0x33, KEY_SHIFT}, /* : */
samux 1:80ab0d068708 256 {0x33, 0}, /* ; */
samux 1:80ab0d068708 257 {0x36, KEY_SHIFT}, /* < */
samux 1:80ab0d068708 258 {0x2e, 0}, /* = */
samux 1:80ab0d068708 259 {0x37, KEY_SHIFT}, /* > */
samux 1:80ab0d068708 260 {0x38, KEY_SHIFT}, /* ? */
samux 1:80ab0d068708 261 {0x34, KEY_SHIFT}, /* @ */
samux 1:80ab0d068708 262 {0x04, KEY_SHIFT}, /* A */
samux 1:80ab0d068708 263 {0x05, KEY_SHIFT}, /* B */
samux 1:80ab0d068708 264 {0x06, KEY_SHIFT}, /* C */
samux 1:80ab0d068708 265 {0x07, KEY_SHIFT}, /* D */
samux 1:80ab0d068708 266 {0x08, KEY_SHIFT}, /* E */
samux 1:80ab0d068708 267 {0x09, KEY_SHIFT}, /* F */
samux 1:80ab0d068708 268 {0x0a, KEY_SHIFT}, /* G */
samux 1:80ab0d068708 269 {0x0b, KEY_SHIFT}, /* H */
samux 1:80ab0d068708 270 {0x0c, KEY_SHIFT}, /* I */
samux 1:80ab0d068708 271 {0x0d, KEY_SHIFT}, /* J */
samux 1:80ab0d068708 272 {0x0e, KEY_SHIFT}, /* K */
samux 1:80ab0d068708 273 {0x0f, KEY_SHIFT}, /* L */
samux 1:80ab0d068708 274 {0x10, KEY_SHIFT}, /* M */
samux 1:80ab0d068708 275 {0x11, KEY_SHIFT}, /* N */
samux 1:80ab0d068708 276 {0x12, KEY_SHIFT}, /* O */
samux 1:80ab0d068708 277 {0x13, KEY_SHIFT}, /* P */
samux 1:80ab0d068708 278 {0x14, KEY_SHIFT}, /* Q */
samux 1:80ab0d068708 279 {0x15, KEY_SHIFT}, /* R */
samux 1:80ab0d068708 280 {0x16, KEY_SHIFT}, /* S */
samux 1:80ab0d068708 281 {0x17, KEY_SHIFT}, /* T */
samux 1:80ab0d068708 282 {0x18, KEY_SHIFT}, /* U */
samux 1:80ab0d068708 283 {0x19, KEY_SHIFT}, /* V */
samux 1:80ab0d068708 284 {0x1a, KEY_SHIFT}, /* W */
samux 1:80ab0d068708 285 {0x1b, KEY_SHIFT}, /* X */
samux 1:80ab0d068708 286 {0x1c, KEY_SHIFT}, /* Y */
samux 1:80ab0d068708 287 {0x1d, KEY_SHIFT}, /* Z */
samux 1:80ab0d068708 288 {0x2f, 0}, /* [ */
samux 1:80ab0d068708 289 {0x64, 0}, /* \ */
samux 1:80ab0d068708 290 {0x30, 0}, /* ] */
samux 1:80ab0d068708 291 {0x23, KEY_SHIFT}, /* ^ */
samux 1:80ab0d068708 292 {0x2d, KEY_SHIFT}, /* _ */
samux 1:80ab0d068708 293 {0x35, 0}, /* ` */
samux 1:80ab0d068708 294 {0x04, 0}, /* a */
samux 1:80ab0d068708 295 {0x05, 0}, /* b */
samux 1:80ab0d068708 296 {0x06, 0}, /* c */
samux 1:80ab0d068708 297 {0x07, 0}, /* d */
samux 1:80ab0d068708 298 {0x08, 0}, /* e */
samux 1:80ab0d068708 299 {0x09, 0}, /* f */
samux 1:80ab0d068708 300 {0x0a, 0}, /* g */
samux 1:80ab0d068708 301 {0x0b, 0}, /* h */
samux 1:80ab0d068708 302 {0x0c, 0}, /* i */
samux 1:80ab0d068708 303 {0x0d, 0}, /* j */
samux 1:80ab0d068708 304 {0x0e, 0}, /* k */
samux 1:80ab0d068708 305 {0x0f, 0}, /* l */
samux 1:80ab0d068708 306 {0x10, 0}, /* m */
samux 1:80ab0d068708 307 {0x11, 0}, /* n */
samux 1:80ab0d068708 308 {0x12, 0}, /* o */
samux 1:80ab0d068708 309 {0x13, 0}, /* p */
samux 1:80ab0d068708 310 {0x14, 0}, /* q */
samux 1:80ab0d068708 311 {0x15, 0}, /* r */
samux 1:80ab0d068708 312 {0x16, 0}, /* s */
samux 1:80ab0d068708 313 {0x17, 0}, /* t */
samux 1:80ab0d068708 314 {0x18, 0}, /* u */
samux 1:80ab0d068708 315 {0x19, 0}, /* v */
samux 1:80ab0d068708 316 {0x1a, 0}, /* w */
samux 1:80ab0d068708 317 {0x1b, 0}, /* x */
samux 1:80ab0d068708 318 {0x1c, 0}, /* y */
samux 1:80ab0d068708 319 {0x1d, 0}, /* z */
samux 1:80ab0d068708 320 {0x2f, KEY_SHIFT}, /* { */
samux 1:80ab0d068708 321 {0x64, KEY_SHIFT}, /* | */
samux 1:80ab0d068708 322 {0x30, KEY_SHIFT}, /* } */
samux 1:80ab0d068708 323 {0x32, KEY_SHIFT}, /* ~ */
samux 1:80ab0d068708 324 {0,0}, /* DEL */
samux 1:80ab0d068708 325
samux 1:80ab0d068708 326 {0x3a, 0}, /* F1 */
samux 1:80ab0d068708 327 {0x3b, 0}, /* F2 */
samux 1:80ab0d068708 328 {0x3c, 0}, /* F3 */
samux 1:80ab0d068708 329 {0x3d, 0}, /* F4 */
samux 1:80ab0d068708 330 {0x3e, 0}, /* F5 */
samux 1:80ab0d068708 331 {0x3f, 0}, /* F6 */
samux 1:80ab0d068708 332 {0x40, 0}, /* F7 */
samux 1:80ab0d068708 333 {0x41, 0}, /* F8 */
samux 1:80ab0d068708 334 {0x42, 0}, /* F9 */
samux 1:80ab0d068708 335 {0x43, 0}, /* F10 */
samux 1:80ab0d068708 336 {0x44, 0}, /* F11 */
samux 1:80ab0d068708 337 {0x45, 0}, /* F12 */
samux 1:80ab0d068708 338
samux 1:80ab0d068708 339 {0x46, 0}, /* PRINT_SCREEN */
samux 1:80ab0d068708 340 {0x47, 0}, /* SCROLL_LOCK */
samux 1:80ab0d068708 341 {0x39, 0}, /* CAPS_LOCK */
samux 1:80ab0d068708 342 {0x53, 0}, /* NUM_LOCK */
samux 1:80ab0d068708 343 {0x49, 0}, /* INSERT */
samux 1:80ab0d068708 344 {0x4a, 0}, /* HOME */
samux 1:80ab0d068708 345 {0x4b, 0}, /* PAGE_UP */
samux 1:80ab0d068708 346 {0x4e, 0}, /* PAGE_DOWN */
mbed_official 25:7c72828865f3 347
samux 1:80ab0d068708 348 {0x4f, 0}, /* RIGHT_ARROW */
samux 1:80ab0d068708 349 {0x50, 0}, /* LEFT_ARROW */
samux 1:80ab0d068708 350 {0x51, 0}, /* DOWN_ARROW */
samux 1:80ab0d068708 351 {0x52, 0}, /* UP_ARROW */
samux 1:80ab0d068708 352 };
samux 1:80ab0d068708 353 #endif
samux 1:80ab0d068708 354
mjr 50:946bc763c068 355 const uint8_t *USBKeyboard::reportDesc(int idx, uint16_t &len)
mjr 50:946bc763c068 356 {
mjr 49:03527ce6840e 357 static const uint8_t reportDescriptor[] = {
samux 1:80ab0d068708 358 USAGE_PAGE(1), 0x01, // Generic Desktop
samux 1:80ab0d068708 359 USAGE(1), 0x06, // Keyboard
samux 1:80ab0d068708 360 COLLECTION(1), 0x01, // Application
samux 1:80ab0d068708 361 REPORT_ID(1), REPORT_ID_KEYBOARD,
samux 1:80ab0d068708 362
samux 1:80ab0d068708 363 USAGE_PAGE(1), 0x07, // Key Codes
samux 1:80ab0d068708 364 USAGE_MINIMUM(1), 0xE0,
samux 1:80ab0d068708 365 USAGE_MAXIMUM(1), 0xE7,
samux 1:80ab0d068708 366 LOGICAL_MINIMUM(1), 0x00,
samux 1:80ab0d068708 367 LOGICAL_MAXIMUM(1), 0x01,
samux 1:80ab0d068708 368 REPORT_SIZE(1), 0x01,
samux 1:80ab0d068708 369 REPORT_COUNT(1), 0x08,
samux 1:80ab0d068708 370 INPUT(1), 0x02, // Data, Variable, Absolute
samux 1:80ab0d068708 371 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 372 REPORT_SIZE(1), 0x08,
samux 1:80ab0d068708 373 INPUT(1), 0x01, // Constant
samux 1:80ab0d068708 374
samux 1:80ab0d068708 375
samux 1:80ab0d068708 376 REPORT_COUNT(1), 0x05,
samux 1:80ab0d068708 377 REPORT_SIZE(1), 0x01,
samux 1:80ab0d068708 378 USAGE_PAGE(1), 0x08, // LEDs
samux 1:80ab0d068708 379 USAGE_MINIMUM(1), 0x01,
samux 1:80ab0d068708 380 USAGE_MAXIMUM(1), 0x05,
samux 1:80ab0d068708 381 OUTPUT(1), 0x02, // Data, Variable, Absolute
samux 1:80ab0d068708 382 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 383 REPORT_SIZE(1), 0x03,
samux 1:80ab0d068708 384 OUTPUT(1), 0x01, // Constant
samux 1:80ab0d068708 385
samux 1:80ab0d068708 386
samux 1:80ab0d068708 387 REPORT_COUNT(1), 0x06,
samux 1:80ab0d068708 388 REPORT_SIZE(1), 0x08,
samux 1:80ab0d068708 389 LOGICAL_MINIMUM(1), 0x00,
samux 1:80ab0d068708 390 LOGICAL_MAXIMUM(1), 0x65,
samux 1:80ab0d068708 391 USAGE_PAGE(1), 0x07, // Key Codes
samux 1:80ab0d068708 392 USAGE_MINIMUM(1), 0x00,
samux 1:80ab0d068708 393 USAGE_MAXIMUM(1), 0x65,
samux 1:80ab0d068708 394 INPUT(1), 0x00, // Data, Array
samux 1:80ab0d068708 395 END_COLLECTION(0),
samux 1:80ab0d068708 396
samux 1:80ab0d068708 397 // Media Control
samux 1:80ab0d068708 398 USAGE_PAGE(1), 0x0C,
samux 1:80ab0d068708 399 USAGE(1), 0x01,
samux 1:80ab0d068708 400 COLLECTION(1), 0x01,
samux 1:80ab0d068708 401 REPORT_ID(1), REPORT_ID_VOLUME,
samux 1:80ab0d068708 402 USAGE_PAGE(1), 0x0C,
samux 1:80ab0d068708 403 LOGICAL_MINIMUM(1), 0x00,
samux 1:80ab0d068708 404 LOGICAL_MAXIMUM(1), 0x01,
samux 1:80ab0d068708 405 REPORT_SIZE(1), 0x01,
samux 1:80ab0d068708 406 REPORT_COUNT(1), 0x07,
samux 1:80ab0d068708 407 USAGE(1), 0xB5, // Next Track
samux 1:80ab0d068708 408 USAGE(1), 0xB6, // Previous Track
samux 1:80ab0d068708 409 USAGE(1), 0xB7, // Stop
samux 1:80ab0d068708 410 USAGE(1), 0xCD, // Play / Pause
samux 1:80ab0d068708 411 USAGE(1), 0xE2, // Mute
samux 1:80ab0d068708 412 USAGE(1), 0xE9, // Volume Up
samux 1:80ab0d068708 413 USAGE(1), 0xEA, // Volume Down
samux 1:80ab0d068708 414 INPUT(1), 0x02, // Input (Data, Variable, Absolute)
samux 1:80ab0d068708 415 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 416 INPUT(1), 0x01,
samux 1:80ab0d068708 417 END_COLLECTION(0),
samux 1:80ab0d068708 418 };
mjr 50:946bc763c068 419
mjr 50:946bc763c068 420 if (idx == 0) {
mjr 50:946bc763c068 421 len = sizeof(reportDescriptor);
mjr 50:946bc763c068 422 return reportDescriptor;
mjr 50:946bc763c068 423 }
mjr 50:946bc763c068 424 else {
mjr 50:946bc763c068 425 len = 0;
mjr 50:946bc763c068 426 return 0;
mjr 50:946bc763c068 427 }
samux 1:80ab0d068708 428 }
samux 1:80ab0d068708 429
samux 1:80ab0d068708 430
samux 1:80ab0d068708 431 bool USBKeyboard::EP1_OUT_callback() {
samux 1:80ab0d068708 432 uint32_t bytesRead = 0;
samux 1:80ab0d068708 433 uint8_t led[65];
samux 1:80ab0d068708 434 USBDevice::readEP(EPINT_OUT, led, &bytesRead, MAX_HID_REPORT_SIZE);
mbed_official 25:7c72828865f3 435
samux 1:80ab0d068708 436 // we take led[1] because led[0] is the report ID
samux 1:80ab0d068708 437 lock_status = led[1] & 0x07;
mbed_official 25:7c72828865f3 438
samux 1:80ab0d068708 439 // We activate the endpoint to be able to recceive data
samux 1:80ab0d068708 440 if (!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
samux 1:80ab0d068708 441 return false;
samux 1:80ab0d068708 442 return true;
samux 1:80ab0d068708 443 }
samux 1:80ab0d068708 444
samux 1:80ab0d068708 445 uint8_t USBKeyboard::lockStatus() {
samux 1:80ab0d068708 446 return lock_status;
samux 1:80ab0d068708 447 }
samux 1:80ab0d068708 448
samux 1:80ab0d068708 449 int USBKeyboard::_putc(int c) {
samux 1:80ab0d068708 450 return keyCode(c, keymap[c].modifier);
samux 1:80ab0d068708 451 }
samux 1:80ab0d068708 452
samux 1:80ab0d068708 453 bool USBKeyboard::keyCode(uint8_t key, uint8_t modifier) {
samux 1:80ab0d068708 454 // Send a simulated keyboard keypress. Returns true if successful.
samux 1:80ab0d068708 455 HID_REPORT report;
samux 1:80ab0d068708 456
samux 1:80ab0d068708 457 report.data[0] = REPORT_ID_KEYBOARD;
samux 1:80ab0d068708 458 report.data[1] = modifier;
samux 1:80ab0d068708 459 report.data[2] = 0;
samux 1:80ab0d068708 460 report.data[3] = keymap[key].usage;
samux 1:80ab0d068708 461 report.data[4] = 0;
samux 1:80ab0d068708 462 report.data[5] = 0;
samux 1:80ab0d068708 463 report.data[6] = 0;
samux 1:80ab0d068708 464 report.data[7] = 0;
samux 1:80ab0d068708 465 report.data[8] = 0;
samux 1:80ab0d068708 466
samux 1:80ab0d068708 467 report.length = 9;
samux 1:80ab0d068708 468
samux 1:80ab0d068708 469 if (!send(&report)) {
samux 1:80ab0d068708 470 return false;
samux 1:80ab0d068708 471 }
samux 1:80ab0d068708 472
samux 1:80ab0d068708 473 report.data[1] = 0;
samux 1:80ab0d068708 474 report.data[3] = 0;
samux 1:80ab0d068708 475
samux 1:80ab0d068708 476 if (!send(&report)) {
samux 1:80ab0d068708 477 return false;
samux 1:80ab0d068708 478 }
samux 1:80ab0d068708 479
samux 1:80ab0d068708 480 return true;
samux 1:80ab0d068708 481
samux 1:80ab0d068708 482 }
samux 1:80ab0d068708 483
samux 1:80ab0d068708 484
samux 1:80ab0d068708 485 bool USBKeyboard::mediaControl(MEDIA_KEY key) {
samux 1:80ab0d068708 486 HID_REPORT report;
samux 1:80ab0d068708 487
samux 1:80ab0d068708 488 report.data[0] = REPORT_ID_VOLUME;
samux 1:80ab0d068708 489 report.data[1] = (1 << key) & 0x7f;
samux 1:80ab0d068708 490
samux 1:80ab0d068708 491 report.length = 2;
samux 1:80ab0d068708 492
samux 1:80ab0d068708 493 if (!send(&report)) {
samux 1:80ab0d068708 494 return false;
samux 1:80ab0d068708 495 }
samux 1:80ab0d068708 496
samux 1:80ab0d068708 497 report.data[0] = REPORT_ID_VOLUME;
samux 1:80ab0d068708 498 report.data[1] = 0;
samux 1:80ab0d068708 499
samux 1:80ab0d068708 500 report.length = 2;
samux 1:80ab0d068708 501
samux 1:80ab0d068708 502 return send(&report);
samux 1:80ab0d068708 503 }
samux 1:80ab0d068708 504
samux 1:80ab0d068708 505
samux 1:80ab0d068708 506 #define DEFAULT_CONFIGURATION (1)
samux 1:80ab0d068708 507 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
samux 1:80ab0d068708 508 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
samux 1:80ab0d068708 509 + (1 * HID_DESCRIPTOR_LENGTH) \
samux 1:80ab0d068708 510 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
samux 1:80ab0d068708 511
mjr 50:946bc763c068 512 const uint8_t *USBKeyboard::configurationDesc()
mjr 50:946bc763c068 513 {
mjr 50:946bc763c068 514 const uint16_t rdl = reportDescLength(0);
mjr 49:03527ce6840e 515 static const uint8_t configurationDescriptor[] = {
samux 1:80ab0d068708 516 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
samux 1:80ab0d068708 517 CONFIGURATION_DESCRIPTOR, // bDescriptorType
samux 1:80ab0d068708 518 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
samux 1:80ab0d068708 519 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
samux 1:80ab0d068708 520 0x01, // bNumInterfaces
samux 1:80ab0d068708 521 DEFAULT_CONFIGURATION, // bConfigurationValue
samux 1:80ab0d068708 522 0x00, // iConfiguration
samux 1:80ab0d068708 523 C_RESERVED | C_SELF_POWERED, // bmAttributes
samux 1:80ab0d068708 524 C_POWER(0), // bMaxPowerHello World from Mbed
samux 1:80ab0d068708 525
samux 1:80ab0d068708 526 INTERFACE_DESCRIPTOR_LENGTH, // bLength
samux 1:80ab0d068708 527 INTERFACE_DESCRIPTOR, // bDescriptorType
samux 1:80ab0d068708 528 0x00, // bInterfaceNumber
samux 1:80ab0d068708 529 0x00, // bAlternateSetting
samux 1:80ab0d068708 530 0x02, // bNumEndpoints
samux 1:80ab0d068708 531 HID_CLASS, // bInterfaceClass
samux 1:80ab0d068708 532 1, // bInterfaceSubClass
samux 1:80ab0d068708 533 1, // bInterfaceProtocol (keyboard)
samux 1:80ab0d068708 534 0x00, // iInterface
samux 1:80ab0d068708 535
samux 1:80ab0d068708 536 HID_DESCRIPTOR_LENGTH, // bLength
samux 1:80ab0d068708 537 HID_DESCRIPTOR, // bDescriptorType
samux 1:80ab0d068708 538 LSB(HID_VERSION_1_11), // bcdHID (LSB)
samux 1:80ab0d068708 539 MSB(HID_VERSION_1_11), // bcdHID (MSB)
samux 1:80ab0d068708 540 0x00, // bCountryCode
samux 1:80ab0d068708 541 0x01, // bNumDescriptors
samux 1:80ab0d068708 542 REPORT_DESCRIPTOR, // bDescriptorType
mjr 50:946bc763c068 543 (uint8_t)(LSB(rdl)), // wDescriptorLength (LSB)
mjr 50:946bc763c068 544 (uint8_t)(MSB(rdl)), // wDescriptorLength (MSB)
samux 1:80ab0d068708 545
samux 1:80ab0d068708 546 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
samux 1:80ab0d068708 547 ENDPOINT_DESCRIPTOR, // bDescriptorType
samux 1:80ab0d068708 548 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
samux 1:80ab0d068708 549 E_INTERRUPT, // bmAttributes
samux 1:80ab0d068708 550 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
samux 1:80ab0d068708 551 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
mjr 50:946bc763c068 552 1, // bInterval (milliseconds)
samux 1:80ab0d068708 553
samux 1:80ab0d068708 554 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
samux 1:80ab0d068708 555 ENDPOINT_DESCRIPTOR, // bDescriptorType
mjr 50:946bc763c068 556 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
samux 1:80ab0d068708 557 E_INTERRUPT, // bmAttributes
samux 1:80ab0d068708 558 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
samux 1:80ab0d068708 559 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
mjr 50:946bc763c068 560 1 // bInterval (milliseconds)
samux 1:80ab0d068708 561 };
samux 1:80ab0d068708 562 return configurationDescriptor;
samux 1:80ab0d068708 563 }