Simplified Microchip PIC USB demos (C18/XC8 MLA)

Re: Simplified Microchip PIC USB demos (C18/XC8 MLA)

Postby vloki » Thu Mar 31, 2016 7:31 am

susi wrote:I know that in HID class and interrupt mode, in each transaction, only 64 bytes should be send or receive. But my data is more than that, about 300 bytes ...

Stop fiddling around.
- Create a new Button in PC software
- Define a new command
- add this new definition to the PICs command list
- add a new flag.EEread or something
- in APPcmd() you set this flag when you decode the new command
- add a check for the new flag in APP_CustomHIDTasks()
Code: Select all
void APP_CustomHIDTasks()
{
    if(!HIDRxHandleBusy(USBOutHandle))
        APP_usbOUT(); // Data USB(pc) -> PIC

    if( (!HIDTxHandleBusy(USBInHandle)){
        if(Flags.IN_TODO){
            APP_usbIN(); // Data PIC -> USB(pc)
            Flags.IN_TODO = 0;
        }
        else if(Flags.EEread){
            send one package a time only ...
            decrement number of bytes to send remaining
            if all send clear flag
        }
    }
}//end ProcessIO

- first you can send back the acknowledge for the read request
- next time you send first 64 bytes
- than second 64
- ...
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: Simplified Microchip PIC USB demos (C18/XC8 MLA)

Postby susi » Sat Apr 02, 2016 11:46 am

Thank you Vloki for your response.
Yeah, I should make a new button, command, and flag.
but, what about sending procedure, my main problem?

- add a new flag.EEread or something
- in APPcmd() you set this flag when you decode the new command

so"
Code: Select all
void APP_cmd(void) {
    switch (ReceivedDataBuffer[1]) {
        case CID_READ_EE: // decoding new command & also sending (CID_READ_EE) as the acknowledge for // the read request
            ToSendDataBuffer[1] = CID_READ_EE | ID_MESSAGE;  //CID_READ_EE = 0x70;
            Flags.EEread = 1;
       break;
}}


now, checking new flag :

Code: Select all
void APP_CustomHIDTasks()
{
    if(!HIDRxHandleBusy(USBOutHandle))
        APP_usbOUT(); // Data USB(pc) -> PIC

    if( (!HIDTxHandleBusy(USBInHandle)){
        if(Flags.IN_TODO){
            APP_usbIN(); // Data PIC -> USB(pc)
            Flags.IN_TODO = 0;
        }
        else if(Flags.EEread){
            for (unsigned char packet_num = 0 ; packet_num < 4 ; packet_num ++)
            {   
                for (  num_of_samples = 2 ; num_of_samples < 64 ; num_of_samples++ )
                ToSendDataBuffer[num_of_samples] = my_data; //to fill the buffer with required data
                 
                APP_usbIN(); // Data PIC -> USB(pc)
            }
            Flags.EEread = 0;
        }
}//end ProcessIO


should I prepare whole of the data before starting of sending packets? or is it possible to prepare it during every cycle of packet sending (like above line)? these are my main questions.

the PC side:

Code: Select all
void MainWindow::rxHandler()
{
io_result = hid_read(connected_device, inBuffer, 65);
switch (inBuffer[1] - ID_MESSAGE)   //---------------------- MESSAGE
   {
        case  CID_READ_EE:
                for (int packet_num = 0; packet_num < max_packet_num ; packet_num++)
      {
         for (long int count = 0; count < 65; count++){
               long int numm = count + packet_num * 64;
               my_store[numm] = inBuffer[count];
         }
         io_result = hid_read(connected_device, inBuffer, 65);
      }
      myfile.open("file.txt", ios::out | ios::binary);
      for (int ii=0 ; ii < 300 ; ii++)
      myfile << " " << hex << int(my_store[ii]) << endl;
      myfile.close();
        break;
}


how should "sending from PIC" and "receiving in PC" become synchronized? what should I do to prevent form packet losses?
given these, what I see in file.txt is the last (4th) packet which is copies 4 times...
it seems only the last packet receives...
should I send a specific packet to show that sending whole of the data is finished?
susi
 
Posts: 9
Joined: Wed Mar 30, 2016 6:02 am
PIC experience: EE Student

Re: Simplified Microchip PIC USB demos (C18/XC8 MLA)

Postby vloki » Sun Apr 03, 2016 9:11 am

With "send one package a time only" I meant that you only have one transaction in a call of APP_CustomHIDTasks()

(in your code you do not check HIDTxHandleBusy(USBInHandle) for the other transactions)


Forget the idea that you will get the whole data at once. You receive it step by step and then you put it together an check if everything is all right.
You need some kind of ID (like the commands) to do that. If your command for EE_read is 0x70 you could send back the data with IDs x70 x71 x72 x73 ....
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: Simplified Microchip PIC USB demos (C18/XC8 MLA)

Postby susi » Sun Apr 03, 2016 10:30 am

Forget the idea that you will get the whole data at once. You receive it step by step and then you put it together an check if everything is all right.

Do you mean I should press the new button for sending every packet of data ? It's not a good issue.

you only have one transaction in a call of APP_CustomHIDTasks()

I should call this function in a loop. In every cycle of the loop I should prepare data and fill the "ToSendDataBuffer" . Am I right?
susi
 
Posts: 9
Joined: Wed Mar 30, 2016 6:02 am
PIC experience: EE Student

Re: Simplified Microchip PIC USB demos (C18/XC8 MLA)

Postby vloki » Sun Apr 03, 2016 10:53 am

susi wrote:
Forget the idea that you will get the whole data at once. You receive it step by step and then you put it together an check if everything is all right.

Do you mean I should press the new button for sending every packet of data ? It's not a good issue.

No. You send one request (0x70), but you get back as many responses are needed to transfer your data (70, 71, 72 ...)

susi wrote:
you only have one transaction in a call of APP_CustomHIDTasks()

I should call this function in a loop. In every cycle of the loop I should prepare data and fill the "ToSendDataBuffer" . Am I right?

No. This function is called in the main loop. If you have data you prepare it to be send.
If you have more data than can be processed you simply wait for the next time it is called in the main loop.
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: Simplified Microchip PIC USB demos (C18/XC8 MLA)

Postby vloki » Sun Apr 03, 2016 11:33 am

vloki wrote:If you have more data than can be processed you simply wait for the next time it is called in the main loop.

Hmmm, maybe I shouldn't say "you wait".
Better -> If there is data left, you send the next portion during it the following "CustomHIDTasks"
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: Simplified Microchip PIC USB demos (C18/XC8 MLA)

Postby susi » Sun Apr 03, 2016 11:42 am

Sorry Vloki, I got a little bit confused...
if
"you have one transaction in a call of APP_CustomHIDTasks()"
so if I want to have more than 1 transaction (in my case i need 4) I should call APP_CustomHIDTasks() 4 times, but you said no need for calling this func in a loop, because it is in the main loop of the main_hid.c

so when should I fill the buffer with my data? I want to relate values in every packet, relate to the number of that packet.

Code: Select all
void APP_CustomHIDTasks()
{
    if(!HIDRxHandleBusy(USBOutHandle))
        APP_usbOUT(); // Data USB(pc) -> PIC

    int max_packet_num = MAX_NUM_BYTES/64;
    for (unsigned char packet_num = 0; packet_num < max_packet_num; packet_num ++)
    {
               
        if(!HIDTxHandleBusy(USBInHandle)){
        if(Flags.IN_TODO){
            APP_usbIN(); // Data PIC -> USB(pc)
            Flags.IN_TODO = 0;
        }
        else if(Flags.EEREAD){
                ToSendDataBuffer[1] = CID_EEREAD + packet_num ;
                for (  num_of_samples = 2 ; num_of_samples < 64 ; num_of_samples++ )
                {
                    ToSendDataBuffer[num_of_samples] = packet_num;
                }
                APP_usbIN(); // Data PIC -> USB(pc)
            } 
        }
    }
    Flags.EEREAD = 0;
}//end ProcessIO


above code does not work fine. sorry if my question is stupid!! I am new in usb and maybe that's the reason
susi
 
Posts: 9
Joined: Wed Mar 30, 2016 6:02 am
PIC experience: EE Student

Re: Simplified Microchip PIC USB demos (C18/XC8 MLA)

Postby vloki » Sun Apr 03, 2016 7:11 pm

Ok, it has got nothing to do with USB but may be it is more simple to understand without the additional flag.

Try something like this:
Code: Select all
void APP_cmd(void) {
    switch (ReceivedDataBuffer[1]) {
        case CID_READ_EE: // decoding new command & also sending (CID_READ_EE) as the acknowledge for // the read request
            ToSendDataBuffer[1] = CID_READ_EE | ID_MESSAGE;  //CID_READ_EE = 0x70;
            EEbytes2read = 300;
            packet_num = 1;
       break;
}}


Code: Select all
void APP_CustomHIDTasks()
{
    if(!HIDRxHandleBusy(USBOutHandle))
        APP_usbOUT(); // Data USB(pc) -> PIC

    if(!HIDTxHandleBusy(USBInHandle)){
        if(Flags.IN_TODO){
            APP_usbIN(); // Data PIC -> USB(pc)
            Flags.IN_TODO = 0;
        }
        else if(EEbytes2read){
                ToSendDataBuffer[1] = CID_EEREAD + packet_num ;

                if(EEbytes2read >62) num_of_samples = 62;
                else num_of_samples = EEbytes2read;

                for (  i=0 ; i<num_of_samples ; i++ )
                {
                    ToSendDataBuffer[3+i] = packet_num;
                }
                EEbytes2read -= num_of_samples;
                packet_num++;
                APP_usbIN(); // Data PIC -> USB(pc)
        }
    }
}
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: Simplified Microchip PIC USB demos (C18/XC8 MLA)

Postby susi » Mon Apr 04, 2016 12:11 pm

Thanx Vloki for the code.

I wrote what you said. It seems communication is doing. EEbytes2read = 250. There are 3 issues:

1--- By pressing the new button I get fig.1 result in sniffer:
1.jpg
fig.1 - usb port is captured by usblyzer after pressing button, for the first time
1.jpg (110.39 KiB) Viewed 8201 times

but when I pressed the button for second or third or ... time, the result changed. fig.2 shows it
2.jpg
fig.2 - usb port is captured by usblyzer after pressing button, for the 2nd,3rd, and more time
2.jpg (122.61 KiB) Viewed 8201 times

why the communicating data is different in these transitions?

2--- what does the packet "01 F0 20 20 20 20 20 ... " mean? I searched in commands list "F0" but there is no such a command.

3--- is there any limit in the number of bytes allowed to send continuously in one transition?
For ex. when "EEbytes2read = 255 ", the sniffer shows correct packets of data (4 pakcet of 62 byte + 7 single byte in 5th packet). but when "EEbytes2read >255" , it doesn't work properly and sometimes it sends back nothing to the pc or it sends the packet "01 F0 20 20 20 20 20 ... " . what should I do if I want to send more data from PIC to pc? maybe I will need to send 3 million bytes...
susi
 
Posts: 9
Joined: Wed Mar 30, 2016 6:02 am
PIC experience: EE Student

Re: Simplified Microchip PIC USB demos (C18/XC8 MLA)

Postby vloki » Mon Apr 04, 2016 12:32 pm

Is there a special reason that all the transactions are shown three times?

2 - 0xF0 = CID_READ_EE | ID_MESSAGE; (0x70 | 0x80)

3 - the limit is the endpoint size (64) but your issue probably is that you defined EEbytes2read as an unsigned char?

3a - are you sure you want to use HID? 3 million bytes sounds like MSD ;-)
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

PreviousNext

Return to USB

Who is online

Users browsing this forum: No registered users and 3 guests

cron