-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPIUIO_ctrl.c
More file actions
320 lines (263 loc) · 9.7 KB
/
PIUIO_ctrl.c
File metadata and controls
320 lines (263 loc) · 9.7 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "PIUIO.h"
#include "PIUIO_ctrl.h"
#include "Descriptors.h"
// Useful commands to test the descriptors
// sudo lsusb -d 0d2f:1020 -vv
// sudo usbhid-dump -d 0d2f:1020 -e descriptor
unsigned char LampData[8];
unsigned char InputData[8];
unsigned char LXLampData[16];
unsigned char LXInputData[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff};
int next_device = 0;
unsigned char EEPROM_read(unsigned int uiAddress);
//unsigned char EEPROM_write(unsigned int uiAddress, unsigned char ucData);
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware(void)
{
#if (ARCH == ARCH_AVR8)
/* Disable watchdog if enabled by bootloader/fuses */
wdt_reset();
MCUSR &= ~(1 << WDRF);
wdt_disable();
/* Disable clock division */
clock_prescale_set(clock_div_1);
#elif (ARCH == ARCH_XMEGA)
/* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
/* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
#endif
// Get the status of the EEPROM
unsigned char dat = EEPROM_read(0);
piuio_which_device = (int)dat;
if(piuio_which_device < 0 || piuio_which_device > 2) piuio_which_device = 0;
next_device = piuio_which_device;
/* Hardware Initialization */
USB_Init();
}
/** Event handler for the library USB Connection event. */
void EVENT_USB_Device_Connect(void)
{
}
/** Event handler for the library USB Disconnection event. */
void EVENT_USB_Device_Disconnect(void)
{
}
const USB_Endpoint_Table_t ReportINEndpoint = {
.Address = LXIO_EP0ADDR,
.Size = LXIO_EPSIZE,
.Type = EP_TYPE_INTERRUPT,
.Banks = 1,
};
const USB_Endpoint_Table_t ReportOUTEndpoint = {
.Address = LXIO_EP1ADDR,
.Size = LXIO_EPSIZE,
.Type = EP_TYPE_INTERRUPT,
.Banks = 1,
};
/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void)
{
//bool ConfigSuccess = true;
if(piuio_which_device != 0) {
Endpoint_ConfigureEndpointTable(&ReportINEndpoint, 1);
Endpoint_ConfigureEndpointTable(&ReportOUTEndpoint, 1);
}
USB_Device_EnableSOFEvents();
}
int nControl = 0;
void EVENT_USB_Device_ControlRequestPIUIO(void) {
if(USB_ControlRequest.bRequest == 0xAE && piuio_which_device == 0) { // Access Game IO as PIUIO
nControl++;
if(!(USB_ControlRequest.bmRequestType & 0x80)) {
Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
while (!(Endpoint_IsINReady()));
// Read the lamp values
Endpoint_ClearSETUP();
Endpoint_Read_Control_Stream_LE(LampData, 8);
Endpoint_ClearIN();
/* mark the whole request as successful: */
//Endpoint_ClearStatusStage();
//LEDoff(RXLED);
} // Reading input data
else
{
//LEDon(TXLED);
Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
// Set the switch state
Endpoint_ClearSETUP();
Endpoint_Write_Control_Stream_LE(InputData, 8);
Endpoint_ClearOUT();
/* mark the whole request as successful: */
//Endpoint_ClearStatusStage();
//LEDoff(TXLED);
}
}
}
bool using_report_protocol = true;
uint16_t IdleCount = 0;
void EVENT_USB_Device_ControlRequestLXIO(void) {
// Extracted from LUFA/Drivers/USB/Class/Device/HIDClassDevice.c
// Modified to match the LXIO
switch (USB_ControlRequest.bRequest)
{
case HID_REQ_GetReport:
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) // 0xA1
{
uint16_t ReportSize = 16;
uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
//uint8_t ReportType = (USB_ControlRequest.wValue >> 8) - 1;
uint8_t ReportData[16];
memcpy(ReportData, LXInputData, 16);
Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
Endpoint_ClearSETUP();
if (ReportID)
Endpoint_Write_8(ReportID);
Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
Endpoint_ClearOUT();
}
break;
case HID_REQ_SetReport:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) // 0x21
{
uint16_t ReportSize = 16;
//uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
//uint8_t ReportType = (USB_ControlRequest.wValue >> 8) - 1;
uint8_t ReportData[ReportSize];
Endpoint_ClearSETUP();
Endpoint_Read_Control_Stream_LE(ReportData, ReportSize);
Endpoint_ClearIN();
memcpy(LXLampData, ReportData, 16);
}
break;
case HID_REQ_GetProtocol:
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();
while (!(Endpoint_IsINReady()));
Endpoint_Write_8(using_report_protocol);
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
case HID_REQ_SetProtocol:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();
Endpoint_ClearStatusStage();
using_report_protocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
}
break;
case HID_REQ_SetIdle:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();
Endpoint_ClearStatusStage();
IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);
}
break;
case HID_REQ_GetIdle:
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();
while (!(Endpoint_IsINReady()));
Endpoint_Write_8(IdleCount >> 2);
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
}
}
/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void)
{
if (!(Endpoint_IsSETUPReceived()))
return;
if (USB_ControlRequest.wIndex != INTERFACE_ID_PIUIO)
return;
if(piuio_which_device == 0) {
EVENT_USB_Device_ControlRequestPIUIO();
return;
}
if(piuio_which_device > 0 && piuio_which_device <= 2) {
EVENT_USB_Device_ControlRequestLXIO();
return;
}
}
/** Event handler for the USB device Start Of Frame event. */
void EVENT_USB_Device_StartOfFrame(void)
{
}
void USB_Loop(void) {
// Do not handle endpoints if not an LXIO
if(piuio_which_device == 0) return;
// Do the conversion from the Lights and Input from regular IO
// to the LX Lights and Input format
// NOTE: Note that InputData will carry the 4 sensor anyways
LXInputData[0] = InputData[0];
LXInputData[1] = InputData[0];
LXInputData[2] = InputData[0];
LXInputData[3] = InputData[0];
LXInputData[4] = InputData[2];
LXInputData[5] = InputData[2];
LXInputData[6] = InputData[2];
LXInputData[7] = InputData[2];
LXInputData[8] = InputData[1];
LXInputData[9] = InputData[3];
LXInputData[10] = 0xFF; // TODO: We do not have a handler for the front buttons
LXInputData[11] = 0xFF; // TODO: We do not have a handler for the front buttons
LXInputData[14] = 0xFF;
LXInputData[15] = 0xFF;
// Handle in endpoint
Endpoint_SelectEndpoint(LXIO_EP0ADDR); // An input
if (Endpoint_IsINReady())
{
nControl++;
Endpoint_Write_Stream_LE(LXInputData, sizeof(LXInputData), NULL);
Endpoint_ClearIN();
}
// Handle out endpoint
Endpoint_SelectEndpoint(LXIO_EP1ADDR); // An output
if (Endpoint_IsOUTReceived())
{
if (Endpoint_IsReadWriteAllowed())
{
nControl++;
Endpoint_Read_Stream_LE(&LXLampData, sizeof(LXLampData), NULL);
}
Endpoint_ClearOUT();
}
memcpy(LampData, LXLampData, 8); // Just copy in the case of lamps
}
// Extracted from the datasheet
unsigned char EEPROM_read(unsigned int uiAddress)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEPE));
/* Set up address register */
EEAR = uiAddress;
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/* Return data from Data Register */
return EEDR;
}
void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEPE));
/* Set up address and Data Registers */
EEAR = uiAddress;
EEDR = ucData;
/* Write logical one to EEMPE */
EECR |= (1<<EEMPE);
/* Start eeprom write by setting EEPE */
EECR |= (1<<EEPE);
}
void go_next_device() {
next_device++;
if(next_device >= 3) next_device = 0;
EEPROM_write(0, (unsigned char)next_device);
}