// GameCube controller to USB adapter // 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 version 3 as // published by the Free Software Foundation. // // 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. #define floUsbHidReportDescriptorLen 862 PROGMEM const char floUsbHidReportDescriptor[862] = { 0x05, 0x01, //USAGE_PAGE (Generic Desktop) 0x15, 0x00, //LOGICAL_MINIMUM (0) 0x09, 0x04, //USAGE (Joystick) 0xA1, 0x01, //COLLECTION (Application) // Start Joystick input definition 0x85, 0x01, // REPORT_ID (1) 0x05, 0x02, // USAGE_PAGE (Simulation Controls) 0x09, 0xBB, // USAGE (Throttle) 0x15, 0x81, // LOGICAL_MINIMUM (-127) 0x25, 0x7F, // LOGICAL_MAXIMUM (127) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x01, // REPORT_COUNT (1) 0x81, 0x02, // INPUT (Data,Var,Abs) 0x05, 0x01, // USAGE_PAGE (Generic Desktop) // Define the axes 0x09, 0x01, // USAGE (Pointer) 0xA1, 0x00, // COLLECTION (Physical) 0x09, 0x30, // USAGE (X) 0x09, 0x31, // USAGE (Y) 0x95, 0x02, // REPORT_COUNT (2) 0x81, 0x02, // INPUT (Data,Var,Abs) 0xC0, // END_COLLECTION // Define the hat switch 0x09, 0x39, // USAGE (Hat switch) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x03, // LOGICAL_MAXIMUM (3) 0x35, 0x00, // PHYSICAL_MINIMUM (0) 0x46, 0x0E, 0x01, // PHYSICAL_MAXIMUM (270) 0x66, 0x14, 0x00, // UNIT (Eng Rot:Angular Pos) 0x75, 0x04, // REPORT_SIZE (4) 0x95, 0x01, // REPORT_COUNT (1) 0x81, 0x02, // INPUT (Data,Var,Abs) 0x45, 0x00, // PHYSICAL_MAXIMUM (0) 0x66, 0x00, 0x00, // UNIT (None) // Define the buttons 0x05, 0x09, // USAGE_PAGE (Button) 0x19, 0x01, // USAGE_MINIMUM (Button 1) 0x29, 0x04, // USAGE_MAXIMUM (Button 4) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x95, 0x04, // REPORT_COUNT (4) 0x75, 0x01, // REPORT_SIZE (1) 0x35, 0x00, // PHYSICAL_MINIMUM (0) 0x81, 0x02, // INPUT (Data,Var,Abs) // End Joystick Input definition // Start Force Feedback command definitions 0x05, 0x0F, // USAGE_PAGE (Physical Interface) // Effect Report Definition 0x09, 0x21, // USAGE (Set Effect Report) 0xA1, 0x02, // COLLECTION (Logical) 0x09, 0x22, // USAGE (Effect Block Index) 0x25, 0x7F, // LOGICAL_MAXIMUM (127) 0x75, 0x07, // REPORT_SIZE (7) 0x95, 0x01, // REPORT_COUNT (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x24, // USAGE (ROM Flag) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) // Define the available effect types. Effect Type is a named array that will // accept any of the ET usages listed. 0x09, 0x25, // USAGE (Effect Type) 0xA1, 0x02, // COLLECTION (Logical) 0x09, 0x26, // USAGE (ET Constant Force) 0x09, 0x27, // USAGE (ET Ramp) 0x09, 0x30, // USAGE (ET Square) 0x09, 0x31, // USAGE (ET Sine) 0x09, 0x32, // USAGE (ET Triangle) 0x09, 0x33, // USAGE (ET Sawtooth Up) 0x09, 0x34, // USAGE (ET Sawtooth Down) 0x09, 0x40, // USAGE (ET Spring) 0x09, 0x41, // USAGE (ET Damper) 0x09, 0x42, // USAGE (ET Inertia) 0x15, 0x01, // LOGICAL_MINIMUM (1) 0x25, 0x0A, // LOGICAL_MAXIMUM (10) 0x75, 0x08, // REPORT_SIZE (8) 0x91, 0x00, // OUTPUT (Data,Ary,Abs) 0xC0, // END_COLLECTION 0x09, 0x50, // USAGE (Duration) 0x09, 0x54, // USAGE (Trigger Repeat Interval) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x26, 0x10, 0x27, // LOGICAL_MAXIMUM (10000) 0x46, 0x10, 0x27, // PHYSICAL_MAXIMUM (10000) 0x75, 0x10, // REPORT_SIZE (16) 0x66, 0x03, 0x10, // UNIT (Eng Lin:Time) 0x55, 0x0D, // UNIT_EXPONENT (-3) 0x95, 0x02, // REPORT_COUNT (2) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x55, 0x0A, // UNIT_EXPONENT (-6) 0x09, 0x51, // USAGE (Sample Period) 0x95, 0x01, // REPORT_COUNT (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x45, 0x00, // PHYSICAL_MAXIMUM (0) 0x55, 0x00, // UNIT_EXPONENT (0) 0x65, 0x00, // UNIT (None) 0x09, 0x52, // USAGE (Gain) 0x09, 0x53, // USAGE (Trigger Button) 0x25, 0x7F, // LOGICAL_MAXIMUM (127) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x02, // REPORT_COUNT (2) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x55, // USAGE (Axes Enable) (Tie these axes to the stick) 0xA1, 0x02, // COLLECTION (Logical) 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x01, // USAGE (Pointer) 0xA1, 0x00, // COLLECTION (Physical) 0x09, 0x30, // USAGE (X) 0x09, 0x31, // USAGE (Y) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x95, 0x02, // REPORT_COUNT (2) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0xC0, // END_COLLECTION 0xC0, // END_COLLECTION 0x95, 0x06, // REPORT_COUNT (6) (6-bit pad) 0x91, 0x03, // OUTPUT (Cnst,Var,Abs) 0x05, 0x0F, // USAGE_PAGE (Physical Interface) 0x09, 0x57, // USAGE (Direction) 0xA1, 0x02, // COLLECTION (Logical) 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x01, // USAGE (Pointer)// Tie these axes to the stick 0xA1, 0x00, // COLLECTION (Physical) 0x09, 0x30, // USAGE (X) 0x09, 0x31, // USAGE (Y) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x46, 0x68, 0x01, // PHYSICAL_MAXIMUM (360) 0x66, 0x14, 0x00, // UNIT (Eng Rot:Angular Pos) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x02, // REPORT_COUNT (2) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x65, 0x00, // UNIT (None) 0x45, 0x00, // PHYSICAL_MAXIMUM (0) 0xC0, // END_COLLECTION 0xC0, // END_COLLECTION 0x05, 0x0F, // USAGE_PAGE (Physical Interface) 0x09, 0x58, // USAGE (Type Specific Block Offset) 0xA1, 0x02, // COLLECTION (Logical) 0x0B, 0x01, 0x00, 0x0A, 0x00, // USAGE (Ordinals:Instance 1) 0x0B, 0x02, 0x00, 0x0A, 0x00, // USAGE (Ordinals:Instance 2) 0x26, 0xFD, 0x7F, // LOGICAL_MAXIMUM (32765)// 32K RAM or ROM max. 0x75, 0x10, // REPORT_SIZE (16) 0x95, 0x02, // REPORT_COUNT (2) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0xC0, // END_COLLECTION 0xC0, // END_COLLECTION // Envelope Report Definition 0x09, 0x5A, // USAGE (Set Envelope Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x02, // REPORT_ID (2) 0x09, 0x23, // USAGE (Parameter Block Offset) 0x26, 0xFD, 0x7F, // LOGICAL_MAXIMUM (32765)// 32K RAM or ROM max 0x75, 0x0F, // REPORT_SIZE (15) 0x95, 0x01, // REPORT_COUNT (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x24, // USAGE (ROM Flag) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x5B, // USAGE (Attack Level) 0x09, 0x5D, // USAGE (Fade Level) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x02, // REPORT_COUNT (2) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x5C, // USAGE (Attack Time) 0x09, 0x5E, // USAGE (Fade Time) 0x26, 0x10, 0x27, // LOGICAL_MAXIMUM (10000) 0x46, 0x10, 0x27, // PHYSICAL_MAXIMUM (10000) 0x66, 0x03, 0x10, // UNIT (Eng Lin:Time) 0x55, 0x0D, // UNIT_EXPONENT (-3) 0x75, 0x10, // REPORT_SIZE (16) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x45, 0x00, // PHYSICAL_MAXIMUM (0) 0x65, 0x00, // UNIT (None) 0x55, 0x00, // UNIT_EXPONENT (0) 0xC0, // END_COLLECTION // Condition Report Definition 0x09, 0x5F, // USAGE (Set Condition Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x03, // REPORT_ID (3) 0x09, 0x23, // USAGE (Parameter Block Offset) 0x26, 0xFD, 0x7F, // LOGICAL_MAXIMUM (32765) 0x75, 0x0F, // REPORT_SIZE (15) 0x95, 0x01, // REPORT_COUNT (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x24, // USAGE (ROM Flag) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x60, // USAGE (CP Offset) 0x09, 0x61, // USAGE (Positive Coefficient) 0x09, 0x62, // USAGE (Negative Coefficient) 0x09, 0x63, // USAGE (Positive Saturation) 0x09, 0x64, // USAGE (Negative Saturation) 0x09, 0x65, // USAGE (Dead Band) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x06, // REPORT_COUNT (6) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0xC0, // END_COLLECTION // Periodic Report Definition 0x09, 0x6E, // USAGE (Set Periodic Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x04, // REPORT_ID (4) 0x09, 0x23, // USAGE (Parameter Block Offset) 0x26, 0xFD, 0x7F, // LOGICAL_MAXIMUM (32765) 0x75, 0x0F, // REPORT_SIZE (15) 0x95, 0x01, // REPORT_COUNT (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x24, // USAGE (ROM Flag) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x70, // USAGE (Magnitude) 0x09, 0x6F, // USAGE (Offset) 0x09, 0x71, // USAGE (Phase) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x03, // REPORT_COUNT (3) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x72, // USAGE (Period) 0x26, 0x10, 0x27, // LOGICAL_MAXIMUM (10000) 0x46, 0x10, 0x27, // PHYSICAL_MAXIMUM (10000) 0x66, 0x03, 0x10, // UNIT (Eng Lin:Time) 0x55, 0x0D, // UNIT_EXPONENT (-3) 0x75, 0x10, // REPORT_SIZE (16) 0x95, 0x01, // REPORT_COUNT (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x45, 0x00, // PHYSICAL_MAXIMUM (0) 0x65, 0x00, // UNIT (None) 0x55, 0x00, // UNIT_EXPONENT (0) 0xC0, // END_COLLECTION // Constant Force Report Definition 0x09, 0x73, // USAGE (Set Constant Force Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x05, // REPORT_ID (5) 0x09, 0x23, // USAGE (Parameter Block Offset) 0x26, 0xFD, 0x7F, // LOGICAL_MAXIMUM (32765) 0x75, 0x0F, // REPORT_SIZE (15) 0x95, 0x01, // REPORT_COUNT (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x24, // USAGE (ROM Flag) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x70, // USAGE (Magnitude) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x75, 0x08, // REPORT_SIZE (8) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0xC0, // END_COLLECTION // Ramp Force Report Definition 0x09, 0x74, // USAGE (Set Ramp Force Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x06, // REPORT_ID (6) 0x09, 0x23, // USAGE (Parameter Block Offset) 0x26, 0xFD, 0x7F, // LOGICAL_MAXIMUM (32765) 0x75, 0x0F, // REPORT_SIZE (15) 0x95, 0x01, // REPORT_COUNT (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x24, // USAGE (ROM Flag) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x75, // USAGE (Ramp Start) 0x09, 0x76, // USAGE (Ramp End) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x02, // REPORT_COUNT (2) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0xC0, // END_COLLECTION // Custom Force Data Report Definition // Downloads are always into RAM space so the ROM usage is not declared. 0x09, 0x68, // USAGE (Custom Force Data Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x07, // REPORT_ID (7) 0x09, 0x23, // USAGE (Parameter Block Offset) 0x26, 0xFD, 0x7F, // LOGICAL_MAXIMUM (32765) 0x75, 0x0F, // REPORT_SIZE (15) 0x95, 0x01, // REPORT_COUNT (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x0B, 0x3B, 0x00, 0x01, 0x00,// USAGE (Generic Desktop:Byte Count) 0x26, 0x00, 0x01, // LOGICAL_MAXIMUM (256) 0x75, 0x09, // REPORT_SIZE (9) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x69, // USAGE (Custom Force Data) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x75, 0x08, // REPORT_SIZE (8) 0x96, 0x00, 0x01, // REPORT_COUNT (256) 0x92, 0x02, 0x01, // OUTPUT (Data,Var,Abs,Buf) 0xC0, // END_COLLECTION // Download Force Sample Definition 0x09, 0x66, // USAGE (Download Force Sample) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x08, // REPORT_ID (8) 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x01, // USAGE (Pointer) 0xA1, 0x02, // COLLECTION (Logical) 0x09, 0x30, // USAGE (X) 0x09, 0x31, // USAGE (Y) 0x15, 0x81, // LOGICAL_MINIMUM (-127) 0x25, 0x7F, // LOGICAL_MAXIMUM (127) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x02, // REPORT_COUNT (2) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0xC0, // END_COLLECTION 0xC0, // END_COLLECTION 0x05, 0x0F, // USAGE_PAGE (Physical Interface) // Define the Custom Force parameter block // Custom Effects are always RAM based // so ROM flags are not declared. 0x09, 0x6B, // USAGE (Set Custom Force Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x09, // REPORT_ID (9) // Parameter block offset in pool // Custom Force data offset in pool 0x09, 0x23, // USAGE (Parameter Block Offset) 0x09, 0x6C, // USAGE (Custom Force Data Offset) 0x09, 0x6D, // USAGE (Sample Count) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x26, 0xFD, 0x7F, // LOGICAL_MAXIMUM (32765)// 32K of RAM or ROM max. 0x95, 0x03, // REPORT_COUNT (3) 0x75, 0x10, // REPORT_SIZE (16) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0xC0, // END_COLLECTION // Effect Operation Report Definition 0x09, 0x77, // USAGE (Effect Operation Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x0A, // REPORT_ID (10) 0x09, 0x22, // USAGE (Effect Block Index) 0x25, 0x7F, // LOGICAL_MAXIMUM (127) 0x75, 0x07, // REPORT_SIZE (7) 0x95, 0x01, // REPORT_COUNT (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x24, // USAGE (ROM Flag) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x09, 0x78, // USAGE (Effect Operation) 0xA1, 0x02, // COLLECTION (Logical) 0x09, 0x79, // USAGE (Op Effect Start) 0x09, 0x7A, // USAGE (Op Effect Start Solo) 0x09, 0x7B, // USAGE (Op Effect Stop) 0x15, 0x01, // LOGICAL_MINIMUM (1) 0x25, 0x03, // LOGICAL_MAXIMUM (3) 0x75, 0x08, // REPORT_SIZE (8) 0x91, 0x00, // OUTPUT (Data,Ary,Abs) 0xC0, // END_COLLECTION 0x09, 0x7C, // USAGE (Loop Count) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0xC0, // END_COLLECTION // PID Pool Report Definition 0x09, 0x7F, // USAGE (PID Pool Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x01, // REPORT_ID (1) 0x09, 0x80, // USAGE (RAM Pool Size) 0x09, 0x81, // USAGE (ROM Pool Size) 0x09, 0x82, // USAGE (ROM Effect Block Count) 0x26, 0xFD, 0x7F, // LOGICAL_MAXIMUM (32765) 0x95, 0x03, // REPORT_COUNT (3) 0x75, 0x10, // REPORT_SIZE (16) 0xB1, 0x02, // FEATURE (Data,Var,Abs) 0x09, 0xA8, // USAGE (Parameter Block Size) 0xA1, 0x02, // COLLECTION (Logical) 0x09, 0x21, // USAGE (Set Effect Report) 0x09, 0x5A, // USAGE (Set Envelope Report) 0x09, 0x5F, // USAGE (Set Condition Report) 0x09, 0x6E, // USAGE (Set Periodic Report) 0x09, 0x73, // USAGE (Set Constant Force Report) 0x09, 0x74, // USAGE (Set Ramp Force Report) 0x09, 0x6B, // USAGE (Set Custom Force Report) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x07, // REPORT_COUNT (7) 0xB1, 0x02, // FEATURE (Data,Var,Abs) 0xC0, // END_COLLECTION 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x07, // REPORT_SIZE (7) 0x95, 0x01, // REPORT_COUNT (1) 0xB1, 0x03, // FEATURE (Cnst,Var,Abs)// 7-bit pad 0x09, 0x67, // USAGE (Isoch Custom Force Enable) 0x75, 0x01, // REPORT_SIZE (1) 0xB1, 0x02, // FEATURE (Data,Var,Abs) 0xC0, // END_COLLECTION // PID State Report Definition 0x09, 0x92, // USAGE (PID State Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x02, // REPORT_ID (2) 0x09, 0x22, // USAGE (Effect Block Index) 0x25, 0x7F, // LOGICAL_MAXIMUM (127) 0x75, 0x07, // REPORT_SIZE (7) 0x81, 0x02, // INPUT (Data,Var,Abs) 0x09, 0x24, // USAGE (ROM Flag) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x95, 0x01, // REPORT_COUNT (1) 0x81, 0x02, // INPUT (Data,Var,Abs) 0x09, 0x94, // USAGE (Effect Playing) 0x09, 0xA0, // USAGE (Actuators Enabled) 0x09, 0xA4, // USAGE (Safety Switch) 0x09, 0xA6, // USAGE (Actuator Power) 0x75, 0x01, // REPORT_SIZE (1) 0x95, 0x04, // REPORT_COUNT (4) 0x81, 0x02, // INPUT (Data,Var,Abs) 0x81, 0x03, // INPUT (Cnst,Var,Abs)// 4-bit pad 0xC0, // END_COLLECTION // PID Device Control Report Definition 0x09, 0x95, // USAGE (PID Device Control Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x0B, // REPORT_ID (11) 0x09, 0x96, // USAGE (PID Device Control) 0xA1, 0x02, // COLLECTION (LOGICAL) 0x09, 0x97, // USAGE (0xDC, Enable Actuators) 0x09, 0x98, // USAGE (0xDC, Disable Actuators) 0x09, 0x99, // USAGE (0xDC, Stop All Effects) 0x09, 0x9A, // USAGE (0xDC, Reset) 0x09, 0x9B, // USAGE (0xDC, Pause) 0x09, 0x9C, // USAGE (0xDC, Continue) 0x15, 0x01, // LOGICAL_MINIMUM (1) 0x25, 0x06, // LOGICAL_MAXIMUM (6) 0x75, 0x01, // REPORT_SIZE (1) 0x95, 0x08, // REPORT_COUNT (4) 0x91, 0x02, // OUTPUT(Data,Ary,Abs) 0xC0, // END_COLLECTION 0xC0, // END_COLLECTION // PID Pool Move Report Definition 0x09, 0x85, // USAGE (PID Pool Move Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x0C, // REPORT_ID (12) 0x09, 0x86, // USAGE (Move Source) 0x09, 0x87, // USAGE (Move Destination) 0x09, 0x88, // USAGE (Move Length) 0x26, 0xFF, 0x7F, // LOGICAL_MAXIMUM (32767) 0x75, 0x10, // REPORT_SIZE (16) 0x95, 0x03, // REPORT_COUNT (3) 0x92, 0x02, 0x01, // OUTPUT (Data,Var,Abs,Buf) 0xC0, // END_COLLECTION // Device Gain Report Definition 0x09, 0x7D, // USAGE (Device Gain Report) 0xA1, 0x02, // COLLECTION (Logical) 0x85, 0x02, // REPORT_ID (2) 0x09, 0x7E, // USAGE (Device Gain) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x01, // REPORT_COUNT (1) 0xB1, 0x02, // FEATURE (Data,Var,Abs) 0xC0, // END_COLLECTION 0xC0 //END_COLLECTION }; typedef struct { uchar buttons; uchar dpad; uchar x; uchar y; uchar cx; uchar cy; uchar ltrig; uchar rtrig; } report_t; report_t reportBuffer; usbMsgLen_t usbFunctionDescriptor(struct usbRequest* rq) { if ((rq->bmRequestType & USBRQ_TYPE_MASK) != USBRQ_TYPE_STANDARD) return 0; if (rq->bRequest == USBRQ_GET_DESCRIPTOR && rq->wValue.bytes[1]==USBDESCR_HID_REPORT) { usbMsgPtr = (void*)floUsbHidReportDescriptor; return floUsbHidReportDescriptorLen; } return 0; } /* device is detected, however unreliably. dunno why. */ void debug(int i) { if (i >= 63) i=63; PORTB = ~i; } USB_PUBLIC usbMsgLen_t 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)) { // 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. } /* reads eight exploded bits from bufptr, and makes a 8bit-char from them. * Bit ordering is MSB first. A bit is considered as 1, if bufptr[i] & 0x80 is * *not* set, and vice versa. */ char decode_byte(char* bufptr) { char result=0; for (int i=0;i<8;i++) result|= ( (bufptr[7-i]&0x80)?0:(1<=256+ltrig) count2=0; count3++; if (count3>=25) count3=0; // DEBUGGING CODE ends here. // USB CODE starts here usbPoll(); if(usbInterruptIsReady()) usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer)); // USB CODE ends here _delay_us(10); // GAMECUBE COMMUNICATION starts here char getid_msg=0x00; send_recv_gc(&getid_msg, 1); // send get-id message; probably unneccessary. char getstatus_msg[] = { 0x40, 0x03, 0x00 }; if (count2<256 && count3