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

Errors in USB

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



Joined: 08 Nov 2016
Posts: 11
Location: Hungary

View user's profile Send private message

Errors in USB
PostPosted: Thu Jun 28, 2018 11:57 pm     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Fri Jun 29, 2018 1:00 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 29, 2018 9:43 am     Reply with quote

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. Confused

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: 19195

View user's profile Send private message

PostPosted: Fri Jun 29, 2018 11:29 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 29, 2018 12:02 pm     Reply with quote

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 Sad
_________________
shirke
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sat Jun 30, 2018 1:13 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 03, 2018 11:07 am     Reply with quote

Thank you for your ideas. Cool

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: 19195

View user's profile Send private message

PostPosted: Tue Jul 03, 2018 12:10 pm     Reply with quote

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...
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