N64 to USB HID interface

Dependencies:   mbed

Revision:
2:b38d6345dd14
Parent:
1:38815edb0ecb
Child:
3:b9cecf3e2496
--- a/main.cpp	Sun Sep 25 16:22:03 2011 +0000
+++ b/main.cpp	Sun Sep 25 17:34:08 2011 +0000
@@ -128,26 +128,32 @@
 
 /** EINT3_IRQHandler
  */
+ void delay (unsigned int);
 void ISR(void);
 extern "C" void EINT3_IRQHandler(void) {
 
     // The "event" is connected to pin p10 which is LPC1768 P0_1
-    // so lets trap that and ignore all other GPIO interrupts.    
+    // so lets trap that and ignore all other GPIO interrupts.
     // Test for IRQ on Port0.
 
-    if (LPC_GPIOINT->IntStatus & 0x1) {
+    // if (LPC_GPIOINT->IntStatus & 0x1) {
     // If P0_16/p14 rises, call atint()
-        if (LPC_GPIOINT->IO0IntStatF & (1 << 16))  ISR();
+    if (LPC_GPIOINT->IO0IntStatF & (1 << 16)) {
+        delay(30);
+        if (inout.read()) controls.result_32 |= (1UL<< bit_count);
+        else controls.result_32  &= ~(1UL << bit_count);
+        bit_count++;
     }
-    // Clear this and all other possible GPIO generated interrupts as they don't concern us.    
-    LPC_GPIOINT->IO2IntClr = (LPC_GPIOINT->IO2IntStatR | LPC_GPIOINT->IO2IntStatF);
+    //}
+    // Clear this and all other possible GPIO generated interrupts as they don't concern us.
+     LPC_GPIOINT->IO2IntClr = (LPC_GPIOINT->IO2IntStatR | LPC_GPIOINT->IO2IntStatF);
     LPC_GPIOINT->IO0IntClr = (LPC_GPIOINT->IO0IntStatR | LPC_GPIOINT->IO0IntStatF);
 
 }
 
 void event_irq_init(void) {
     // Use macro to set p10 as an input.
-    //p10_AS_INPUT; 
+    //p10_AS_INPUT;
     // Enable P0_16/p14 for falling edge interrupt generation.
     LPC_GPIOINT->IO0IntEnF |= (1UL << 16);
     // Enable the interrupt.
@@ -166,8 +172,8 @@
         ;
 }
 
-void ISR(){
-    delay(48);
+void ISR() {
+    wait_us(2);
     if (inout.read()) controls.result_32 |= (1UL<< bit_count);
     else controls.result_32  &= ~(1UL << bit_count);
     bit_count++;
@@ -180,27 +186,25 @@
  */
 
 int receive(uint8_t *data_array, unsigned char n) {
-       
+
     int i;
     bit_count=0;
-
+    //inout.write(1);
     NVIC_EnableIRQ(EINT3_IRQn);
 
-    while(bit_count<33){
-        ;//Wait for Interrupts
-        }
-       led=1;
+    wait_us(500);
+    //led=1;
     NVIC_DisableIRQ(EINT3_IRQn);
 
-        x=y=0;
-        for (i=0;i<=7;i++) {
-            if (controls.array[2] & (1 << i))
-                x=x | (1 << (7-i));
-            if (controls.array[3] & (1 << i))
-                y=y | (1 << (7-i));
-        }
-        data_array[2]=x;
-        data_array[3]=y;
+    x=y=0;
+    for (i=0; i<=7; i++) {
+        if (controls.array[2] & (1 << i))
+            x=x | (1 << (7-i));
+        if (controls.array[3] & (1 << i))
+            y=y | (1 << (7-i));
+    }
+    data_array[2]=x;
+    data_array[3]=y;
     return n;
 }
 
@@ -239,18 +243,18 @@
     inout.mode(OpenDrain);           // Must use open-drain for N64 Controller!
     event_irq_init();
     uint32_t previous_result=0;                           // Used for the bit-shifting for x and y values
-    send_byte(RESET_CONTROLLER);
-              wait_ms(10);
+    //send_byte(RESET_CONTROLLER);
+    wait_ms(10);
     while (1) {
         wait_ms(10);
         previous_result=controls.result_32;
         send_byte(1);                // Send the request byte
         receive(controls.array, 4);           // Start receiving bit stream
-        
+        pc.printf("bitcount: %d\n",bit_count);
         if (controls.result_32==previous_result) continue;
-        
-        
-        pc.printf("%3d %3d %d %d\n ", controls.array[0], controls.array[1], (int8_t)controls.array[2], (int8_t)controls.array[3]);
+
+
+        // pc.printf("%3d %3d %d %d\n ", controls.array[0], controls.array[1], (int8_t)controls.array[2], (int8_t)controls.array[3]);
         //pc.printf("%d\n",controls.result_32);
 //joystick.joystick(controls.array[0],controls.array[1], controls.array[2], controls.array[3]);
     }