/* 
 * Project name:
     HID_Read_Write (USB HID Read & Write Test)
 * Copyright:
     (c) Mikroelektronika, 2012.
 * Revision History:
     20120810:
       - initial release (FJ);
 * Description:
     This example establishes connection with the HID terminal that is active
     on the PC. Upon connection establishment, the HID Device Name will appear
     in the respective window. The character that user sends to PIC from the HID
     terminal will be re-sent back to user.
 * Test configuration:
     MCU:             P32MX795F512L
                      http://ww1.microchip.com/downloads/en/DeviceDoc/61156F.pdf
     Dev.Board:       EasyPIC Fusion v7 - ac:USB
                      http://www.mikroe.com/easypic-fusion/
     Oscillator:      XT-PLL, 80.000MHz
     Ext. Modules:    None.
     SW:              mikroC PRO for PIC32
                      http://www.mikroe.com/mikroc/pic32/
 * NOTES:  
     - None.
 */

char cnt;
char readbuff[64];
char writebuff[64];

void USB1Interrupt() iv IVT_USB_1 ilevel 7 ics ICS_SRS{
  USB_Interrupt_Proc();
}

void main(void){
  int Count = 0;
  TRISB.RB0 = 0; PORTB.RB0 = 0;
  AD1PCFG = 0xFFFF;                        // Configure AN pins as digital I/O        

  USBIE_bit = 0;
  IPC11bits.USBIP = 7;
  EnableInterrupts();

  HID_Enable(&readbuff,&writebuff);

  while(1){
    while(!HID_Read())
      ;

    for(cnt=0;cnt<64;cnt++)
      writebuff[cnt]=readbuff[cnt];

     LATB.RB0 = readbuff[63] ;
     writebuff[0] = ++Count;
     
    while(!HID_Write(&writebuff,64))
      ;
  }
}