手動機アーム、mbed基盤のspiをおくるだけのプログラムです(9/4)
Dependencies: SPI_master_arm_shudouki mbed
Fork of SPI_master_arm_shudouki2 by
USBHost.h@0:76d1c7f13415, 2017-08-21 (annotated)
- Committer:
- yoka06
- Date:
- Mon Aug 21 08:49:06 2017 +0000
- Revision:
- 0:76d1c7f13415
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yoka06 | 0:76d1c7f13415 | 1 | |
yoka06 | 0:76d1c7f13415 | 2 | /* |
yoka06 | 0:76d1c7f13415 | 3 | Copyright (c) 2010 Peter Barrett |
yoka06 | 0:76d1c7f13415 | 4 | |
yoka06 | 0:76d1c7f13415 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
yoka06 | 0:76d1c7f13415 | 6 | of this software and associated documentation files (the "Software"), to deal |
yoka06 | 0:76d1c7f13415 | 7 | in the Software without restriction, including without limitation the rights |
yoka06 | 0:76d1c7f13415 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yoka06 | 0:76d1c7f13415 | 9 | copies of the Software, and to permit persons to whom the Software is |
yoka06 | 0:76d1c7f13415 | 10 | furnished to do so, subject to the following conditions: |
yoka06 | 0:76d1c7f13415 | 11 | |
yoka06 | 0:76d1c7f13415 | 12 | The above copyright notice and this permission notice shall be included in |
yoka06 | 0:76d1c7f13415 | 13 | all copies or substantial portions of the Software. |
yoka06 | 0:76d1c7f13415 | 14 | |
yoka06 | 0:76d1c7f13415 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yoka06 | 0:76d1c7f13415 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yoka06 | 0:76d1c7f13415 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yoka06 | 0:76d1c7f13415 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yoka06 | 0:76d1c7f13415 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yoka06 | 0:76d1c7f13415 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yoka06 | 0:76d1c7f13415 | 21 | THE SOFTWARE. |
yoka06 | 0:76d1c7f13415 | 22 | */ |
yoka06 | 0:76d1c7f13415 | 23 | |
yoka06 | 0:76d1c7f13415 | 24 | #ifndef USBHOST_H |
yoka06 | 0:76d1c7f13415 | 25 | #define USBHOST_H |
yoka06 | 0:76d1c7f13415 | 26 | |
yoka06 | 0:76d1c7f13415 | 27 | #ifndef u8 |
yoka06 | 0:76d1c7f13415 | 28 | typedef unsigned char u8; |
yoka06 | 0:76d1c7f13415 | 29 | typedef unsigned short u16; |
yoka06 | 0:76d1c7f13415 | 30 | typedef unsigned long u32; |
yoka06 | 0:76d1c7f13415 | 31 | |
yoka06 | 0:76d1c7f13415 | 32 | typedef char s8; |
yoka06 | 0:76d1c7f13415 | 33 | typedef short s16; |
yoka06 | 0:76d1c7f13415 | 34 | typedef char s32; |
yoka06 | 0:76d1c7f13415 | 35 | #endif |
yoka06 | 0:76d1c7f13415 | 36 | |
yoka06 | 0:76d1c7f13415 | 37 | #define ENDPOINT_CONTROL 0 |
yoka06 | 0:76d1c7f13415 | 38 | #define ENDPOINT_ISOCRONOUS 1 |
yoka06 | 0:76d1c7f13415 | 39 | #define ENDPOINT_BULK 2 |
yoka06 | 0:76d1c7f13415 | 40 | #define ENDPOINT_INTERRUPT 3 |
yoka06 | 0:76d1c7f13415 | 41 | |
yoka06 | 0:76d1c7f13415 | 42 | #define DESCRIPTOR_TYPE_DEVICE 1 |
yoka06 | 0:76d1c7f13415 | 43 | #define DESCRIPTOR_TYPE_CONFIGURATION 2 |
yoka06 | 0:76d1c7f13415 | 44 | #define DESCRIPTOR_TYPE_STRING 3 |
yoka06 | 0:76d1c7f13415 | 45 | #define DESCRIPTOR_TYPE_INTERFACE 4 |
yoka06 | 0:76d1c7f13415 | 46 | #define DESCRIPTOR_TYPE_ENDPOINT 5 |
yoka06 | 0:76d1c7f13415 | 47 | |
yoka06 | 0:76d1c7f13415 | 48 | #define DESCRIPTOR_TYPE_HID 0x21 |
yoka06 | 0:76d1c7f13415 | 49 | #define DESCRIPTOR_TYPE_REPORT 0x22 |
yoka06 | 0:76d1c7f13415 | 50 | #define DESCRIPTOR_TYPE_PHYSICAL 0x23 |
yoka06 | 0:76d1c7f13415 | 51 | #define DESCRIPTOR_TYPE_HUB 0x29 |
yoka06 | 0:76d1c7f13415 | 52 | |
yoka06 | 0:76d1c7f13415 | 53 | enum USB_CLASS_CODE |
yoka06 | 0:76d1c7f13415 | 54 | { |
yoka06 | 0:76d1c7f13415 | 55 | CLASS_DEVICE, |
yoka06 | 0:76d1c7f13415 | 56 | CLASS_AUDIO, |
yoka06 | 0:76d1c7f13415 | 57 | CLASS_COMM_AND_CDC_CONTROL, |
yoka06 | 0:76d1c7f13415 | 58 | CLASS_HID, |
yoka06 | 0:76d1c7f13415 | 59 | CLASS_PHYSICAL = 0x05, |
yoka06 | 0:76d1c7f13415 | 60 | CLASS_STILL_IMAGING, |
yoka06 | 0:76d1c7f13415 | 61 | CLASS_PRINTER, |
yoka06 | 0:76d1c7f13415 | 62 | CLASS_MASS_STORAGE, |
yoka06 | 0:76d1c7f13415 | 63 | CLASS_HUB, |
yoka06 | 0:76d1c7f13415 | 64 | CLASS_CDC_DATA, |
yoka06 | 0:76d1c7f13415 | 65 | CLASS_SMART_CARD, |
yoka06 | 0:76d1c7f13415 | 66 | CLASS_CONTENT_SECURITY = 0x0D, |
yoka06 | 0:76d1c7f13415 | 67 | CLASS_VIDEO = 0x0E, |
yoka06 | 0:76d1c7f13415 | 68 | CLASS_DIAGNOSTIC_DEVICE = 0xDC, |
yoka06 | 0:76d1c7f13415 | 69 | CLASS_WIRELESS_CONTROLLER = 0xE0, |
yoka06 | 0:76d1c7f13415 | 70 | CLASS_MISCELLANEOUS = 0xEF, |
yoka06 | 0:76d1c7f13415 | 71 | CLASS_APP_SPECIFIC = 0xFE, |
yoka06 | 0:76d1c7f13415 | 72 | CLASS_VENDOR_SPECIFIC = 0xFF |
yoka06 | 0:76d1c7f13415 | 73 | }; |
yoka06 | 0:76d1c7f13415 | 74 | |
yoka06 | 0:76d1c7f13415 | 75 | #define DEVICE_TO_HOST 0x80 |
yoka06 | 0:76d1c7f13415 | 76 | #define HOST_TO_DEVICE 0x00 |
yoka06 | 0:76d1c7f13415 | 77 | #define REQUEST_TYPE_STANDARD 0x00 |
yoka06 | 0:76d1c7f13415 | 78 | #define REQUEST_TYPE_CLASS 0x20 |
yoka06 | 0:76d1c7f13415 | 79 | #define REQUEST_TYPE_VENDOR 0x40 |
yoka06 | 0:76d1c7f13415 | 80 | #define RECIPIENT_DEVICE 0x00 |
yoka06 | 0:76d1c7f13415 | 81 | #define RECIPIENT_INTERFACE 0x01 |
yoka06 | 0:76d1c7f13415 | 82 | #define RECIPIENT_ENDPOINT 0x02 |
yoka06 | 0:76d1c7f13415 | 83 | #define RECIPIENT_OTHER 0x03 |
yoka06 | 0:76d1c7f13415 | 84 | |
yoka06 | 0:76d1c7f13415 | 85 | #define GET_STATUS 0 |
yoka06 | 0:76d1c7f13415 | 86 | #define CLEAR_FEATURE 1 |
yoka06 | 0:76d1c7f13415 | 87 | #define SET_FEATURE 3 |
yoka06 | 0:76d1c7f13415 | 88 | #define SET_ADDRESS 5 |
yoka06 | 0:76d1c7f13415 | 89 | #define GET_DESCRIPTOR 6 |
yoka06 | 0:76d1c7f13415 | 90 | #define SET_DESCRIPTOR 7 |
yoka06 | 0:76d1c7f13415 | 91 | #define GET_CONFIGURATION 8 |
yoka06 | 0:76d1c7f13415 | 92 | #define SET_CONFIGURATION 9 |
yoka06 | 0:76d1c7f13415 | 93 | #define GET_INTERFACE 10 |
yoka06 | 0:76d1c7f13415 | 94 | #define SET_INTERFACE 11 |
yoka06 | 0:76d1c7f13415 | 95 | #define SYNCH_FRAME 11 |
yoka06 | 0:76d1c7f13415 | 96 | |
yoka06 | 0:76d1c7f13415 | 97 | /* HID Request Codes */ |
yoka06 | 0:76d1c7f13415 | 98 | #define HID_REQUEST_GET_REPORT 0x01 |
yoka06 | 0:76d1c7f13415 | 99 | #define HID_REQUEST_GET_IDLE 0x02 |
yoka06 | 0:76d1c7f13415 | 100 | #define HID_REQUEST_GET_PROTOCOL 0x03 |
yoka06 | 0:76d1c7f13415 | 101 | #define HID_REQUEST_SET_REPORT 0x09 |
yoka06 | 0:76d1c7f13415 | 102 | #define HID_REQUEST_SET_IDLE 0x0A |
yoka06 | 0:76d1c7f13415 | 103 | #define HID_REQUEST_SET_PROTOCOL 0x0B |
yoka06 | 0:76d1c7f13415 | 104 | |
yoka06 | 0:76d1c7f13415 | 105 | /* HID Report Types */ |
yoka06 | 0:76d1c7f13415 | 106 | #define HID_REPORT_INPUT 0x01 |
yoka06 | 0:76d1c7f13415 | 107 | #define HID_REPORT_OUTPUT 0x02 |
yoka06 | 0:76d1c7f13415 | 108 | #define HID_REPORT_FEATURE 0x03 |
yoka06 | 0:76d1c7f13415 | 109 | |
yoka06 | 0:76d1c7f13415 | 110 | |
yoka06 | 0:76d1c7f13415 | 111 | // -5 is nak |
yoka06 | 0:76d1c7f13415 | 112 | /* |
yoka06 | 0:76d1c7f13415 | 113 | 0010 ACK Handshake |
yoka06 | 0:76d1c7f13415 | 114 | 1010 NAK Handshake |
yoka06 | 0:76d1c7f13415 | 115 | 1110 STALL Handshake |
yoka06 | 0:76d1c7f13415 | 116 | 0110 NYET (No Response Yet) |
yoka06 | 0:76d1c7f13415 | 117 | */ |
yoka06 | 0:76d1c7f13415 | 118 | |
yoka06 | 0:76d1c7f13415 | 119 | #define IO_PENDING -100 |
yoka06 | 0:76d1c7f13415 | 120 | #define ERR_ENDPOINT_NONE_LEFT -101 |
yoka06 | 0:76d1c7f13415 | 121 | #define ERR_ENDPOINT_NOT_FOUND -102 |
yoka06 | 0:76d1c7f13415 | 122 | #define ERR_DEVICE_NOT_FOUND -103 |
yoka06 | 0:76d1c7f13415 | 123 | #define ERR_DEVICE_NONE_LEFT -104 |
yoka06 | 0:76d1c7f13415 | 124 | #define ERR_HUB_INIT_FAILED -105 |
yoka06 | 0:76d1c7f13415 | 125 | #define ERR_INTERFACE_NOT_FOUND -106 |
yoka06 | 0:76d1c7f13415 | 126 | |
yoka06 | 0:76d1c7f13415 | 127 | typedef struct |
yoka06 | 0:76d1c7f13415 | 128 | { |
yoka06 | 0:76d1c7f13415 | 129 | u8 bLength; |
yoka06 | 0:76d1c7f13415 | 130 | u8 bDescriptorType; |
yoka06 | 0:76d1c7f13415 | 131 | u16 bcdUSB; |
yoka06 | 0:76d1c7f13415 | 132 | u8 bDeviceClass; |
yoka06 | 0:76d1c7f13415 | 133 | u8 bDeviceSubClass; |
yoka06 | 0:76d1c7f13415 | 134 | u8 bDeviceProtocol; |
yoka06 | 0:76d1c7f13415 | 135 | u8 bMaxPacketSize; |
yoka06 | 0:76d1c7f13415 | 136 | u16 idVendor; |
yoka06 | 0:76d1c7f13415 | 137 | u16 idProduct; |
yoka06 | 0:76d1c7f13415 | 138 | u16 bcdDevice; // version |
yoka06 | 0:76d1c7f13415 | 139 | u8 iManufacturer; |
yoka06 | 0:76d1c7f13415 | 140 | u8 iProduct; |
yoka06 | 0:76d1c7f13415 | 141 | u8 iSerialNumber; |
yoka06 | 0:76d1c7f13415 | 142 | u8 bNumConfigurations; |
yoka06 | 0:76d1c7f13415 | 143 | } DeviceDescriptor; // 16 bytes |
yoka06 | 0:76d1c7f13415 | 144 | |
yoka06 | 0:76d1c7f13415 | 145 | typedef struct |
yoka06 | 0:76d1c7f13415 | 146 | { |
yoka06 | 0:76d1c7f13415 | 147 | u8 bLength; |
yoka06 | 0:76d1c7f13415 | 148 | u8 bDescriptorType; |
yoka06 | 0:76d1c7f13415 | 149 | u16 wTotalLength; |
yoka06 | 0:76d1c7f13415 | 150 | u8 bNumInterfaces; |
yoka06 | 0:76d1c7f13415 | 151 | u8 bConfigurationValue; // Value to use as an argument to select this configuration |
yoka06 | 0:76d1c7f13415 | 152 | u8 iConfiguration; // Index of String Descriptor describing this configuration |
yoka06 | 0:76d1c7f13415 | 153 | u8 bmAttributes; // Bitmap D7 Reserved, set to 1. (USB 1.0 Bus Powered),D6 Self Powered,D5 Remote Wakeup,D4..0 = 0 |
yoka06 | 0:76d1c7f13415 | 154 | u8 bMaxPower; // Maximum Power Consumption in 2mA units |
yoka06 | 0:76d1c7f13415 | 155 | } ConfigurationDescriptor; |
yoka06 | 0:76d1c7f13415 | 156 | |
yoka06 | 0:76d1c7f13415 | 157 | typedef struct |
yoka06 | 0:76d1c7f13415 | 158 | { |
yoka06 | 0:76d1c7f13415 | 159 | u8 bLength; |
yoka06 | 0:76d1c7f13415 | 160 | u8 bDescriptorType; |
yoka06 | 0:76d1c7f13415 | 161 | u8 bInterfaceNumber; |
yoka06 | 0:76d1c7f13415 | 162 | u8 bAlternateSetting; |
yoka06 | 0:76d1c7f13415 | 163 | u8 bNumEndpoints; |
yoka06 | 0:76d1c7f13415 | 164 | u8 bInterfaceClass; |
yoka06 | 0:76d1c7f13415 | 165 | u8 bInterfaceSubClass; |
yoka06 | 0:76d1c7f13415 | 166 | u8 bInterfaceProtocol; |
yoka06 | 0:76d1c7f13415 | 167 | u8 iInterface; // Index of String Descriptor Describing this interface |
yoka06 | 0:76d1c7f13415 | 168 | } InterfaceDescriptor; |
yoka06 | 0:76d1c7f13415 | 169 | |
yoka06 | 0:76d1c7f13415 | 170 | typedef struct |
yoka06 | 0:76d1c7f13415 | 171 | { |
yoka06 | 0:76d1c7f13415 | 172 | u8 bLength; |
yoka06 | 0:76d1c7f13415 | 173 | u8 bDescriptorType; |
yoka06 | 0:76d1c7f13415 | 174 | u8 bEndpointAddress; // Bits 0:3 endpoint, Bits 7 Direction 0 = Out, 1 = In (Ignored for Control Endpoints) |
yoka06 | 0:76d1c7f13415 | 175 | u8 bmAttributes; // Bits 0:1 00 = Control, 01 = Isochronous, 10 = Bulk, 11 = Interrupt |
yoka06 | 0:76d1c7f13415 | 176 | u16 wMaxPacketSize; |
yoka06 | 0:76d1c7f13415 | 177 | u8 bInterval; // Interval for polling endpoint data transfers. |
yoka06 | 0:76d1c7f13415 | 178 | } EndpointDescriptor; |
yoka06 | 0:76d1c7f13415 | 179 | |
yoka06 | 0:76d1c7f13415 | 180 | typedef struct { |
yoka06 | 0:76d1c7f13415 | 181 | u8 bLength; |
yoka06 | 0:76d1c7f13415 | 182 | u8 bDescriptorType; |
yoka06 | 0:76d1c7f13415 | 183 | u16 bcdHID; |
yoka06 | 0:76d1c7f13415 | 184 | u8 bCountryCode; |
yoka06 | 0:76d1c7f13415 | 185 | u8 bNumDescriptors; |
yoka06 | 0:76d1c7f13415 | 186 | u8 bDescriptorType2; |
yoka06 | 0:76d1c7f13415 | 187 | u16 wDescriptorLength; |
yoka06 | 0:76d1c7f13415 | 188 | } HIDDescriptor; |
yoka06 | 0:76d1c7f13415 | 189 | |
yoka06 | 0:76d1c7f13415 | 190 | //============================================================================ |
yoka06 | 0:76d1c7f13415 | 191 | //============================================================================ |
yoka06 | 0:76d1c7f13415 | 192 | |
yoka06 | 0:76d1c7f13415 | 193 | |
yoka06 | 0:76d1c7f13415 | 194 | void USBInit(); |
yoka06 | 0:76d1c7f13415 | 195 | void USBLoop(); |
yoka06 | 0:76d1c7f13415 | 196 | u8* USBGetBuffer(u32* len); |
yoka06 | 0:76d1c7f13415 | 197 | |
yoka06 | 0:76d1c7f13415 | 198 | // Optional callback for transfers, called at interrupt time |
yoka06 | 0:76d1c7f13415 | 199 | typedef void (*USBCallback)(int device, int endpoint, int status, u8* data, int len, void* userData); |
yoka06 | 0:76d1c7f13415 | 200 | |
yoka06 | 0:76d1c7f13415 | 201 | // Transfers |
yoka06 | 0:76d1c7f13415 | 202 | int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback = 0, void* userData = 0); |
yoka06 | 0:76d1c7f13415 | 203 | int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); |
yoka06 | 0:76d1c7f13415 | 204 | int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); |
yoka06 | 0:76d1c7f13415 | 205 | |
yoka06 | 0:76d1c7f13415 | 206 | // Standard Device methods |
yoka06 | 0:76d1c7f13415 | 207 | int GetDescriptor(int device, int descType, int descIndex, u8* data, int length); |
yoka06 | 0:76d1c7f13415 | 208 | int GetString(int device, int index, char* dst, int length); |
yoka06 | 0:76d1c7f13415 | 209 | int SetAddress(int device, int new_addr); |
yoka06 | 0:76d1c7f13415 | 210 | int SetConfiguration(int device, int configNum); |
yoka06 | 0:76d1c7f13415 | 211 | int SetInterface(int device, int ifNum, int altNum); |
yoka06 | 0:76d1c7f13415 | 212 | |
yoka06 | 0:76d1c7f13415 | 213 | // Implemented to notify app of the arrival of a device |
yoka06 | 0:76d1c7f13415 | 214 | void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc); |
yoka06 | 0:76d1c7f13415 | 215 | |
yoka06 | 0:76d1c7f13415 | 216 | #endif |