USBDevice for STM support

Dependents:   Nucleo_Usb_JoyMouse Nucleo_usbmouse ELEC350_1-referral-2018-usb-hid USBJoystick_HelloWorld2_wip ... more

This library contains all mbed usb device library (mbed-os\features\unsupported\USBDevice).

Committer:
frq08711@LMECWL0871.LME.ST.COM
Date:
Thu Dec 15 17:40:59 2016 +0100
Revision:
1:2a3ae13b45ef
copy of git hub directory unsupported/USBDevice

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 2 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 4 * and associated documentation files (the "Software"), to deal in the Software without
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 7 * Software is furnished to do so, subject to the following conditions:
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 8 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 9 * The above copyright notice and this permission notice shall be included in all copies or
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 10 * substantial portions of the Software.
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 11 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 17 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 18
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 19 #ifndef USBKEYBOARD_H
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 20 #define USBKEYBOARD_H
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 21
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 22 #include "USBHID.h"
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 23 #include "Stream.h"
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 24
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 25 /* Modifiers */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 26 enum MODIFIER_KEY {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 27 KEY_CTRL = 1,
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 28 KEY_SHIFT = 2,
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 29 KEY_ALT = 4,
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 30 };
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 31
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 32
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 33 enum MEDIA_KEY {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 34 KEY_NEXT_TRACK, /*!< next Track Button */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 35 KEY_PREVIOUS_TRACK, /*!< Previous track Button */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 36 KEY_STOP, /*!< Stop Button */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 37 KEY_PLAY_PAUSE, /*!< Play/Pause Button */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 38 KEY_MUTE, /*!< Mute Button */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 39 KEY_VOLUME_UP, /*!< Volume Up Button */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 40 KEY_VOLUME_DOWN, /*!< Volume Down Button */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 41 };
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 42
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 43 enum FUNCTION_KEY {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 44 KEY_F1 = 128, /* F1 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 45 KEY_F2, /* F2 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 46 KEY_F3, /* F3 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 47 KEY_F4, /* F4 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 48 KEY_F5, /* F5 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 49 KEY_F6, /* F6 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 50 KEY_F7, /* F7 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 51 KEY_F8, /* F8 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 52 KEY_F9, /* F9 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 53 KEY_F10, /* F10 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 54 KEY_F11, /* F11 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 55 KEY_F12, /* F12 key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 56
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 57 KEY_PRINT_SCREEN, /* Print Screen key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 58 KEY_SCROLL_LOCK, /* Scroll lock */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 59 KEY_CAPS_LOCK, /* caps lock */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 60 KEY_NUM_LOCK, /* num lock */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 61 KEY_INSERT, /* Insert key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 62 KEY_HOME, /* Home key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 63 KEY_PAGE_UP, /* Page Up key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 64 KEY_PAGE_DOWN, /* Page Down key */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 65
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 66 RIGHT_ARROW, /* Right arrow */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 67 LEFT_ARROW, /* Left arrow */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 68 DOWN_ARROW, /* Down arrow */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 69 UP_ARROW, /* Up arrow */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 70 };
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 71
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 72 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 73 * USBKeyboard example
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 74 * @code
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 75 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 76 * #include "mbed.h"
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 77 * #include "USBKeyboard.h"
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 78 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 79 * USBKeyboard key;
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 80 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 81 * int main(void)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 82 * {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 83 * while (1)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 84 * {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 85 * key.printf("Hello World\r\n");
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 86 * wait(1);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 87 * }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 88 * }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 89 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 90 * @endcode
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 91 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 92 class USBKeyboard: public USBHID, public Stream {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 93 public:
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 94
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 95 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 96 * Constructor
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 97 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 98 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 99 * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 100 * @param vendor_id Your vendor_id (default: 0x1235)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 101 * @param product_id Your product_id (default: 0x0050)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 102 * @param product_release Your preoduct_release (default: 0x0001)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 103 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 104 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 105 USBKeyboard(uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001):
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 106 USBHID(0, 0, vendor_id, product_id, product_release, false) {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 107 lock_status = 0;
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 108 connect();
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 109 };
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 110
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 111 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 112 * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 113 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 114 * @code
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 115 * //To send CTRL + s (save)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 116 * keyboard.keyCode('s', KEY_CTRL);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 117 * @endcode
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 118 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 119 * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 120 * @param key character to send
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 121 * @returns true if there is no error, false otherwise
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 122 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 123 bool keyCode(uint8_t key, uint8_t modifier = 0);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 124
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 125 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 126 * Send a character
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 127 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 128 * @param c character to be sent
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 129 * @returns true if there is no error, false otherwise
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 130 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 131 virtual int _putc(int c);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 132
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 133 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 134 * Control media keys
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 135 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 136 * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 137 * @returns true if there is no error, false otherwise
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 138 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 139 bool mediaControl(MEDIA_KEY key);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 140
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 141 /*
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 142 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 143 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 144 * @returns pointer to the report descriptor
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 145 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 146 virtual uint8_t * reportDesc();
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 147
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 148 /*
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 149 * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 150 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 151 * @returns if handle by subclass, return true
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 152 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 153 virtual bool EPINT_OUT_callback();
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 154
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 155 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 156 * Read status of lock keys. Useful to switch-on/off leds according to key pressed. Only the first three bits of the result is important:
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 157 * - First bit: NUM_LOCK
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 158 * - Second bit: CAPS_LOCK
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 159 * - Third bit: SCROLL_LOCK
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 160 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 161 * @returns status of lock keys
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 162 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 163 uint8_t lockStatus();
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 164
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 165 protected:
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 166 /*
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 167 * Get configuration descriptor
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 168 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 169 * @returns pointer to the configuration descriptor
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 170 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 171 virtual uint8_t * configurationDesc();
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 172
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 173 private:
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 174 //dummy otherwise it doesn,t compile (we must define all methods of an abstract class)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 175 virtual int _getc() {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 176 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 177 };
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 178
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 179 uint8_t lock_status;
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 180
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 181 };
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 182
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 183 #endif