/*********************************************************************
* Function: void APP_DeviceCustomHIDTasks(void);
*
* Overview: Keeps the Custom HID demo running.
*
* PreCondition: The demo should have been initialized and started via
*   the APP_DeviceCustomHIDInitialize() and APP_DeviceCustomHIDStart() demos
*   respectively.
*
* Input: None
*
* Output: None
*
********************************************************************/
void APP_DeviceCustomHIDTasks()
{   
    //Check if we have received an OUT data packet from the host
    if(HIDRxHandleBusy(USBOutHandle) == false)
    {   
        //We just received a packet of data from the USB host.
        //Check the first uint8_t of the packet to see what command the host
        //application software wants us to fulfill.
        switch(ReceivedDataBuffer[0])				//Look at the data the host sent, to see what kind of application specific command it sent.
        {
            case COMMAND_TOGGLE_LED:  //Toggle LEDs command
                LED_Toggle(LED_USB_DEVICE_HID_CUSTOM);
                break;
            case COMMAND_GET_BUTTON_STATUS:  //Get push button state
                //Check to make sure the endpoint/buffer is free before we modify the contents
                if(!HIDTxHandleBusy(USBInHandle))
                {
                    ToSendDataBuffer[0] = 0x81;				//Echo back to the host PC the command we are fulfilling in the first uint8_t.  In this case, the Get Pushbutton State command.
                    if(BUTTON_IsPressed(BUTTON_USB_DEVICE_HID_CUSTOM) == false)	//pushbutton not pressed, pull up resistor on circuit board is pulling the PORT pin high
                    {
                            ToSendDataBuffer[1] = 0x01;
                    }
                    else									//sw3 must be == 0, pushbutton is pressed and overpowering the pull up resistor
                    {
                            ToSendDataBuffer[1] = 0x00;
                    }
                    //Prepare the USB module to send the data packet to the host
                    USBInHandle = HIDTxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*)&ToSendDataBuffer[0],64);
                }
                break;

            case COMMAND_READ_POTENTIOMETER:	//Read POT command.  Uses ADC to measure an analog voltage on one of the ANxx I/O pins, and returns the result to the host
                {
                    uint16_t pot;

                    //Check to make sure the endpoint/buffer is free before we modify the contents
                    if(!HIDTxHandleBusy(USBInHandle))
                    {
                        //Use ADC to read the I/O pin voltage.  See the relevant HardwareProfile - xxxxx.h file for the I/O pin that it will measure.
                        //Some demo boards, like the PIC18F87J50 FS USB Plug-In Module board, do not have a potentiometer (when used stand alone).
                        //This function call will still measure the analog voltage on the I/O pin however.  To make the demo more interesting, it
                        //is suggested that an external adjustable analog voltage should be applied to this pin.

                        pot = ADC_Read10bit(ADC_CHANNEL_POTENTIOMETER);

                        ToSendDataBuffer[0] = 0x37;  	//Echo back to the host the command we are fulfilling in the first uint8_t.  In this case, the Read POT (analog voltage) command.
                        ToSendDataBuffer[1] = (uint8_t)pot; //LSB
                        ToSendDataBuffer[2] = pot >> 8;     //MSB


                        //Prepare the USB module to send the data packet to the host
                        USBInHandle = HIDTxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*)&ToSendDataBuffer[0],64);
                    }
                }
                break;
        }
        //Re-arm the OUT endpoint, so we can receive the next OUT data packet 
        //that the host may try to send us.
        USBOutHandle = HIDRxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*)&ReceivedDataBuffer, 64);
    }
/*********************************************************************
* Function: void APP_DeviceCustomHIDTasks(void);
*
* Overview: Keeps the Custom HID demo running.
*
* PreCondition: The demo should have been initialized and started via
*   the APP_DeviceCustomHIDInitialize() and APP_DeviceCustomHIDStart() demos
*   respectively.
*
* Input: None
*
* Output: None
*
********************************************************************/
void APP_DeviceCustomHIDTasks()
{   
    //Check if we have received an OUT data packet from the host
    if(HIDRxHandleBusy(USBOutHandle) == false)
    {   
        //We just received a packet of data from the USB host.
        //Check the first uint8_t of the packet to see what command the host
        //application software wants us to fulfill.
        if (ReceivedDataBuffer[0] == COMMAND_TOGGLE_LED)				//Look at the data the host sent, to see what kind of application specific command it sent.
        {
            //Toggle LEDs command
            LED_Toggle(LED_GREEN);
        }
        else
        {
            if (!check_command())
            {
                //                strcpypgm2ram((char *) ToSendDataBuffer, (farchar *) "null");
                *ToSendDataBuffer = 0xFF;
            }
            if (!HIDTxHandleBusy(USBInHandle))
            {
                USBInHandle = HIDTxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*) & ToSendDataBuffer[0], 64);
            }
        }
        //Re-arm the OUT endpoint, so we can receive the next OUT data packet 
        //that the host may try to send us.
        USBOutHandle = HIDRxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*)&ReceivedDataBuffer, 64);
    }
}
Exemplo n.º 3
0
/*
 * This routine will poll for a received Input report, process it
 * and send an Output report to the host.
 * Both directions use interrupt transfers.
 * The ownership of the USB buffers will change according
 * to the required operation.
 */
void send_receive (void)
{
	static int usb_state = 'r';
	static USB_HANDLE last_transmit = 0;
	static USB_HANDLE last_receive = 0;

	switch (usb_state) {
	case 'r':
	   	if (! HIDRxHandleBusy (last_receive)) {
			// The CPU owns the endpoint. Start receiving data.
			last_receive = HIDRxPacket (HID_EP,
				(unsigned char*) &hid_report_out,
                                HID_INT_OUT_EP_SIZE);
			usb_state = 'p';
		}
		break;
	case 'p':
		if (! HIDRxHandleBusy (last_receive)) {
			// The CPU owns the endpoint.
			if (last_receive->CNT > 0) {
				// Data was received. Copy it to the output buffer for sending.
				process_data();

				// Ready to transmit the received data back to the host.
				usb_state = 't';
			} else {
				// No data was received. Return to checking for new received data.
				usb_state = 'r';
			}
		}
		break;
	case 't':
		if (! HIDTxHandleBusy (last_transmit)) {
			// The CPU owns the endpoint. Start sending data.
			last_transmit = HIDTxPacket (HID_EP,
				(unsigned char*) &hid_report_in,
				HID_INPUT_REPORT_BYTES);

			// Return to checking for new received data.
			usb_state = 'r';
		}
		break;
	default:
	        // Cannot happen.
		break;
	}
}
Exemplo n.º 4
0
void Keyboard(void) {
  static char* c = g_tx_queue;

  if (SwitchIsPressed()) {
    g_tx_queue[0] = 0;
    if (g_config_mode) {
      ++g_lgtm_strings_idx;
      if (g_lgtm_strings_idx >= NUM_LGTM_STRINGS)
        g_lgtm_strings_idx = 0;
      EEADR = 0;
      EEDATA = g_lgtm_strings_idx;
      EECON1bits.EEPGD = 0;
      EECON1bits.CFGS = 0;
      EECON1bits.WREN = 1;
      EECON2 = 0x55;
      EECON2 = 0xAA;
      EECON1bits.WR = 1;
      strcatpgm2ram(g_tx_queue, CFG_MODE_STR);
    }
    strcatpgm2ram(g_tx_queue, LGTM_STRINGS[g_lgtm_strings_idx]);
    c = g_tx_queue;
  }

  // Check if the IN endpoint is not busy, and if it isn't check if we want to
  // send keystroke data to the host.
  if (!HIDTxHandleBusy(g_usb_handle_in)) {
    memset(hid_report_in, 0, sizeof(hid_report_in));
    if (*c) {
      HIDKey key;
      key = AsciiToHid(*c);
      ++c;
      hid_report_in[0] = key.modifier;
      hid_report_in[2] = key.code;
    }
    // Send the 8 byte packet over USB to the host.
    g_usb_handle_in = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x08);
  }

  // Check if any data was sent from the PC to the keyboard device.  Report
  // descriptor allows
  // host to send 1 byte of data.  Bits 0-4 are LED states, bits 5-7 are unused
  // pad bits.
  // The host can potentially send this OUT report data through the HID OUT
  // endpoint (EP1 OUT),
  // or, alternatively, the host may try to send LED state information by
  // sending a
  // SET_REPORT control transfer on EP0.  See the USBHIDCBSetReportHandler()
  // function.
  if (!HIDRxHandleBusy(g_usb_handle_out)) {
    // Do something useful with the data now.  Data is in the OutBuffer[0].
    // Num Lock LED state is in Bit0.
    // if (hid_report_out[0] & 0x01)  // Make LED1 and LED2 match Num Lock
    // state.
    g_usb_handle_out = HIDRxPacket(HID_EP, (BYTE*)&hid_report_out, 1);
  }

  return;
}
Exemplo n.º 5
0
void usb_tasks() {
    // User Application USB tasks
    if (USBDeviceState < CONFIGURED_STATE || USBSuspendControl == 1)
        return;
    
    // Check if we have received an OUT data packet from the host, and nothing is left in buffer
    if (!HIDRxHandleBusy(USBOutHandleCfg) && !HIDTxHandleBusy(USBInHandleCfg)) {
        // We just received a packet of data from the USB host.
        // Check the first byte of the packet to see what command the host
        // application software wants us to fulfill.
        uint8_t cmd = usbOutBuffer[0];
        if (cmd == CFG_CMD_READ) {
            // format response
            cfg_read_report_t* report = (cfg_read_report_t*)usbInBuffer;
            report->command_id = CFG_CMD_READ;
            report->fw_major = FW_MAJOR;
            report->fw_minor = FW_MINOR;
            report->hw_revision = HW_REVISION;
            report->device_type = MUNIA_DEVICETYPE;

            uint8_t* r = &report->devid1;
            TBLPTR = 0x3FFFFF;
            asm("tblrd*-");
            *r++ = TABLAT;
            asm("tblrd*");
            *r = TABLAT;
            TBLPTRU = 0;

            memcpy(&report->config, &config, sizeof(config_t));
            memset(usbInBuffer + sizeof(cfg_read_report_t), 0, sizeof(usbInBuffer) - sizeof(cfg_read_report_t));       
            
            USBInHandleCfg = HIDTxPacket(HID_EP_CFG, usbInBuffer, CFG_CMD_REPORT_SIZE);
        }
        else if (cmd == CFG_CMD_WRITE) {
            // extract config
            cfg_write_report_t* report = (cfg_write_report_t*)usbOutBuffer;
            memcpy(&config, &report->config, sizeof(config_t));
            save_config();
            apply_config();
            
            // response
            usbInBuffer[0] = CFG_CMD_WRITE;
            usbInBuffer[1] = 1; // signifies ok
            memset(usbInBuffer + 2, 0, sizeof(usbInBuffer) - 2);
            
            USBInHandleCfg = HIDTxPacket(HID_EP_CFG, usbInBuffer, CFG_CMD_REPORT_SIZE);
        }
        
        else if (cmd == CFG_CMD_ENTER_BL) {
            // dbgs("jumping to bootloader\n");
            asm("goto 0x001C");
        }
        
        // re-arm
        USBOutHandleCfg = HIDRxPacket(HID_EP_CFG, usbOutBuffer, sizeof (usbOutBuffer));
    }
}
Exemplo n.º 6
0
void Keyboard(void)
{
	unsigned char i = 0;

	if(!HIDTxHandleBusy(lastINTransmission))
	{
		hid_report_in[0] = 0;
		hid_report_in[1] = 0;
		hid_report_in[2] = 0;
		hid_report_in[3] = 0;
		hid_report_in[4] = 0;
		hid_report_in[5] = 0;
		hid_report_in[6] = 0;
		hid_report_in[7] = 0;

		if(dynamicFlags > 0) {
			hid_report_in[0] = dynamicFlags;
		} else {
			hid_report_in[0] = 0;
		}

		for(i = 0; i < reportIndex; i++) {
			hid_report_in[i + 2] = report[i];
		}

/*
		if(!PORTAbits.RA0)
			hid_report_in[2] = 0x28;	//[ENTER]
		else
			hid_report_in[2] = 0;
*/
		//Send the 8 byte packet over USB to the host.
		lastINTransmission = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x08);
	}
	
	if(!HIDRxHandleBusy(lastOUTTransmission))
	{
/*
		if(hid_report_out[0] & 0x02)	//[CAPS]lock時に点灯
			LATBbits.LATB4 = 1;
		else
			LATBbits.LATB4 = 0;

		if(hid_report_out[0] & 0x01)	//[NUM]lock時に点灯
			LATBbits.LATB5 = 1;
		else
			LATBbits.LATB5 = 0;
*/
		lastOUTTransmission = HIDRxPacket(HID_EP,(BYTE*)&hid_report_out,1);
	} 

	return;
}//end keyboard()
/********************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user
 *                  routines. It is a mixture of both USB and
 *                  non-USB tasks.
 *
 * Note:            None
 *******************************************************************/
void ProcessIO(void)
{
    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1)) {
        return;
    }

    // Indicate USB is connected.
    xSystemState.bUsbConnected = 1;
    

    // If data was received from the host...
    if(!HIDRxHandleBusy(USBOutHandle)) {
        // Make sure the transmitter is not busy.
        if (!HIDTxHandleBusy(USBInHandle)) {
            // Process the data.
            unsigned char ucTransmitDataLength = ucProcessCommandPacket(ReceivedDataBuffer, ToSendDataBuffer);

            // Send the data if there is any.
            if (ucTransmitDataLength > 0) {
                // Fill the remaining bytes with 0xff if it's not full.
                unsigned char i;
                for (i = ucTransmitDataLength; i < 64; i++) {
                    ToSendDataBuffer[i] = 0xff;
                }

                // Transmit the data.
                USBInHandle = HIDTxPacket(HID_EP, ToSendDataBuffer, 64);
            }

            // Re-arm the OUT endpoint to receive next packet if there is no data to send.
            else {
                USBOutHandle = HIDRxPacket(HID_EP, ReceivedDataBuffer, 64);
            }
        }
    }


    MSDTasks();
}
Exemplo n.º 8
0
// Process USB commands
void processUsbCommands(void)
{   
    // Check if we are in the configured state; otherwise just return
    if((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1))
    {
	    // We are not configured
	    return;
	}

	// Check if data was received from the host.
    if (!HIDRxHandleBusy(USBOutHandle)) {
        // Command mode
        switch (ReceivedDataBuffer[0]) {
            case 0x01: // System Commands
                switch (ReceivedDataBuffer[1]) {
                    case 0x01: // System Commands
                        // Copy any waiting debug text to the send data buffer
                        ToSendDataBuffer[0] = 0xFF;
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }
                        break;


                    default: // Unknown command received
                        break;
                }
                break;

            case 0x02: // Feeder Commands
                switch (ReceivedDataBuffer[1]) {
                    case 0x02: // Feeder Status
                        if (atmegaFeederRunning){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                            ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }
                        break;

                    case 0x03: // Go to feeder
                        StartI2C();
                        WriteI2C(0x28); // sends address to the device
                        IdleI2C();
                        WriteI2C(ReceivedDataBuffer[2]); // sends a control byte to the device
                        IdleI2C();
                        StopI2C();
                        break;

                    case 0x04: // Reset Feeder Z
                        StartI2C();
                        WriteI2C(0x28); // sends address to the device
                        IdleI2C();
                        WriteI2C(80); // sends a control byte to the device
                        IdleI2C();
                        StopI2C();
                        break;

                    case 0x05: // Full feeder reset
                        StartI2C();
                        WriteI2C(0x28); // sends address to the device
                        IdleI2C();
                        WriteI2C(70); // sends a control byte to the device
                        IdleI2C();
                        StopI2C();
                        break;
                     case 0x06: // Reset ATMEGA IC
                        atmegaResetPin = 0;
                        Delay1KTCYx(10);
                        atmegaResetPin = 1;
                        break;


                    default: // Unknown command received
                        break;
                        
                }
            break;
            case 0x03: // Vacuum and Vibration Commands
                switch (ReceivedDataBuffer[1]) {
                    case 0x01: // Vacuum 1 set
                        if (ReceivedDataBuffer[2] == 0x01){
                            setVac1on;
                        }
                        else{
                            setVac1off;
                        }
                        break;

                    case 0x02: // Vacuum 2 set
                        if (ReceivedDataBuffer[2] == 0x01){
                            setVac2on;
                        }
                        else{
                            setVac2off;
                        }
                        break;

                    case 0x03: // Vibration Motor set
                        if (ReceivedDataBuffer[2] == 0x01){
                           setVibrationon;
                            StartI2C();
                            WriteI2C(0x16); // sends address to the device
                            IdleI2C();
                            WriteI2C(0x01); // sends a control byte to the device
                            IdleI2C();
                            StopI2C();

                        }
                        else{
                           setVibrationoff;
                           StartI2C();
                            WriteI2C(0x16); // sends address to the device
                            IdleI2C();
                            WriteI2C(0x02); // sends a control byte to the device
                            IdleI2C();
                            StopI2C();
                        }
                        break;
                    case 0x04: // Vacuum 1 status
                        if (vac1running == 1){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                           ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }
                        break;
                    case 0x05: // Vacuum 2 status
                        if (vac2running == 1){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                           ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }
                        break;
                    case 0x06: // Vibration Motor status
                        if (vibrationrunning == 1){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                           ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }
                        break;
                    case 0x07: // Vibration Motor status
                        vibrationmotor_duty_cycle = ReceivedDataBuffer[2];
                        StartI2C();
                            WriteI2C(0x16); // sends address to the device
                            IdleI2C();
                            WriteI2C(0x03); // sends a control byte to the device
                            IdleI2C();
                            WriteI2C(vibrationmotor_duty_cycle); // sends a control byte to the device
                            IdleI2C();
                            StopI2C();
                        break;

                    default: // Unknown command received
                        break;
                        
                }
                break;
            case 0x04: // LED Commands
                switch (ReceivedDataBuffer[1]) {
                    case 0x01: // LED Base Camera on/off
                        if (ReceivedDataBuffer[2] == 0x01){
                        OpenPWM1(0xFF);
                        led1_duty_cycle = led1_duty_cycle * 4;
                        SetDCPWM1(led1_duty_cycle);
                        //    outBaseLED = 1;
                        led1running = 1;
                        }else{
                           ClosePWM1();
                           // outBaseLED = 0;
                           led1running = 0;
                        }
                        break;

                    case 0x02: // LED Base Camera PWM set
                        led1_duty_cycle = ReceivedDataBuffer[2];
                        Write_b_eep(baseLED_EEPROM_address, led1_duty_cycle);
                        led1_duty_cycle = led1_duty_cycle * 4;
                        SetDCPWM1(led1_duty_cycle);
                        break;

                    case 0x03: // LED Head Camera on/off
                        if (ReceivedDataBuffer[2] == 0x01){
                        OpenPWM2(0xFF);
                        led2_duty_cycle = led2_duty_cycle * 4;
                        SetDCPWM2(led2_duty_cycle);
                        //    outBaseLED = 1;
                        led2running = 1;
                        }else{
                           ClosePWM2();
                           // outBaseLED = 0;
                           led2running = 0;
                        }
                        break;

                    case 0x04: // LED Head Camera PWM set
                         led2_duty_cycle = ReceivedDataBuffer[2];
                         Write_b_eep(headLED_EEPROM_address, led2_duty_cycle);
                         led2_duty_cycle = led2_duty_cycle * 4;
                        SetDCPWM2(led2_duty_cycle);

                        break;
                    case 0x05: // LED Base Status
                        if (led1running == 1){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                           ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }

                        break;
                    case 0x06: // LED Head Status
                        if (led2running == 1){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                           ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }


                        break;

                    default: // Unknown command received
                        break;
                }
                break;



            default: // Unknown command received
                break;
        }
        
        // Re-arm the OUT endpoint for the next packet
        USBOutHandle = HIDRxPacket(HID_EP, (BYTE*) & ReceivedDataBuffer, 64);
    }
}
Exemplo n.º 9
0
/********************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user
 *                  routines. It is a mixture of both USB and
 *                  non-USB tasks.
 *
 * Note:            None
 *******************************************************************/
void ProcessIO(void)
{
    if(DemoIntroState == 0xFF)
    {
        BL_CheckLoaderEnabled();
    }

    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;

    // Soft Start the APP_VDD
    if(AppPowerReady() == FALSE) return;

    DemoIntroduction();

    if(!HIDRxHandleBusy(USBOutHandle))        //Check if data was received from the host.
    {

        switch(ReceivedDataBuffer[0])       //Look at the data the host sent, to see what kind of application specific command it sent.
        {
        case 0x80:  //mTouch callibration command
            mTouchCalibrate();
            break;

        case 0x20:
        {
            WORD potVoltage;

            if(!HIDTxHandleBusy(USBInHandle))
            {
                /* Select ADC channel */
                ADCON0bits.CHS = 4;

                /* Make sure A/D interrupt is not set */
                PIR1bits.ADIF = 0;

                /* Begin A/D conversion */
                ADCON0bits.GO=1;
                //Wait for A/D convert complete
                while(!PIR1bits.ADIF);

                /* Get the value from the A/D */
                potVoltage = ADRES;

                ToSendDataBuffer[0] = 0x20;
                ToSendDataBuffer[1] = (BYTE)(potVoltage);
                ToSendDataBuffer[2] = (BYTE)(potVoltage>>8);

                USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
            }
        }
        break;

        case 0x30:
        {
            WORD w;

            if(!HIDTxHandleBusy(USBInHandle))
            {
                w = mTouchReadButton(0);

                ToSendDataBuffer[0] = 0x30;
                ToSendDataBuffer[1] = (BYTE)w;
                ToSendDataBuffer[2] = (BYTE)(w>>8);

                USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
            }
        }
        break;

        case 0x31:
        {
            WORD w;

            if(!HIDTxHandleBusy(USBInHandle))
            {
                w = mTouchReadButton(1);

                ToSendDataBuffer[0] = 0x31;
                ToSendDataBuffer[1] = (BYTE)w;
                ToSendDataBuffer[2] = (BYTE)(w>>8);

                USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
            }
        }
Exemplo n.º 10
0
/*********************************************************************
* Function: void APP_DeviceCustomHIDTasks(void);
*
* Overview: Keeps the Custom HID demo running.
*
* PreCondition: The demo should have been initialized and started via
*   the APP_DeviceCustomHIDInitialize() and APP_DeviceCustomHIDStart() demos
*   respectively.
*
* Input: None
*
* Output: None
*
********************************************************************/
void APP_DeviceCustomHIDTasks()
{   
    //Check if we have received an OUT data packet from the host
    if(HIDRxHandleBusy(USBOutHandle) == false)
    {   
        //We just received a packet of data from the USB host.
        //Check the first uint8_t of the packet to see what command the host
        //application software wants us to fulfill.
        switch(ReceivedDataBuffer[0])				//Look at the data the host sent, to see what kind of application specific command it sent.
        {
            case READ_SFR:

                ReadSfr();
                break;

            case WRITE_SFR:

                WriteSfr();
                break;

            case ADC_CFG:

                AdcCfg();
                break;

            case ADC_VALUE:

                AdcValue();
                break;

            case ANALOGCMP_CFG:

                AnalogCmpCfg();
                break;

            case SPI_CFG:

                SpiCfg();
                break;

            case SPI_TRANSFERENCE:

                SpiTransference();
                break;

            case SFR_CHANGE_BIT_VALUE:

                SfrChangeBitValue();
                break;

            case I2C_CFG:

                I2cCfg();
                break;

            case I2C_TRANSFERENCE:

                I2cTransference();
                break;

            case CCP_CFG:

                CcpCfg();
                break;

            case PWM_FPWM:

                PwmFpwm();
                break;

            case PWM_DC:

                PwmDc();
                break;

            case SFR_READ_BIT_VALUE:

                SfrReadBitValue();
                break;

            case EUSART_TX:

                EusartTx();
                break;

            case EUSART_RX:

                EusartRx();
                break;

            case TEST:

                Test();
                break;

            case UDF_CALL:

                UDF();
                break;

            case UDF_PROGRAM:

                UDF_Program();
                break;
        }
        //Re-arm the OUT endpoint, so we can receive the next OUT data packet 
        //that the host may try to send us.
        USBOutHandle = HIDRxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*)&ReceivedDataBuffer, 64);
    }
}
Exemplo n.º 11
0
/**
 * ProcessIO:
 **/
static void
ProcessIO(void)
{
	uint16_t address;
	uint16_t erase_length;
	uint8_t length;
	uint8_t checksum;
	uint8_t cmd;
	uint8_t rc = CH_ERROR_NONE;

	/* reset the LED state */
	led_counter--;
	if (led_counter == 0) {
		CHugSetLEDsInternal(led_color);
		led_color *= 2;
		if (led_color > CH_STATUS_LED_BLUE)
			led_color = CH_STATUS_LED_GREEN;
		led_counter = BOOTLOADER_FLASH_INTERVAL;
	}

	/* User Application USB tasks */
	if ((USBDeviceState < CONFIGURED_STATE) ||
	    (USBSuspendControl == 1))
		return;

	/* no data was received */
	if(HIDRxHandleBusy(USBOutHandle)) {
		if (idle_counter++ == 0xff &&
		    idle_command != 0x00)
			CHugDeviceIdle();
		return;
	}

	/* got data, reset idle counter */
	idle_counter = 0;

	/* clear for debugging */
	memset (TxBuffer, 0xff, sizeof (TxBuffer));

	cmd = RxBuffer[CH_BUFFER_INPUT_CMD];
	switch(cmd) {
	case CH_CMD_GET_HARDWARE_VERSION:
		TxBuffer[CH_BUFFER_OUTPUT_DATA] = PORTB & 0x0f;
		break;
	case CH_CMD_RESET:
		/* only reset when USB stack is not busy */
		idle_command = CH_CMD_RESET;
		break;
	case CH_CMD_GET_FIRMWARE_VERSION:
		memcpy (&TxBuffer[CH_BUFFER_OUTPUT_DATA],
			&FirmwareVersion,
			2 * 3);
		break;
	case CH_CMD_ERASE_FLASH:
		/* are we lost or stolen */
		if (flash_success == 0xff) {
			rc = CH_ERROR_DEVICE_DEACTIVATED;
			break;
		}
		memcpy (&address,
			(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0],
			2);
		/* allow to erase any address but not the bootloader */
		if (address < CH_EEPROM_ADDR_RUNCODE) {
			rc = CH_ERROR_INVALID_ADDRESS;
			break;
		}
		memcpy (&erase_length,
			(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+2],
			2);
		EraseFlash(address, address + erase_length);
		break;
	case CH_CMD_READ_FLASH:
		/* are we lost or stolen */
		if (flash_success == 0xff) {
			rc = CH_ERROR_DEVICE_DEACTIVATED;
			break;
		}
		/* allow to read any address */
		memcpy (&address,
			(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0],
			2);
		length = RxBuffer[CH_BUFFER_INPUT_DATA+2];
		if (length > 60) {
			rc = CH_ERROR_INVALID_LENGTH;
			break;
		}
		ReadFlash(address, length,
			  (unsigned char *) &TxBuffer[CH_BUFFER_OUTPUT_DATA+1]);
		checksum = CHugCalculateChecksum (&TxBuffer[CH_BUFFER_OUTPUT_DATA+1],
						  length);
		TxBuffer[CH_BUFFER_OUTPUT_DATA+0] = checksum;
		break;
	case CH_CMD_WRITE_FLASH:
		/* are we lost or stolen */
		if (flash_success == 0xff) {
			rc = CH_ERROR_DEVICE_DEACTIVATED;
			break;
		}
		/* write to flash that's not the bootloader */
		memcpy (&address,
			(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0],
			2);
		if (address < CH_EEPROM_ADDR_RUNCODE) {
			rc = CH_ERROR_INVALID_ADDRESS;
			break;
		}
		length = RxBuffer[CH_BUFFER_INPUT_DATA+2];
		if (length > CH_FLASH_TRANSFER_BLOCK_SIZE) {
			rc = CH_ERROR_INVALID_LENGTH;
			break;
		}
		checksum = CHugCalculateChecksum(&RxBuffer[CH_BUFFER_INPUT_DATA+4],
						 length);
		if (checksum != RxBuffer[CH_BUFFER_INPUT_DATA+3]) {
			rc = CH_ERROR_INVALID_CHECKSUM;
			break;
		}

		/* copy low 32 bytes into flash buffer, and only write
		 * in 64 byte chunks as this is a limitation of the
		 * hardware */
		if ((address & CH_FLASH_TRANSFER_BLOCK_SIZE) == 0) {
			memset (FlashBuffer,
				0xff,
				CH_FLASH_WRITE_BLOCK_SIZE);
			memcpy (FlashBuffer,
				(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+4],
				length);
		} else {
			memcpy (FlashBuffer + CH_FLASH_TRANSFER_BLOCK_SIZE,
				(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+4],
				length);
			WriteBytesFlash(address - CH_FLASH_TRANSFER_BLOCK_SIZE,
					CH_FLASH_WRITE_BLOCK_SIZE,
					(unsigned char *) FlashBuffer);
		}
		break;
	case CH_CMD_BOOT_FLASH:
		/* are we lost or stolen */
		if (flash_success == 0xff) {
			rc = CH_ERROR_DEVICE_DEACTIVATED;
			break;
		}
		/* only boot when USB stack is not busy */
		idle_command = CH_CMD_BOOT_FLASH;
		break;
	case CH_CMD_SET_FLASH_SUCCESS:
		if (RxBuffer[CH_BUFFER_INPUT_DATA] != 0x00) {
			rc = CH_ERROR_INVALID_VALUE;
			break;
		}
		flash_success = RxBuffer[CH_BUFFER_INPUT_DATA];
		EraseFlash(CH_EEPROM_ADDR_FLASH_SUCCESS,
			   CH_EEPROM_ADDR_FLASH_SUCCESS + 1);
		WriteBytesFlash(CH_EEPROM_ADDR_FLASH_SUCCESS, 1,
				(unsigned char *) &RxBuffer[CH_BUFFER_INPUT_DATA]);
		break;
	case CH_CMD_SELF_TEST:
		rc = CHugSelfTest();
		break;
	default:
		rc = CH_ERROR_UNKNOWN_CMD_FOR_BOOTLOADER;
		break;
	}

	/* always send return code */
	if(!HIDTxHandleBusy(USBInHandle)) {
		TxBuffer[CH_BUFFER_OUTPUT_RETVAL] = rc;
		TxBuffer[CH_BUFFER_OUTPUT_CMD] = cmd;
		USBInHandle = HIDTxPacket(HID_EP,
					  (BYTE*)&TxBuffer[0],
					  CH_USB_HID_EP_SIZE);
	}

	/* re-arm the OUT endpoint for the next packet */
	USBOutHandle = HIDRxPacket(HID_EP,
				   (BYTE*)&RxBuffer,
				   CH_USB_HID_EP_SIZE);
}
Exemplo n.º 12
0
/********************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user
 *                  routines. It is a mixture of both USB and
 *                  non-USB tasks.
 *
 * Note:            None
 *******************************************************************/
void ProcessIO(void)
{
    
    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;

    //Check if we have received an OUT data packet from the host
    if(!HIDRxHandleBusy(USBOutHandle))
    {
        
        //We just received a packet of data from the USB host.
        //Check the first byte of the packet to see what command the host
        //application software wants us to fulfill.
        switch (ReceivedDataBuffer[0]) {
            case 0x01: // System Commands
                switch (ReceivedDataBuffer[1]) {
                    case 0x01: // System Commands
                        // Copy any waiting debug text to the send data buffer
                        ToSendDataBuffer[0] = 0xFF;
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }
                        break;


                    default: // Unknown command received
                        break;
                }
                break;

            case 0x02: // Feeder Commands
                switch (ReceivedDataBuffer[1]) {
                    case 0x02: // Feeder Status
                        if (FeederStatus() == 1){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                            ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }
                        break;

                    case 0x03: // Go to feeder
                        if ((ReceivedDataBuffer[2] >= 0) && (ReceivedDataBuffer[2] <= 16)){
                            moveToPosition(ReceivedDataBuffer[2]);
                        }
                        break;

                    case 0x04: // Picker Up
                        PickerUp();
                        break;

                    case 0x05: // Zero Feeder
                        ZeroFeeder();
                        break;
                     case 0x06: // Picker Down
                         PickerDown();
                        break;
                     case 0x07: // Set Picker Port Output
                        pickerBusval = ((ReceivedDataBuffer[3] << 8) | ReceivedDataBuffer[2]);
                        pickerBus = pickerBusval;
                        break;
                     case 0x08: // Go to feeder
                        if ((ReceivedDataBuffer[2] > 0) && (ReceivedDataBuffer[2] <= 16)){
                            moveToPositionWithoutPick(ReceivedDataBuffer[2]);
                        }
                        break;

                    default: // Unknown command received
                        break;

                }
            break;
            case 0x03: // Vacuum and Vibration Commands
                
                switch (ReceivedDataBuffer[1]) {
                    case 0x01: // Vacuum 1 set
                        if (ReceivedDataBuffer[2] == 0x01){
                            setVac1on;
                        }
                        else{
                            setVac1off;
                        }
                        break;

                    case 0x02: // Vacuum 2 set
                        if (ReceivedDataBuffer[2] == 0x01){
                            setVac2on;
                        }
                        else{
                            setVac2off;
                        }
                        break;

                    case 0x03: // Vibration Motor set
                        if (ReceivedDataBuffer[2] == 0x01){
                           SetDCOC1PWM(vibrationmotor_duty_cycle);
                           vibrationrunning = 1;
                        }
                        else{
                           SetDCOC1PWM(0);
                           vibrationrunning = 0;
                        }
                        break;
                    case 0x04: // Vacuum 1 status
                        if (vac1running == 1){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                           ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }
                        break;
                    case 0x05: // Vacuum 2 status
                        if (vac2running == 1){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                           ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }
                        break;
                    case 0x06: // Vibration Motor status
                        if (vibrationrunning == 1){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                           ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }
                        break;
                    case 0x07: // Vibration Motor status
                        vibrationmotor_duty_cycle = ReceivedDataBuffer[2] * 4;
                        if (vibrationrunning == 1){
                            SetDCOC1PWM(vibrationmotor_duty_cycle);
                        }
                        break;

                    default: // Unknown command received
                        break;

                }
                break;
            case 0x04: // LED Commands
                switch (ReceivedDataBuffer[1]) {
                    case 0x01: // LED Base Camera on/off
                        if (ReceivedDataBuffer[2] == 0x01){
                            SetDCOC2PWM(led1_duty_cycle);
                            led1running = 1;
                        }else{
                           SetDCOC2PWM(0);
                           led1running = 0;
                        }
                        break;

                    case 0x02: // LED Base Camera PWM set
                        led1_duty_cycle = ReceivedDataBuffer[2] * 4;
                        if (led1running == 1){
                            SetDCOC2PWM(led1_duty_cycle);
                        }
                        break;

                    case 0x03: // LED Head Camera on/off
                        if (ReceivedDataBuffer[2] == 0x01){
                            SetDCOC3PWM(led2_duty_cycle);
                            led2running = 1;
                        }else{
                           SetDCOC3PWM(0);
                           led2running = 0;
                        }
                        break;

                    case 0x04: // LED Head Camera PWM set
                        led2_duty_cycle = ReceivedDataBuffer[2] * 4;
                        if (led2running == 1){
                            SetDCOC3PWM(led2_duty_cycle);
                        }
                        break;
                    case 0x05: // LED Base Status
                        if (led1running == 1){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                           ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }

                        break;
                    case 0x06: // LED Head Status
                        if (led2running == 1){
                            ToSendDataBuffer[0] = 0x01;
                        }
                        else{
                           ToSendDataBuffer[0] = 0x00;
                        }
                        // Transmit the response to the host
                        if (!HIDTxHandleBusy(USBInHandle)) {
                            USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64);
                        }


                        break;

                    default: // Unknown command received
                        break;
                }
                break;



            default: // Unknown command received
                break;
        }
        //Re-arm the OUT endpoint, so we can receive the next OUT data packet
        //that the host may try to send us.
        USBOutHandle = HIDRxPacket(HID_EP, (BYTE*)&ReceivedDataBuffer, 64);
    }


}//end ProcessIO
Exemplo n.º 13
0
void USBUpdate()
{
	int a,b;
    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
    
    if(!HIDRxHandleBusy(USBOutHandle))				//Check if data was received from the host.
    {   
        switch(ReceivedDataBuffer[0])				//Look at the data the host sent, to see what kind of application specific command it sent.
        {
			case 0x01:
				a=EPPoll(*(int16*)&ReceivedDataBuffer[1]);
				if(!USBBusy())
                {
					ToSendDataBuffer[0]=0x03;
					if(a!=0)
						ToSendDataBuffer[1]=0;
					else
						ToSendDataBuffer[1]=255;
					USBWrite();
                }
				break;
			case 0x02:
				EPBufferSize=ReceivedDataBuffer[3];
				for(a=0;a<EPBufferSize;a++)Set8(a,ReceivedDataBuffer[a+4]);
				EPSend(*(int16*)&ReceivedDataBuffer[1]);
				if(!USBBusy())
                {
					ToSendDataBuffer[0]=0x02;
                    USBWrite();
                }
				break;
			case 0x06:
				ToSendDataBuffer[1]=KismetExecuteEvent(((int16*)&ReceivedDataBuffer[1])[0],ReceivedDataBuffer[3]);
				if(!USBBusy())
                {
					ToSendDataBuffer[0]=0x06;
                    USBWrite();
                }
				break;
			case 0x07:
				OperationEnabled=0;
				if(!USBBusy())
                {
					ToSendDataBuffer[0]=0x07;
                    USBWrite();
                }
				break;
			case 0x08:
				OperationEnabled=0xff;
				if(!USBBusy())
                {
					ToSendDataBuffer[0]=0x08;
                    USBWrite();
                }
				break;
			case 0x03://EEPROM Write
				MemoryBeginWrite(((int16*)&ReceivedDataBuffer[1])[0]);
				MemoryWrite(ReceivedDataBuffer[3]);
				MemoryEndWrite();
				if(!USBBusy())
                {
					ToSendDataBuffer[0]=0x03;
                    USBWrite();
                }
				break;
			case 0x04://EEPROM WRITE PAGE
				MemoryBeginWrite(((int16*)&ReceivedDataBuffer[1])[0]);
				for(a=0;a<32;a++)
					MemoryWrite(ReceivedDataBuffer[3+a]);
				MemoryEndWrite();
				if(!USBBusy())
                {
					ToSendDataBuffer[0]=0x04;
                    USBWrite();
                }
				break;
			case 0x05://EEPROM READ
				if(!USBBusy())
                {
					MemoryBeginRead(((int16*)&ReceivedDataBuffer[1])[0]);
					ToSendDataBuffer[0]=0x05;
					ToSendDataBuffer[1]=MemoryReadInt8();
					MemoryEndRead();
                    USBWrite();
                }
				break;
			case 0x40://READ TIME
				if(!USBBusy())
                {
					ToSendDataBuffer[0]=0x40;
					ToSendDataBuffer[1]=RTCSecond;
					ToSendDataBuffer[2]=RTCMinute;
					ToSendDataBuffer[3]=RTCHour;
					INT16(ToSendDataBuffer[4])=RTCDay;
					USBWrite();
                }
				break;
			case 0x41://WRITE TIME
				RTCSecond	=ReceivedDataBuffer[1];
				RTCMinute	=ReceivedDataBuffer[2];
				RTCHour		=ReceivedDataBuffer[3];
				RTCDay		=INT16(ReceivedDataBuffer[4]);
				if(!USBBusy())
                {
					ToSendDataBuffer[0]=0x41;
					USBWrite();
                }
				break;
			case 0x50://SET TIMER
				SetTimer(ReceivedDataBuffer[1],ReceivedDataBuffer[2],INT16(ReceivedDataBuffer[3]));
				if(!USBBusy())
                {
					ToSendDataBuffer[0]=0x50;
					USBWrite();
                }
				break;
        }

        if(!HIDTxHandleBusy(USBInHandle))
        {
            USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
        }
		ReceivedDataBuffer[0]=0;
        //Re-arm the OUT endpoint for the next packet
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64);
    }
}
Exemplo n.º 14
0
// Process input and output
void ProcessIO(void)
{   
	//LED0 = ON;
    // If we are not in the configured state just return
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1))
    {
	    // We are not configured, set LED1 to off to show status
	    //LED1 = OFF;
	    return;
	}
	
	// We are configured, show state in LED1
	//LED1 = ON;
	
	// Check to see if the success indicator is on and update the delay counter
	if (successIndicatorFlag == TRUE)
	{
		//LED2 = ON;
		successIndicatorCounter++;
		
		if (successIndicatorCounter == 20000)
		{
			//LED2 = OFF;
			successIndicatorCounter = 0;
			successIndicatorFlag = FALSE;
		}
	}
    
    // Check to see if the failure indicator is on and update the delay counter
    if (failureIndicatorFlag == TRUE)
	{
		//LED3 = ON;
		failureIndicatorCounter++;
		
		if (failureIndicatorCounter == 80000)
		{
			//LED3 = OFF;
			failureIndicatorCounter = 0;
			failureIndicatorFlag = FALSE;
		}
	}
	
	// Note: For all tests we expect to receive a 64 byte packet containing
	// the command in byte[0] and then the numbers 0-62 in bytes 1-63.

	unsigned char bufferPointer;
    unsigned char expectedData;
    unsigned char dataReceivedOk;
    unsigned char dataSentOk;

	// Check if data was received from the host.
    if(!HIDRxHandleBusy(USBOutHandle))
    {   
	    // Test to see if we are in bulk send/receieve mode, or if we are waiting for a command
	    if (bulkSendFlag == TRUE || bulkReceiveFlag == TRUE)
	    {
		    // We are either bulk sending or receieving
		    
		    // If we are bulk sending, check that we are not busy and send the next packet
		    if (bulkSendFlag == TRUE && !HIDTxHandleBusy(USBInHandle))
		    {
				// Send the next packet
				expectedData = bulkSendPacketCounter;
	            
	            for (bufferPointer = 0; bufferPointer < 64; bufferPointer++)
	            {
		            	ToSendDataBuffer[bufferPointer] = expectedData;
		        }
		        
		        // Transmit the response to the host
                USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
                
                // Add this following delay in if you want to simulate a slow transfer
                // from a busy PIC when testing bulk sends:
                
                // for (long int counter = 0; counter < 1000; counter++) __delay_us(100); 
				
				bulkSendPacketCounter++; // Next packet
				
				// Are we done yet?
				if (bulkSendPacketCounter == bulkSendExpectedPackets)
				{
					// All done, indicate success and go back to command mode
					bulkSendFlag = FALSE;
					successIndicatorFlag = TRUE;
				}	
			}
			
			// If we are bulk receiving get the next packet
			if (bulkReceiveFlag == TRUE)
			{
				// The received data buffer is already filled by the USB stack
				// we just have to confirm the data integrity
				
				expectedData = bulkReceivePacketCounter;
				
				for (bufferPointer = 0; bufferPointer < 64; bufferPointer++)
				{
					// If the data isn't what we expected, turn on the failure light
					if (ReceivedDataBuffer[bufferPointer] != expectedData)
						failureIndicatorFlag = TRUE;
				}
				
				bulkReceivePacketCounter++;
				
				// Are we done yet?
				if (bulkReceivePacketCounter == bulkReceiveExpectedPackets)
				{
					// All done, indicate success and go back to command mode
					bulkReceiveFlag = FALSE;
					successIndicatorFlag = TRUE;
				}
			}
		}
		else
		{
			// Command mode    
	        switch(ReceivedDataBuffer[0])
			{
				case 'M':
					dataReceivedOk = TRUE;
					ToSendDataBuffer[0] = highByte(frequency);
					ToSendDataBuffer[1] = lowByte(frequency);
					
					if (!HIDTxHandleBusy(USBInHandle)) {
						USBInHandle = HIDTxPacket(HID_EP, (BYTE*)&ToSendDataBuffer[0], 64);
					}
					break;

	            case 0x80:  // Test 1 - Single packet write to device
	            	// Test the receieved data
	            	expectedData = 0;
	            	dataReceivedOk = TRUE;
	            	for (bufferPointer = 1; bufferPointer < 64; bufferPointer++)
	            	{
		            	if (ReceivedDataBuffer[bufferPointer] != expectedData)
		            		dataReceivedOk = FALSE;
		            	
		            	expectedData++;
		            }
		            
		            // Display the test result
		            if (dataReceivedOk == TRUE) successIndicatorFlag = TRUE;
		            	else failureIndicatorFlag = TRUE;
	            	break;
	            	
	            case 0x81:	// Test 2 - Single packet read/write
	            	// Test the receieved data
	            	expectedData = 0;
	            	dataReceivedOk = TRUE;
	            	for (bufferPointer = 1; bufferPointer < 64; bufferPointer++)
	            	{
		            	if (ReceivedDataBuffer[bufferPointer] != expectedData)
		            		dataReceivedOk = FALSE;
		            	
		            	expectedData++;
		            }
		            
		            // If we got the data correctly, send the response packet
		            if (dataReceivedOk == TRUE)
		            {
			            expectedData = 0;
			            dataSentOk = TRUE;
			            
			            for (bufferPointer = 0; bufferPointer < 64; bufferPointer++)
			            {
				            ToSendDataBuffer[bufferPointer] = expectedData;
				            expectedData++;
				        }
				        
				        // Transmit the response to the host
		                if(!HIDTxHandleBusy(USBInHandle))
						{
							USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
						}
						
						// Show our success
						successIndicatorFlag = TRUE;
			        }
			        else failureIndicatorFlag = TRUE;
	            	break;
	            	
	            case 0x82:	// Test 3 - Single packet write, 128 packets read
	                // Test the receieved data
	            	expectedData = 0;
	            	dataReceivedOk = TRUE;
	            	for (bufferPointer = 1; bufferPointer < 64; bufferPointer++)
	            	{
		            	if (ReceivedDataBuffer[bufferPointer] != expectedData)
		            		dataReceivedOk = FALSE;
		            	
		            	expectedData++;
		            }
		            
		            // If the receive was ok, go into bulk sending mode
		            if (dataReceivedOk == TRUE)
		            {
			            // Go into bulk sending mode...
			            bulkSendExpectedPackets = 128;
			            bulkSendFlag = TRUE;
			            bulkSendPacketCounter = 0;
			        }
		            else failureIndicatorFlag = TRUE;
	            	break;
	            	
	            case 0x83:	// Test 4 - 128 packets write, single packet read
	            	// Test the receieved data
	            	expectedData = 0;
	            	dataReceivedOk = TRUE;
	            	for (bufferPointer = 1; bufferPointer < 64; bufferPointer++)
	            	{
		            	if (ReceivedDataBuffer[bufferPointer] != expectedData)
		            		dataReceivedOk = FALSE;
		            	
		            	expectedData++;
		            }
		            
		            // If the receive was ok, go into bulk receiving mode
		            if (dataReceivedOk == TRUE)
		            {
			            // Go into bulk sending mode...
			            bulkReceiveExpectedPackets = 127;
			            bulkReceiveFlag = TRUE;
			            bulkReceivePacketCounter = 0;
			        }
		            else failureIndicatorFlag = TRUE;
	            	break;
	            	
	            case 0x84:	// Test 5 - Single Packet write, timeout on read
	            	// Here we receive a command which expects a reply, but we
	            	// deliberately don't send one to test the timeout mechanisms
	            	// in the communication class
	            	
	            	// Test the receieved data
	            	expectedData = 0;
	            	dataReceivedOk = TRUE;
	            	for (bufferPointer = 1; bufferPointer < 64; bufferPointer++)
	            	{
		            	if (ReceivedDataBuffer[bufferPointer] != expectedData)
		            		dataReceivedOk = FALSE;
		            	
		            	expectedData++;
		            }
		            
		            // Display the test result
		            if (dataReceivedOk == TRUE) successIndicatorFlag = TRUE;
		            	else failureIndicatorFlag = TRUE;
		            	
		            // Now we quit without replying
	            	break;

	            default:	// Unknown command received
	           		break;
			}
		}
		
		// Only rearm the OUT endpoint if we are not bulk sending
		if (bulkSendFlag == FALSE)
		{      
	        // Re-arm the OUT endpoint for the next packet
	        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64);
  		}      
    }  
}   
Exemplo n.º 15
0
void APP_tick() {
    if(HIDRxHandleBusy(USBOutHandle) == false) {
        switch(ReceivedDataBuffer[0]) {
            /*case 0x80:  //Toggle LEDs command
                //LED_Toggle(LED_USB_DEVICE_HID_CUSTOM);
                break;*/

            case 0x54:
                PWM_R_setPWM1(ReceivedDataBuffer[1]); // front
                PWM_R_setPWM2(ReceivedDataBuffer[2]); // back

                // Send Left & right to slave 18F2550 by SPI
                Left = ReceivedDataBuffer[3];
                Right = ReceivedDataBuffer[4];
                LeftRight[0] = (Left >=  0 && Left <= 255) ? Left : 256;
                LeftRight[1] = (Right >=  0 && Right <= 255) ? Right : 256;
                SPI_R_putsSPI(LeftRight);

                break;
                
            /*case 0x81:  //Get push button state
                //Check to make sure the endpoint/buffer is free before we modify the contents
                if(!HIDTxHandleBusy(USBInHandle)) {
                    ToSendDataBuffer[0] = 0x81;				//Echo back to the host PC the command we are fulfilling in the first uint8_t.  In this case, the Get Pushbutton State command.
                    if(BUTTON_IsPressed(BUTTON_USB_DEVICE_HID_CUSTOM) == false) {
                        //pushbutton not pressed, pull up resistor on circuit board is pulling the PORT pin high
                        ToSendDataBuffer[1] = 0x01;
                    } else {
                        //sw3 must be == 0, pushbutton is pressed and overpowering the pull up resistor
                        ToSendDataBuffer[1] = 0x00;
                    }
                    //Prepare the USB module to send the data packet to the host
                    USBInHandle = HIDTxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*)&ToSendDataBuffer[0],64);
                }
                break;

            case 0x37:
                {
                    uint16_t pot;

                    //Check to make sure the endpoint/buffer is free before we modify the contents
                    if(!HIDTxHandleBusy(USBInHandle)) {
                        //Use ADC to read the I/O pin voltage.  See the relevant HardwareProfile - xxxxx.h file for the I/O pin that it will measure.
                        //Some demo boards, like the PIC18F87J50 FS USB Plug-In Module board, do not have a potentiometer (when used stand alone).
                        //This function call will still measure the analog voltage on the I/O pin however.  To make the demo more interesting, it
                        //is suggested that an external adjustable analog voltage should be applied to this pin.

                        pot = ADC_Read10bit(ADC_CHANNEL_POTENTIOMETER);

                        ToSendDataBuffer[0] = 0x37;  	//Echo back to the host the command we are fulfilling in the first uint8_t.  In this case, the Read POT (analog voltage) command.
                        ToSendDataBuffer[1] = (uint8_t)pot; //LSB
                        ToSendDataBuffer[2] = pot >> 8;     //MSB

                        //Prepare the USB module to send the data packet to the host
                        USBInHandle = HIDTxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*)&ToSendDataBuffer[0],64);
                    }
                }
                break;*/
        }
        //Re-arm the OUT endpoint, so we can receive the next OUT data packet 
        //that the host may try to send us.
        USBOutHandle = HIDRxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*)&ReceivedDataBuffer, 64);
    }
}