partly working USB Device lib for STM32F746NG Discovery both Interface are working

Dependents:   DISCO-F746NG-USB_Device McLighTT project_Keyboard_to_the_Keyboard MIDIInstrumentPADProject ... more

Committer:
DieterGraef
Date:
Sun Jul 31 17:47:35 2016 +0000
Revision:
0:0a2eaa300982
partly working USB Device library - serial and MIDI is working

Who changed what in which revision?

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