Cascoda SDK
Cascoda SDK for building software to run with CA-821x transceivers
cascoda_usbhid.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, Cascoda Ltd.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
33 #ifndef CASCODA_USBHID_H
34 #define CASCODA_USBHID_H
35 
36 #include "cascoda-bm/cascoda-bm-config.h"
37 #include "cascoda-bm/cascoda_usb.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /******************************************************************************/
44 /****** Endpoints to be used ******/
45 /******************************************************************************/
48 #define HID_IN_EP_NO (4)
51 #define HID_OUT_EP_NO (5)
53 #define HID_CTRL_MAX_SIZE (64)
56 #define HID_FRAGMENT_SIZE (64)
57 
58 /******************************************************************************/
59 /****** Size of HID report descriptor See below ******/
60 /******************************************************************************/
61 
62 #define HID_RPT_SIZE (29)
63 
65 typedef struct UsbHIDDescriptor
66 {
69  u8_t bcdHID[2];
75 
78 {
85 
86 /******************************************************************************/
87 /****** HID Request codes ******/
88 /******************************************************************************/
89 #define HID_GET_REPORT 0x01
90 #define HID_GET_IDLE 0x02
91 #define HID_GET_PROTOCOL 0x03
92 #define HID_SET_REPORT 0x09
93 #define HID_SET_IDLE 0x0A
94 #define HID_SET_PROTOCOL 0x0B
95 
96 extern u8_t USB_StringDescs[];
97 
98 #if defined(USE_USB)
99 
100 #ifndef EP_INPUT
101 #define EP_INPUT (0x80)
102 #endif
103 
104 #ifndef EP_OUTPUT
105 #define EP_OUTPUT (0x00)
106 #endif
107 /******************************************************************************/
108 /****** Device descriptor for HID device ******/
109 /******************************************************************************/
110 
111 static const UsbDeviceDescriptor_t HidDeviceDescriptor = {
112  sizeof(UsbDeviceDescriptor_t), // bLength
113  USB_DT_DEVICE, // bDescriptorType
114  USB_BCDUSBVER, // bcdUSB version
115  0, // bDeviceClass (in interface descriptor)
116  0, // bDeviceSubClass
117  0, // bDeviceProtocol
118  HID_CTRL_MAX_SIZE, // bMaxPacketSize0
119  USB_IDVENDOR, // idVendor ( Nuvoton need Cascoda )
120  USB_IDPRODUCT, // idProduct
121  USB_BCDDEVVER, // bcdDevice version
122  USB_IMANUFACTURER, // iManufacture: String-Index of Manufacture
123  USB_IPRODUCT, // iProduct: String-Index of Product
124  USB_ISERIAL, // iSerialNumber: String-Index of Serial Number
125  1 // bNumConfigurations: Number of possible configurations
126 };
127 
128 static const HidConfigurationDescriptor_t HidConfDesc = {
129  {
130  sizeof(UsbConfigurationDescriptor_t), // bLength
131  USB_DT_CONFIGURATION, // bDescriptorType
132  {sizeof(HidConfigurationDescriptor_t), 0}, // wTotalLength
133  0x01, // bNumInterfaces: Number of interfaces
134  0x01, // iConfigurationValue: Number of this configuration
135  0x00, // iConfiguration: String index of this configuration
136  0x80, // bmAttributes: Bus-Powered, Remote-Wakeup not supported
137  0x19 // MaxPower: (in 2mA) 25->50mA
138  },
139  {
140  sizeof(UsbInterfaceDescriptor_t), // bLength
141  USB_DT_INTERFACE, // bDescriptorType
142  0x00, // bInterfaceNumber: Interface Number
143  0x00, // bAlternateSetting: Alternate setting for this interface
144  0x02, // bNumEndpoints: Number of endpoints in this interface excluding endpoint 0
145  0x03, // iInterfaceClass: Class Code: HID
146  0x00, // iInterfaceSubClass: SubClass Code
147  0x00, // bInterfaceProtocol: Protocol Code
148  0x00, // iInterface: String index
149  },
150  {
151  sizeof(UsbHIDDescriptor_t), // bLength: Length of this descriptor
152  USB_DT_HID, // bDescriptorType: HID descriptor
153  {0x11, 0x01}, // bcdHID: version 1.11
154  0x00, // bCountryCode
155  0x01, // bNumDescriptors
156  USB_DT_HID_RPT, // bDescriptorType: Report desc type
157  {HID_RPT_SIZE, 0x00} // ReportDesc. Size 29 bytes
158  },
159  {
160  sizeof(UsbEndpointDescriptor_t), // bLength: Length of this descriptor
161  USB_DT_ENDPOINT, // bDescriptorType: Endpoint Descriptor Type
162  HID_IN_EP_NO | EP_INPUT, // bEndpointAddress: Endpoint address (IN,EP1)
163  0x03, // bmAttributes: Transfer Type: INTERRUPT_TRANSFER
164  {HID_FRAGMENT_SIZE, 0x00}, // wMaxPacketSize: Endpoint Size
165  0x01 // bInterval: Polling Interval
166  },
167  {
168  sizeof(UsbEndpointDescriptor_t), // bLength: Length of this descriptor
169  USB_DT_ENDPOINT, // bDescriptorType: Endpoint Descriptor Type
170  HID_OUT_EP_NO | EP_OUTPUT, // bEndpointAddress: Endpoint address (OUT,EP2)
171  0x03, // bmAttributes: Transfer Type: INTERRUPT_TRANSFER
172  {HID_FRAGMENT_SIZE, 0x00}, // wMaxPacketSize: Endpoint Size
173  0x01 // bInterval: Polling Interval
174  }};
175 
176 /******************************************************************************/
177 /****** Report descriptor for HID device ******/
178 /******************************************************************************/
179 
180 static const u8_t HidReportDescriptor[HID_RPT_SIZE] = {
181  // Usage Page item specifying vendor defined
182  0x06, // item tag bSize 2 bType 1 (global) bTag 0
183  0x00,
184  0xff, // data 0xFF00 vendor defined
185  // Usage item specifying vendor usage 1
186  0x09, // item tag bSize 1 bType 2 (local) bTag 0
187  0x01, // data 0x01
188  // Collection item specifying application
189  0xA1, // item tag bSize 1 bType 0 (Main) bTag 0xA
190  0x01, // data 0x01 (application)
191  // Usage minimum item
192  0x19, // item tag bSize 1 bType 2 (Local) bTag 0x1
193  0x01, // data 0x01
194  // Usage maximum item
195  0x29, // item tag bSize 1 bType 2 (Local) bTag 0x2
196  0x3F, // data 0x3F (63)
197  // Logical Minimum (data bytes in the report may have minimum value = 0x00)
198  0x15, // item tag bSize 1 bType 1 (Global) bTag 0x1
199  0x00, // data 0x00
200  // Logical Maximum (data bytes in the report may have maximum value = 0x00FF = unsigned 255)
201  0x26, // item tag bSize 2 bType 1 (Global) bTag 0x2
202  0xFF,
203  0x00, // data 0x00FF
204  // Report Size: 8-bit field size
205  0x75, // item tag bSize 1 bType 1 (Global) btag 0x7
206  0x08, // data 0x08
207  // Report Count: Make sixty-four 8-bit fields (the next time the parser hits an "Input", "Output", or "Feature" item)
208  0x95, // item tag bSize 1 bType 1 (Global) bTag 0x9
209  0x40, // data 0x40 (64)
210  // Input (Data, Array, Abs): Instantiates input packet fields based on the above report size, count, logical min/max, and usage.
211  0x91, // item tag bSize 1 bType 0 (Main) bTag 0x9
212  0x00, // 0x00
213  // Usage Minimum
214  0x19, // item tag bSize 1 bType 2 (Local) bTag 0x1
215  0x01, // data 0x01
216  // Usage maximum item
217  0x29, // item tag bSize 1 bType 2 (Local) bTag 0x2
218  0x3F, // data 0x3F (63)
219  // Output (Data, Array, Abs): Instantiates output packet fields. Uses same report size and count as "Input" fields, since nothing new/different was specified to the parser since the "Input" item.
220  0x81, // item tag bSize 1 bType 0 (Main) bTag 0x8
221  0x00, // data 0x00
222  // End collection
223  0xC0, // item tag bSize 0 bType 0 (Main) bTag 0xC
224 };
225 
226 #endif // USB_HID
227 
228 #ifdef __cplusplus
229 }
230 #endif
231 
232 #endif // CASCODA_USBHID_H
unsigned char u8_t
Definition: cascoda_types.h:46
Internal USB definitions.
#define USB_DT_ENDPOINT
Definition: cascoda_usb.h:49
#define USB_DT_CONFIGURATION
Definition: cascoda_usb.h:46
#define USB_DT_HID
Definition: cascoda_usb.h:54
struct UsbInterfaceDescriptor UsbInterfaceDescriptor_t
Interface descriptor.
#define USB_DT_INTERFACE
Definition: cascoda_usb.h:48
#define USB_IPRODUCT
Definition: cascoda_usb.h:154
struct UsbEndpointDescriptor UsbEndpointDescriptor_t
Endpoint descriptor.
#define USB_IMANUFACTURER
Definition: cascoda_usb.h:153
struct UsbConfigurationDescriptor UsbConfigurationDescriptor_t
Configuration descriptor.
struct UsbDeviceDescriptor UsbDeviceDescriptor_t
Device descriptor.
#define USB_DT_DEVICE
Definition: cascoda_usb.h:45
#define USB_DT_HID_RPT
Definition: cascoda_usb.h:55
#define USB_ISERIAL
Definition: cascoda_usb.h:155
#define HID_FRAGMENT_SIZE
Size of fragments exchanged over the user data endpoints (HID_IN_EP_NO and HID_OUT_EP_NO)
Definition: cascoda_usbhid.h:56
#define HID_OUT_EP_NO
Endpoint number forOUT events.
Definition: cascoda_usbhid.h:51
#define HID_CTRL_MAX_SIZE
Maximum size for messages exchanged over the CONTROL endpoints.
Definition: cascoda_usbhid.h:53
u8_t USB_StringDescs[]
struct UsbHIDDescriptor UsbHIDDescriptor_t
HID descriptor.
#define HID_IN_EP_NO
Endpoint number for IN events.
Definition: cascoda_usbhid.h:48
#define HID_RPT_SIZE
Definition: cascoda_usbhid.h:62
struct HidConfigurationDescriptor HidConfigurationDescriptor_t
Configuration descriptor for HID device.
Configuration descriptor for HID device.
Definition: cascoda_usbhid.h:78
UsbEndpointDescriptor_t EPOut
Definition: cascoda_usbhid.h:83
UsbEndpointDescriptor_t EPIn
Definition: cascoda_usbhid.h:82
UsbInterfaceDescriptor_t Interface
Definition: cascoda_usbhid.h:80
UsbHIDDescriptor_t Hid
Definition: cascoda_usbhid.h:81
UsbConfigurationDescriptor_t Config
Definition: cascoda_usbhid.h:79
Configuration descriptor.
Definition: cascoda_usb.h:106
Device descriptor.
Definition: cascoda_usb.h:65
Endpoint descriptor.
Definition: cascoda_usb.h:133
HID descriptor.
Definition: cascoda_usbhid.h:66
u8_t ReportDescSize[2]
Definition: cascoda_usbhid.h:73
u8_t bcdHID[2]
bcd HID spec version
Definition: cascoda_usbhid.h:69
u8_t bLength
Length in bytes ( of this )
Definition: cascoda_usbhid.h:67
u8_t bDescriptorTypeRpt
Report descriptor type.
Definition: cascoda_usbhid.h:72
u8_t bCountryCode
Country code.
Definition: cascoda_usbhid.h:70
u8_t bNumDescriptors
HID class descs following.
Definition: cascoda_usbhid.h:71
u8_t bDescriptorType
USB_DT_HID.
Definition: cascoda_usbhid.h:68
Interface descriptor.
Definition: cascoda_usb.h:119