View previous topic :: View next topic |
Author |
Message |
Lele
Joined: 04 Oct 2009 Posts: 5
|
CDC USB Communication |
Posted: Mon Nov 30, 2009 8:43 am |
|
|
Hi,
I've got a problem using the CDC sample program.
Here's my version:
Code: |
#include <18f4550.h>
#device PASS_STRINGS = IN_RAM
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48M)
#include <ctype.h>
#include <stdlib.h>
#include <C:\TAPS\elettronica\plating\pic18.h>
#include <math.h>
#include <C:\TAPS\elettronica\plating\usb_cdc.h>
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#define LED1 PORTC_RC1
#define LED2 PORTC_RC2
void main(void)
{
char c;
int8 i;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL );
set_tris_a(0b11111111);
set_tris_b(0b11111111);
set_tris_c(0b11111001);
set_tris_d(0b11111111);
set_tris_e(0b11111000);
LED1=LED2=0;
for (i=0;i<10;i++)
{
LED1=1;
delay_ms(200);
LED1=0;
delay_ms(200);
LED2=1;
delay_ms(200);
LED2=0;
}
usb_cdc_init();
usb_init();
while (TRUE)
{
usb_task();
if (usb_enumerated())
{
if (usb_cdc_kbhit())
{
c=usb_cdc_getc();
if (c=='1')
LED1=0;
if (c=='2')
LED1=1;
if (c=='3')
LED2=0;
if (c=='4')
LED2=1;
usb_cdc_putc(c+5);
}
}
}
} |
The problem seems to be in the usb_cdc_putc function.
If I remove it the program works. From hyperterminal or from my own VB program I can turn the leds on/off as requested.
If I leave the function call the program works once, sending the correct value but then it hangs (actually also the PC side is hanging for a while).
I checked on the forum and I saw that the most of the times this is due to a poor VUSB. I added to the suggested 220nF another 220nF but without success. The PIC itself is decoupled as (my) usual.
Moreover the PIC is well recognized by the PC and VUSB is 3.3V.
Any hint ?
Thanks.
Daniele |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Nov 30, 2009 8:56 am |
|
|
I experienced cases, where usb_cdc_putc() returned with global interrupts disabled. Just to exclude, that
this happens again, can you add this line after usb_cdc_putc()? Normally it hasn't any effect, because the
interrupts should be enabled at this point.
Code: | enable_interrupts(GLOBAL); |
|
|
|
Lele
Joined: 04 Oct 2009 Posts: 5
|
|
Posted: Mon Nov 30, 2009 9:21 am |
|
|
Thanks !
It works. I had already saw your advice but I forgot to apply it.
Daniele |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Nov 30, 2009 9:52 am |
|
|
Interesting indeed. What's your compiler version?
I'm still not clear, why the problem only reveals under specific conditions, respectively what these conditions are. Maybe it's caused by "echoing" PC data back, which isn't done in the standard CDC examples. My application had to be changed in a way, that a block of data is send back to the PC, this involves disabling and enabling interrupts anyway. So I didn't have a chance to check if the original problem continues with newer compiler or USB driver versions. I wasn't aware of any change related to the problem, however. |
|
|
Lele
Joined: 04 Oct 2009 Posts: 5
|
|
Posted: Wed Dec 02, 2009 12:39 pm |
|
|
The version is 4.078. I'll have a look at the CDC library.
I'm worried the workaround could be problematic later in the project. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Dec 02, 2009 11:59 pm |
|
|
Quote: | I'm worried the workaround could be problematic later in the project. | Why? Enabling global interrupts at a point where they should be enabled anyway. |
|
|
Lele
Joined: 04 Oct 2009 Posts: 5
|
|
Posted: Thu Dec 03, 2009 10:39 am |
|
|
I'm quite sure you're right. It was just a thought. |
|
|
|