CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with HID example

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
gwolfsystems



Joined: 25 Mar 2015
Posts: 3

View user's profile Send private message Send e-mail

Problem with HID example
PostPosted: Wed Mar 25, 2015 8:43 am     Reply with quote

Hi! I'm working with the usb library, I want to create an example using HID. I post my code here. The code has to receive a message from PC and hecho the message again to the PC. The problem is that the pic receives the code but, when it answer the PC don't receive any message. In the PC I make a program in Java using javaHIDApi http://code.google.com/p/javahidapi/ . I don´t know if the firmware has a problem or if the library of the PC doesn't work. Has anybody used another library? Can you help me? Thanks!!

Code:

#include <18F2550.h> // Definición de registros internos del PIC18F2550.
#fuses NOMCLR,XTPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL1,CPUDIV1,VREGEN,NOPBADEN
// CPUDIV1: El PLL postscaler decide la división en 2 de la frecuencia de salida del PLL de 96MHZ, si queremos 48MHZ, lo dejamos como está.
// VREGEN: habilita el regulador de 3.3 volts que usa el módulo USB.
// NOPBADEN: Deshabilitamos el módulo conversor ADC del puerto B.
#use fast_io(c)
#use delay(clock=48000000)

#DEFINE USB_HID_DEVICE TRUE // Vamos a utilizar el protocolo HID.

#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT // Definición del tamaño del buffer de salida.
#define USB_EP1_TX_SIZE 32

#define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT // Definición del tamaño del buffer de entrada.
#define USB_EP1_RX_SIZE 32

/*********************************************************************************************************/

// Definición de las librerías utilizadas.
#include <pic18_usb.h>    // Drivers's USB del PIC18F2550.
#include "usb_desc_hid.h" // Descriptores USB.
#include <usb.c> // Funciones del USB.

/*********************************************************************************************************/

int8 enviar[USB_CONFIG_HID_TX_SIZE];
int8 recepcion[USB_CONFIG_HID_RX_SIZE];
int8 aux;

void ledParpadea()
{
   output_high(PIN_B7);
   delay_ms(500);
   output_low(PIN_B7);
   delay_ms(500);
}

void ledParpadea2()
{
   output_high(PIN_B6);
   delay_ms(500);
   output_low(PIN_B6);
   delay_ms(500);
}

void main()
{
   set_tris_c(0xFF);
   usb_init_cs();    // Iniciamos el puerto USB y salimos.
   Enviar[0] = 20;
   for (aux = 1; aux < USB_CONFIG_HID_TX_SIZE; ++aux)
   {
      Enviar[aux] = aux;
   }
   LedParpadea();
   while(1)
   {
      usb_task(); // Configuramos el puerto USB.
      if (usb_enumerated()) // Si el puerto es enumerado y configurado por el host..
      {
         if (usb_kbhit(1)){
            usb_get_packet(1, recepcion, USB_CONFIG_HID_RX_SIZE); 
           
            int aux2 = 0;
            for (aux2 = 0; aux2 < 32; ++aux2){
               enviar[aux] = recepcion[aux];
            } 
           
            if (usb_puts(1,enviar,32,100)) {
               ledParpadea();
            }
            else {
               ledParpadea2();
            }
         }
      }
   }

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Mar 25, 2015 9:00 am     Reply with quote

Start with, you are not defining USB_CONFIG_HID_TX_SIZE (or RX_SIZE), so the default size will be selected. 2bytes. Your arrays will then be two bytes long. You write 32bytes to one....
gwolfsystems



Joined: 25 Mar 2015
Posts: 3

View user's profile Send private message Send e-mail

PostPosted: Wed Mar 25, 2015 10:36 am     Reply with quote

Thanks Ttelmah!!! I didn't see that. Now the program is working except for one problem. When you send a packet to the PC, the program receives 31bytes and lost the first one. Is it normal?
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Mar 25, 2015 11:35 am     Reply with quote

I'd try leaving the endpoint sizes at 64. The default 2byte HID uses 64bytes for the transfer size, and the code may have a problem with the smaller size, and this will be forcing the transfers to be in two parts.

However I'd be suspicious of something like the difference between array indexes. Both Java, and C use zero indexes. Is it possible you are starting at index 1?.
gwolfsystems



Joined: 25 Mar 2015
Posts: 3

View user's profile Send private message Send e-mail

PostPosted: Wed Mar 25, 2015 2:21 pm     Reply with quote

I looked about that problem. It seems that when you send a packet, you have to specify a Report ID in the first byte. Then if you have to send 32 bytes to the PIC, you have to create an Array with 33 bytes and left the first byte in 0 (I think that pic doesn't have multiple report ID)
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Mar 25, 2015 3:27 pm     Reply with quote

That is correct, the report ID is always the first byte, but I thought the puts USB function automatically encapsulates the message into the transmission packet, so should add the report ID.
I must admit I don't use this, always assembling packets myself.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group