View previous topic :: View next topic |
Author |
Message |
shirke
Joined: 08 Nov 2016 Posts: 11 Location: Hungary
|
Errors in USB |
Posted: Thu Jun 28, 2018 11:57 pm |
|
|
Hello Guys!
I try to use the USB with a dsPIC33EP256MU806 uC. I would like to use CDC communication, with the own library of CCS. I need to send 8192 bytes via USB to a PC where a script receives the bytes and checks them.
The sent example pattern is: 0x11000000..0x110007ff.
I checked the line with scope and I realized that some double words in the message is different than those I wanted to send, so they automatically changes right at sending.
Eg.: The previous double word is: 0x11000304 so I expect that the next is: 0x11000305 but it is 0x11000300.
OR
The previous double word is: 0x110003CF so I expect that the next is: 0x110003D0 but it is 0x1100C1D0.
I tried the same code on a PIC18F2550 and the result was wonderful: there were no errors.
Do you have any idea what I should try? Could you help me? _________________ shirke |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Fri Jun 29, 2018 1:00 am |
|
|
How are you sending the data to the CDC driver?. usb_put_packet, usb_cdc_putc, usb_cdc_putc_fast?. If you are using 'fast', and not checking the buffer has space, you could be overflowing the transmit buffer. Remember in particular, that this chip will get data 'ready to send' a lot faster than the PIC18, while the USB transmission would stay at the same speed.
It has the 'ring' to me that the buffer is getting overflowed, and data is therefore being inserted into the wrong place. |
|
|
shirke
Joined: 08 Nov 2016 Posts: 11 Location: Hungary
|
|
Posted: Fri Jun 29, 2018 9:43 am |
|
|
I use the 'usb_cdc_putc_fast()' function. Here you can see the code that I use on both uC. I think the buffer can not be overwritten because I check it with the 'usb_cdc_putready()' function.
Code: | int main(int argc, char** argv)
{
output_high(PIN_F3);
usb_cdc_init();
usb_init();
output_toggle(PIN_B0);
char array[512];
unsigned long looper;
unsigned long looper2=0;
unsigned long Size=4096;
looper=0;
unsigned int InputMessageSignal=0;
unsigned long long looper3=0x11000000;
unsigned int ByteSign=0;
while(1)
{
if(usb_cdc_kbhit())
{
//usb_cdc_putc('w');
switch(InputMessageSignal)
{
case 0x0:
if(usb_cdc_getc()=='S') InputMessageSignal |= 1;
break;
case 0x1:
if(usb_cdc_getc()==0x01) InputMessageSignal |= 2;
break;
case 0x3:
usb_cdc_getc();
InputMessageSignal |= 4;
break;
case 0x7:
usb_cdc_getc();
InputMessageSignal |= 8;
output_toggle(PIN_B0);
looper2=4;
usb_cdc_putc_fast('S');
usb_cdc_putc_fast(0x02);
usb_cdc_putc_fast(Size>>8);
usb_cdc_putc_fast(Size);
break;
default:
usb_cdc_getc();
}
}
if(InputMessageSignal == 0x0F)
{
unsigned int BufferSize=usb_cdc_putready();
while(looper < BufferSize && (looper3&0xffff) < Size/4)
{
switch(ByteSign&0x3)
{
case 0:
usb_cdc_putc_fast(0x11);
break;
case 1:
usb_cdc_putc_fast(looper3>>16);
break;
case 2:
usb_cdc_putc_fast(looper3>>8);
break;
case 3:
usb_cdc_putc_fast(looper3>>0);
looper3++;
break;
break;
default:
break;
}
ByteSign++;
looper++;
}
//delay_us(1000);
looper=0;
if((looper3&0xffff) == Size/4)
{
InputMessageSignal=0;
looper=0;
looper3=0x11000000;
}
}
}
return (1);
} |
_________________ shirke |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Fri Jun 29, 2018 11:29 am |
|
|
Using fast, you need to ensure there is space in the buffer for what you want to send. Use the standard usb_cdc_putc instead. It checks for space before accepting the data. |
|
|
shirke
Joined: 08 Nov 2016 Posts: 11 Location: Hungary
|
|
Posted: Fri Jun 29, 2018 12:02 pm |
|
|
I have tried it, I have changed all of the 'usb_cdc_putc_fast()' function to 'usb_cdc_putc()' but the result is the same. Is it possible that the error is in the silicon? Do you have any other idea? I don't know what I should do _________________ shirke |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Sat Jun 30, 2018 1:13 am |
|
|
You don't show your clock settings for the USB.
I know that on this particular chip it is very awkward to get the USB clocking right. USB should repeat the data request when a CRC is found on comms, but if the USB buffer has been changed while the renegotiation is going on, this could easily cause misplaced data. |
|
|
shirke
Joined: 08 Nov 2016 Posts: 11 Location: Hungary
|
|
Posted: Tue Jul 03, 2018 11:07 am |
|
|
Thank you for your ideas.
The problem was the overwrite of the buffer. I thought that the usb_cdc_putready() function was enough, but it wasn't.
Now I fill and flush the buffer manually with the _usb_cdc_putc_fast_noflush() and usb_cdc_flush_tx_buffer() function, and I write into the buffer when usb_cdc_put_buffer_free() function enables it. _________________ shirke |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Tue Jul 03, 2018 12:10 pm |
|
|
Good.
Yes it had all the sounds of data getting out of order in the buffer. Glad you have sorted it.
It is interesting, because the USB files disagree with themselves on this. One says putready just returns TRUE/FALSE and another says it returns the amount of free space. They say it was changed in 2013, to return the space.
However looking at your code, there is at least one location where you put three bytes without checking that there is space. Would suggest that this is possibly the problem... |
|
|
|