forked from arpruss/USBComposite_stm32f1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_hid.h
146 lines (116 loc) · 4.04 KB
/
usb_hid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/******************************************************************************
* The MIT License
*
* Copyright (c) 2011 LeafLabs LLC.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*****************************************************************************/
/**
* @file libmaple/include/libmaple/usb_device.h
* @brief USB Composite with CDC ACM and HID support
*
* IMPORTANT: this API is unstable, and may change without notice.
*/
#ifndef _USB_HID_H_
#define _USB_HID_H_
#include <libmaple/libmaple_types.h>
#include <libmaple/usb.h>
#include "usb_generic.h"
#define MAX_HID_BUFFERS 8
#define HID_BUFFER_SIZE(n,reportID) ((n)+((reportID)!=0))
#define HID_BUFFER_ALLOCATE_SIZE(n,reportID) ((HID_BUFFER_SIZE((n),(reportID))+1)/2*2)
#define HID_BUFFER_MODE_NO_WAIT 1
#define HID_BUFFER_MODE_OUTPUT 2
#define HID_BUFFER_EMPTY 0
#define HID_BUFFER_UNREAD USB_CONTROL_DONE
#define HID_BUFFER_READ 2
extern USBCompositePart usbHIDPart;
typedef struct HIDBuffer_t {
volatile uint8_t* buffer; // use HID_BUFFER_ALLOCATE_SIZE() to calculate amount of memory to allocate
uint16_t bufferSize; // this should match HID_BUFFER_SIZE
uint8_t reportID;
uint8_t mode;
uint8_t state; // HID_BUFFER_EMPTY, etc.
#ifdef __cplusplus
inline HIDBuffer_t(volatile uint8_t* _buffer=NULL, uint16_t _bufferSize=0, uint8_t _reportID=0, uint8_t _mode=0) {
reportID = _reportID;
buffer = _buffer;
bufferSize = _bufferSize;
mode = _mode;
}
#endif
} HIDBuffer_t;
#ifdef __cplusplus
extern "C" {
#endif
void usb_hid_set_report_descriptor(struct usb_chunk* chunks);
void usb_hid_clear_buffers(uint8_t type);
uint8_t usb_hid_add_buffer(uint8_t type, volatile HIDBuffer_t* buf);
void usb_hid_set_buffers(uint8_t type, volatile HIDBuffer_t* featureBuffers, int count);
uint16_t usb_hid_get_data(uint8_t type, uint8_t reportID, uint8_t* out, uint8_t poll);
void usb_hid_set_feature(uint8_t reportID, uint8_t* data);
void usb_hid_setTXEPSize(uint32_t size);
uint32 usb_hid_get_pending(void);
/*
* HID Requests
*/
typedef enum _HID_REQUESTS
{
GET_REPORT = 1,
GET_IDLE,
GET_PROTOCOL,
SET_REPORT = 9,
SET_IDLE,
SET_PROTOCOL
} HID_REQUESTS;
#define HID_REPORT_TYPE_INPUT 0x01
#define HID_REPORT_TYPE_OUTPUT 0x02
#define HID_REPORT_TYPE_FEATURE 0x03
/*
* HID Descriptors, etc.
*/
#define HID_ENDPOINT_INT 1
#define HID_DESCRIPTOR_TYPE 0x21
#define REPORT_DESCRIPTOR 0x22
typedef struct
{
uint8_t len; // 9
uint8_t dtype; // 0x21
uint8_t versionL; // 0x101
uint8_t versionH; // 0x101
uint8_t country;
uint8_t numDesc;
uint8_t desctype; // 0x22 report
uint8_t descLenL;
uint8_t descLenH;
} HIDDescriptor;
#define USB_INTERFACE_CLASS_HID 0x03
#define USB_INTERFACE_SUBCLASS_HID 0x01
/*
* HID interface
*/
uint32 usb_hid_tx(const uint8* buf, uint32 len);
uint32 usb_hid_tx_mod(const uint8* buf, uint32 len);
uint32 usb_hid_data_available(void); /* in RX buffer */
#ifdef __cplusplus
}
#endif
#endif