USBCDC Buffer Problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

USBCDC Buffer Problem

2,782 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by KaraMuraT on Sat Jul 10 06:09:53 MST 2010
Hi,

As you can see on my previous post (http://knowledgebase.nxp.com/showthread.php?t=505) I'm working over a USBCDC device with external power output. With Zero's help I was able to make it work as expected.

Right now my problem is related to this device but not related to other thread. Therefore I'm asking it in a new thread.

If I send more than 63 characters from the PC via USB, it fails to reach target UART microcontroller. For example if I send 64 characters, USBCDC device sends non to UART, If I send 65 chars, sends only one character.

It reminded me a classical buffer problem. Buffer vector has a modulus of 64 (size) and if you send this size, resulting buffer vector is zero, therefore the device thinks that there is nothing to send. Therefore I thought that it might be a buffer overrun problem.

So my working firmware is exactly as the included USBCDC example, except the external power output and a blinking LED. So you can reproduce, if you want to check this fact easily.

P.S: I've checked the USB Code optimization problem in this thread (http://knowledgebase.nxp.com/showthread.php?t=388) but it's not related to Debug version. (I have to add, the workaround helped. The device does not freeze in Release version anymore, it was freezing before).

Thanks in advance.
0 Kudos
Reply
19 Replies

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Sep 30 08:41:38 MST 2010
It you can see a VCom Port in your device manager, USB seems to be connected (if you use a transistor / fet and LED at USB_CONNECT, that should show it also).
Next step: open this serial port and read data. Is it possible to open the port?
Try something like Tera Term

http://logmett.com/forum/index.php

That's working more reliable.
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by fplesinger on Thu Sep 30 07:12:53 MST 2010
Hi,
I have got a problem and it is most probably some elementary mistake:confused:.

I am trying to send some text data from lpc1343 through USB to PC.
So it is the code below published by Zero I suppose.

VCOM was initialized properly (at least I can see it in device manager). But I can not see any data in hyperterminal. Ehm...any hint what is going wrong?

Thanks,
Filip

PS: Feeling like a lama. Lot of hours left and nearly no progress. Terrible:(
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Wed Jul 21 12:13:09 MST 2010
[FONT=Arial][SIZE=2]No :rolleyes:[/SIZE][/FONT]

[FONT=Arial][SIZE=2]Use NXP_LPC13xx_USBCDC, put in buffer & counter:[/SIZE][/FONT]
[FONT=Arial][SIZE=2][B][COLOR=#7f0055][COLOR=#7f0055]char [/COLOR][/COLOR][/B]txBuf[128];[/SIZE][/FONT]
[FONT=Arial][SIZE=2][B][COLOR=#7f0055][COLOR=#7f0055]int [/COLOR][/COLOR][/B]tx_count=0;[/SIZE][/FONT]
[FONT=Arial][SIZE=2][B][COLOR=#7f0055][COLOR=#7f0055]char[/COLOR][/COLOR][/B] txt1[70]= [COLOR=#2a00ff][COLOR=#2a00ff]"This is an USB CDC speed test with a packet length of 64 byte.\n\r"[/COLOR][/COLOR];[/SIZE][/FONT]


[FONT=Arial][SIZE=2]a simple buffer and transmit function:[/SIZE][/FONT]
[FONT=Arial][SIZE=2][B][COLOR=#7f0055][COLOR=#7f0055]int [/COLOR][/COLOR][/B][B]putstring[/B]([B][COLOR=#7f0055][COLOR=#7f0055]char[/COLOR][/COLOR][/B] *ptr)[/SIZE][/FONT]
[FONT=Arial][SIZE=2]{[/SIZE][/FONT]
[LEFT][FONT=Arial][SIZE=2][B][COLOR=#7f0055][COLOR=#7f0055]while[/COLOR][/COLOR][/B]((*ptr != 0) && (tx_count<64))[/SIZE][/FONT][/LEFT]
[FONT=Arial][SIZE=2]{[/SIZE][/FONT]
[FONT=Arial][SIZE=2]txBuf[tx_count] = *ptr; [/SIZE][/FONT]
[LEFT][FONT=Arial][SIZE=2]ptr++;              [COLOR=#3f7f5f][COLOR=#3f7f5f]//[U]inc[/U] pointer[/COLOR][/COLOR] [/SIZE][/FONT]
[FONT=Arial][SIZE=2]tx_count++;   [COLOR=#3f7f5f][COLOR=#3f7f5f]//count [U]tx[/U] chars[/COLOR][/COLOR] [/SIZE][/FONT][/LEFT]
[FONT=Arial][SIZE=2]} [/SIZE][/FONT]
[FONT=Arial][SIZE=2][B][COLOR=#7f0055][COLOR=#7f0055]return[/COLOR][/COLOR][/B] tx_count; [/SIZE][/FONT]
[FONT=Arial][SIZE=2]} [/SIZE][/FONT]
 
[LEFT][FONT=Arial][SIZE=2][B][COLOR=#7f0055][COLOR=#7f0055]void [/COLOR][/COLOR][/B][B]usb_transmit[/B]([B][COLOR=#7f0055][COLOR=#7f0055]void[/COLOR][/COLOR][/B]) [COLOR=#3f7f5f][COLOR=#3f7f5f]//transmit buffer[/COLOR][/COLOR] [/SIZE][/FONT][/LEFT]
[FONT=Arial][SIZE=2]{ [/SIZE][/FONT]
[FONT=Arial][SIZE=2][COLOR=#7f0055][COLOR=#7f0055][B]if[/B][/COLOR][/COLOR](tx_count)[/SIZE][/FONT]
[FONT=Arial][SIZE=2]{[/SIZE][/FONT]
[FONT=Arial][SIZE=2][B][COLOR=#7f0055][COLOR=#7f0055]while[/COLOR][/COLOR][/B](LPC_USB->[COLOR=#0000c0][COLOR=#0000c0]TxPLen[/COLOR][/COLOR] & 0x01F){} [/SIZE][/FONT]
[LEFT][FONT=Arial][SIZE=2]CDC_DepInEmpty = 0;[/SIZE][/FONT][/LEFT]
[FONT=Arial][SIZE=2]USB_WriteEP (CDC_DEP_IN, ([B][COLOR=#7f0055][COLOR=#7f0055]unsigned[/COLOR][/COLOR][/B][B][COLOR=#7f0055][COLOR=#7f0055]char[/COLOR][/COLOR][/B] *)&txBuf[0], tx_count);[/SIZE][/FONT]
[LEFT][FONT=Arial][SIZE=2]}[/SIZE][/FONT]
[FONT=Arial][SIZE=2]tx_count=0;  [COLOR=#3f7f5f][COLOR=#3f7f5f]//reset transmit counter[/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Arial][SIZE=2]}[/SIZE][/FONT][/LEFT]


[FONT=Arial][SIZE=2]clean the main loop and insert[/SIZE][/FONT]

[FONT=Arial][SIZE=2][B][COLOR=#7f0055][COLOR=#7f0055]while[/COLOR][/COLOR][/B](1)[/SIZE][/FONT]
[FONT=Arial][SIZE=2]{ [COLOR=#3f7f5f][COLOR=#3f7f5f]// Loop forever[/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Arial][SIZE=2]LPC_GPIO2->[COLOR=#0000c0][COLOR=#0000c0]DATA[/COLOR][/COLOR] |= (1 << 9); [COLOR=#3f7f5f][COLOR=#3f7f5f]//set scope output[/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Arial][SIZE=2]putstring(&txt1[0]);[/SIZE][/FONT]
[FONT=Arial][SIZE=2]usb_transmit(); [COLOR=#3f7f5f][COLOR=#3f7f5f]//send string[/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Arial][SIZE=2]LPC_GPIO2->[COLOR=#0000c0][COLOR=#0000c0]DATA[/COLOR][/COLOR] &=~(1 << 9); [COLOR=#3f7f5f][COLOR=#3f7f5f]//clear scope output[/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Arial][SIZE=2]} [COLOR=#3f7f5f][COLOR=#3f7f5f]// end while [/COLOR][/COLOR][/SIZE][/FONT]


[FONT=Arial][SIZE=2]and start a terminal program to see how fast this string can be sent :)[/SIZE][/FONT]
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by amendae on Wed Jul 21 11:09:02 MST 2010
I guess I'm doing something fundamentally wrong then. Would you mind sharing a small project with me ?
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue Jul 20 12:05:31 MST 2010
Yes, made a test up to 4,5 MBytes in 10 seconds = 450kBytes/s :cool:
Using

[SIZE=2]USB_WriteEP (CDC_DEP_IN, ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]unsigned [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]char[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] *)&txBuf[0], tx_count);[/SIZE]

to write 64 byte packages every 125 µs. Should result in 512 kBytes/s, but obviously USB is sometimes busy with other things

Checking transmitted bytes with
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](LPC_USB->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TxPLen[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & 0x01F){}[/SIZE][/LEFT]

and pumping packages with max. speed to PC results in 1MBytes/s which seems to be fast enough for Full Speed (12Mbit/s).
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by amendae on Mon Jul 19 20:24:41 MST 2010
Hi Zero,
Yes, you're right, it seems that endpoints have static sizes.
Did you manage to get decent performances through CDC ? I'm stuck at 48KBytes/s, slower than HID it seems.
Thanks
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon Jul 19 16:23:48 MST 2010
I'm no USB expert, but


Quote:
[FONT=Arial]Table 9-122 [/FONT][FONT=Arial]shows the supported endpoint configurations. The packet size is fixed for each type of end point.[/FONT]



describes a bulk in/out packet size of 64 bytes (LPC1343) :eek:
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by amendae on Mon Jul 19 13:40:28 MST 2010
Well, that's really bizarre. It works only with 64 bytes for me. If I'm using 128 or 256 for USB_CDC_BUFFSIZE, the enumeration fails. Help !!!
:D
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by KaraMuraT on Mon Jul 19 03:18:20 MST 2010
Yep, the buffer is now 256 bytes, which is my target devices maximum.

But there should be a minor adjustment. The buffer mentioned here is the USB -> UART buffer. Probably you'll have to increase the UART -> USB buffer. (It is default 32 bytes long)

Also if you are using Release version, you should add a "volatile" keyword to a variable which is mentioned in this thread: http://knowledgebase.nxp.com/showthread.php?t=388

Taking care all of these, you should be fine.

If you are not, then you have to look at other things you have changed while making practices and general LPCXpresso debugging or compiling guidelines.
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by amendae on Sat Jul 17 19:44:40 MST 2010
I've experienced the same issue with sending more than 64 bytes to the LPC. I did find though the buffer definition and the current 64 bytes allocation. However, when I'm trying to increase that buffer size, for some reason, the VCOM is not recognized anymore. KaraMaraT, did you manage to make it work with a larger buffer ?
Thanks.
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by KaraMuraT on Mon Jul 12 03:34:13 MST 2010
Yep, found the problem. It's because of the GSM Impulses over the power circuit. It was designed to be robust and should not affect to another device, but somehow it effects the USBCDC device. I can trace 400 mV bursts over the RxD line. I haven't seen any pulses before on any device (this is a general port and connected many devices before).

The cause might be a ground loop, too.

Anyway, a 47K pull-up solved the problem. (At least seems like so :) )
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by KaraMuraT on Sun Jul 11 11:01:19 MST 2010
I tried the project you have, it's very similar, but the default example doesn't have the Echo function.

With echo, everything is OK. But if I connect the target device, something goes wrong. And as you said I have to check with scope.

Interestingly, yesterday I was getting just FCh and F0h now added also F8h. If it is just a noise over the RxD, it should be very random (like a non matching UART speed, or frame error). But it has a pattern. Hmmm...

I love my job. Something can go wrong out of nothing. And I have plenty things to solve :)
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by KaraMuraT on Sat Jul 10 14:37:15 MST 2010
It's the second one :) I'm using the default example found in the LPCXpresso 3.4 installation. And they are different as you have shown.

I'm sober enough to check the problem, but wanna go sleep now :). I'll check it tomorrow afternoon and let you know. Thanks again.
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat Jul 10 12:59:34 MST 2010
FYI:

http://www.support.code-red-tech.com/CodeRedWiki/NXPExamples

Code Red NXPExamples File: LPC13xx_SW_Bundle.zip

http://www.support.code-red-tech.com/CodeRedWiki/NXPExamples?action=AttachFile&do=get&target=LPC13xx...

It contains NXP_LPC13xx_USBCDC, the project I'm using  :D
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat Jul 10 12:48:49 MST 2010
Either you had a beer to much or we are using different examples :cool:
If you don't find this definition in your vcomdemo.c which switches receiving UART / USB in main loop

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (1) { [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Loop forever[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// CodeRed [/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]#ifdef [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]JUST_ECHO_OVER_USB[/SIZE]
 
[LEFT][SIZE=2]VCOM_UsbJustEcho();[/SIZE]

[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]#else[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[LEFT][SIZE=2]VCOM_Serial2Usb(); [/SIZE][/LEFT]
[/LEFT]

 
 
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// read serial port and initiate USB event[/COLOR][/SIZE][/COLOR][/SIZE]
 
[LEFT][SIZE=2]VCOM_CheckSerialState();[/SIZE]

[SIZE=2]VCOM_Usb2Serial();[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]#endif[/COLOR][/SIZE][/COLOR][/SIZE][/B][/LEFT]
[/LEFT]

 
 
[LEFT][SIZE=2]} [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// end while [/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]


[LEFT]we are using different examples :eek:.[/LEFT]
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by KaraMuraT on Sat Jul 10 12:20:29 MST 2010
Unfortunately it didn't helped, Zero. It looks like only a definition, I couldn't find any usage of it in the project files.

I hate to be ignorant :) But beginning is always the hard part. Now I have to find a way, which acts as a software loopback. (I'm home, and don't have a scope. I can check it on Monday at office)
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat Jul 10 08:30:57 MST 2010
Use

#define JUST_ECHO_OVER_USB 1


in vcomdemo.c to switch off UART and just echo back USB to PC.
If your problem disappears, your problem is UART software/ hardware.
Use a scope to control UART RX pin for unexpected behaviour.
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by KaraMuraT on Sat Jul 10 08:05:14 MST 2010
Zero,

I think I'm gonna get your picture and make a t-shirt from it :)

Thanks, that was what I'm looking for. Buffer problem solved. (I couldn't find Ctrl+H search window. Looked for minutes to find it in the EDIT menu, and still don't know in which menu. It was really necessary for me and I was going to ask it :) )

Right now I have another problem. I thought that it was related to buffer problem but seems not.

I'm getting random 0xF0 and 0xFC characters from the CDC device to the PC. And it is not a response, comes at random times (or at fixed frequency which I'm not aware). This causes a failure in my system. What could it be?

(I've added an extra 100K pull-up resistor to UART RxD line, for precaution)
0 Kudos
Reply

2,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat Jul 10 07:35:02 MST 2010
And now use Search (Ctrl-H) to look for '64' :rolleyes:

Result in cdcuser.c:

[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]#define[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] CDC_BUF_SIZE  (64) [/SIZE]


Search CDC_BUF_SIZE, result in cdcuser.c:

[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// CDC output buffer[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]typedef[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]struct[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] __CDC_BUF_T {[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]unsigned[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]char[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]data[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][CDC_BUF_SIZE];[/SIZE][/LEFT]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]unsigned[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]wrIdx[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]unsigned[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]rdIdx[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]} [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]CDC_BUF_T[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]



[LEFT]and voila, there's your buffer.[/LEFT]
0 Kudos
Reply