// USB HID test // Copyright (C) 2014 Florian Jung // // This program 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. // // This program 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 . /* Setup: lfuse: 0xe0 hfuse: 0xd9 attach a 12MHz quartz to the atmega8, using the appropriate XTAL pins. connect avr.PD{2,3} over 68 Ohm to usb.DATA{+,-} connect usb.DATA{+,-} over 3v6-Z-Diode ( ----|<---- ) to GND connect usb.DATA- over 1.5kOhm to usb.Vcc connect usb.GND to avr.GND. supply 3.3V to the 3.3V line of the gamecube controller. supply 5V to the 5V line of the gamecube controller. connect the GND and SHIELD lines of the controller to the common GND. connect the DATA line of the gamecube controller to the C5 pin. !!! ensure that the PORTC has bit 0 cleared at any time. !!! !!! otherwise, +5V is supplied to the pin; the gamecube !!! !!! controller, however, may shortcut the line down to !!! !!! GND at any time, leading to HARDWARE DAMAGE. !!! */ #define F_CPU 12000000L #include #include #include #include #include #include "usbdrv/usbdrv.h" static char buffer[300]; // contains the exploded gamecube bits, i.e. each bit sent/received occupies one byte here. PROGMEM const char usbHidReportDescriptor[52] = { /* USB report descriptor, size must match usbconfig.h */ 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x02, // USAGE (Mouse) 0xa1, 0x01, // COLLECTION (Application) 0x09, 0x01, // USAGE (Pointer) 0xA1, 0x00, // COLLECTION (Physical) 0x05, 0x09, // USAGE_PAGE (Button) 0x19, 0x01, // USAGE_MINIMUM 0x29, 0x03, // USAGE_MAXIMUM 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x95, 0x03, // REPORT_COUNT (3) 0x75, 0x01, // REPORT_SIZE (1) 0x81, 0x02, // INPUT (Data,Var,Abs) 0x95, 0x01, // REPORT_COUNT (1) 0x75, 0x05, // REPORT_SIZE (5) 0x81, 0x03, // INPUT (Const,Var,Abs) 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x30, // USAGE (X) 0x09, 0x31, // USAGE (Y) 0x09, 0x38, // USAGE (Wheel) 0x15, 0x81, // LOGICAL_MINIMUM (-127) 0x25, 0x7F, // LOGICAL_MAXIMUM (127) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x03, // REPORT_COUNT (3) 0x81, 0x06, // INPUT (Data,Var,Rel) 0xC0, // END_COLLECTION 0xC0, // END COLLECTION }; /* This is the same report descriptor as seen in a Logitech mouse. The data * described by this descriptor consists of 4 bytes: * . . . . . B2 B1 B0 .... one byte with mouse button states * X7 X6 X5 X4 X3 X2 X1 X0 .... 8 bit signed relative coordinate x * Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 .... 8 bit signed relative coordinate y * W7 W6 W5 W4 W3 W2 W1 W0 .... 8 bit signed relative coordinate wheel */ typedef struct{ uchar buttonMask; char dx; char dy; char dWheel; } report_t; report_t reportBuffer; /* device is detected, however unreliably. dunno why. */ void debug(int i) { if (i >= 63) i=63; PORTB = ~i; } USB_PUBLIC uchar usbFunctionSetup(uchar data[8]) { usbRequest_t* rq = (usbRequest_t*) data; if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS && (rq->bRequest == USBRQ_HID_GET_REPORT)) { debug(32); // wValue: ReportType (highbyte), ReportID (lowbyte) usbMsgPtr = (void *)&reportBuffer; // we only have this one return sizeof(reportBuffer); } return 0; } #define OUT0 "sbi %2, 5 \n" /* pull line to GND */ #define OUT1 "cbi %2, 5 \n" /* tristate the line */ #define WAIT10 "ldi r16, 1 \n rcall delay_loop \n" #define WAIT34 "ldi r16, 9 \n rcall delay_loop \n" #define WAIT10MINUS10 "" #define WAIT34MINUS10 "ldi r16,5 \n rcall delay_loop \n nop \n nop \n" /* send the first "len" bytes stored in "bytes" to the controller, * then receive the reply and store it into the global "buffer". */ int send_recv_gc(char* bytes, int len) { /* Phase 1: Send the data. * Phase 1.1: Explode the bits into buffer. buffer[0..7] will contain the bits of bytes[0], * buffer[8..15] contains the bits of bytes[1], and so on. MSB first. * Phase 1.2: Actually send the data. * * intermediate Phase: wait for data line to become high again. * * Phase 2: Receive the reply. * Phase 2.1: Busy-loop until the line is pulled down for the first time. * Phase 2.2: Actually receive now: * A counter is set to 0x80. * Busy-loop until the line becomes high. Increment the counter in each iteration. * (The line is now high.) * Busy-loop until the line becomes low again. Decrement the counter each iteration. * (Done receiving the bit) * Write out the counter to buffer[], and proceed with the next bit. * * If the counter over- or underflows, stop receiving, because the line * seems to be idle again (i.e. data transfer is finished). That's a timeout * of ca. 53 us. (when running at 12 MHz) * * buffer[] now contains the counter values of the bits. * if (buffer[42] > 0x80), i.e. (buffer[42] & 0x80), then the line was longer LOW than HIGH -> bit42 = 0 * otherwise, the line was longer HIGH than LOW -> bit42 = 1. */ // The NOPs are there because of symmetry reasons. // the "// 2" comments after the assembly directives are the number of cycles this // instruction will take to execute. char* buf_ptr = buffer; /****** SEND PART ******/ int k=0; for (int i=0; i>1, k++) buffer[k] = ((bytes[i] & j)!=0)?1:0 ; len=len*8+1; asm volatile( "push r31 ; save Z\n" "push r30 \n\n" ";;;;;;;; SEND PART ;;;;;;;;\n\n" "send_loop: \n" "sbiw %3, 1 \n" // 2 "breq send_done \n" // 1 if not done, 2 if done "ld r16, z+ \n" // 2 "tst r16 \n" // 1 "brne send_one \n\n" // 1 if zero, 2 if nonzero "; otherwise, send zero \n" "nop \n" // 1 OUT0 WAIT34 OUT1 WAIT10MINUS10 "rjmp send_loop \n\n" // 2 "send_one: \n" OUT0 WAIT10 OUT1 WAIT34MINUS10 "rjmp send_loop \n\n" "delay_loop:\n" "; this costs 7 + 3*(r16) cycles \n" "dec r16\n" "brne delay_loop\n" "ret\n\n" "send_done: \n" "; now send the stop bit and release the line \n" "nop \n nop \n nop \n" OUT0 "; instead of WAIT10, do sensible work \n" "pop r30 ; restore Z \n" // 2 "pop r31 \n" // 2 "clr r16 \n" // 1 "nop \n nop \n nop \n nop \n nop \n" // 5 OUT1 "; done :) \n\n\n" "; now the final thing is to wait for DATA become high again (should be immediately anyway) \n" "send_final_loop: \n" "inc r16 \n" "breq timeout \n" "sbis %1, 5 \n" "rjmp send_final_loop \n\n\n" ";;;;;;;; RECEIVE PART ;;;;;;;;\n\n" "clr r16 \n" "recv_wait_low_initial_loop: \n" "inc r16 \n" "breq timeout \n" "sbic %1, 5 \n" "rjmp recv_wait_low_initial_loop \n" // from low to the start of counting: 6 cycles = 0.5us "nop \n" "nop \n" "nop \n" "nop \n" "recv_loop: \n" "recv_low_begin: \n" "ldi r16, 128 \n" // 1 "recv_low_loop: \n" "inc r16 \n" // 1 "breq timeout \n" // 1 if no timeout "sbis %1, 5 \n" // 1 // von high auf dec: 6 "rjmp recv_low_loop \n\n" // 2 if executed, 1 if skipped. "nop \n" // to account for the rjmp recv_loop below. "nop \n" "nop \n" "nop \n" "recv_high_begin: \n" "nop \n" // to account for the ldi in recv_low_begin. "recv_high_loop: \n" "dec r16 \n" // 1 "breq timeout \n" // 1 if no timeout "sbic %1, 5 \n" // 1 // von low auf inc: 6 "rjmp recv_high_loop \n\n" // 2 if executed, 1 if skipped "st z+, r16 \n" // 2 "rjmp recv_loop \n\n" // 2 "timeout: \n" : "+z" ((unsigned char volatile*) buf_ptr) : "I" (_SFR_IO_ADDR(PINC)), "I" (_SFR_IO_ADDR(DDRC)), "w" (len) : "r16", "memory" ); return buf_ptr-buffer; // a value of >=128 means "0", <127 means "1" bit. } int main (void) { char rand=123; char gc_x=3, gc_y=3; unsigned char ltrig=0, rtrig=0; DDRC=0x00; PORTC=0x00; DDRB=0xFF; wdt_enable(WDTO_1S); usbInit(); usbDeviceDisconnect(); for (int i=0;i<100;i++) { PORTB=~i; wdt_reset(); _delay_ms(5); } usbDeviceConnect(); PORTB=~42; sei(); debug(4); int temp=5; int n_received; int toggle=0; int count=0; int count2=0; int count3=0; while(1) { toggle=~toggle; debug(toggle); count++; if (count>=1500) count=0; count2++; if (count2>=512-ltrig) count2=0; count3++; if (count3>=25) count3=0; wdt_reset(); usbPoll(); if(usbInterruptIsReady()) { // if the interrupt is ready, feed data // pseudo-random sequence generator, thanks to Dan Frederiksen @AVRfreaks // http://en.wikipedia.org/wiki/Linear_congruential_generator rand=(rand*109+89)%251; // move to a random direction reportBuffer.dx = gc_x; reportBuffer.dy = -gc_y; usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer)); // debug(2); } /* temp++; if (!(PIND & 0x08)) // check if uC is hung up { //PORTB=~(1 << ((temp>>9)%6)); PORTB=~temp; } else if (!(PIND & 0x20)) // debug num_received { PORTB=~n_received>>3; } else if (!(PIND & 0x40)) // clear debug output { PORTB=~0x00; } else*/ { // decode "buffer" and write button states to PORTB //PORTB=~buffer[4]; } //debug(temp % 64); // _delay_ms(0.3); _delay_us(10); char foo[] = { 0x40, 0x03, 0x00 }; //char foo[] = { 0x40, 0x03, 0x02 }; if (count2<256 && count3 tmp2 ) // tmp|=1; //PORTB=~gc_x; } else { temp++; } } return 0; // never reached }