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

CDC USB Communication

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



Joined: 04 Oct 2009
Posts: 5

View user's profile Send private message

CDC USB Communication
PostPosted: Mon Nov 30, 2009 8:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 30, 2009 8:56 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 30, 2009 9:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 30, 2009 9:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 02, 2009 12:39 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 02, 2009 11:59 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 03, 2009 10:39 am     Reply with quote

I'm quite sure you're right. It was just a thought.
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