Igor Martinovski
/
USBJoystick
N64 to USB HID interface
Diff: main.cpp
- Revision:
- 1:38815edb0ecb
- Parent:
- 0:547c5459faa6
- Child:
- 2:b38d6345dd14
--- a/main.cpp Sun Sep 25 03:44:08 2011 +0000 +++ b/main.cpp Sun Sep 25 16:22:03 2011 +0000 @@ -48,7 +48,7 @@ #include "mbed.h" #include "stdint.h" #include "usbhid.h" - +#include "IOMacros.h" USBJoystick joystick; @@ -118,13 +118,43 @@ DigitalInOut inout(p14); // Connect controller here // using 220 ohm pull-up Serial pc(USBTX, USBRX); - +DigitalOut led(LED1); uint8_t x, y; +volatile char bit_count; union controls { /* A definition and a declaration */ uint8_t array[4]; uint32_t result_32; } controls; +/** EINT3_IRQHandler + */ +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. + // Test for IRQ on Port0. + + if (LPC_GPIOINT->IntStatus & 0x1) { + // If P0_16/p14 rises, call atint() + if (LPC_GPIOINT->IO0IntStatF & (1 << 16)) ISR(); + } + // 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; + // Enable P0_16/p14 for falling edge interrupt generation. + LPC_GPIOINT->IO0IntEnF |= (1UL << 16); + // Enable the interrupt. + //NVIC_EnableIRQ(EINT3_IRQn); +} + + // Temp array for controller data /* void delay(unsigned int n) is used as a short delay to @@ -136,6 +166,13 @@ ; } +void ISR(){ + delay(48); + if (inout.read()) controls.result_32 |= (1UL<< bit_count); + else controls.result_32 &= ~(1UL << bit_count); + bit_count++; +} + /* int receive(char *data_array, unsigned char n) is used to receive a bit stream of bits into an array of n bytes coming from the controller. This must be called immediately after @@ -143,36 +180,18 @@ */ int receive(uint8_t *data_array, unsigned char n) { - unsigned char sample, previous_sample; - unsigned char bit, byte; + int i; - + bit_count=0; - //inout.input(); // Not sure about this.. + NVIC_EnableIRQ(EINT3_IRQn); - sample = inout.read(); - for (i=0;i < n ;i++) { - byte = 0; - bit = 0; - while (bit<8) { - previous_sample = sample; - sample = inout.read(); - if ((previous_sample ^ sample) & previous_sample) { + while(bit_count<33){ + ;//Wait for Interrupts + } + led=1; + NVIC_DisableIRQ(EINT3_IRQn); - delay(60); - sample=inout.read(); - if (sample) - byte = byte | (1<<bit); - bit++; - } - - data_array[i]= byte; - } - - } - /* The for loop here is used to reverse the bits for the x and y values retured - by the controller. I am not entirely sure of the format. I'm missing a bit - somewhere.. Values don't range from -127..128. */ x=y=0; for (i=0;i<=7;i++) { if (controls.array[2] & (1 << i)) @@ -184,6 +203,8 @@ data_array[3]=y; return n; } + + /* void send_byte(unsigned char byte) is used to send a single byte to the controller. */ @@ -216,8 +237,8 @@ int main() { inout.mode(OpenDrain); // Must use open-drain for N64 Controller! - - uint32_t i, previous_result=0; // Used for the bit-shifting for x and y values + 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); while (1) {