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 #include "stdint.h"
DieterGraef 0:0a2eaa300982 20 #include "USBMouse.h"
DieterGraef 0:0a2eaa300982 21
DieterGraef 0:0a2eaa300982 22 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
DieterGraef 0:0a2eaa300982 23 switch (mouse_type) {
DieterGraef 0:0a2eaa300982 24 case REL_MOUSE:
DieterGraef 0:0a2eaa300982 25 while (x > 127) {
DieterGraef 0:0a2eaa300982 26 if (!mouseSend(127, 0, button, z)) return false;
DieterGraef 0:0a2eaa300982 27 x = x - 127;
DieterGraef 0:0a2eaa300982 28 }
DieterGraef 0:0a2eaa300982 29 while (x < -128) {
DieterGraef 0:0a2eaa300982 30 if (!mouseSend(-128, 0, button, z)) return false;
DieterGraef 0:0a2eaa300982 31 x = x + 128;
DieterGraef 0:0a2eaa300982 32 }
DieterGraef 0:0a2eaa300982 33 while (y > 127) {
DieterGraef 0:0a2eaa300982 34 if (!mouseSend(0, 127, button, z)) return false;
DieterGraef 0:0a2eaa300982 35 y = y - 127;
DieterGraef 0:0a2eaa300982 36 }
DieterGraef 0:0a2eaa300982 37 while (y < -128) {
DieterGraef 0:0a2eaa300982 38 if (!mouseSend(0, -128, button, z)) return false;
DieterGraef 0:0a2eaa300982 39 y = y + 128;
DieterGraef 0:0a2eaa300982 40 }
DieterGraef 0:0a2eaa300982 41 return mouseSend(x, y, button, z);
DieterGraef 0:0a2eaa300982 42 case ABS_MOUSE:
DieterGraef 0:0a2eaa300982 43 HID_REPORT report;
DieterGraef 0:0a2eaa300982 44
DieterGraef 0:0a2eaa300982 45 report.data[0] = x & 0xff;
DieterGraef 0:0a2eaa300982 46 report.data[1] = (x >> 8) & 0xff;
DieterGraef 0:0a2eaa300982 47 report.data[2] = y & 0xff;
DieterGraef 0:0a2eaa300982 48 report.data[3] = (y >> 8) & 0xff;
DieterGraef 0:0a2eaa300982 49 report.data[4] = -z;
DieterGraef 0:0a2eaa300982 50 report.data[5] = button & 0x07;
DieterGraef 0:0a2eaa300982 51
DieterGraef 0:0a2eaa300982 52 report.length = 6;
DieterGraef 0:0a2eaa300982 53
DieterGraef 0:0a2eaa300982 54 return send(&report);
DieterGraef 0:0a2eaa300982 55 default:
DieterGraef 0:0a2eaa300982 56 return false;
DieterGraef 0:0a2eaa300982 57 }
DieterGraef 0:0a2eaa300982 58 }
DieterGraef 0:0a2eaa300982 59
DieterGraef 0:0a2eaa300982 60 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
DieterGraef 0:0a2eaa300982 61 HID_REPORT report;
DieterGraef 0:0a2eaa300982 62 report.data[0] = buttons & 0x07;
DieterGraef 0:0a2eaa300982 63 report.data[1] = x;
DieterGraef 0:0a2eaa300982 64 report.data[2] = y;
DieterGraef 0:0a2eaa300982 65 report.data[3] = -z; // >0 to scroll down, <0 to scroll up
DieterGraef 0:0a2eaa300982 66
DieterGraef 0:0a2eaa300982 67 report.length = 4;
DieterGraef 0:0a2eaa300982 68
DieterGraef 0:0a2eaa300982 69 return send(&report);
DieterGraef 0:0a2eaa300982 70 }
DieterGraef 0:0a2eaa300982 71
DieterGraef 0:0a2eaa300982 72 bool USBMouse::move(int16_t x, int16_t y) {
DieterGraef 0:0a2eaa300982 73 return update(x, y, button, 0);
DieterGraef 0:0a2eaa300982 74 }
DieterGraef 0:0a2eaa300982 75
DieterGraef 0:0a2eaa300982 76 bool USBMouse::scroll(int8_t z) {
DieterGraef 0:0a2eaa300982 77 return update(0, 0, button, z);
DieterGraef 0:0a2eaa300982 78 }
DieterGraef 0:0a2eaa300982 79
DieterGraef 0:0a2eaa300982 80
DieterGraef 0:0a2eaa300982 81 bool USBMouse::doubleClick() {
DieterGraef 0:0a2eaa300982 82 if (!click(MOUSE_LEFT))
DieterGraef 0:0a2eaa300982 83 return false;
DieterGraef 0:0a2eaa300982 84 wait(0.1);
DieterGraef 0:0a2eaa300982 85 return click(MOUSE_LEFT);
DieterGraef 0:0a2eaa300982 86 }
DieterGraef 0:0a2eaa300982 87
DieterGraef 0:0a2eaa300982 88 bool USBMouse::click(uint8_t button) {
DieterGraef 0:0a2eaa300982 89 if (!update(0, 0, button, 0))
DieterGraef 0:0a2eaa300982 90 return false;
DieterGraef 0:0a2eaa300982 91 wait(0.01);
DieterGraef 0:0a2eaa300982 92 return update(0, 0, 0, 0);
DieterGraef 0:0a2eaa300982 93 }
DieterGraef 0:0a2eaa300982 94
DieterGraef 0:0a2eaa300982 95 bool USBMouse::press(uint8_t button_) {
DieterGraef 0:0a2eaa300982 96 button = button_ & 0x07;
DieterGraef 0:0a2eaa300982 97 return update(0, 0, button, 0);
DieterGraef 0:0a2eaa300982 98 }
DieterGraef 0:0a2eaa300982 99
DieterGraef 0:0a2eaa300982 100 bool USBMouse::release(uint8_t button_) {
DieterGraef 0:0a2eaa300982 101 button = (button & (~button_)) & 0x07;
DieterGraef 0:0a2eaa300982 102 return update(0, 0, button, 0);
DieterGraef 0:0a2eaa300982 103 }
DieterGraef 0:0a2eaa300982 104
DieterGraef 0:0a2eaa300982 105
DieterGraef 0:0a2eaa300982 106 uint8_t * USBMouse::reportDesc() {
DieterGraef 0:0a2eaa300982 107
DieterGraef 0:0a2eaa300982 108 if (mouse_type == REL_MOUSE) {
DieterGraef 0:0a2eaa300982 109 static uint8_t reportDescriptor[] = {
DieterGraef 0:0a2eaa300982 110 USAGE_PAGE(1), 0x01, // Genric Desktop
DieterGraef 0:0a2eaa300982 111 USAGE(1), 0x02, // Mouse
DieterGraef 0:0a2eaa300982 112 COLLECTION(1), 0x01, // Application
DieterGraef 0:0a2eaa300982 113 USAGE(1), 0x01, // Pointer
DieterGraef 0:0a2eaa300982 114 COLLECTION(1), 0x00, // Physical
DieterGraef 0:0a2eaa300982 115
DieterGraef 0:0a2eaa300982 116 REPORT_COUNT(1), 0x03,
DieterGraef 0:0a2eaa300982 117 REPORT_SIZE(1), 0x01,
DieterGraef 0:0a2eaa300982 118 USAGE_PAGE(1), 0x09, // Buttons
DieterGraef 0:0a2eaa300982 119 USAGE_MINIMUM(1), 0x1,
DieterGraef 0:0a2eaa300982 120 USAGE_MAXIMUM(1), 0x3,
DieterGraef 0:0a2eaa300982 121 LOGICAL_MINIMUM(1), 0x00,
DieterGraef 0:0a2eaa300982 122 LOGICAL_MAXIMUM(1), 0x01,
DieterGraef 0:0a2eaa300982 123 INPUT(1), 0x02,
DieterGraef 0:0a2eaa300982 124 REPORT_COUNT(1), 0x01,
DieterGraef 0:0a2eaa300982 125 REPORT_SIZE(1), 0x05,
DieterGraef 0:0a2eaa300982 126 INPUT(1), 0x01,
DieterGraef 0:0a2eaa300982 127
DieterGraef 0:0a2eaa300982 128 REPORT_COUNT(1), 0x03,
DieterGraef 0:0a2eaa300982 129 REPORT_SIZE(1), 0x08,
DieterGraef 0:0a2eaa300982 130 USAGE_PAGE(1), 0x01,
DieterGraef 0:0a2eaa300982 131 USAGE(1), 0x30, // X
DieterGraef 0:0a2eaa300982 132 USAGE(1), 0x31, // Y
DieterGraef 0:0a2eaa300982 133 USAGE(1), 0x38, // scroll
DieterGraef 0:0a2eaa300982 134 LOGICAL_MINIMUM(1), 0x81,
DieterGraef 0:0a2eaa300982 135 LOGICAL_MAXIMUM(1), 0x7f,
DieterGraef 0:0a2eaa300982 136 INPUT(1), 0x06, // Relative data
DieterGraef 0:0a2eaa300982 137
DieterGraef 0:0a2eaa300982 138 END_COLLECTION(0),
DieterGraef 0:0a2eaa300982 139 END_COLLECTION(0),
DieterGraef 0:0a2eaa300982 140 };
DieterGraef 0:0a2eaa300982 141 reportLength = sizeof(reportDescriptor);
DieterGraef 0:0a2eaa300982 142 return reportDescriptor;
DieterGraef 0:0a2eaa300982 143 } else if (mouse_type == ABS_MOUSE) {
DieterGraef 0:0a2eaa300982 144 static uint8_t reportDescriptor[] = {
DieterGraef 0:0a2eaa300982 145
DieterGraef 0:0a2eaa300982 146 USAGE_PAGE(1), 0x01, // Generic Desktop
DieterGraef 0:0a2eaa300982 147 USAGE(1), 0x02, // Mouse
DieterGraef 0:0a2eaa300982 148 COLLECTION(1), 0x01, // Application
DieterGraef 0:0a2eaa300982 149 USAGE(1), 0x01, // Pointer
DieterGraef 0:0a2eaa300982 150 COLLECTION(1), 0x00, // Physical
DieterGraef 0:0a2eaa300982 151
DieterGraef 0:0a2eaa300982 152 USAGE_PAGE(1), 0x01, // Generic Desktop
DieterGraef 0:0a2eaa300982 153 USAGE(1), 0x30, // X
DieterGraef 0:0a2eaa300982 154 USAGE(1), 0x31, // Y
DieterGraef 0:0a2eaa300982 155 LOGICAL_MINIMUM(1), 0x00, // 0
DieterGraef 0:0a2eaa300982 156 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
DieterGraef 0:0a2eaa300982 157 REPORT_SIZE(1), 0x10,
DieterGraef 0:0a2eaa300982 158 REPORT_COUNT(1), 0x02,
DieterGraef 0:0a2eaa300982 159 INPUT(1), 0x02, // Data, Variable, Absolute
DieterGraef 0:0a2eaa300982 160
DieterGraef 0:0a2eaa300982 161 USAGE_PAGE(1), 0x01, // Generic Desktop
DieterGraef 0:0a2eaa300982 162 USAGE(1), 0x38, // scroll
DieterGraef 0:0a2eaa300982 163 LOGICAL_MINIMUM(1), 0x81, // -127
DieterGraef 0:0a2eaa300982 164 LOGICAL_MAXIMUM(1), 0x7f, // 127
DieterGraef 0:0a2eaa300982 165 REPORT_SIZE(1), 0x08,
DieterGraef 0:0a2eaa300982 166 REPORT_COUNT(1), 0x01,
DieterGraef 0:0a2eaa300982 167 INPUT(1), 0x06, // Data, Variable, Relative
DieterGraef 0:0a2eaa300982 168
DieterGraef 0:0a2eaa300982 169 USAGE_PAGE(1), 0x09, // Buttons
DieterGraef 0:0a2eaa300982 170 USAGE_MINIMUM(1), 0x01,
DieterGraef 0:0a2eaa300982 171 USAGE_MAXIMUM(1), 0x03,
DieterGraef 0:0a2eaa300982 172 LOGICAL_MINIMUM(1), 0x00, // 0
DieterGraef 0:0a2eaa300982 173 LOGICAL_MAXIMUM(1), 0x01, // 1
DieterGraef 0:0a2eaa300982 174 REPORT_COUNT(1), 0x03,
DieterGraef 0:0a2eaa300982 175 REPORT_SIZE(1), 0x01,
DieterGraef 0:0a2eaa300982 176 INPUT(1), 0x02, // Data, Variable, Absolute
DieterGraef 0:0a2eaa300982 177 REPORT_COUNT(1), 0x01,
DieterGraef 0:0a2eaa300982 178 REPORT_SIZE(1), 0x05,
DieterGraef 0:0a2eaa300982 179 INPUT(1), 0x01, // Constant
DieterGraef 0:0a2eaa300982 180
DieterGraef 0:0a2eaa300982 181 END_COLLECTION(0),
DieterGraef 0:0a2eaa300982 182 END_COLLECTION(0)
DieterGraef 0:0a2eaa300982 183 };
DieterGraef 0:0a2eaa300982 184 reportLength = sizeof(reportDescriptor);
DieterGraef 0:0a2eaa300982 185 return reportDescriptor;
DieterGraef 0:0a2eaa300982 186 }
DieterGraef 0:0a2eaa300982 187 return NULL;
DieterGraef 0:0a2eaa300982 188 }
DieterGraef 0:0a2eaa300982 189
DieterGraef 0:0a2eaa300982 190 #define DEFAULT_CONFIGURATION (1)
DieterGraef 0:0a2eaa300982 191 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
DieterGraef 0:0a2eaa300982 192 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
DieterGraef 0:0a2eaa300982 193 + (1 * HID_DESCRIPTOR_LENGTH) \
DieterGraef 0:0a2eaa300982 194 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
DieterGraef 0:0a2eaa300982 195
DieterGraef 0:0a2eaa300982 196 uint8_t * USBMouse::configurationDesc() {
DieterGraef 0:0a2eaa300982 197 static uint8_t configurationDescriptor[] = {
DieterGraef 0:0a2eaa300982 198 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
DieterGraef 0:0a2eaa300982 199 CONFIGURATION_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 200 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
DieterGraef 0:0a2eaa300982 201 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
DieterGraef 0:0a2eaa300982 202 0x01, // bNumInterfaces
DieterGraef 0:0a2eaa300982 203 DEFAULT_CONFIGURATION, // bConfigurationValue
DieterGraef 0:0a2eaa300982 204 0x00, // iConfiguration
DieterGraef 0:0a2eaa300982 205 C_RESERVED | C_SELF_POWERED, // bmAttributes
DieterGraef 0:0a2eaa300982 206 C_POWER(0), // bMaxPowerHello World from Mbed
DieterGraef 0:0a2eaa300982 207
DieterGraef 0:0a2eaa300982 208 INTERFACE_DESCRIPTOR_LENGTH, // bLength
DieterGraef 0:0a2eaa300982 209 INTERFACE_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 210 0x00, // bInterfaceNumber
DieterGraef 0:0a2eaa300982 211 0x00, // bAlternateSetting
DieterGraef 0:0a2eaa300982 212 0x02, // bNumEndpoints
DieterGraef 0:0a2eaa300982 213 HID_CLASS, // bInterfaceClass
DieterGraef 0:0a2eaa300982 214 1, // bInterfaceSubClass
DieterGraef 0:0a2eaa300982 215 2, // bInterfaceProtocol (mouse)
DieterGraef 0:0a2eaa300982 216 0x00, // iInterface
DieterGraef 0:0a2eaa300982 217
DieterGraef 0:0a2eaa300982 218 HID_DESCRIPTOR_LENGTH, // bLength
DieterGraef 0:0a2eaa300982 219 HID_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 220 LSB(HID_VERSION_1_11), // bcdHID (LSB)
DieterGraef 0:0a2eaa300982 221 MSB(HID_VERSION_1_11), // bcdHID (MSB)
DieterGraef 0:0a2eaa300982 222 0x00, // bCountryCode
DieterGraef 0:0a2eaa300982 223 0x01, // bNumDescriptors
DieterGraef 0:0a2eaa300982 224 REPORT_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 225 (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB)
DieterGraef 0:0a2eaa300982 226 (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB)
DieterGraef 0:0a2eaa300982 227
DieterGraef 0:0a2eaa300982 228 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
DieterGraef 0:0a2eaa300982 229 ENDPOINT_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 230 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
DieterGraef 0:0a2eaa300982 231 E_INTERRUPT, // bmAttributes
DieterGraef 0:0a2eaa300982 232 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
DieterGraef 0:0a2eaa300982 233 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
DieterGraef 0:0a2eaa300982 234 1, // bInterval (milliseconds)
DieterGraef 0:0a2eaa300982 235
DieterGraef 0:0a2eaa300982 236 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
DieterGraef 0:0a2eaa300982 237 ENDPOINT_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 238 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
DieterGraef 0:0a2eaa300982 239 E_INTERRUPT, // bmAttributes
DieterGraef 0:0a2eaa300982 240 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
DieterGraef 0:0a2eaa300982 241 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
DieterGraef 0:0a2eaa300982 242 1, // bInterval (milliseconds)
DieterGraef 0:0a2eaa300982 243 };
DieterGraef 0:0a2eaa300982 244 return configurationDescriptor;
DieterGraef 0:0a2eaa300982 245 }