Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Committer:
hudakz
Date:
Sun Jul 12 20:39:26 2020 +0000
Revision:
0:84353c479782
Child:
1:2263e77400e9
MAX3421E-based USB Host Shield Library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:84353c479782 1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
hudakz 0:84353c479782 2
hudakz 0:84353c479782 3 This software may be distributed and modified under the terms of the GNU
hudakz 0:84353c479782 4 General Public License version 2 (GPL2) as published by the Free Software
hudakz 0:84353c479782 5 Foundation and appearing in the file GPL2.TXT included in the packaging of
hudakz 0:84353c479782 6 this file. Please note that GPL2 Section 2[b] requires that all works based
hudakz 0:84353c479782 7 on this software must also be made publicly available under the terms of
hudakz 0:84353c479782 8 the GPL2 ("Copyleft").
hudakz 0:84353c479782 9
hudakz 0:84353c479782 10 Contact information
hudakz 0:84353c479782 11 -------------------
hudakz 0:84353c479782 12
hudakz 0:84353c479782 13 Circuits At Home, LTD
hudakz 0:84353c479782 14 Web : http://www.circuitsathome.com
hudakz 0:84353c479782 15 e-mail : support@circuitsathome.com
hudakz 0:84353c479782 16 */
hudakz 0:84353c479782 17 #if !defined(__USBHID_H__)
hudakz 0:84353c479782 18 #define __USBHID_H__
hudakz 0:84353c479782 19
hudakz 0:84353c479782 20 #include "Usb.h"
hudakz 0:84353c479782 21 #include "hidusagestr.h"
hudakz 0:84353c479782 22
hudakz 0:84353c479782 23 #define MAX_REPORT_PARSERS 2
hudakz 0:84353c479782 24 #define HID_MAX_HID_CLASS_DESCRIPTORS 5
hudakz 0:84353c479782 25
hudakz 0:84353c479782 26 #define DATA_SIZE_MASK 0x03
hudakz 0:84353c479782 27 #define TYPE_MASK 0x0C
hudakz 0:84353c479782 28 #define TAG_MASK 0xF0
hudakz 0:84353c479782 29
hudakz 0:84353c479782 30 #define DATA_SIZE_0 0x00
hudakz 0:84353c479782 31 #define DATA_SIZE_1 0x01
hudakz 0:84353c479782 32 #define DATA_SIZE_2 0x02
hudakz 0:84353c479782 33 #define DATA_SIZE_4 0x03
hudakz 0:84353c479782 34
hudakz 0:84353c479782 35 #define TYPE_MAIN 0x00
hudakz 0:84353c479782 36 #define TYPE_GLOBAL 0x04
hudakz 0:84353c479782 37 #define TYPE_LOCAL 0x08
hudakz 0:84353c479782 38
hudakz 0:84353c479782 39 #define TAG_MAIN_INPUT 0x80
hudakz 0:84353c479782 40 #define TAG_MAIN_OUTPUT 0x90
hudakz 0:84353c479782 41 #define TAG_MAIN_COLLECTION 0xA0
hudakz 0:84353c479782 42 #define TAG_MAIN_FEATURE 0xB0
hudakz 0:84353c479782 43 #define TAG_MAIN_ENDCOLLECTION 0xC0
hudakz 0:84353c479782 44
hudakz 0:84353c479782 45 #define TAG_GLOBAL_USAGEPAGE 0x00
hudakz 0:84353c479782 46 #define TAG_GLOBAL_LOGICALMIN 0x10
hudakz 0:84353c479782 47 #define TAG_GLOBAL_LOGICALMAX 0x20
hudakz 0:84353c479782 48 #define TAG_GLOBAL_PHYSMIN 0x30
hudakz 0:84353c479782 49 #define TAG_GLOBAL_PHYSMAX 0x40
hudakz 0:84353c479782 50 #define TAG_GLOBAL_UNITEXP 0x50
hudakz 0:84353c479782 51 #define TAG_GLOBAL_UNIT 0x60
hudakz 0:84353c479782 52 #define TAG_GLOBAL_REPORTSIZE 0x70
hudakz 0:84353c479782 53 #define TAG_GLOBAL_REPORTID 0x80
hudakz 0:84353c479782 54 #define TAG_GLOBAL_REPORTCOUNT 0x90
hudakz 0:84353c479782 55 #define TAG_GLOBAL_PUSH 0xA0
hudakz 0:84353c479782 56 #define TAG_GLOBAL_POP 0xB0
hudakz 0:84353c479782 57
hudakz 0:84353c479782 58 #define TAG_LOCAL_USAGE 0x00
hudakz 0:84353c479782 59 #define TAG_LOCAL_USAGEMIN 0x10
hudakz 0:84353c479782 60 #define TAG_LOCAL_USAGEMAX 0x20
hudakz 0:84353c479782 61
hudakz 0:84353c479782 62 /* HID requests */
hudakz 0:84353c479782 63 #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
hudakz 0:84353c479782 64 #define bmREQ_HID_IN USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
hudakz 0:84353c479782 65 #define bmREQ_HID_REPORT USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_INTERFACE
hudakz 0:84353c479782 66
hudakz 0:84353c479782 67 /* HID constants. Not part of chapter 9 */
hudakz 0:84353c479782 68 /* Class-Specific Requests */
hudakz 0:84353c479782 69 #define HID_REQUEST_GET_REPORT 0x01
hudakz 0:84353c479782 70 #define HID_REQUEST_GET_IDLE 0x02
hudakz 0:84353c479782 71 #define HID_REQUEST_GET_PROTOCOL 0x03
hudakz 0:84353c479782 72 #define HID_REQUEST_SET_REPORT 0x09
hudakz 0:84353c479782 73 #define HID_REQUEST_SET_IDLE 0x0A
hudakz 0:84353c479782 74 #define HID_REQUEST_SET_PROTOCOL 0x0B
hudakz 0:84353c479782 75
hudakz 0:84353c479782 76 /* Class Descriptor Types */
hudakz 0:84353c479782 77 #define HID_DESCRIPTOR_HID 0x21
hudakz 0:84353c479782 78 #define HID_DESCRIPTOR_REPORT 0x22
hudakz 0:84353c479782 79 #define HID_DESRIPTOR_PHY 0x23
hudakz 0:84353c479782 80
hudakz 0:84353c479782 81 /* Protocol Selection */
hudakz 0:84353c479782 82 #define USB_HID_BOOT_PROTOCOL 0x00
hudakz 0:84353c479782 83 #define HID_RPT_PROTOCOL 0x01
hudakz 0:84353c479782 84
hudakz 0:84353c479782 85 /* HID Interface Class Code */
hudakz 0:84353c479782 86 #define HID_INTF 0x03
hudakz 0:84353c479782 87
hudakz 0:84353c479782 88 /* HID Interface Class SubClass Codes */
hudakz 0:84353c479782 89 #define HID_BOOT_INTF_SUBCLASS 0x01
hudakz 0:84353c479782 90
hudakz 0:84353c479782 91 /* HID Interface Class Protocol Codes */
hudakz 0:84353c479782 92 #define USB_HID_PROTOCOL_NONE 0x00
hudakz 0:84353c479782 93 #define USB_HID_PROTOCOL_KEYBOARD 0x01
hudakz 0:84353c479782 94 #define USB_HID_PROTOCOL_MOUSE 0x02
hudakz 0:84353c479782 95
hudakz 0:84353c479782 96 #define HID_ITEM_TYPE_MAIN 0
hudakz 0:84353c479782 97 #define HID_ITEM_TYPE_GLOBAL 1
hudakz 0:84353c479782 98 #define HID_ITEM_TYPE_LOCAL 2
hudakz 0:84353c479782 99 #define HID_ITEM_TYPE_RESERVED 3
hudakz 0:84353c479782 100
hudakz 0:84353c479782 101 #define HID_LONG_ITEM_PREFIX 0xfe // Long item prefix value
hudakz 0:84353c479782 102
hudakz 0:84353c479782 103 #define bmHID_MAIN_ITEM_TAG 0xfc // Main item tag mask
hudakz 0:84353c479782 104
hudakz 0:84353c479782 105 #define bmHID_MAIN_ITEM_INPUT 0x80 // Main item Input tag value
hudakz 0:84353c479782 106 #define bmHID_MAIN_ITEM_OUTPUT 0x90 // Main item Output tag value
hudakz 0:84353c479782 107 #define bmHID_MAIN_ITEM_FEATURE 0xb0 // Main item Feature tag value
hudakz 0:84353c479782 108 #define bmHID_MAIN_ITEM_COLLECTION 0xa0 // Main item Collection tag value
hudakz 0:84353c479782 109 #define bmHID_MAIN_ITEM_END_COLLECTION 0xce // Main item End Collection tag value
hudakz 0:84353c479782 110
hudakz 0:84353c479782 111 #define HID_MAIN_ITEM_COLLECTION_PHYSICAL 0
hudakz 0:84353c479782 112 #define HID_MAIN_ITEM_COLLECTION_APPLICATION 1
hudakz 0:84353c479782 113 #define HID_MAIN_ITEM_COLLECTION_LOGICAL 2
hudakz 0:84353c479782 114 #define HID_MAIN_ITEM_COLLECTION_REPORT 3
hudakz 0:84353c479782 115 #define HID_MAIN_ITEM_COLLECTION_NAMED_ARRAY 4
hudakz 0:84353c479782 116 #define HID_MAIN_ITEM_COLLECTION_USAGE_SWITCH 5
hudakz 0:84353c479782 117 #define HID_MAIN_ITEM_COLLECTION_USAGE_MODIFIER 6
hudakz 0:84353c479782 118
hudakz 0:84353c479782 119 struct HidItemPrefix {
hudakz 0:84353c479782 120 uint8_t bSize : 2;
hudakz 0:84353c479782 121 uint8_t bType : 2;
hudakz 0:84353c479782 122 uint8_t bTag : 4;
hudakz 0:84353c479782 123 };
hudakz 0:84353c479782 124
hudakz 0:84353c479782 125 struct MainItemIOFeature {
hudakz 0:84353c479782 126 uint8_t bmIsConstantOrData : 1;
hudakz 0:84353c479782 127 uint8_t bmIsArrayOrVariable : 1;
hudakz 0:84353c479782 128 uint8_t bmIsRelativeOrAbsolute : 1;
hudakz 0:84353c479782 129 uint8_t bmIsWrapOrNoWrap : 1;
hudakz 0:84353c479782 130 uint8_t bmIsNonLonearOrLinear : 1;
hudakz 0:84353c479782 131 uint8_t bmIsNoPreferedOrPrefered : 1;
hudakz 0:84353c479782 132 uint8_t bmIsNullOrNoNull : 1;
hudakz 0:84353c479782 133 uint8_t bmIsVolatileOrNonVolatile : 1;
hudakz 0:84353c479782 134 };
hudakz 0:84353c479782 135
hudakz 0:84353c479782 136 class USBHID;
hudakz 0:84353c479782 137
hudakz 0:84353c479782 138 class HIDReportParser {
hudakz 0:84353c479782 139 public:
hudakz 0:84353c479782 140 virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) = 0;
hudakz 0:84353c479782 141 };
hudakz 0:84353c479782 142
hudakz 0:84353c479782 143 class USBHID : public USBDeviceConfig, public UsbConfigXtracter {
hudakz 0:84353c479782 144 protected:
hudakz 0:84353c479782 145 USB *pUsb; // USB class instance pointer
hudakz 0:84353c479782 146 uint8_t bAddress; // address
hudakz 0:84353c479782 147
hudakz 0:84353c479782 148 protected:
hudakz 0:84353c479782 149 static const uint8_t epInterruptInIndex = 1; // InterruptIN endpoint index
hudakz 0:84353c479782 150 static const uint8_t epInterruptOutIndex = 2; // InterruptOUT endpoint index
hudakz 0:84353c479782 151
hudakz 0:84353c479782 152 static const uint8_t maxHidInterfaces = 3;
hudakz 0:84353c479782 153 static const uint8_t maxEpPerInterface = 2;
hudakz 0:84353c479782 154 static const uint8_t totalEndpoints = (maxHidInterfaces * maxEpPerInterface + 1); // We need to make room for the control endpoint
hudakz 0:84353c479782 155
hudakz 0:84353c479782 156 void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
hudakz 0:84353c479782 157 void PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc);
hudakz 0:84353c479782 158
hudakz 0:84353c479782 159 virtual HIDReportParser* GetReportParser(uint8_t id __attribute__((unused))) {
hudakz 0:84353c479782 160 return NULL;
hudakz 0:84353c479782 161 };
hudakz 0:84353c479782 162
hudakz 0:84353c479782 163 public:
hudakz 0:84353c479782 164
hudakz 0:84353c479782 165 USBHID(USB *pusb) : pUsb(pusb) {
hudakz 0:84353c479782 166 };
hudakz 0:84353c479782 167
hudakz 0:84353c479782 168 const USB* GetUsb() {
hudakz 0:84353c479782 169 return pUsb;
hudakz 0:84353c479782 170 };
hudakz 0:84353c479782 171
hudakz 0:84353c479782 172 virtual bool SetReportParser(uint8_t id __attribute__((unused)), HIDReportParser *prs __attribute__((unused))) {
hudakz 0:84353c479782 173 return false;
hudakz 0:84353c479782 174 };
hudakz 0:84353c479782 175
hudakz 0:84353c479782 176 uint8_t SetProtocol(uint8_t iface, uint8_t protocol);
hudakz 0:84353c479782 177 uint8_t GetProtocol(uint8_t iface, uint8_t* dataptr);
hudakz 0:84353c479782 178 uint8_t GetIdle(uint8_t iface, uint8_t reportID, uint8_t* dataptr);
hudakz 0:84353c479782 179 uint8_t SetIdle(uint8_t iface, uint8_t reportID, uint8_t duration);
hudakz 0:84353c479782 180
hudakz 0:84353c479782 181 uint8_t GetReportDescr(uint16_t wIndex, USBReadParser *parser = NULL);
hudakz 0:84353c479782 182
hudakz 0:84353c479782 183 uint8_t GetHidDescr(uint8_t ep, uint16_t nbytes, uint8_t* dataptr);
hudakz 0:84353c479782 184 uint8_t GetReport(uint8_t ep, uint8_t iface, uint8_t report_type, uint8_t report_id, uint16_t nbytes, uint8_t* dataptr);
hudakz 0:84353c479782 185 uint8_t SetReport(uint8_t ep, uint8_t iface, uint8_t report_type, uint8_t report_id, uint16_t nbytes, uint8_t* dataptr);
hudakz 0:84353c479782 186 };
hudakz 0:84353c479782 187
hudakz 0:84353c479782 188 #endif // __USBHID_H__