-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
163 lines (131 loc) · 3.62 KB
/
main.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
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
/*
* main.c
*
* Created on:
* Author: Tom
*/
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "utils/uartstdio.h"
#include "GCN64/device.h"
#include <string.h>
#include "inc/hw_ints.h"
#include "driverlib/interrupt.h"
#include "usblib/usb-ids.h"
#include "usblib/usblib.h"
#include "usblib/device/usbdevice.h"
#include "usblib/usbhid.h"
#include "usblib/device/usbdhid.h"
#include "USB/usbhidgamepad.h"
#define LED_BASE GPIO_PORTF_BASE
#define RED_LED GPIO_PIN_1
#define BLUE_LED GPIO_PIN_2
#define GREEN_LED GPIO_PIN_3
#define NUM_CONTROLLERS 4
extern const tUSBDHIDGamepadDevice g_sGamepadDevice;
static void *g_psDevice;
volatile static tBoolean g_ucSendReport;
void main(void)
{
unsigned char pucReport[1 + sizeof(tN64Buttons)];
//tGCN64Status *status;
tN64Buttons *buttons;
unsigned long i;
// 80MHz
SysCtlPeripheralEnable(SYSCTL_PERIPH_PLL);
SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
// LED
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(LED_BASE, RED_LED | BLUE_LED | GREEN_LED);
GPIOPinWrite(LED_BASE, RED_LED | BLUE_LED | GREEN_LED, RED_LED);
// UART
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
SysCtlPeripheralReset(SYSCTL_PERIPH_UART0);
UARTStdioInitExpClk(0, 115200);
UARTprintf("\033[2JN64 Controller\n");
// CONTROLLER
GCN64DevInitialize();
// USB
memset(pucReport, 0, sizeof pucReport);
pucReport[0] = 1;
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinTypeUSBAnalog(GPIO_PORTD_BASE, GPIO_PIN_4 | GPIO_PIN_5);
USBStackModeSet(0, USB_MODE_FORCE_DEVICE, 0);
g_psDevice = USBDHIDGamepadInit(0, &g_sGamepadDevice, pucReport, 0);
if (!g_psDevice)
{
UARTprintf("USBDHIDGamepadInit failed.\n");
return;
}
/*GPIOPinWrite(LED_BASE, RED_LED | BLUE_LED | GREEN_LED, GREEN_LED);
while (1)
{
status = GCN64DevStatus(0);
if (status)
{
UARTprintf("ID%04x HasPak%u HadPak%u\n", status->ID, status->IsConnected, status->WasConnected);
break;
}
}*/
GPIOPinWrite(LED_BASE, RED_LED | BLUE_LED | GREEN_LED, BLUE_LED);
for (i = 0; ; i = (i + 1) % NUM_CONTROLLERS)
{
while (!g_ucSendReport)
{
}
//IntDisable(INT_USB0);
buttons = N64DevButtons(i);
//IntEnable(INT_USB0);
if (buttons)
{
pucReport[0] = i + 1;
UARTprintf("%u A%u B%u Z%u Start%u Up%u Down%u Left%u Right%u L%u R%u CUp%u CDown%u CLeft%u CRight%u Reset%u X%03i Y%03i\n",
i + 1,
buttons->A,
buttons->B,
buttons->Z,
buttons->Start,
buttons->Up,
buttons->Down,
buttons->Left,
buttons->Right,
buttons->L,
buttons->R,
buttons->CUp,
buttons->CDown,
buttons->CLeft,
buttons->CRight,
buttons->Reset,
buttons->X,
buttons->Y);
memcpy(pucReport + 1, buttons, sizeof(tN64Buttons));
g_ucSendReport = false;
USBDHIDGamepadStateChange(g_psDevice, 1 + sizeof(tN64Buttons));
}
}
GPIOPinWrite(LED_BASE, RED_LED | BLUE_LED | GREEN_LED, RED_LED);
}
unsigned long USBHIDGamepadEventHandler(void *pvCBData, unsigned long ulEvent, unsigned long ulMsgParam, void *pvMsgData)
{
UARTprintf("pvCBData%04x ulEvent%04x ulMsgParam%04x pvMsgData%04x\n", pvCBData, ulEvent, ulMsgParam, pvMsgData);
switch (ulEvent)
{
case USB_EVENT_CONNECTED:
case USB_EVENT_TX_COMPLETE:
{
g_ucSendReport = true;
break;
}
/*case USBD_HID_EVENT_SET_REPORT:
{
GPIOPinWrite(LED_BASE, RED_LED | BLUE_LED | GREEN_LED, ((unsigned char*)pvMsgData)[0]);
break;
}*/
}
return 0;
}