hid-arcade/firmware/main.c

185 lines
5.2 KiB
C
Executable File

#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h> /* for sei() */
#include <util/delay.h> /* for _delay_ms() */
#include <avr/pgmspace.h> /* required by usbdrv.h */
#include "usbdrv.h"
#define NES_LATCH (1<<PB4)
#define NES_CLOCK (1<<PB3)
#define NES_DATA (1<<PB5)
#define NES_LATCH_LOW() PORTB &= ~(NES_LATCH);
#define NES_LATCH_HIGH() PORTB |= NES_LATCH;
#define NES_CLOCK_LOW() PORTB &= ~(NES_CLOCK);
#define NES_CLOCK_HIGH() PORTB |= NES_CLOCK;
#define NES_GET_DATA() (PINB & NES_DATA)
static uchar g_report_buffer[3];
static uchar g_idle_rate;
static uchar g_nes_buttons;
PROGMEM char usbHidReportDescriptor[49] =
{
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x05, // USAGE (Game Pad)
0xa1, 0x01, // COLLECTION (Application)
0x09, 0x01, // USAGE (Pointer)
0xa1, 0x00, // COLLECTION (Physical)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x02, // REPORT_COUNT (2)
0x81, 0x02, // INPUT (Data,Var,Abs)
0xc0, // END_COLLECTION
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x04, // USAGE_MAXIMUM (Button 4)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x04, // REPORT_COUNT (4)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x04, // REPORT_COUNT (4)
0x81, 0x03, // INPUT (Constant,Var,Abs)
0xc0, // END_COLLECTION
};
void nes_update(void) {
g_nes_buttons = 0;
// Tell the controller to latch the button states
NES_LATCH_HIGH();
// Read the A button - what a hack
_delay_us(2);
g_nes_buttons |= !NES_GET_DATA() << 0;
_delay_us(10);
NES_LATCH_LOW();
// Wait 6 more microseconds...
_delay_us(6);
// Read all the rest of the buttons...
uchar i;
for (i = 1; i < 8; i++)
{
//Pulse clock
NES_CLOCK_HIGH();
_delay_us(6);
NES_CLOCK_LOW();
g_nes_buttons |= (!NES_GET_DATA() << i);
// Wait the low part of the cycle
_delay_us(6);
}
// Let's pulse out one last time... Just like the NES does
NES_CLOCK_HIGH();
_delay_us(6);
NES_CLOCK_LOW();
}
void usb_build_report(void)
{
uchar y, x;
x = 128;
y = 128;
if (g_nes_buttons & (1 << 6))
x = 0;
if (g_nes_buttons & (1 << 7))
x = 255;
if (g_nes_buttons & (1 << 4))
y = 0;
if (g_nes_buttons & (1 << 5))
y = 255;
g_report_buffer[0] = x;
g_report_buffer[1] = y;
g_report_buffer[2] = g_nes_buttons;
}
usbMsgLen_t usbFunctionSetup(uchar data[8])
{
usbRequest_t *rq = (void *)data;
/* The following requests are never used. But since they are required by
* the specification, we implement them in this example.
*/
if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
/* we only have one report type, so don't look at wValue */
usbMsgPtr = (void *)&g_report_buffer;
return sizeof(g_report_buffer);
}else if(rq->bRequest == USBRQ_HID_GET_IDLE){
usbMsgPtr = &g_idle_rate;
return 1;
}else if(rq->bRequest == USBRQ_HID_SET_IDLE){
g_idle_rate = rq->wValue.bytes[1];
}
}else{
/* no vendor specific requests implemented */
}
return 0; /* default for not implemented requests: return no data back to host */
}
void adcInit(){
DDRC=0;
DDRB=0;
PORTC=0xff;
PORTB=0xff;
//PORTB=(1<<PB1)|(1<<PB2);
//ADMUX=(1<<REFS0);
//ADCSRA=(1<<ADEN)|(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2);
}
/* ------------------------------------------------------------------------- */
int main(void)
{
wdt_enable(WDTO_1S);
/* Even if you don't use the watchdog, turn it off here. On newer devices,
* the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
*/
/* RESET status: all port bits are inputs without pull-up.
* That's the way we need D+ and D-. Therefore we don't need any
* additional hardware initialization.
*/
usbInit();
usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
uchar i = 0;
while(--i){ /* fake USB disconnect for > 250 ms */
wdt_reset();
_delay_ms(1);
}
DDRB = NES_LATCH | NES_CLOCK;
usbDeviceConnect();
sei();
for(;;) { //main event loop
wdt_reset();
usbPoll();
nes_update();
if(usbInterruptIsReady()){
usb_build_report();
usbSetInterrupt(g_report_buffer, sizeof(g_report_buffer));
}
}
return 0;
}