View previous topic :: View next topic |
Author |
Message |
gwolfsystems
Joined: 25 Mar 2015 Posts: 3
|
Problem with HID example |
Posted: Wed Mar 25, 2015 8:43 am |
|
|
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
|
|
Posted: Wed Mar 25, 2015 9:00 am |
|
|
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
|
|
Posted: Wed Mar 25, 2015 10:36 am |
|
|
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
|
|
Posted: Wed Mar 25, 2015 11:35 am |
|
|
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
|
|
Posted: Wed Mar 25, 2015 2:21 pm |
|
|
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
|
|
Posted: Wed Mar 25, 2015 3:27 pm |
|
|
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. |
|
|
|