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 #include "USBMouseKeyboard.h"
samux 1:80ab0d068708 21
samux 1:80ab0d068708 22 typedef struct {
samux 1:80ab0d068708 23 unsigned char usage;
samux 1:80ab0d068708 24 unsigned char modifier;
samux 1:80ab0d068708 25 } KEYMAP;
samux 1:80ab0d068708 26
samux 1:80ab0d068708 27 #ifdef US_KEYBOARD
samux 1:80ab0d068708 28 /* US keyboard (as HID standard) */
samux 1:80ab0d068708 29 #define KEYMAP_SIZE (152)
samux 1:80ab0d068708 30 const KEYMAP keymap[KEYMAP_SIZE] = {
samux 1:80ab0d068708 31 {0, 0}, /* NUL */
samux 1:80ab0d068708 32 {0, 0}, /* SOH */
samux 1:80ab0d068708 33 {0, 0}, /* STX */
samux 1:80ab0d068708 34 {0, 0}, /* ETX */
samux 1:80ab0d068708 35 {0, 0}, /* EOT */
samux 1:80ab0d068708 36 {0, 0}, /* ENQ */
samux 1:80ab0d068708 37 {0, 0}, /* ACK */
samux 1:80ab0d068708 38 {0, 0}, /* BEL */
samux 1:80ab0d068708 39 {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */
samux 1:80ab0d068708 40 {0x2b, 0}, /* TAB */ /* Keyboard Tab */
samux 1:80ab0d068708 41 {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */
samux 1:80ab0d068708 42 {0, 0}, /* VT */
samux 1:80ab0d068708 43 {0, 0}, /* FF */
samux 1:80ab0d068708 44 {0, 0}, /* CR */
samux 1:80ab0d068708 45 {0, 0}, /* SO */
samux 1:80ab0d068708 46 {0, 0}, /* SI */
samux 1:80ab0d068708 47 {0, 0}, /* DEL */
samux 1:80ab0d068708 48 {0, 0}, /* DC1 */
samux 1:80ab0d068708 49 {0, 0}, /* DC2 */
samux 1:80ab0d068708 50 {0, 0}, /* DC3 */
samux 1:80ab0d068708 51 {0, 0}, /* DC4 */
samux 1:80ab0d068708 52 {0, 0}, /* NAK */
samux 1:80ab0d068708 53 {0, 0}, /* SYN */
samux 1:80ab0d068708 54 {0, 0}, /* ETB */
samux 1:80ab0d068708 55 {0, 0}, /* CAN */
samux 1:80ab0d068708 56 {0, 0}, /* EM */
samux 1:80ab0d068708 57 {0, 0}, /* SUB */
samux 1:80ab0d068708 58 {0, 0}, /* ESC */
samux 1:80ab0d068708 59 {0, 0}, /* FS */
samux 1:80ab0d068708 60 {0, 0}, /* GS */
samux 1:80ab0d068708 61 {0, 0}, /* RS */
samux 1:80ab0d068708 62 {0, 0}, /* US */
samux 1:80ab0d068708 63 {0x2c, 0}, /* */
samux 1:80ab0d068708 64 {0x1e, KEY_SHIFT}, /* ! */
samux 1:80ab0d068708 65 {0x34, KEY_SHIFT}, /* " */
samux 1:80ab0d068708 66 {0x20, KEY_SHIFT}, /* # */
samux 1:80ab0d068708 67 {0x21, KEY_SHIFT}, /* $ */
samux 1:80ab0d068708 68 {0x22, KEY_SHIFT}, /* % */
samux 1:80ab0d068708 69 {0x24, KEY_SHIFT}, /* & */
samux 1:80ab0d068708 70 {0x34, 0}, /* ' */
samux 1:80ab0d068708 71 {0x26, KEY_SHIFT}, /* ( */
samux 1:80ab0d068708 72 {0x27, KEY_SHIFT}, /* ) */
samux 1:80ab0d068708 73 {0x25, KEY_SHIFT}, /* * */
samux 1:80ab0d068708 74 {0x2e, KEY_SHIFT}, /* + */
samux 1:80ab0d068708 75 {0x36, 0}, /* , */
samux 1:80ab0d068708 76 {0x2d, 0}, /* - */
samux 1:80ab0d068708 77 {0x37, 0}, /* . */
samux 1:80ab0d068708 78 {0x38, 0}, /* / */
samux 1:80ab0d068708 79 {0x27, 0}, /* 0 */
samux 1:80ab0d068708 80 {0x1e, 0}, /* 1 */
samux 1:80ab0d068708 81 {0x1f, 0}, /* 2 */
samux 1:80ab0d068708 82 {0x20, 0}, /* 3 */
samux 1:80ab0d068708 83 {0x21, 0}, /* 4 */
samux 1:80ab0d068708 84 {0x22, 0}, /* 5 */
samux 1:80ab0d068708 85 {0x23, 0}, /* 6 */
samux 1:80ab0d068708 86 {0x24, 0}, /* 7 */
samux 1:80ab0d068708 87 {0x25, 0}, /* 8 */
samux 1:80ab0d068708 88 {0x26, 0}, /* 9 */
samux 1:80ab0d068708 89 {0x33, KEY_SHIFT}, /* : */
samux 1:80ab0d068708 90 {0x33, 0}, /* ; */
samux 1:80ab0d068708 91 {0x36, KEY_SHIFT}, /* < */
samux 1:80ab0d068708 92 {0x2e, 0}, /* = */
samux 1:80ab0d068708 93 {0x37, KEY_SHIFT}, /* > */
samux 1:80ab0d068708 94 {0x38, KEY_SHIFT}, /* ? */
samux 1:80ab0d068708 95 {0x1f, KEY_SHIFT}, /* @ */
samux 1:80ab0d068708 96 {0x04, KEY_SHIFT}, /* A */
samux 1:80ab0d068708 97 {0x05, KEY_SHIFT}, /* B */
samux 1:80ab0d068708 98 {0x06, KEY_SHIFT}, /* C */
samux 1:80ab0d068708 99 {0x07, KEY_SHIFT}, /* D */
samux 1:80ab0d068708 100 {0x08, KEY_SHIFT}, /* E */
samux 1:80ab0d068708 101 {0x09, KEY_SHIFT}, /* F */
samux 1:80ab0d068708 102 {0x0a, KEY_SHIFT}, /* G */
samux 1:80ab0d068708 103 {0x0b, KEY_SHIFT}, /* H */
samux 1:80ab0d068708 104 {0x0c, KEY_SHIFT}, /* I */
samux 1:80ab0d068708 105 {0x0d, KEY_SHIFT}, /* J */
samux 1:80ab0d068708 106 {0x0e, KEY_SHIFT}, /* K */
samux 1:80ab0d068708 107 {0x0f, KEY_SHIFT}, /* L */
samux 1:80ab0d068708 108 {0x10, KEY_SHIFT}, /* M */
samux 1:80ab0d068708 109 {0x11, KEY_SHIFT}, /* N */
samux 1:80ab0d068708 110 {0x12, KEY_SHIFT}, /* O */
samux 1:80ab0d068708 111 {0x13, KEY_SHIFT}, /* P */
samux 1:80ab0d068708 112 {0x14, KEY_SHIFT}, /* Q */
samux 1:80ab0d068708 113 {0x15, KEY_SHIFT}, /* R */
samux 1:80ab0d068708 114 {0x16, KEY_SHIFT}, /* S */
samux 1:80ab0d068708 115 {0x17, KEY_SHIFT}, /* T */
samux 1:80ab0d068708 116 {0x18, KEY_SHIFT}, /* U */
samux 1:80ab0d068708 117 {0x19, KEY_SHIFT}, /* V */
samux 1:80ab0d068708 118 {0x1a, KEY_SHIFT}, /* W */
samux 1:80ab0d068708 119 {0x1b, KEY_SHIFT}, /* X */
samux 1:80ab0d068708 120 {0x1c, KEY_SHIFT}, /* Y */
samux 1:80ab0d068708 121 {0x1d, KEY_SHIFT}, /* Z */
samux 1:80ab0d068708 122 {0x2f, 0}, /* [ */
samux 1:80ab0d068708 123 {0x31, 0}, /* \ */
samux 1:80ab0d068708 124 {0x30, 0}, /* ] */
samux 1:80ab0d068708 125 {0x23, KEY_SHIFT}, /* ^ */
samux 1:80ab0d068708 126 {0x2d, KEY_SHIFT}, /* _ */
samux 1:80ab0d068708 127 {0x35, 0}, /* ` */
samux 1:80ab0d068708 128 {0x04, 0}, /* a */
samux 1:80ab0d068708 129 {0x05, 0}, /* b */
samux 1:80ab0d068708 130 {0x06, 0}, /* c */
samux 1:80ab0d068708 131 {0x07, 0}, /* d */
samux 1:80ab0d068708 132 {0x08, 0}, /* e */
samux 1:80ab0d068708 133 {0x09, 0}, /* f */
samux 1:80ab0d068708 134 {0x0a, 0}, /* g */
samux 1:80ab0d068708 135 {0x0b, 0}, /* h */
samux 1:80ab0d068708 136 {0x0c, 0}, /* i */
samux 1:80ab0d068708 137 {0x0d, 0}, /* j */
samux 1:80ab0d068708 138 {0x0e, 0}, /* k */
samux 1:80ab0d068708 139 {0x0f, 0}, /* l */
samux 1:80ab0d068708 140 {0x10, 0}, /* m */
samux 1:80ab0d068708 141 {0x11, 0}, /* n */
samux 1:80ab0d068708 142 {0x12, 0}, /* o */
samux 1:80ab0d068708 143 {0x13, 0}, /* p */
samux 1:80ab0d068708 144 {0x14, 0}, /* q */
samux 1:80ab0d068708 145 {0x15, 0}, /* r */
samux 1:80ab0d068708 146 {0x16, 0}, /* s */
samux 1:80ab0d068708 147 {0x17, 0}, /* t */
samux 1:80ab0d068708 148 {0x18, 0}, /* u */
samux 1:80ab0d068708 149 {0x19, 0}, /* v */
samux 1:80ab0d068708 150 {0x1a, 0}, /* w */
samux 1:80ab0d068708 151 {0x1b, 0}, /* x */
samux 1:80ab0d068708 152 {0x1c, 0}, /* y */
samux 1:80ab0d068708 153 {0x1d, 0}, /* z */
samux 1:80ab0d068708 154 {0x2f, KEY_SHIFT}, /* { */
samux 1:80ab0d068708 155 {0x31, KEY_SHIFT}, /* | */
samux 1:80ab0d068708 156 {0x30, KEY_SHIFT}, /* } */
samux 1:80ab0d068708 157 {0x35, KEY_SHIFT}, /* ~ */
samux 1:80ab0d068708 158 {0,0}, /* DEL */
samux 1:80ab0d068708 159
samux 1:80ab0d068708 160 {0x3a, 0}, /* F1 */
samux 1:80ab0d068708 161 {0x3b, 0}, /* F2 */
samux 1:80ab0d068708 162 {0x3c, 0}, /* F3 */
samux 1:80ab0d068708 163 {0x3d, 0}, /* F4 */
samux 1:80ab0d068708 164 {0x3e, 0}, /* F5 */
samux 1:80ab0d068708 165 {0x3f, 0}, /* F6 */
samux 1:80ab0d068708 166 {0x40, 0}, /* F7 */
samux 1:80ab0d068708 167 {0x41, 0}, /* F8 */
samux 1:80ab0d068708 168 {0x42, 0}, /* F9 */
samux 1:80ab0d068708 169 {0x43, 0}, /* F10 */
samux 1:80ab0d068708 170 {0x44, 0}, /* F11 */
samux 1:80ab0d068708 171 {0x45, 0}, /* F12 */
samux 1:80ab0d068708 172
samux 1:80ab0d068708 173 {0x46, 0}, /* PRINT_SCREEN */
samux 1:80ab0d068708 174 {0x47, 0}, /* SCROLL_LOCK */
samux 1:80ab0d068708 175 {0x39, 0}, /* CAPS_LOCK */
samux 1:80ab0d068708 176 {0x53, 0}, /* NUM_LOCK */
samux 1:80ab0d068708 177 {0x49, 0}, /* INSERT */
samux 1:80ab0d068708 178 {0x4a, 0}, /* HOME */
samux 1:80ab0d068708 179 {0x4b, 0}, /* PAGE_UP */
samux 1:80ab0d068708 180 {0x4e, 0}, /* PAGE_DOWN */
mbed_official 25:7c72828865f3 181
samux 1:80ab0d068708 182 {0x4f, 0}, /* RIGHT_ARROW */
samux 1:80ab0d068708 183 {0x50, 0}, /* LEFT_ARROW */
samux 1:80ab0d068708 184 {0x51, 0}, /* DOWN_ARROW */
samux 1:80ab0d068708 185 {0x52, 0}, /* UP_ARROW */
samux 1:80ab0d068708 186 };
samux 1:80ab0d068708 187
samux 1:80ab0d068708 188 #else
samux 1:80ab0d068708 189 /* UK keyboard */
samux 1:80ab0d068708 190 #define KEYMAP_SIZE (152)
samux 1:80ab0d068708 191 const KEYMAP keymap[KEYMAP_SIZE] = {
samux 1:80ab0d068708 192 {0, 0}, /* NUL */
samux 1:80ab0d068708 193 {0, 0}, /* SOH */
samux 1:80ab0d068708 194 {0, 0}, /* STX */
samux 1:80ab0d068708 195 {0, 0}, /* ETX */
samux 1:80ab0d068708 196 {0, 0}, /* EOT */
samux 1:80ab0d068708 197 {0, 0}, /* ENQ */
samux 1:80ab0d068708 198 {0, 0}, /* ACK */
samux 1:80ab0d068708 199 {0, 0}, /* BEL */
samux 1:80ab0d068708 200 {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */
samux 1:80ab0d068708 201 {0x2b, 0}, /* TAB */ /* Keyboard Tab */
samux 1:80ab0d068708 202 {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */
samux 1:80ab0d068708 203 {0, 0}, /* VT */
samux 1:80ab0d068708 204 {0, 0}, /* FF */
samux 1:80ab0d068708 205 {0, 0}, /* CR */
samux 1:80ab0d068708 206 {0, 0}, /* SO */
samux 1:80ab0d068708 207 {0, 0}, /* SI */
samux 1:80ab0d068708 208 {0, 0}, /* DEL */
samux 1:80ab0d068708 209 {0, 0}, /* DC1 */
samux 1:80ab0d068708 210 {0, 0}, /* DC2 */
samux 1:80ab0d068708 211 {0, 0}, /* DC3 */
samux 1:80ab0d068708 212 {0, 0}, /* DC4 */
samux 1:80ab0d068708 213 {0, 0}, /* NAK */
samux 1:80ab0d068708 214 {0, 0}, /* SYN */
samux 1:80ab0d068708 215 {0, 0}, /* ETB */
samux 1:80ab0d068708 216 {0, 0}, /* CAN */
samux 1:80ab0d068708 217 {0, 0}, /* EM */
samux 1:80ab0d068708 218 {0, 0}, /* SUB */
samux 1:80ab0d068708 219 {0, 0}, /* ESC */
samux 1:80ab0d068708 220 {0, 0}, /* FS */
samux 1:80ab0d068708 221 {0, 0}, /* GS */
samux 1:80ab0d068708 222 {0, 0}, /* RS */
samux 1:80ab0d068708 223 {0, 0}, /* US */
samux 1:80ab0d068708 224 {0x2c, 0}, /* */
samux 1:80ab0d068708 225 {0x1e, KEY_SHIFT}, /* ! */
samux 1:80ab0d068708 226 {0x1f, KEY_SHIFT}, /* " */
samux 1:80ab0d068708 227 {0x32, 0}, /* # */
samux 1:80ab0d068708 228 {0x21, KEY_SHIFT}, /* $ */
samux 1:80ab0d068708 229 {0x22, KEY_SHIFT}, /* % */
samux 1:80ab0d068708 230 {0x24, KEY_SHIFT}, /* & */
samux 1:80ab0d068708 231 {0x34, 0}, /* ' */
samux 1:80ab0d068708 232 {0x26, KEY_SHIFT}, /* ( */
samux 1:80ab0d068708 233 {0x27, KEY_SHIFT}, /* ) */
samux 1:80ab0d068708 234 {0x25, KEY_SHIFT}, /* * */
samux 1:80ab0d068708 235 {0x2e, KEY_SHIFT}, /* + */
samux 1:80ab0d068708 236 {0x36, 0}, /* , */
samux 1:80ab0d068708 237 {0x2d, 0}, /* - */
samux 1:80ab0d068708 238 {0x37, 0}, /* . */
samux 1:80ab0d068708 239 {0x38, 0}, /* / */
samux 1:80ab0d068708 240 {0x27, 0}, /* 0 */
samux 1:80ab0d068708 241 {0x1e, 0}, /* 1 */
samux 1:80ab0d068708 242 {0x1f, 0}, /* 2 */
samux 1:80ab0d068708 243 {0x20, 0}, /* 3 */
samux 1:80ab0d068708 244 {0x21, 0}, /* 4 */
samux 1:80ab0d068708 245 {0x22, 0}, /* 5 */
samux 1:80ab0d068708 246 {0x23, 0}, /* 6 */
samux 1:80ab0d068708 247 {0x24, 0}, /* 7 */
samux 1:80ab0d068708 248 {0x25, 0}, /* 8 */
samux 1:80ab0d068708 249 {0x26, 0}, /* 9 */
samux 1:80ab0d068708 250 {0x33, KEY_SHIFT}, /* : */
samux 1:80ab0d068708 251 {0x33, 0}, /* ; */
samux 1:80ab0d068708 252 {0x36, KEY_SHIFT}, /* < */
samux 1:80ab0d068708 253 {0x2e, 0}, /* = */
samux 1:80ab0d068708 254 {0x37, KEY_SHIFT}, /* > */
samux 1:80ab0d068708 255 {0x38, KEY_SHIFT}, /* ? */
samux 1:80ab0d068708 256 {0x34, KEY_SHIFT}, /* @ */
samux 1:80ab0d068708 257 {0x04, KEY_SHIFT}, /* A */
samux 1:80ab0d068708 258 {0x05, KEY_SHIFT}, /* B */
samux 1:80ab0d068708 259 {0x06, KEY_SHIFT}, /* C */
samux 1:80ab0d068708 260 {0x07, KEY_SHIFT}, /* D */
samux 1:80ab0d068708 261 {0x08, KEY_SHIFT}, /* E */
samux 1:80ab0d068708 262 {0x09, KEY_SHIFT}, /* F */
samux 1:80ab0d068708 263 {0x0a, KEY_SHIFT}, /* G */
samux 1:80ab0d068708 264 {0x0b, KEY_SHIFT}, /* H */
samux 1:80ab0d068708 265 {0x0c, KEY_SHIFT}, /* I */
samux 1:80ab0d068708 266 {0x0d, KEY_SHIFT}, /* J */
samux 1:80ab0d068708 267 {0x0e, KEY_SHIFT}, /* K */
samux 1:80ab0d068708 268 {0x0f, KEY_SHIFT}, /* L */
samux 1:80ab0d068708 269 {0x10, KEY_SHIFT}, /* M */
samux 1:80ab0d068708 270 {0x11, KEY_SHIFT}, /* N */
samux 1:80ab0d068708 271 {0x12, KEY_SHIFT}, /* O */
samux 1:80ab0d068708 272 {0x13, KEY_SHIFT}, /* P */
samux 1:80ab0d068708 273 {0x14, KEY_SHIFT}, /* Q */
samux 1:80ab0d068708 274 {0x15, KEY_SHIFT}, /* R */
samux 1:80ab0d068708 275 {0x16, KEY_SHIFT}, /* S */
samux 1:80ab0d068708 276 {0x17, KEY_SHIFT}, /* T */
samux 1:80ab0d068708 277 {0x18, KEY_SHIFT}, /* U */
samux 1:80ab0d068708 278 {0x19, KEY_SHIFT}, /* V */
samux 1:80ab0d068708 279 {0x1a, KEY_SHIFT}, /* W */
samux 1:80ab0d068708 280 {0x1b, KEY_SHIFT}, /* X */
samux 1:80ab0d068708 281 {0x1c, KEY_SHIFT}, /* Y */
samux 1:80ab0d068708 282 {0x1d, KEY_SHIFT}, /* Z */
samux 1:80ab0d068708 283 {0x2f, 0}, /* [ */
samux 1:80ab0d068708 284 {0x64, 0}, /* \ */
samux 1:80ab0d068708 285 {0x30, 0}, /* ] */
samux 1:80ab0d068708 286 {0x23, KEY_SHIFT}, /* ^ */
samux 1:80ab0d068708 287 {0x2d, KEY_SHIFT}, /* _ */
samux 1:80ab0d068708 288 {0x35, 0}, /* ` */
samux 1:80ab0d068708 289 {0x04, 0}, /* a */
samux 1:80ab0d068708 290 {0x05, 0}, /* b */
samux 1:80ab0d068708 291 {0x06, 0}, /* c */
samux 1:80ab0d068708 292 {0x07, 0}, /* d */
samux 1:80ab0d068708 293 {0x08, 0}, /* e */
samux 1:80ab0d068708 294 {0x09, 0}, /* f */
samux 1:80ab0d068708 295 {0x0a, 0}, /* g */
samux 1:80ab0d068708 296 {0x0b, 0}, /* h */
samux 1:80ab0d068708 297 {0x0c, 0}, /* i */
samux 1:80ab0d068708 298 {0x0d, 0}, /* j */
samux 1:80ab0d068708 299 {0x0e, 0}, /* k */
samux 1:80ab0d068708 300 {0x0f, 0}, /* l */
samux 1:80ab0d068708 301 {0x10, 0}, /* m */
samux 1:80ab0d068708 302 {0x11, 0}, /* n */
samux 1:80ab0d068708 303 {0x12, 0}, /* o */
samux 1:80ab0d068708 304 {0x13, 0}, /* p */
samux 1:80ab0d068708 305 {0x14, 0}, /* q */
samux 1:80ab0d068708 306 {0x15, 0}, /* r */
samux 1:80ab0d068708 307 {0x16, 0}, /* s */
samux 1:80ab0d068708 308 {0x17, 0}, /* t */
samux 1:80ab0d068708 309 {0x18, 0}, /* u */
samux 1:80ab0d068708 310 {0x19, 0}, /* v */
samux 1:80ab0d068708 311 {0x1a, 0}, /* w */
samux 1:80ab0d068708 312 {0x1b, 0}, /* x */
samux 1:80ab0d068708 313 {0x1c, 0}, /* y */
samux 1:80ab0d068708 314 {0x1d, 0}, /* z */
samux 1:80ab0d068708 315 {0x2f, KEY_SHIFT}, /* { */
samux 1:80ab0d068708 316 {0x64, KEY_SHIFT}, /* | */
samux 1:80ab0d068708 317 {0x30, KEY_SHIFT}, /* } */
samux 1:80ab0d068708 318 {0x32, KEY_SHIFT}, /* ~ */
samux 1:80ab0d068708 319 {0,0}, /* DEL */
samux 1:80ab0d068708 320
samux 1:80ab0d068708 321 {0x3a, 0}, /* F1 */
samux 1:80ab0d068708 322 {0x3b, 0}, /* F2 */
samux 1:80ab0d068708 323 {0x3c, 0}, /* F3 */
samux 1:80ab0d068708 324 {0x3d, 0}, /* F4 */
samux 1:80ab0d068708 325 {0x3e, 0}, /* F5 */
samux 1:80ab0d068708 326 {0x3f, 0}, /* F6 */
samux 1:80ab0d068708 327 {0x40, 0}, /* F7 */
samux 1:80ab0d068708 328 {0x41, 0}, /* F8 */
samux 1:80ab0d068708 329 {0x42, 0}, /* F9 */
samux 1:80ab0d068708 330 {0x43, 0}, /* F10 */
samux 1:80ab0d068708 331 {0x44, 0}, /* F11 */
samux 1:80ab0d068708 332 {0x45, 0}, /* F12 */
samux 1:80ab0d068708 333
samux 1:80ab0d068708 334 {0x46, 0}, /* PRINT_SCREEN */
samux 1:80ab0d068708 335 {0x47, 0}, /* SCROLL_LOCK */
samux 1:80ab0d068708 336 {0x39, 0}, /* CAPS_LOCK */
samux 1:80ab0d068708 337 {0x53, 0}, /* NUM_LOCK */
samux 1:80ab0d068708 338 {0x49, 0}, /* INSERT */
samux 1:80ab0d068708 339 {0x4a, 0}, /* HOME */
samux 1:80ab0d068708 340 {0x4b, 0}, /* PAGE_UP */
samux 1:80ab0d068708 341 {0x4e, 0}, /* PAGE_DOWN */
mbed_official 25:7c72828865f3 342
samux 1:80ab0d068708 343 {0x4f, 0}, /* RIGHT_ARROW */
samux 1:80ab0d068708 344 {0x50, 0}, /* LEFT_ARROW */
samux 1:80ab0d068708 345 {0x51, 0}, /* DOWN_ARROW */
samux 1:80ab0d068708 346 {0x52, 0}, /* UP_ARROW */
samux 1:80ab0d068708 347 };
samux 1:80ab0d068708 348 #endif
samux 1:80ab0d068708 349
samux 1:80ab0d068708 350
mjr 50:946bc763c068 351 const uint8_t *USBMouseKeyboard::reportDesc(int idx, uint16_t &len)
mjr 50:946bc763c068 352 {
mjr 50:946bc763c068 353 // we only support one interface
mjr 50:946bc763c068 354 if (idx != 0) {
mjr 50:946bc763c068 355 len = 0;
mjr 50:946bc763c068 356 return 0;
mjr 50:946bc763c068 357 }
mjr 50:946bc763c068 358
mjr 50:946bc763c068 359 if (mouse_type == REL_MOUSE)
mjr 50:946bc763c068 360 {
mjr 49:03527ce6840e 361 static const uint8_t reportDescriptor[] = {
samux 1:80ab0d068708 362 // Keyboard
samux 1:80ab0d068708 363 USAGE_PAGE(1), 0x01,
samux 1:80ab0d068708 364 USAGE(1), 0x06,
samux 1:80ab0d068708 365 COLLECTION(1), 0x01,
samux 1:80ab0d068708 366 REPORT_ID(1), REPORT_ID_KEYBOARD,
samux 1:80ab0d068708 367 USAGE_PAGE(1), 0x07,
samux 1:80ab0d068708 368 USAGE_MINIMUM(1), 0xE0,
samux 1:80ab0d068708 369 USAGE_MAXIMUM(1), 0xE7,
samux 1:80ab0d068708 370 LOGICAL_MINIMUM(1), 0x00,
samux 1:80ab0d068708 371 LOGICAL_MAXIMUM(1), 0x01,
samux 1:80ab0d068708 372 REPORT_SIZE(1), 0x01,
samux 1:80ab0d068708 373 REPORT_COUNT(1), 0x08,
samux 1:80ab0d068708 374 INPUT(1), 0x02,
samux 1:80ab0d068708 375 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 376 REPORT_SIZE(1), 0x08,
samux 1:80ab0d068708 377 INPUT(1), 0x01,
samux 1:80ab0d068708 378 REPORT_COUNT(1), 0x05,
samux 1:80ab0d068708 379 REPORT_SIZE(1), 0x01,
samux 1:80ab0d068708 380 USAGE_PAGE(1), 0x08,
samux 1:80ab0d068708 381 USAGE_MINIMUM(1), 0x01,
samux 1:80ab0d068708 382 USAGE_MAXIMUM(1), 0x05,
samux 1:80ab0d068708 383 OUTPUT(1), 0x02,
samux 1:80ab0d068708 384 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 385 REPORT_SIZE(1), 0x03,
samux 1:80ab0d068708 386 OUTPUT(1), 0x01,
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(2), 0xff, 0x00,
samux 1:80ab0d068708 391 USAGE_PAGE(1), 0x07,
samux 1:80ab0d068708 392 USAGE_MINIMUM(1), 0x00,
samux 1:80ab0d068708 393 USAGE_MAXIMUM(2), 0xff, 0x00,
samux 1:80ab0d068708 394 INPUT(1), 0x00,
samux 1:80ab0d068708 395 END_COLLECTION(0),
samux 1:80ab0d068708 396
samux 1:80ab0d068708 397 // Mouse
samux 1:80ab0d068708 398 USAGE_PAGE(1), 0x01, // Generic Desktop
samux 1:80ab0d068708 399 USAGE(1), 0x02, // Mouse
samux 1:80ab0d068708 400 COLLECTION(1), 0x01, // Application
samux 1:80ab0d068708 401 USAGE(1), 0x01, // Pointer
samux 1:80ab0d068708 402 COLLECTION(1), 0x00, // Physical
samux 1:80ab0d068708 403 REPORT_ID(1), REPORT_ID_MOUSE,
samux 1:80ab0d068708 404 REPORT_COUNT(1), 0x03,
samux 1:80ab0d068708 405 REPORT_SIZE(1), 0x01,
samux 1:80ab0d068708 406 USAGE_PAGE(1), 0x09, // Buttons
samux 1:80ab0d068708 407 USAGE_MINIMUM(1), 0x1,
samux 1:80ab0d068708 408 USAGE_MAXIMUM(1), 0x3,
samux 1:80ab0d068708 409 LOGICAL_MINIMUM(1), 0x00,
samux 1:80ab0d068708 410 LOGICAL_MAXIMUM(1), 0x01,
samux 1:80ab0d068708 411 INPUT(1), 0x02,
samux 1:80ab0d068708 412 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 413 REPORT_SIZE(1), 0x05,
samux 1:80ab0d068708 414 INPUT(1), 0x01,
samux 1:80ab0d068708 415 REPORT_COUNT(1), 0x03,
samux 1:80ab0d068708 416 REPORT_SIZE(1), 0x08,
samux 1:80ab0d068708 417 USAGE_PAGE(1), 0x01,
samux 1:80ab0d068708 418 USAGE(1), 0x30, // X
samux 1:80ab0d068708 419 USAGE(1), 0x31, // Y
samux 1:80ab0d068708 420 USAGE(1), 0x38, // scroll
samux 1:80ab0d068708 421 LOGICAL_MINIMUM(1), 0x81,
samux 1:80ab0d068708 422 LOGICAL_MAXIMUM(1), 0x7f,
samux 1:80ab0d068708 423 INPUT(1), 0x06,
samux 1:80ab0d068708 424 END_COLLECTION(0),
samux 1:80ab0d068708 425 END_COLLECTION(0),
samux 1:80ab0d068708 426
samux 1:80ab0d068708 427
samux 1:80ab0d068708 428 // Media Control
samux 1:80ab0d068708 429 USAGE_PAGE(1), 0x0C,
samux 1:80ab0d068708 430 USAGE(1), 0x01,
samux 1:80ab0d068708 431 COLLECTION(1), 0x01,
samux 1:80ab0d068708 432 REPORT_ID(1), REPORT_ID_VOLUME,
samux 1:80ab0d068708 433 USAGE_PAGE(1), 0x0C,
samux 1:80ab0d068708 434 LOGICAL_MINIMUM(1), 0x00,
samux 1:80ab0d068708 435 LOGICAL_MAXIMUM(1), 0x01,
samux 1:80ab0d068708 436 REPORT_SIZE(1), 0x01,
samux 1:80ab0d068708 437 REPORT_COUNT(1), 0x07,
samux 1:80ab0d068708 438 USAGE(1), 0xB5, // Next Track
samux 1:80ab0d068708 439 USAGE(1), 0xB6, // Previous Track
samux 1:80ab0d068708 440 USAGE(1), 0xB7, // Stop
samux 1:80ab0d068708 441 USAGE(1), 0xCD, // Play / Pause
samux 1:80ab0d068708 442 USAGE(1), 0xE2, // Mute
samux 1:80ab0d068708 443 USAGE(1), 0xE9, // Volume Up
samux 1:80ab0d068708 444 USAGE(1), 0xEA, // Volume Down
samux 1:80ab0d068708 445 INPUT(1), 0x02, // Input (Data, Variable, Absolute)
samux 1:80ab0d068708 446 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 447 INPUT(1), 0x01,
samux 1:80ab0d068708 448 END_COLLECTION(0),
samux 1:80ab0d068708 449 };
mjr 50:946bc763c068 450
mjr 50:946bc763c068 451 len = sizeof(reportDescriptor);
samux 1:80ab0d068708 452 return reportDescriptor;
mjr 50:946bc763c068 453 }
mjr 50:946bc763c068 454 else if (mouse_type == ABS_MOUSE)
mjr 50:946bc763c068 455 {
mjr 49:03527ce6840e 456 static const uint8_t reportDescriptor[] = {
samux 1:80ab0d068708 457
samux 1:80ab0d068708 458 // Keyboard
samux 1:80ab0d068708 459 USAGE_PAGE(1), 0x01,
samux 1:80ab0d068708 460 USAGE(1), 0x06,
samux 1:80ab0d068708 461 COLLECTION(1), 0x01,
samux 1:80ab0d068708 462 REPORT_ID(1), REPORT_ID_KEYBOARD,
samux 1:80ab0d068708 463 USAGE_PAGE(1), 0x07,
samux 1:80ab0d068708 464 USAGE_MINIMUM(1), 0xE0,
samux 1:80ab0d068708 465 USAGE_MAXIMUM(1), 0xE7,
samux 1:80ab0d068708 466 LOGICAL_MINIMUM(1), 0x00,
samux 1:80ab0d068708 467 LOGICAL_MAXIMUM(1), 0x01,
samux 1:80ab0d068708 468 REPORT_SIZE(1), 0x01,
samux 1:80ab0d068708 469 REPORT_COUNT(1), 0x08,
samux 1:80ab0d068708 470 INPUT(1), 0x02,
samux 1:80ab0d068708 471 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 472 REPORT_SIZE(1), 0x08,
samux 1:80ab0d068708 473 INPUT(1), 0x01,
samux 1:80ab0d068708 474 REPORT_COUNT(1), 0x05,
samux 1:80ab0d068708 475 REPORT_SIZE(1), 0x01,
samux 1:80ab0d068708 476 USAGE_PAGE(1), 0x08,
samux 1:80ab0d068708 477 USAGE_MINIMUM(1), 0x01,
samux 1:80ab0d068708 478 USAGE_MAXIMUM(1), 0x05,
samux 1:80ab0d068708 479 OUTPUT(1), 0x02,
samux 1:80ab0d068708 480 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 481 REPORT_SIZE(1), 0x03,
samux 1:80ab0d068708 482 OUTPUT(1), 0x01,
samux 1:80ab0d068708 483 REPORT_COUNT(1), 0x06,
samux 1:80ab0d068708 484 REPORT_SIZE(1), 0x08,
samux 1:80ab0d068708 485 LOGICAL_MINIMUM(1), 0x00,
samux 1:80ab0d068708 486 LOGICAL_MAXIMUM(2), 0xff, 0x00,
samux 1:80ab0d068708 487 USAGE_PAGE(1), 0x07,
samux 1:80ab0d068708 488 USAGE_MINIMUM(1), 0x00,
samux 1:80ab0d068708 489 USAGE_MAXIMUM(2), 0xff, 0x00,
samux 1:80ab0d068708 490 INPUT(1), 0x00,
samux 1:80ab0d068708 491 END_COLLECTION(0),
samux 1:80ab0d068708 492
samux 1:80ab0d068708 493 // Mouse
samux 1:80ab0d068708 494 USAGE_PAGE(1), 0x01, // Generic Desktop
samux 1:80ab0d068708 495 USAGE(1), 0x02, // Mouse
samux 1:80ab0d068708 496 COLLECTION(1), 0x01, // Application
samux 1:80ab0d068708 497 USAGE(1), 0x01, // Pointer
samux 1:80ab0d068708 498 COLLECTION(1), 0x00, // Physical
samux 1:80ab0d068708 499 REPORT_ID(1), REPORT_ID_MOUSE,
samux 1:80ab0d068708 500
samux 1:80ab0d068708 501 USAGE_PAGE(1), 0x01, // Generic Desktop
samux 1:80ab0d068708 502 USAGE(1), 0x30, // X
samux 1:80ab0d068708 503 USAGE(1), 0x31, // Y
samux 1:80ab0d068708 504 LOGICAL_MINIMUM(1), 0x00, // 0
samux 1:80ab0d068708 505 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
samux 1:80ab0d068708 506 REPORT_SIZE(1), 0x10,
samux 1:80ab0d068708 507 REPORT_COUNT(1), 0x02,
samux 1:80ab0d068708 508 INPUT(1), 0x02, // Data, Variable, Absolute
samux 1:80ab0d068708 509
samux 1:80ab0d068708 510 USAGE_PAGE(1), 0x01, // Generic Desktop
samux 1:80ab0d068708 511 USAGE(1), 0x38, // scroll
samux 1:80ab0d068708 512 LOGICAL_MINIMUM(1), 0x81, // -127
samux 1:80ab0d068708 513 LOGICAL_MAXIMUM(1), 0x7f, // 127
samux 1:80ab0d068708 514 REPORT_SIZE(1), 0x08,
samux 1:80ab0d068708 515 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 516 INPUT(1), 0x06, // Data, Variable, Relative
samux 1:80ab0d068708 517
samux 1:80ab0d068708 518 USAGE_PAGE(1), 0x09, // Buttons
samux 1:80ab0d068708 519 USAGE_MINIMUM(1), 0x01,
samux 1:80ab0d068708 520 USAGE_MAXIMUM(1), 0x03,
samux 1:80ab0d068708 521 LOGICAL_MINIMUM(1), 0x00, // 0
samux 1:80ab0d068708 522 LOGICAL_MAXIMUM(1), 0x01, // 1
samux 1:80ab0d068708 523 REPORT_COUNT(1), 0x03,
samux 1:80ab0d068708 524 REPORT_SIZE(1), 0x01,
samux 1:80ab0d068708 525 INPUT(1), 0x02, // Data, Variable, Absolute
samux 1:80ab0d068708 526 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 527 REPORT_SIZE(1), 0x05,
samux 1:80ab0d068708 528 INPUT(1), 0x01, // Constant
samux 1:80ab0d068708 529
samux 1:80ab0d068708 530 END_COLLECTION(0),
samux 1:80ab0d068708 531 END_COLLECTION(0),
samux 1:80ab0d068708 532
samux 1:80ab0d068708 533 // Media Control
samux 1:80ab0d068708 534 USAGE_PAGE(1), 0x0C,
samux 1:80ab0d068708 535 USAGE(1), 0x01,
samux 1:80ab0d068708 536 COLLECTION(1), 0x01,
samux 1:80ab0d068708 537 REPORT_ID(1), REPORT_ID_VOLUME,
samux 1:80ab0d068708 538 USAGE_PAGE(1), 0x0C,
samux 1:80ab0d068708 539 LOGICAL_MINIMUM(1), 0x00,
samux 1:80ab0d068708 540 LOGICAL_MAXIMUM(1), 0x01,
samux 1:80ab0d068708 541 REPORT_SIZE(1), 0x01,
samux 1:80ab0d068708 542 REPORT_COUNT(1), 0x07,
samux 1:80ab0d068708 543 USAGE(1), 0xB5, // Next Track
samux 1:80ab0d068708 544 USAGE(1), 0xB6, // Previous Track
samux 1:80ab0d068708 545 USAGE(1), 0xB7, // Stop
samux 1:80ab0d068708 546 USAGE(1), 0xCD, // Play / Pause
samux 1:80ab0d068708 547 USAGE(1), 0xE2, // Mute
samux 1:80ab0d068708 548 USAGE(1), 0xE9, // Volume Up
samux 1:80ab0d068708 549 USAGE(1), 0xEA, // Volume Down
samux 1:80ab0d068708 550 INPUT(1), 0x02, // Input (Data, Variable, Absolute)
samux 1:80ab0d068708 551 REPORT_COUNT(1), 0x01,
samux 1:80ab0d068708 552 INPUT(1), 0x01,
samux 1:80ab0d068708 553 END_COLLECTION(0),
samux 1:80ab0d068708 554 };
mjr 50:946bc763c068 555
mjr 50:946bc763c068 556 len = sizeof(reportDescriptor);
samux 1:80ab0d068708 557 return reportDescriptor;
samux 1:80ab0d068708 558 }
samux 1:80ab0d068708 559
mjr 50:946bc763c068 560 len = 0;
samux 1:80ab0d068708 561 return NULL;
samux 1:80ab0d068708 562 }
samux 1:80ab0d068708 563
samux 1:80ab0d068708 564 bool USBMouseKeyboard::EP1_OUT_callback() {
samux 1:80ab0d068708 565 uint32_t bytesRead = 0;
samux 1:80ab0d068708 566 uint8_t led[65];
samux 1:80ab0d068708 567 USBDevice::readEP(EPINT_OUT, led, &bytesRead, MAX_HID_REPORT_SIZE);
mbed_official 25:7c72828865f3 568
samux 1:80ab0d068708 569 // we take led[1] because led[0] is the report ID
samux 1:80ab0d068708 570 lock_status = led[1] & 0x07;
mbed_official 25:7c72828865f3 571
samux 1:80ab0d068708 572 // We activate the endpoint to be able to recceive data
samux 1:80ab0d068708 573 if (!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
samux 1:80ab0d068708 574 return false;
samux 1:80ab0d068708 575 return true;
samux 1:80ab0d068708 576 }
samux 1:80ab0d068708 577
samux 1:80ab0d068708 578 uint8_t USBMouseKeyboard::lockStatus() {
samux 1:80ab0d068708 579 return lock_status;
samux 1:80ab0d068708 580 }
samux 1:80ab0d068708 581
samux 1:80ab0d068708 582 bool USBMouseKeyboard::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
samux 1:80ab0d068708 583 switch (mouse_type) {
samux 1:80ab0d068708 584 case REL_MOUSE:
samux 1:80ab0d068708 585 while (x > 127) {
samux 1:80ab0d068708 586 if (!mouseSend(127, 0, button, z)) return false;
samux 1:80ab0d068708 587 x = x - 127;
samux 1:80ab0d068708 588 }
samux 1:80ab0d068708 589 while (x < -128) {
samux 1:80ab0d068708 590 if (!mouseSend(-128, 0, button, z)) return false;
samux 1:80ab0d068708 591 x = x + 128;
samux 1:80ab0d068708 592 }
samux 1:80ab0d068708 593 while (y > 127) {
samux 1:80ab0d068708 594 if (!mouseSend(0, 127, button, z)) return false;
samux 1:80ab0d068708 595 y = y - 127;
samux 1:80ab0d068708 596 }
samux 1:80ab0d068708 597 while (y < -128) {
samux 1:80ab0d068708 598 if (!mouseSend(0, -128, button, z)) return false;
samux 1:80ab0d068708 599 y = y + 128;
samux 1:80ab0d068708 600 }
samux 1:80ab0d068708 601 return mouseSend(x, y, button, z);
samux 1:80ab0d068708 602 case ABS_MOUSE:
samux 1:80ab0d068708 603 HID_REPORT report;
samux 1:80ab0d068708 604
samux 1:80ab0d068708 605 report.data[0] = REPORT_ID_MOUSE;
samux 1:80ab0d068708 606 report.data[1] = x & 0xff;
samux 1:80ab0d068708 607 report.data[2] = (x >> 8) & 0xff;
samux 1:80ab0d068708 608 report.data[3] = y & 0xff;
samux 1:80ab0d068708 609 report.data[4] = (y >> 8) & 0xff;
samux 1:80ab0d068708 610 report.data[5] = -z;
samux 1:80ab0d068708 611 report.data[6] = button & 0x07;
samux 1:80ab0d068708 612
samux 1:80ab0d068708 613 report.length = 7;
samux 1:80ab0d068708 614
samux 1:80ab0d068708 615 return send(&report);
samux 1:80ab0d068708 616 default:
samux 1:80ab0d068708 617 return false;
samux 1:80ab0d068708 618 }
samux 1:80ab0d068708 619 }
samux 1:80ab0d068708 620
samux 1:80ab0d068708 621 bool USBMouseKeyboard::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
samux 1:80ab0d068708 622 HID_REPORT report;
samux 1:80ab0d068708 623 report.data[0] = REPORT_ID_MOUSE;
samux 1:80ab0d068708 624 report.data[1] = buttons & 0x07;
samux 1:80ab0d068708 625 report.data[2] = x;
samux 1:80ab0d068708 626 report.data[3] = y;
samux 1:80ab0d068708 627 report.data[4] = -z; // >0 to scroll down, <0 to scroll up
samux 1:80ab0d068708 628
samux 1:80ab0d068708 629 report.length = 5;
samux 1:80ab0d068708 630
samux 1:80ab0d068708 631 return send(&report);
samux 1:80ab0d068708 632 }
samux 1:80ab0d068708 633
samux 1:80ab0d068708 634 bool USBMouseKeyboard::move(int16_t x, int16_t y) {
samux 1:80ab0d068708 635 return update(x, y, button, 0);
samux 1:80ab0d068708 636 }
samux 1:80ab0d068708 637
samux 1:80ab0d068708 638 bool USBMouseKeyboard::scroll(int8_t z) {
samux 1:80ab0d068708 639 return update(0, 0, button, z);
samux 1:80ab0d068708 640 }
samux 1:80ab0d068708 641
samux 1:80ab0d068708 642 bool USBMouseKeyboard::doubleClick() {
samux 1:80ab0d068708 643 if (!click(MOUSE_LEFT))
samux 1:80ab0d068708 644 return false;
samux 1:80ab0d068708 645 wait(0.1);
samux 1:80ab0d068708 646 return click(MOUSE_LEFT);
samux 1:80ab0d068708 647 }
samux 1:80ab0d068708 648
samux 1:80ab0d068708 649 bool USBMouseKeyboard::click(uint8_t button) {
samux 1:80ab0d068708 650 if (!update(0, 0, button, 0))
samux 1:80ab0d068708 651 return false;
samux 1:80ab0d068708 652 wait(0.01);
samux 1:80ab0d068708 653 return update(0, 0, 0, 0);
samux 1:80ab0d068708 654 }
samux 1:80ab0d068708 655
samux 1:80ab0d068708 656 bool USBMouseKeyboard::press(uint8_t button_) {
samux 1:80ab0d068708 657 button = button_ & 0x07;
samux 1:80ab0d068708 658 return update(0, 0, button, 0);
samux 1:80ab0d068708 659 }
samux 1:80ab0d068708 660
samux 1:80ab0d068708 661 bool USBMouseKeyboard::release(uint8_t button_) {
samux 1:80ab0d068708 662 button = (button & (~button_)) & 0x07;
samux 1:80ab0d068708 663 return update(0, 0, button, 0);
samux 1:80ab0d068708 664 }
samux 1:80ab0d068708 665
samux 1:80ab0d068708 666 int USBMouseKeyboard::_putc(int c) {
samux 1:80ab0d068708 667 return keyCode(c, keymap[c].modifier);
samux 1:80ab0d068708 668 }
samux 1:80ab0d068708 669
samux 1:80ab0d068708 670 bool USBMouseKeyboard::keyCode(uint8_t key, uint8_t modifier) {
samux 1:80ab0d068708 671 // Send a simulated keyboard keypress. Returns true if successful.
samux 1:80ab0d068708 672
samux 1:80ab0d068708 673 HID_REPORT report;
samux 1:80ab0d068708 674
samux 1:80ab0d068708 675 report.data[0] = REPORT_ID_KEYBOARD;
samux 1:80ab0d068708 676 report.data[1] = modifier;
samux 1:80ab0d068708 677 report.data[2] = 0;
samux 1:80ab0d068708 678 report.data[3] = keymap[key].usage;
samux 1:80ab0d068708 679 report.data[4] = 0;
samux 1:80ab0d068708 680 report.data[5] = 0;
samux 1:80ab0d068708 681 report.data[6] = 0;
samux 1:80ab0d068708 682 report.data[7] = 0;
samux 1:80ab0d068708 683 report.data[8] = 0;
samux 1:80ab0d068708 684
samux 1:80ab0d068708 685 report.length = 9;
samux 1:80ab0d068708 686
samux 1:80ab0d068708 687 if (!send(&report)) {
samux 1:80ab0d068708 688 return false;
samux 1:80ab0d068708 689 }
samux 1:80ab0d068708 690
samux 1:80ab0d068708 691 report.data[1] = 0;
samux 1:80ab0d068708 692 report.data[3] = 0;
samux 1:80ab0d068708 693
samux 1:80ab0d068708 694 if (!send(&report)) {
samux 1:80ab0d068708 695 return false;
samux 1:80ab0d068708 696 }
samux 1:80ab0d068708 697
samux 1:80ab0d068708 698 return true;
samux 1:80ab0d068708 699
samux 1:80ab0d068708 700 }
samux 1:80ab0d068708 701
samux 1:80ab0d068708 702
samux 1:80ab0d068708 703 bool USBMouseKeyboard::mediaControl(MEDIA_KEY key) {
samux 1:80ab0d068708 704 HID_REPORT report;
samux 1:80ab0d068708 705
samux 1:80ab0d068708 706 report.data[0] = REPORT_ID_VOLUME;
samux 1:80ab0d068708 707 report.data[1] = (1 << key) & 0x7f;
samux 1:80ab0d068708 708
samux 1:80ab0d068708 709 report.length = 2;
samux 1:80ab0d068708 710
samux 1:80ab0d068708 711 send(&report);
mbed_official 25:7c72828865f3 712
samux 1:80ab0d068708 713 report.data[0] = REPORT_ID_VOLUME;
samux 1:80ab0d068708 714 report.data[1] = 0;
samux 1:80ab0d068708 715
samux 1:80ab0d068708 716 report.length = 2;
samux 1:80ab0d068708 717
samux 1:80ab0d068708 718 return send(&report);
samux 1:80ab0d068708 719 }