// Now create any global variables such as motors, servos, sensors etc // This routine is called once only and allows you to do set up the hardware // Dont use any 'clock' functions here - use 'delay' functions instead void appInitHardware(void){ a2dSetPrescaler(ADC_PRESCALE_DIV128); cyrf6936_Initialise_hard(); // Initialise SPI bus as master. (RF modules connected to hardware SPI) spiBusInit(&bus, TRUE); spiDeviceSelect(&cyrf_0, TRUE); spiDeviceSelect(&cyrf_0, FALSE); spiDeviceSelect(&cyrf_1, TRUE); spiDeviceSelect(&cyrf_1, FALSE); // set I/O pins for RF module(s). pin_make_input(D4, FALSE); // set PACTL pin to input. (module on connector J1) pin_make_input(D5, FALSE); // set PACTLn pin to input. (module on connector J1) pin_make_input(D6, FALSE); // set PACTL pin to input. (module on connector J2) pin_make_input(D7, FALSE); // set PACTLn pin to input. (module on connector J2) #ifdef RF_MODULE_ARTAFLEX // pin_make_output(D4, FALSE); // set RXPA pin to output. (module on connector J1) pin_make_output(D5, FALSE); // set TXPA pin to output. (module on connector J1) pin_make_output(D6, FALSE); // set RXPA pin to output. (module on connector J2) pin_make_output(D7, FALSE); // set TXPA pin to output. (module on connector J2) #endif pin_make_input(E7, TRUE); // set UNIGEN RF module IRQ pin to input. (module on connector J1) pin_make_input(E6, TRUE); // set UNIGEN RF module IRQ pin to input. (module on connector J2) pin_make_output(G3, FALSE); // set UNIGEN RF module RST pin to output. (both modules) //pin_low(G3); // don't reset yet. // set I/O pins for status LEDs pin_make_output(C0, TRUE); // set LED pin for output pin_make_output(C1, TRUE); // set LED pin for output pin_make_output(C2, FALSE); // set LED pin for output //pin_high(C0); // LED off //pin_high(C1); // LED off //pin_low(C2); // LED on // Set UART1 to 19200 baud uartInit(UART1, 38400); // Tell rprintf to output to UART1 rprintfInit(&uart1SendByte); // Initialise the servo controller using Hardware PWM servoPWMInit(&bank1); // Initialise WatchDog Timer wdt_enable( WDTO_500MS ); }
static void sendCmd(SONY_PS2* controller, const uint8_t* data, size_t len){ for(int i=0; i<len; i++){ spiDeviceSendByte(controller,pgm_read_byte(&data[i])); } spiDeviceSelect(controller,FALSE); /* Deselect the device */ delay_ms(10); }
boolean sonyPS2_read(SONY_PS2* controller){ boolean ok = FALSE; // inferred // spiDeviceSelect(controller,TRUE); /* Select the device */ // if(spiAbstractBusGetClock(spiDeviceGetBus(controller)) < SPI_CLOCK_DIV32 ){ // spiAbstractBusSetClock(spiDeviceGetBus(controller), SPI_CLOCK_DIV32); // } controller->previous = sonyPS2_buttonsRaw(controller); spiDeviceSendByte(controller,0x01); controller->mode = spiDeviceXferByte(controller,0x42); controller->reply = spiDeviceReceiveByte(controller); uint8_t numb = 0; if(controller->reply == 0x5A ){ if(controller->mode == PS2_MODE_ANALOGUE_PRESSURE){ // read 2 bytes for buttons, 4 for joysticks, 12 for button pressures = 18 numb = 18; }else if(controller->mode == PS2_MODE_ANALOGUE_RED){ // read 2 bytes for buttons, 4 for joysticks = 6 numb = 6; }else if(controller->mode == PS2_MODE_DIGITAL){ // Read 2 bytes from controller for all 16 buttons numb = 2; } if(numb!=0){ uint8_t i=0; // If rumbling then send out the rumble settings if(controller->rumble){ controller->buffer[i++] = spiDeviceXferByte(controller,(controller->rumbleRight) ? 1 : 0); controller->buffer[i++] = spiDeviceXferByte(controller,controller->rumbleLeft); } while(i<numb){ controller->buffer[i++] = spiDeviceReceiveByte(controller); } ok = TRUE; } } spiDeviceSelect(controller,FALSE); /* Deselect the device */ #ifdef DEBUG PRINTF(DEBUG," %x %x",(unsigned)(controller->mode),(unsigned)(controller->reply) ); if(numb!=0){ for(uint8_t i=0; i<numb; i++){ PRINTF(DEBUG," %x",controller->buffer[i]); } } PRINTF(DEBUG,"\n"); #endif // Fill out the rest with the defaults while(numb < sizeof(controller->buffer)){ controller->buffer[numb++] = pgm_read_byte(&reply[numb]); } return ok; }