Пример #1
0
int main(void)
{
	CPU_PRESCALE(CPU_16MHz);
	SPI_SlaveInit();
	InitWEnc();
	
	u08 main_msg;
	
	// Start keeping track of position at 0
	WE_wheel_0 = 0;
	while(!WE_wheel_0);
	WE_position = 0;
	
	// Loop indefinitely
	while(1)
	{
		// obtain command from main uC
		main_msg = SPI_SlaveReceive();
		switch(main_msg)
		{
			case SPI_WHEEL_POS:
			SPI_SlaveReceiveX(WE_position);
			break;
			
			case SPI_WHEEL_RPM:
			SPI_SlaveReceiveX(WE_RPM);
			break;
			
			default:
			break;
		}
	}
}
Пример #2
0
int main (void)
{
	TWI_init();					//TWI Bus Initialisieren
	TWI_59116_reset();
	TWI_59116_setup();
	
	if(CORE==Master){
		DDRA=0xff;				//PORTA als Ausgang
		DDRD|=(1<<2);			// PD2 als Ausgang
		Interrupt_init();
		sei();
		SPI_MasterInit();
		Ebene_ein=0;
		
		while(1)
		{
		
				
			effect_blinky2(1); //ok
			effect_planboing(0,AXIS_Z,20); //ok
			effect_planboing(1,AXIS_X,20); //ok
			effect_planboing(0,AXIS_Y,20); //ok
			effect_planboing(1,AXIS_Z,20); //ok
			for (uint8_t ii=0;ii<8;ii++)
				effect_box_shrink_grow (0,ii%4, ii & 4, 20); //ok
			//sendvoxels_rand_z(100,10,20); // ok
			effect_random_sparkle (1,3,20,10); //ok
			effect_box_woopwoop(1,40,1); //naja
			effect_rain(0,300); //ok
			//effect_wormsqueeze (2, AXIS_Z, 1, 100, 25); //ok
			for(int i=0;i<8;i++){			//	Pixel durchtesten
				for(int j=0;j<64;j++)
				{	LED[0][i][j/8][j%8]=0xff;
					_delay_us(500);
				}
				for(int j=0;j<64;j++)
				{	LED[0][i][j/8][j%8]=0;
					_delay_us(500);
				}
			}
		}
	
	}else{
		DDRA=0x00;				//PORTA als Eingang
		DDRD&=~(1<<2);			// PD2 als Eingang
		SPI_SlaveInit();
		Ebene_ein=0;
		while(1)
		{
			
			I2C_Leds_ein(Ebene_ein%8);		//Säulentreiber einschalten für nächste Ebene
							
		}
	}

	
return 0;
}
Пример #3
0
int main(void)
{
	
	DDRA = 0x00; PORTA = 0xFF;	//Input Port, joystick and buttons.
	DDRB = 0xFF; PORTB = 0x00;	//Output Port, test output to led.
	DDRD = 0xFF; PORTD = 0x00;
	DDRC = 0xFF; PORTC = 0x00;	//C0 is motor.

	unsigned char i=0;
	tasks[i].state = JS_SMStart;
	tasks[i].period = periodJoystick;
	tasks[i].elapsedTime = tasks[i].period;
	tasks[i].TickFct = &TickFct_Joystick;
	i++;
	tasks[i].state = JP_SMStart;
	tasks[i].period = periodJump;
	tasks[i].elapsedTime = tasks[i].period;
	tasks[i].TickFct = &TickFct_Jump;
	i++;
	tasks[i].state = CH_SMStart;
	tasks[i].period = periodCrouch;
	tasks[i].elapsedTime = tasks[i].period;
	tasks[i].TickFct = &TickFct_Crouch;
	i++;
	tasks[i].state = RT_SMStart;
	tasks[i].period = periodReset;
	tasks[i].elapsedTime = tasks[i].period;
	tasks[i].TickFct = &TickFct_Reset;
	i++;
	tasks[i].state = TN_SMStart;
	tasks[i].period = periodTransmission;
	tasks[i].elapsedTime = tasks[i].period;
	tasks[i].TickFct = &TickFct_Transmission;
	i++;
	tasks[i].state = MR_SMStart;
	tasks[i].period = periodMotor;
	tasks[i].elapsedTime = tasks[i].period;
	tasks[i].TickFct = &TickFct_Motor;
	i++;
	tasks[i].state = SE_SMStart;
	tasks[i].period = periodScore;
	tasks[i].elapsedTime = tasks[i].period;
	tasks[i].TickFct = &TickFct_Score;

	TimerSet(tasksPeriodGCD);
	TimerOn();
	ADC_init();
	SPI_SlaveInit();

	while (1)
	{
		while(!TimerFlag);
		TimerFlag = 0;
	}

	return 0;
}
int main(void)
{
    uint32_t i = 0;
    uint8_t err = 0;
    spi_transfer_t xfer = {0};
    spi_slave_config_t userConfig;

    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();
    PRINTF("\n\rSlave is working....\n\r");

    /*
     * userConfig.polarity = kSPI_ClockPolarityActiveHigh;
     * userConfig.phase = kSPI_ClockPhaseFirstEdge;
     * userConfig.direction = kSPI_MsbFirst;
     * userConfig.enableStopInWaitMode = false;
     * userConfig.dataMode = kSPI_8BitMode;
     * userConfig.txWatermark = kSPI_TxFifoOneHalfEmpty;
     * userConfig.rxWatermark = kSPI_RxFifoOneHalfFull;
     */
    SPI_SlaveGetDefaultConfig(&userConfig);
    SPI_SlaveInit(EXAMPLE_SPI_SLAVE, &userConfig);
    SPI_SlaveTransferCreateHandle(EXAMPLE_SPI_SLAVE, &handle, slaveCallback, NULL);
    for (i = 0; i < 64; i++)
    {
        sendBuff[i] = i;
    }

    /* receive data from master */
    xfer.txData = sendBuff;
    xfer.rxData = buff;
    xfer.dataSize = BUFFER_SIZE;
    SPI_SlaveTransferNonBlocking(EXAMPLE_SPI_SLAVE, &handle, &xfer);

    while (slaveFinished != true)
    {
    }

    for (i = 0; i < BUFFER_SIZE; i++)
    {
        if (buff[i] != i)
        {
            PRINTF("\n\rThe %d number is wrong! It is %dn\r", i, buff[i]);
            err++;
        }
    }
    PRINTF("\r\n");
    if (err == 0)
    {
        PRINTF("Succeed!\n\r");
    }

    while (1)
    {
    }
}
Пример #5
0
int main (void){

	DIO_voidInit();
	SPI_voidInit();
	SPI_voidEnable();
	SPI_voidInterruptEnable();
	SPI_voidSetISR(SPI_IRQHandelr);
	SPI_SlaveInit();
	UART_voidInit();
	UART_voidTxEnable();

	while(1);


	return 0 ;
}
//__________________________________________________________________________________________________
void anibike_dl_slave_initialize ( void )
{	
	// map PORT C to virtual port 1
	PORT_MapVirtualPort1( PORTCFG_VP1MAP_PORTC_gc );
			
	// set clk out and data in
	DATALINK_PORT.DIRCLR = DATALINK_DATA_PIN|DATALINK_CLK_PIN|DATALINK_CS_PIN;
		
	PORT_ConfigurePins( &DATALINK_PORT,
						DATALINK_CLK_PIN|DATALINK_DATA_PIN,
						false,
						false,
						PORT_OPC_TOTEM_gc,
						PORT_ISC_BOTHEDGES_gc );
						
	PORT_ConfigurePins( &DATALINK_PORT,
						DATALINK_CS_PIN,
						false,
						false,
						PORT_OPC_PULLUP_gc,
						PORT_ISC_FALLING_gc );
						//PORT_ISC_BOTHEDGES_gc );
	
		
	/* Initialize SPI slave on port C. */
	SPI_SlaveInit(&spiSlaveC,
	              &DATALINK_SPI,
	              &DATALINK_PORT,
	              false,
	              SPI_MODE_0_gc,
	              SPI_INTLVL_MED_gc);
				  
	PORT_ConfigureInterrupt0( &DATALINK_PORT, PORT_INT0LVL_HI_gc, DATALINK_CS_PIN );		

	PMIC.CTRL |= PMIC_LOLVLEN_bm|PMIC_MEDLVLEN_bm|PMIC_HILVLEN_bm;	
	
	DL_SLAVE_CIRC_BUFFER_START = 0;
	DL_SLAVE_CIRC_BUFFER_END = 0;	
	
	
	g_receive_buffer = g_buffer_II;
	g_proj_buffer = g_buffer_I;
	g_current_double_buffer = 0;
	set_projection_buffer ( g_proj_buffer );
}
Пример #7
0
void spi_format(spi_t *obj, int bits, int mode, int slave)
{
    spi_master_config_t master_config;
    spi_slave_config_t slave_config;

    /* Bits: values between 4 and 16 are valid */
    MBED_ASSERT(bits >= 4 && bits <= 16);
    obj->bits = bits;

    if (slave) {
        /* Slave config */
        SPI_SlaveGetDefaultConfig(&slave_config);
        slave_config.dataWidth = (spi_data_width_t)(bits - 1);
        slave_config.polarity = (mode & 0x2) ? kSPI_ClockPolarityActiveLow : kSPI_ClockPolarityActiveHigh;
        slave_config.phase = (mode & 0x1) ? kSPI_ClockPhaseSecondEdge : kSPI_ClockPhaseFirstEdge;

        SPI_SlaveInit(spi_address[obj->instance], &slave_config);
    } else {
        /* Master config */
        SPI_MasterGetDefaultConfig(&master_config);
        master_config.dataWidth = (spi_data_width_t)(bits - 1);
        master_config.polarity = (mode & 0x2) ? kSPI_ClockPolarityActiveLow : kSPI_ClockPolarityActiveHigh;
        master_config.phase = (mode & 0x1) ? kSPI_ClockPhaseSecondEdge : kSPI_ClockPhaseFirstEdge;
        master_config.direction = kSPI_MsbFirst;

        switch (obj->instance) {
            case 0:
                CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0);
                RESET_PeripheralReset(kFC0_RST_SHIFT_RSTn);
                break;
            case 1:
                CLOCK_AttachClk(kFRO12M_to_FLEXCOMM1);
                RESET_PeripheralReset(kFC1_RST_SHIFT_RSTn);
                break;
            case 2:
                CLOCK_AttachClk(kFRO12M_to_FLEXCOMM2);
                RESET_PeripheralReset(kFC2_RST_SHIFT_RSTn);
                break;
            case 3:
                CLOCK_AttachClk(kFRO12M_to_FLEXCOMM3);
                RESET_PeripheralReset(kFC3_RST_SHIFT_RSTn);
                break;
            case 4:
                CLOCK_AttachClk(kFRO12M_to_FLEXCOMM4);
                RESET_PeripheralReset(kFC4_RST_SHIFT_RSTn);
                break;
            case 5:
                CLOCK_AttachClk(kFRO12M_to_FLEXCOMM5);
                RESET_PeripheralReset(kFC5_RST_SHIFT_RSTn);
                break;
            case 6:
                CLOCK_AttachClk(kFRO12M_to_FLEXCOMM6);
                RESET_PeripheralReset(kFC6_RST_SHIFT_RSTn);
                break;
            case 7:
                CLOCK_AttachClk(kFRO12M_to_FLEXCOMM7);
                RESET_PeripheralReset(kFC7_RST_SHIFT_RSTn);
                break;
#if (FSL_FEATURE_SOC_FLEXCOMM_COUNT > 8U)
            case 8:
                CLOCK_AttachClk(kFRO12M_to_FLEXCOMM8);
                RESET_PeripheralReset(kFC8_RST_SHIFT_RSTn);
                break;
#endif
#if (FSL_FEATURE_SOC_FLEXCOMM_COUNT > 9U)
            case 9:
                CLOCK_AttachClk(kFRO12M_to_FLEXCOMM9);
                RESET_PeripheralReset(kFC9_RST_SHIFT_RSTn);
                break;
#endif

        }

        SPI_MasterInit(spi_address[obj->instance], &master_config, 12000000);
    }
}
Пример #8
0
/*! \brief Test function.
 *
 *  This function tests the SPI master and slave drivers in polled operation,
 *  with a master (on port C) communicating with a slave (on port D).
 *
 *  Hardware setup:
 *
 *    - Connect PC4 to PD4 (SS)
 *    - Connect PC5 to PD5 (MOSI)
 *    - Connect PC6 to PD6 (MISO)
 *    - Connect PC7 to PD7 (SCK)
 *
 *  The drivers are tested in two phases:
 *
 *  1: Data is transmitted on byte at a time from the master to the slave.
 *     The slave increments the received data and sends it back. The master reads
 *     the data from the slave and verifies that it equals the data sent + 1.
 *
 *  2: Data is transmitted 4 bytes at a time to the slave. As the master sends
 *     a byte to the slave, the preceding byte is sent back to the master.
 *     When all bytes have been sent, it is verified that the last 3 bytes
 *     received at the master, equal the first 3 bytes sent.
 *
 *  The variable, 'success', will be non-zero when the function reaches the
 *  infinite for-loop if the test was successful.
 */
int main(void)
{
	/* Init SS pin as output with wired AND and pull-up. */
	PORTC.DIRSET = PIN4_bm;
	PORTC.PIN4CTRL = PORT_OPC_WIREDANDPULL_gc;

	/* Set SS output to high. (No slave addressed). */
	PORTC.OUTSET = PIN4_bm;

	/* Instantiate pointer to ssPort. */
	PORT_t *ssPort = &PORTC;

	/* Initialize SPI master on port C. */
	SPI_MasterInit(&spiMasterC,
	               &SPIC,
	               &PORTC,
	               false,
	               SPI_MODE_0_gc,
	               SPI_INTLVL_LO_gc,
	               false,
	               SPI_PRESCALER_DIV4_gc);

	/* Initialize SPI slave on port D. */
	SPI_SlaveInit(&spiSlaveD,
	              &SPID,
	              &PORTD,
	              false,
				  SPI_MODE_0_gc,
				  SPI_INTLVL_LO_gc);

	/* PHASE 1: Transceive individual bytes. */

	/* MASTER: Pull SS line low. This has to be done since
	 *         SPI_MasterTransceiveByte() does not control the SS line(s). */
	SPI_MasterSSLow(ssPort, PIN4_bm);

	for(uint8_t i = 0; i < NUM_BYTES; i++) {
		/* MASTER: Transmit data from master to slave. */
		SPI_MasterTransceiveByte(&spiMasterC, masterSendData[i]);

		/* SLAVE: Wait for data to be available. */
		while (SPI_SlaveDataAvailable(&spiSlaveD) == false) {

		}

		/* SLAVE: Get the byte received. */
		uint8_t slaveByte = SPI_SlaveReadByte(&spiSlaveD);

		/* SLAVE: Increment received byte and send back. */
		slaveByte++;
		SPI_SlaveWriteByte(&spiSlaveD, slaveByte);

		/* MASTER: Transmit dummy data to shift data from slave to master. */
		uint8_t masterReceivedByte = SPI_MasterTransceiveByte(&spiMasterC, 0x00);

		/* MASTER: Check if the correct value was received. */
		if (masterReceivedByte != (masterSendData[i] + 1) ) {
			success = false;
		}
	}

	/* MASTER: Release SS to slave. */
	SPI_MasterSSHigh(ssPort, PIN4_bm);

	/* PHASE 2: Transceive data packet. */

	/* Create data packet (SS to slave by PC4). */
	SPI_MasterCreateDataPacket(&dataPacket,
	                           masterSendData,
	                           masterReceivedData,
	                           NUM_BYTES,
	                           &PORTC,
	                           PIN4_bm);

	/* Transceive packet. */
	SPI_MasterTransceivePacket(&spiMasterC, &dataPacket);

	/* Check that correct data was received. Assume success at first. */
	for (uint8_t i = 0; i < NUM_BYTES - 1; i++) {
		if (masterReceivedData[i + 1] != masterSendData[i]) {
			success = false;
		}
	}

	while(true) {
		nop();
	}
}
Пример #9
0
/*! \brief Test function.
 *
 *  This function tests the SPI master and slave drivers in interrupt-driven
 *  operation, with a master (on port C) communicating with a slave (on port D).
 *
 *  Hardware setup:
 *
 *    - Connect PC4 to PD4 (SS)
 *    - Connect PC5 to PD5 (MOSI)
 *    - Connect PC6 to PD6 (MISO)
 *    - Connect PC7 to PD7 (SCK)
 *
 *  The driver is tested by transmitting data from the master to the slave.
 *  The slave increments the received data and sends it back. The master reads
 *  the data from the slave and verifies that it equals the data sent + 1.
 *
 *  The first data transaction is initiated by the main routine. When a
 *  transaction has finished, an interrupt will be triggered which will start
 *  new transactions until all bytes have been transceived.
 *
 *  The variable, 'success', will be non-zero when the function reaches the
 *  infinite for-loop if the test was successful.
 *
 *  \note This example uses multilevel interrupts. For more information on how
 *        to use the interrupt controller, refer to application note AVR1305.
 */
int main( void )
{
	/* Init SS pin as output with wired AND and pull-up. */
	PORTC.DIRSET = PIN4_bm;
	PORTC.PIN4CTRL = PORT_OPC_WIREDANDPULL_gc;

	/* Set SS output to high. (No slave addressed). */
	PORTC.OUTSET = PIN4_bm;

	/* Initialize SPI master on port C. */
	SPI_MasterInit(&spiMasterC,
	               &SPIC,
	               &PORTC,
				   false,
	               SPI_MODE_0_gc,
	               SPI_INTLVL_LO_gc,
	               false,
	               SPI_PRESCALER_DIV4_gc);

	/* Initialize SPI slave on port D. */
	SPI_SlaveInit(&spiSlaveD,
	              &SPID,
	              &PORTD,
	              false,
	              SPI_MODE_0_gc,
	              SPI_INTLVL_MED_gc);

	/* Enable low and medium level interrupts in the interrupt controller. */
	PMIC.CTRL |= PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm;
	sei();

	/* Create data packet (SS to slave by PC4) */
	SPI_MasterCreateDataPacket(&dataPacket,
	                           sendData,
	                           receivedData,
	                           NUM_BYTES + 1,
	                           &PORTC,
	                           PIN4_bm);

	/* Transmit and receive first data byte. */
	uint8_t status;
	do {
		status = SPI_MasterInterruptTransceivePacket(&spiMasterC, &dataPacket);
	} while (status != SPI_OK);

	/* Wait for transmission to complete. */
	while (dataPacket.complete == false) {

	}

	/* Check that correct data was received. Assume success at first. */
	success = true;
	for (uint8_t i = 0; i < NUM_BYTES; i++) {
		if (receivedData[i + 1] != (uint8_t)(sendData[i] + 1)) {
			success = false;
		}
	}
	while(true) {
		nop();
	}
}
Пример #10
0
int main(void)
{	
	char cmd;
	char param1;
	char param2;
	char param3;
	
	cmd = 0;
	param1 = 0;
	param2 = 0;
	param3 = 0;

	DDRB = 0x00;
	PORTB |= 0x01;
	
	// set for 16 MHz clock, and make sure the LED is off
	CPU_PRESCALE(0);
	LED_CONFIG;
	LED_ON;

	SPI_SlaveInit();	
	ACK_CONFIG;
	ACK_HIGH;
	
	TEST_CONFIG;
		
	usb_init();
	_delay_ms(1000);

	usb_serial_flush_input();
	int flashCounter = 0;
	char LEDstate = 1;

	while (1) {

idle:
		TEST_HIGH;
		ACK_HIGH;
		SPDR = 0xff;
		if(ATT_PIN)
		{
			timeoutCounter++;
			if(timeoutCounter >= 30000)//we've been disconnected
			{
				mode = 0x41;
				motorSetting1 = 0xff;
				motorSetting2 = 0xff;
				analogEnabled = 0;
				configMode = 0;
				timeoutCounter = 0;
				flashCounter++;
				if(flashCounter > 5)
				{
					if(LEDstate == 1)
					{
						LED_OFF;
						LEDstate = 0;
					}
					else
					{
						LED_ON;
						LEDstate = 1;	
					}
					flashCounter = 0;
					
				}
			}
			if(usb_serial_available())
			{
				LED_ON;
				int c = usb_serial_getchar();
				if (c >= 0) 
				{
					switch(parseIndex)
					{
					case 0:
						if(c == 0x5A)
							parseIndex++;
						else
							usb_serial_putchar('x');
						break;
					case 1:
						buttons1 &= c;//combine this with any pending presses
						buttons1postReportState = c;
						parseIndex++;
						break;
					case 2:
						buttons2 &= c;//combine this with any pending presses
						buttons2postReportState = c;
						parseIndex++;
						break;
					case 3:
						joyRX = c;
						parseIndex++;
						break;
					case 4:
						joyRY = c;
						parseIndex++;
						break;
					case 5:
						joyLX = c;
						parseIndex++;
						break;
					case 6:
						joyLY = c;
						parseIndex = 0;
						recievedUpdate = 1;
						break;
					}
				}
				else
				{
					parseIndex = 0;
					usb_serial_putchar('x');
				}
			}
			_delay_us(1);
			goto idle;
		}
		timeoutCounter = 0;
		
		if((buttons1 & 0x10) == 0) upPressure = 0xFF; else upPressure = 0x00;
		if((buttons1 & 0x20) == 0) rightPressure = 0xFF; else rightPressure = 0x00;
		if((buttons1 & 0x40) == 0) downPressure = 0xFF; else downPressure = 0x00;
		if((buttons1 & 0x80) == 0) leftPressure = 0xFF; else leftPressure = 0x00;
		if((buttons2 & 0x01) == 0) L2Pressure = 0xFF; else L2Pressure = 0x00;
		if((buttons2 & 0x02) == 0) R2Pressure = 0xFF; else R2Pressure = 0x00;
		if((buttons2 & 0x04) == 0) L1Pressure = 0xFF; else L1Pressure = 0x00;
		if((buttons2 & 0x08) == 0) R1Pressure = 0xFF; else R1Pressure = 0x00;
		if((buttons2 & 0x10) == 0) trianglePressure = 0xFF; else trianglePressure = 0x00;
		if((buttons2 & 0x20) == 0) circlePressure = 0xFF; else circlePressure = 0x00;
		if((buttons2 & 0x40) == 0) crossPressure = 0xFF; else crossPressure = 0x00;
		if((buttons2 & 0x80) == 0) squarePressure = 0xFF; else squarePressure = 0x00;

		cli();//disable usb interrupts
		LED_ON;
		TEST_LOW;
		cmd = SPI_SlaveReceive(0xff,1);		
		TEST_HIGH;
		
		if(cmd != 0x01)
			goto finish;
		
		if(configMode)
		{
			cmd = SPI_SlaveReceive(0xF3,1);		
			SPI_SlaveReceive(0x5A,1);
			switch(cmd)
			{
				case 0x40:
					param1 = SPI_SlaveReceive(0x00,1);
//					if(param1 == 0) //lots on param1 but the reponses are all the same?
					{
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x02,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x5A,0);
					}
					break;
				case 0x41://check response data
					if(analogEnabled==1)
					{
						//button pressures byte mask, appears after 0x44 is called
						SPI_SlaveReceive(0xFF,1);//use mask instead?
						SPI_SlaveReceive(0xFF,1);
						SPI_SlaveReceive(0x03,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x5A,0);
					}
					else
					{
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,0);
					}
					break;
				case 0x43://exit config mode
					param1 = SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,0);
					if(param1 == 0)
					{
						configMode = 0;
					}
					break;
				case 0x44://turn on analog mode
					param1 = SPI_SlaveReceive(0x00,1);
					param2 = SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,0);
					if(param1 == 1)
					{
						mode = 0x73;//no analog button pressures
						analogEnabled = 1;
					}
					else
					{
						mode = 0x41;	
						analogEnabled = 0;
					}
					break;
				case 0x45://query model
					param1 = SPI_SlaveReceive(0x03,1);
					param2 = SPI_SlaveReceive(0x02,1);
					if(analogEnabled==1)
						SPI_SlaveReceive(0x01,1);
					else
						SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x02,1);
					SPI_SlaveReceive(0x01,1);
					SPI_SlaveReceive(0x00,0);
					break;
				case 0x46:
					param1 = SPI_SlaveReceive(0x00,1);
					if(param1 == 0)
					{
						param2 = SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x01,1);
						SPI_SlaveReceive(0x02,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x0F,0);
					}
					if(param1 == 1)
					{
						param2 = SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x01,1);
						SPI_SlaveReceive(0x01,1);
						SPI_SlaveReceive(0x01,1);
						SPI_SlaveReceive(0x0F,0);
					}
					break;
				case 0x47:
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x02,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x01,1);
					SPI_SlaveReceive(0x00,0);
					break;
				case 0x4C:
					param1 = SPI_SlaveReceive(0x00,1);
					if(param1 == 0)
					{
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x04,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,0);
					}
					if(param1 == 1)
					{
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x07,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,0);
					}
					break;
				case 0x4D://setup motors
					motorSetting1 = SPI_SlaveReceive(motorSetting1,1);
					motorSetting2 = SPI_SlaveReceive(motorSetting2,1);
					SPI_SlaveReceive(0xFF,1);
					SPI_SlaveReceive(0xFF,1);
					SPI_SlaveReceive(0xFF,1);
					SPI_SlaveReceive(0xFF,0);
					break;
				case 0x4F://set pressure byte mask, extended pressures
					mode = 0x79;//analog button pressures
					param1 = SPI_SlaveReceive(0x00,1);
					param2 = SPI_SlaveReceive(0x00,1);
					param3 = SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x5A,0);
					break;
			}
		}
		else
		{
			TEST_LOW;
			cmd = SPI_SlaveReceive(mode,1);	
			
			SPI_SlaveReceive(0x5A,1);
			
			if(mode == 0x41)//digital only
			{
				param1 = SPI_SlaveReceive(buttons1,1);
				param2 = SPI_SlaveReceive(buttons2,0);
			}
			if(mode == 0x73)//digital buttons, analog joysticks
			{
				param1 = SPI_SlaveReceive(buttons1,1);
				if(param1 ==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(buttons2,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyRX,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyRY,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyLX,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyLY,0)==TIME_OUT_ERROR_CODE) goto finish;
			}
			if(mode == 0x79)//analog joysticks, analog buttons
			{
				param1 = SPI_SlaveReceive(buttons1,1);
				if(param1 ==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(buttons2,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyRX,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyRY,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyLX,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyLY,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(rightPressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(leftPressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(upPressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(downPressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(trianglePressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(circlePressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(crossPressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(squarePressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(L1Pressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(R1Pressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(L2Pressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(R2Pressure,0)==TIME_OUT_ERROR_CODE) goto finish;
			}
			//cmd should usually be 0x42 for polling
			if(cmd == 0x43)
			{
				if(param1 == 0x01)
					configMode = 1;
			}
		}
finish:
		TEST_HIGH;
		ACK_HIGH;
		if((buttons1 == 0xff)&&(buttons2 == 0xff))
		{
			LED_OFF;
		}

		//copy queued releases into button state
		buttons1 = buttons1postReportState;
		buttons2 = buttons2postReportState;
		sei();//renable usb interrupts
		
		if(recievedUpdate == 1)
		{
			//ack the update was sent to the console
			usb_serial_putchar('k');
			recievedUpdate = 0;
		}

		while(!ATT_PIN)
		{
			_delay_us(20); //conservative so we aren't still in ATT low at the top of the loop		
		}

		
		counter++;
	}
}