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

Cant send more than 4Bytes with Code from ex_usb_hid via USB

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







Cant send more than 4Bytes with Code from ex_usb_hid via USB
PostPosted: Wed Dec 28, 2005 2:37 pm     Reply with quote

Hi,
I'm trying to send 6 Bytes of Data with my PIC2550 over USB. I took the ex_usb_hid example and tuned it a Bit, to send 6Bytes. But only the first time 4Bytes arrive the Host PC the other 2 Bytes are lost. The other Times I get a timeout from my Software, or an device is not working error.

That is my Code:
Code:


#include <18F2550.h>

#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

#DEFINE USB_HID_DEVICE  TRUE

#define USB_EP1_TX_ENABLE  USB_ENABLE_INTERRUPT   //turn on EP1 for IN bulk/interrupt transfers
#define USB_EP1_TX_SIZE    8  //allocate 8 bytes in the hardware for transmission

#define USB_EP1_RX_ENABLE  USB_ENABLE_INTERRUPT   //turn on EP1 for OUT bulk/interrupt transfers
#define USB_EP1_RX_SIZE    8  //allocate 8 bytes in the hardware for reception

#define USB_CON_SENSE_PIN PIN_C2

#include <pic18_usb.h>         //Microchip 18Fxx5x hardware layer for usb.c
#include <usb_desc_hid.h>      //USB Configuration and Device descriptors for this UBS device
#include <usb.c>            //handles usb setup tokens and get descriptor reports

void main(void)
{
   int8 out_data[20];
   int8 in_data[2];
   int16 send_timer=0;

   usb_init();

   while(TRUE)
   {
      usb_task();
      if (usb_enumerated())
      {
         if(send_timer == 0)
         {
                     send_timer=1000;
            out_data[0]=0xAB;
            out_data[1]=0xCD;
            out_data[2]=0xEF;
            out_data[3]=0xAB;
            out_data[4]=0xCD;
            out_data[5]=0xEF;
            usb_put_packet(1, out_data, 6, USB_DTS_TOGGLE);
         }
         if (usb_kbhit(1))
         {
            usb_get_packet(1, in_data, 1);
         }
         send_timer=send_timer-1;
         delay_ms(1);
      }
   }
}

Darren Rook



Joined: 06 Sep 2003
Posts: 287
Location: Milwaukee, WI

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

PostPosted: Wed Dec 28, 2005 3:47 pm     Reply with quote

Did you change the HID descriptor? Also check the max endpoint packet size (in the endpoint descriptors)
_________________
I came, I saw, I compiled.
starkeeper
Guest







PostPosted: Thu Dec 29, 2005 2:50 am     Reply with quote

Darren Rook wrote:
Did you change the HID descriptor? Also check the max endpoint packet size (in the endpoint descriptors)


Well I'm using the standard HID-descriptor from the example:
Code:

#IFNDEF __USB_DESCRIPTORS__
#DEFINE __USB_DESCRIPTORS__

#include <usb.h>

   const char USB_CLASS_SPECIFIC_DESC[] = {
      6, 0, 255,    // Usage Page = Vendor Defined
      9, 1,            // Usage = IO device
      0xa1, 1,       // Collection = Application
      0x19, 1,        // Usage minimum
      0x29, 8,        // Usage maximum

      0x15, 0x80,        // Logical minimum (-128)
      0x25, 0x7F,        // Logical maximum (127)

      0x75, 8,        // Report size = 8 (bits)
      0x95, 2,        // Report count = 16 bits (2 bytes)
      0x81, 2,        // Input (Data, Var, Abs)
      0x19, 1,        // Usage minimum
      0x29, 8,        // Usage maximum
      0x75, 8,        // Report size = 8 (bits)
      0x95, 2,        // Report count = 16 bits (2 bytes)
      0x91, 2,        // Output (Data, Var, Abs)
      0xc0            // End Collection
   };

   const int16 USB_CLASS_SPECIFIC_DESC_LOOKUP[USB_NUM_CONFIGURATIONS][1] =
   {
   //config 1
      //interface 0
         0
   };

   const int16 USB_CLASS_SPECIFIC_DESC_LOOKUP_SIZE[USB_NUM_CONFIGURATIONS][1] =
   {
   //config 1
      //interface 0
         32
   };


   #DEFINE USB_TOTAL_CONFIG_LEN      41  //config+interface+class+endpoint+endpoint (2 endpoints)

   const char USB_CONFIG_DESC[] = {
   //IN ORDER TO COMPLY WITH WINDOWS HOSTS, THE ORDER OF THIS ARRAY MUST BE:
      //    config(s)
      //    interface(s)
      //    class(es)
      //    endpoint(s)

   //config_descriptor for config index 1
         USB_DESC_CONFIG_LEN, //length of descriptor size          ==1
         USB_DESC_CONFIG_TYPE, //constant CONFIGURATION (CONFIGURATION 0x02)     ==2
         USB_TOTAL_CONFIG_LEN,0, //size of all data returned for this config      ==3,4
         1, //number of interfaces this device supports       ==5
         0x01, //identifier for this configuration.  (IF we had more than one configurations)      ==6
         0x00, //index of string descriptor for this configuration      ==7
         0xC0, //bit 6=1 if self powered, bit 5=1 if supports remote wakeup (we don't), bits 0-4 unused and bit7=1         ==8
         0x32, //maximum bus power required (maximum milliamperes/2)  (0x32 = 100mA)

   //interface descriptor 1
         USB_DESC_INTERFACE_LEN, //length of descriptor      =10
         USB_DESC_INTERFACE_TYPE, //constant INTERFACE (INTERFACE 0x04)       =11
         0x00, //number defining this interface (IF we had more than one interface)    ==12
         0x00, //alternate setting     ==13
         2, //number of endpoins, except 0 (pic167xx has 3, but we dont have to use all).       ==14
         0x03, //class code, 03 = HID     ==15
         0x00, //subclass code //boot     ==16
         0x00, //protocol code      ==17
         0x00, //index of string descriptor for interface      ==18

   //class descriptor 1  (HID)
         USB_DESC_CLASS_LEN, //length of descriptor    ==19
         USB_DESC_CLASS_TYPE, //dscriptor type (0x21 == HID)      ==20
         0x00,0x01, //hid class release number (1.0) (try 1.10)      ==21,22
         0x00, //localized country code (0 = none)       ==23
         0x01, //number of hid class descrptors that follow (1)      ==24
         0x22, //report descriptor type (0x22 == HID)                ==25
         USB_CLASS_SPECIFIC_DESC_LOOKUP_SIZE[0][0], 0x00, //length of report descriptor            ==26,27

   //endpoint descriptor
         USB_DESC_ENDPOINT_LEN, //length of descriptor                   ==28
         USB_DESC_ENDPOINT_TYPE, //constant ENDPOINT (ENDPOINT 0x05)          ==29
         0x81, //endpoint number and direction (0x81 = EP1 IN)       ==30
         0x03, //transfer type supported (0x03 is interrupt)         ==31
         USB_EP1_TX_SIZE,0x00, //maximum packet size supported                  ==32,33
      #if USB_USE_FULL_SPEED
         1,  //polling interval, in ms.  (cant be smaller than 10)      ==34
      #else
         10,  //polling interval, in ms.  (cant be smaller than 10)      ==34
      #endif

   //endpoint descriptor
         USB_DESC_ENDPOINT_LEN, //length of descriptor                   ==35
         USB_DESC_ENDPOINT_TYPE, //constant ENDPOINT (ENDPOINT 0x05)          ==36
         0x01, //endpoint number and direction (0x01 = EP1 OUT)      ==37
         0x03, //transfer type supported (0x03 is interrupt)         ==38
         USB_EP1_RX_SIZE,0x00, //maximum packet size supported                  ==39,40
      #if USB_USE_FULL_SPEED
         1
      #else
         10 //polling interval, in ms.  (cant be smaller than 10)    ==41
      #endif
   };


   #define USB_NUM_HID_INTERFACES   1

   #define USB_MAX_NUM_INTERFACES   1

   const char USB_NUM_INTERFACES[USB_NUM_CONFIGURATIONS]={1};

   const int16 USB_CLASS_DESCRIPTORS[USB_NUM_CONFIGURATIONS][1][1]=
   {
   //config 1
      //interface 0
         //class 1
         18
   };

   #if (sizeof(USB_CONFIG_DESC) != USB_TOTAL_CONFIG_LEN)
      #error USB_TOTAL_CONFIG_LEN not defined correctly
   #endif



   const char USB_DEVICE_DESC[USB_DESC_DEVICE_LEN] ={
      //starts of with device configuration. only one possible
         USB_DESC_DEVICE_LEN, //the length of this report   ==1
         0x01, //the constant DEVICE (DEVICE 0x01)  ==2
         0x10,0x01, //usb version in bcd (pic167xx is 1.1) ==3,4
         0x00, //class code ==5
         0x00, //subclass code ==6
         0x00, //protocol code ==7
         USB_MAX_EP0_PACKET_LENGTH, //max packet size for endpoint 0. (SLOW SPEED SPECIFIES 8) ==8
         0x61,0x04, //vendor id (0x04D8 is Microchip, or is it 0x0461 ??)
         0x20,0x00, //product id   ==11,12  //don't use ffff says usb-by-example guy.  oops
         0x00,0x01, //device release number  ==13,14
         0x01, //index of string description of manufacturer. therefore we point to string_1 array (see below)  ==15
         0x02, //index of string descriptor of the product  ==16
         0x00, //index of string descriptor of serial number  ==17
         USB_NUM_CONFIGURATIONS  //number of possible configurations  ==18
   };


char USB_STRING_DESC_OFFSET[]={0,4,12};

char const USB_STRING_DESC[]={
   //string 0
         4, //length of string index
         USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
         0x09,0x04,   //Microsoft Defined for US-English
   //string 1
         8, //length of string index
         USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
         'C',0,
         'C',0,
         'S',0,
   //string 2
         26, //length of string index
         USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
         'C',0,
         'C',0,
         'S',0,
         ' ',0,
         'H',0,
         'I',0,
         'D',0,
         ' ',0,
         'D',0,
         'e',0,
         'm',0,
         'o',0
};

#ENDIF


I thought that the packet size would be defined by theese canstants:
#define USB_EP1_TX_SIZE 8
#define USB_EP1_RX_SIZE 8

I have set them to 8 and I only want to send 6 Bytes.

What is wrong with that HID-Descriptor?
Darren Rook



Joined: 06 Sep 2003
Posts: 287
Location: Milwaukee, WI

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

PostPosted: Thu Dec 29, 2005 8:17 am     Reply with quote

What's wrong with the HID descriptor is that it's configured to send 2 byte report counts.
_________________
I came, I saw, I compiled.
Guest








PostPosted: Thu Dec 29, 2005 2:27 pm     Reply with quote

Darren Rook wrote:
What's wrong with the HID descriptor is that it's configured to send 2 byte report counts.


Code:

const char USB_CLASS_SPECIFIC_DESC[] = {
      6, 0, 255,    // Usage Page = Vendor Defined
      9, 1,            // Usage = IO device
      0xa1, 1,       // Collection = Application
      0x19, 1,        // Usage minimum
      0x29, 8,        // Usage maximum

      0x15, 0x80,        // Logical minimum (-128)
      0x25, 0x7F,        // Logical maximum (127)

      0x75, 8,        // Report size = 8 (bits)
      0x95, 6,        // Report count = 16 bits (2 bytes)   
      0x81, 2,        // Input (Data, Var, Abs)         
      0x19, 1,        // Usage minimum
      0x29, 8,        // Usage maximum
      0x75, 8,        // Report size = 8 (bits)
      0x95, 6,        // Report count = 16 bits (2 bytes)   
      0x91, 2,        // Output (Data, Var, Abs)         
      0xc0            // End Collection
   };


I have set the report count to 6, now I can send 6Bytes. But the only way to see this is thru my USB-Sniffer SnoopyPro.
Any Software I tested says that my device has timeouts and needs to be reset. What else could be wrong?
Darren Rook



Joined: 06 Sep 2003
Posts: 287
Location: Milwaukee, WI

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

PostPosted: Thu Dec 29, 2005 8:19 pm     Reply with quote

Interesting.

You may want to change the output portion of the descriptor to specify 1 byte report count.

Did you change the endpoint descriptor's max endpoint size, or are they still using the constants USB_EP1_TX_SIZE and USB_EP1_RX_SIZE?

Also I believe on the PC side you have to ask for a 7 byte packet because the PC will append a byte to the beginning of the message which is the report ID. You are not using report IDs so your report IDs will always be 0.

This software, is this custom software you wrote?
_________________
I came, I saw, I compiled.
Guest








PostPosted: Fri Dec 30, 2005 3:32 am     Reply with quote

Darren Rook wrote:
Interesting.

You may want to change the output portion of the descriptor to specify 1 byte report count.

Did you change the endpoint descriptor's max endpoint size, or are they still using the constants USB_EP1_TX_SIZE and USB_EP1_RX_SIZE?

Also I believe on the PC side you have to ask for a 7 byte packet because the PC will append a byte to the beginning of the message which is the report ID. You are not using report IDs so your report IDs will always be 0.

This software, is this custom software you wrote?


Wow, that's it!
I had to set the packetsize on the PC side to 7. I don't want to know what the hell the USB-Developers thought by this.

Another problem is that I loose packets. I don't know if this is normal or even volitional. My Device sends answers to my PC, sometimes the PC is not fast enough to catch all packets.
For Instance the PIC sends all 5 seconds a packet. But If the PC requests a packet after 10 seconds, one packet is lost.
How can I prevent this phenomenon? Is this a PIC or a PC side problem?
Darren Rook



Joined: 06 Sep 2003
Posts: 287
Location: Milwaukee, WI

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

PostPosted: Sun Jan 01, 2006 10:31 pm     Reply with quote

Anonymous wrote:

Wow, that's it!
I had to set the packetsize on the PC side to 7. I don't want to know what the hell the USB-Developers thought by this.


It's actually not as strange as you think, you're just new to HID. If you look at ex_usb_kbmouse2.c it has an example of using multiple HID report IDs to send multiple packets of varying types.

Quote:
Another problem is that I loose packets. I don't know if this is normal or even volitional. My Device sends answers to my PC, sometimes the PC is not fast enough to catch all packets.
For Instance the PIC sends all 5 seconds a packet. But If the PC requests a packet after 10 seconds, one packet is lost.
How can I prevent this phenomenon? Is this a PIC or a PC side problem?


I have heard of this problem, but never heard of a solution (people too lazy to post their experiences after they've found a fix, I guess). I'm thinking it's because people are using ReadFile() and ReadFile() doesn't buffer, it only holds the latest packet.
_________________
I came, I saw, I compiled.
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