-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathusb_hid.c
134 lines (109 loc) · 3.87 KB
/
usb_hid.c
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
/*
USB-HID Gamepad for ChibiOS/RT
Copyright (C) 2014, +inf Wenzheng Xu.
EMAIL: [email protected]
This piece of code is FREE SOFTWARE and is released under the terms
of the GNU General Public License <http://www.gnu.org/licenses/>
*/
/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
2011,2012 Giovanni Di Sirio.
This file is part of ChibiOS/RT.
ChibiOS/RT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS/RT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ch.h"
#include "hal.h"
#include "usb_hid.h"
/* CHECK IT!!! */
hid_data hid_in_data={0,0,0};
hid_data hid_out_data={0,0,0};
uint8_t usbInitState=0;
//-----------------------------
void hid_recive(USBDriver *usbp) {
usbPrepareReceive(usbp, HID_OUT_EP_ADDRESS, (uint8_t *)&hid_out_data, sizeof (hid_out_data));
chSysLockFromISR();
usbStartReceiveI(usbp, HID_OUT_EP_ADDRESS);
chSysUnlockFromISR();
}
void hid_transmit(USBDriver *usbp) {
usbPrepareTransmit(usbp, HID_IN_EP_ADDRESS, (uint8_t *)&hid_in_data, sizeof (hid_in_data));
chSysLockFromISR();
usbStartTransmitI(usbp, HID_IN_EP_ADDRESS);
palSetPadMode(GPIOD, 14, PAL_MODE_OUTPUT_PUSHPULL);
palClearPad(GPIOD, 14);
chSysUnlockFromISR();
}
//static uint8_t report_prev[64];
bool_t hidRequestsHook(USBDriver *usbp){
const USBDescriptor *dp;
if ((usbp->setup[0] & (USB_RTYPE_TYPE_MASK | USB_RTYPE_RECIPIENT_MASK)) ==
(USB_RTYPE_TYPE_STD | USB_RTYPE_RECIPIENT_INTERFACE)) {
switch (usbp->setup[1]) {
case USB_REQ_GET_DESCRIPTOR:
dp = usbp->config->get_descriptor_cb(
usbp, usbp->setup[3], usbp->setup[2],
usbFetchWord(&usbp->setup[4]));
if (dp == NULL)
return FALSE;
usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL);
return TRUE;
default:
return FALSE;
}
}
if ((usbp->setup[0] & (USB_RTYPE_TYPE_MASK | USB_RTYPE_RECIPIENT_MASK)) ==
(USB_RTYPE_TYPE_CLASS | USB_RTYPE_RECIPIENT_INTERFACE)) {
switch (usbp->setup[1]) {
case HID_GET_REPORT_REQUEST:
//usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL);
palSetPadMode(GPIOD, 12, PAL_MODE_OUTPUT_PUSHPULL);
palSetPad(GPIOD, 12);
hid_in_data.x=0;
hid_in_data.y=0;
hid_in_data.button=0;
usbSetupTransfer(usbp,(uint8_t *)&hid_in_data,sizeof(hid_in_data), NULL);
usbInitState=1;
return TRUE;
case HID_GET_IDLE_REQUEST:
usbSetupTransfer(usbp,NULL,0, NULL);
return TRUE;
case HID_GET_PROTOCOL_REQUEST:
return TRUE;
case HID_SET_REPORT_REQUEST:
usbSetupTransfer(usbp,NULL,0, NULL);
return TRUE;
case HID_SET_IDLE_REQUEST:
usbSetupTransfer(usbp,NULL,0, NULL);
return TRUE;
case HID_SET_PROTOCOL_REQUEST:
return TRUE;
default:
return FALSE;
}
}
return FALSE;
}
// Callback for THE IN ENDPOINT (INTERRUPT). Device -> HOST
void hidDataTransmitted(USBDriver *usbp, usbep_t ep){
(void)usbp;
(void)ep;
palSetPadMode(GPIOD, 14, PAL_MODE_OUTPUT_PUSHPULL);
palSetPad(GPIOD, 14);
//hid_transmit(usbp,&hid_in_data);
}
// Callback for THE OUT ENDPOINT (INTERRUPT)
void hidDataReceived(USBDriver *usbp, usbep_t ep){
(void)usbp;
(void)ep;
palSetPadMode(GPIOD, 15, PAL_MODE_OUTPUT_PUSHPULL);
palSetPad(GPIOD, 15);
}