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 support@ccsinfo.com

PCD: USB HID example and data transfer

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



Joined: 17 Sep 2009
Posts: 16

View user's profile Send private message

PCD: USB HID example and data transfer
PostPosted: Tue Dec 01, 2009 6:39 am     Reply with quote

I'm using one code that is mostly based on EX_USB_HID.c file.
The connection with PC - and my device is working fine, but i have one problem.

I need to receive over 1MB of different data types from PC.
The data has different length and type.

If i want to be able to receive all this data, what should i do ?
How to organize all this ?
epitalon
Guest







PostPosted: Tue Dec 01, 2009 1:16 pm     Reply with quote

Your question is not very clear ...

If, by "datatype", you refer to report configuration that you must set into the class specific (hid) descriptor,

don't take the pain to describe all your data, their size, their minimum and maximum values, and so on...
Just describe a generic report with :
report size : 8 bits
report count : the number of bytes you need to send at a time
usage minimum : 0
usage maximum : 0xFF
W_Schauberg



Joined: 17 Sep 2009
Posts: 16

View user's profile Send private message

PostPosted: Wed Dec 02, 2009 2:14 pm     Reply with quote

Thank you for your reply epitalon!
Anti aging ? :-)

Back to the question....


Code:
#define USB_CONFIG_HID_TX_SIZE   2
#define USB_CONFIG_HID_RX_SIZE   2

#include <pic24_usb.h>   //Microchip PIC24 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
....
      usb_init_cs();
      usb_task();
      usb_debug_task();
      if (usb_enumerated())
      {
         if (!send_timer)
         {
            send_timer=250;
            out_data[0]=read_adc();
            out_data[1]= 1; //BUTTON_PRESSED();
            if (usb_put_packet(1, out_data, 2, USB_DTS_TOGGLE));
              // printf("\r\n<-- Sending 2 bytes: 0x%X 0x%X", out_data[0], out_data[1]);
         }
         if (usb_kbhit(1))
         {
            usb_get_packet(1, in_data, 2);
            //printf("\r\n--> Received data: 0x%X 0x%X",in_data[0],in_data[1]);
             //if (in_data[0]) {LED_ON(LED2);} else {LED_OFF(LED2);}
             //if (in_data[1]) {LED_ON(LED3);} else {LED_OFF(LED3);}
         }
       //  send_timer--;
       //  delay_ms(1);
      }


I'm receiving bunch of objects from VCC application.
This is example of one of them:
Code:

struct initData {
int idUser;
int counter;
int counter2;
char words[4];
int maxValue;
}

What I need to do now is collect all this data and update it with the old data on my device ( rewrite it ). As far as the USB example goes, he is sending 2 bytes of data and that's it. How to receive this data from the PC objects ?


Last edited by W_Schauberg on Fri Dec 04, 2009 11:48 am; edited 1 time in total
epitalon
Guest







PostPosted: Thu Dec 03, 2009 11:26 am     Reply with quote

You will have to send any element of your data structure byte per byte.
That is, you have to extract every four bytes of an int, pack them into a buffer of char.

You will have then to send all these chars by packets whose length you choose.
On the PIC, you have to call get_packet() and do the reverse process : that is, gather all bytes in integers.

Alternatively, you could copy all the data structure in a char buffer of sufficient lenth, using memcpy(), and do the reverse on the other side.
But doing this, you are exposed to errors due to different layouts of data structures, and integers (Bigendian, little-endian issue) on both architectures (Intel and Microchip).

Jean-Marie Epitalon
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Dec 03, 2009 1:27 pm     Reply with quote

In my opinion, copying complete structures rather than invidual bytes would be matter of economic programming. It should be no problem, if the data structures are exactly defined and works with many applications that are communicating through binary protocols.

HID, that has been considered for the project, is working in packets of data ("reports") rather than streams (as e.g. HID does). To allow an easy assignment of data blocks, I would use a type field and a record number in each packet. The type field can be also used to code commands respectively data requests. Every application, that uses HID to transport larger data entities does something similar, e.g. the Microchip HID bootloeader.
W_Schauberg



Joined: 17 Sep 2009
Posts: 16

View user's profile Send private message

PostPosted: Wed Dec 09, 2009 3:32 pm     Reply with quote

Hello guys.

Can someone write some example code on this. Just basic stuff, on how to start. ? I look at existing samples but couldn't find anything that could fit to what i need.
W_Schauberg



Joined: 17 Sep 2009
Posts: 16

View user's profile Send private message

PostPosted: Thu Dec 10, 2009 6:26 am     Reply with quote

Is this the way to solve the problem....?

Code:
if (usb_kbhit(1))         
 {
       usb_get_packet(1, datain, 64);       //read new packet
       switch(cmd)
       {
                  case '1':
                          adc_value=fread_adc(channel,samples,sample_delay);
                           adcbyte0=make8(adc_value,0);
                           adcbyte1=make8(adc_value,1);
                          usb_put_packet(1, send_buffer, 3, USB_DTS_TOGGLE);
                           printf("\r\nADC data sent");
                           cmd=0;
                           break;
                   case '2':
                           usb_put_packet(1,logo,32,USB_DTS_TOGGLE);
                           printf("\r\n Send !");                           
                           break;
                           
               }
         }
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