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 CCS Technical Support

USB not communicating on PIC18F2550 using CCS library

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



Joined: 20 Dec 2014
Posts: 5

View user's profile Send private message

USB not communicating on PIC18F2550 using CCS library
PostPosted: Sat Dec 20, 2014 3:38 pm     Reply with quote

I'm sure I'm missing something simple and bare with me as this is my first PIC USB project. I wired up a bread board using the pic18f2550 chip, connected USB D+ and D- to the appropriate pins, used Vdd and GND from USB cable to power board, enabled the internal USB voltage regulator and used a 470nF capacitor coming out of VUSB to ground. I'm pretty confident the board layout is good.

I'm using the keyboard example but when I plug the USB cable in, the usb_attached() returns true but the usb_enumerated() continues to return false. When initializing the usb, I switched from the usb_init_cs() method to usb_init() and as expected, it hangs trying to get enumerated. My windows 7 machine doesn't recognize that the device is even being plugged in.

Am I missing something completely obvious here? Is there an extra step I'm missing? Some drivers that need to be loaded? I know the INF files are in the CCS folder but you should be able to point the device manager to them after the device is discovered (as unknown). Any help will be much appreciated.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sun Dec 21, 2014 2:14 pm     Reply with quote

First thing, clock. What are you using?. Fuses.
nhruch



Joined: 20 Dec 2014
Posts: 5

View user's profile Send private message

PostPosted: Sun Dec 21, 2014 6:16 pm     Reply with quote

Thanks for the response. Spent all day yesterday coming to the conclusion that the internal clock is not fast enough to drive USB and requires an external oscillator. That is definitely issue #1. I have a 50MHz DIP crystal oscillator on hand which I've been trying to implement but still seems to be giving me trouble. With the 50MHz, I'm using these fuses and clock declaration:

#fuses HSPLL, NOWDT, NOPROTECT, NOLVP, NODEBUG, USBDIV, PLL12, CPUDIV1, VREGEN
#use delay(oscillator=48MHz,USB_FULL)

For reference, I'm using the keyboard_hid example that came with the compiler and changed the chip to pic18F2550.

Snippet from my PIC setup header:
Code:

#if defined(USB_HW_CCS_PIC18F2550)
   #include <18F2550.h>
   #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL12,CPUDIV1,VREGEN
   #use delay(oscillator=48MHz,USB_FULL)
   //#use delay(clock=20000000)

   //leds ordered from bottom to top
   #DEFINE LED1 PIN_A5  //green
   #define LED2 PIN_B4  //yellow
   #define LED3 PIN_B5  //red
   #define LEDS_OFF()   LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
   #define BUTTON_PRESSED() !input(PIN_A4)

   //see section below labeled USB_CABLE_IS_ATTACHED
   #define PIN_USB_SENSE   PIN_B2

   #define HW_ADC_CONFIG   ADC_CLOCK_INTERNAL
   #define HW_ADC_CHANNEL  0
   #define HW_ADC_PORTS    AN0
   
   #define HW_INIT() setup_adc_ports(HW_ADC_PORTS)
#endif


Snippet from main.c:
Code:

void main(void)
{
// Test clock speed
/*
    while(TRUE) {           // Produces a 1hz square wave on pin B4 to light LED
        output_high(PIN_B4);
        delay_ms(1000);
        output_low(PIN_B4);
        delay_ms(1000);
    }
*/

   HW_INIT();

   LED_ON(LED1);
   LED_OFF(LED2);
   LED_OFF(LED3);

   printf("\r\n\nCCS HID Keyboard Demo");
   printf("\r\nCharacters received over serial get sent as a HID keyboard");
   
  #ifdef __PCH__
   printf("\r\nPCH: v");
   printf(__PCH__);
  #elif defined(__PCD__)
   printf("\r\nPCD: v");
   printf(__PCD__);
  #else
   printf("\r\nPCM: v");
   printf(__PCM__);
  #endif

   usb_init_cs();
   
  #if !(__USB_PIC_PERIF__)
   printf("\r\nUSBN: 0x%X", usbn_get_version());
  #endif
   printf("\r\n");

   while (TRUE)
   {
      usb_task();
      usb_debug_task();

      if (usb_enumerated())
      {
         usb_keyboard_task();
      }
   }

}


In the debug enumerated, we check to see if its attached (which an LED does light up) but it does not succeed for enumeration. I'm almost positive this is a clocking issue. Just getting a little lost/overwhelmed with configuring the clock. Right now the visual clock speed test is running at 20 seconds as opposed to the expected 1 second.

I will say I have some 20MHz crystals coming as that seems to be the preferred component. Right now the oscillator I'm using is a DOC-70ZA (not sure why I got this one...) which is a 4 pin DIP crystal.
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Dec 21, 2014 7:05 pm     Reply with quote

A reading of the 'oscillator configurations' of the datasheet is extremely important. You can use a 4MHz 'can' and get USB to run correctly, just be sure to configure the fuses correctly ! printout figure 2-1 and highlight the 'clock signal' routing you need to use 4MHz in.... USB 96MHz and say 24MHz CPU clock.
It's all there and NO you cannot use a 50MHz xtal and expect to get USB to work though the PIC _might_ run at that overclocked speed.


jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 21, 2014 8:09 pm     Reply with quote

Quote:

I have a 50MHz DIP crystal oscillator on hand which I've been trying to
implement but still seems to be giving me trouble.

The 18F4550 data sheet says:
Quote:
17.8 Oscillator
The USB module has specific clock requirements. For
full-speed operation, the clock source must be 48 MHz.


Quote:
Right now the oscillator I'm using is a DOC-70ZA (50 MHz)

Useless.

Quote:
I have some 20MHz crystals coming as that seems to be the preferred component.

Wait until those come in. Then proceed.
nhruch



Joined: 20 Dec 2014
Posts: 5

View user's profile Send private message

PostPosted: Sun Dec 21, 2014 9:40 pm     Reply with quote

Thanks guys. I was thinking the on-hand oscillator was going to be useless. Hence the recent purchase. Hopefully its smooth sailing from there on. My fuses I believe should be PLL5, USBDIV, VREGEN with the 20MHz input, and use delay(clock=48MHz,USB_FULL) to get that configured.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Mon Dec 22, 2014 1:23 am     Reply with quote

Lets take a quick summary.

To run USB at full speed, the USB peripheral _must_ be clocking at 48MHz. This can be generated by the PLL, from a crystal at 4,8,12,16,20 or 24MHz. Or from an external oscillator at the same frequencies, or 40 and 48MHz. These are the _only_ frequencies supported to run the peripheral at full speed. At slow speed, a clock at 24MHz can also be used. 50MHz is not a legal rate (either for the CPU, or the USB).

The USB peripheral on this chip _cannot run off the internal clock_. Generally internal clocks are not accurate enough for USB, but some of the later PIC's have ones that are accurate enough for low speed USB, and some others have the ability to lock the internal oscillator onto the USB data stream using a PLL, to give full speed support.

The CPU speed is independant of this. You can (for instance) run the CPU off the internal oscillator at 8MHz, while still running the USB at 48MHz. The minimum CPU speed needed to handle USB, is around 4Mhz, though it is generally more reliable at a slightly higher rate (it can become hard to meet the packet timing requirements at 4MHz).

Your symptoms were 'classic' for a USB peripheral being physically 'seen', but then being unable to actually send data at a legal rate.
nhruch



Joined: 20 Dec 2014
Posts: 5

View user's profile Send private message

PostPosted: Tue Dec 30, 2014 3:55 pm     Reply with quote

Thank you thank you! Once I got the exact clock rate in there, the device was recognize and the world is now safe again.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Wed Dec 31, 2014 2:33 am     Reply with quote

Getting the clocking right on the USB chips is more complex that just about any other PIC. Especially since Microchip have about five different layouts for the oscillator section. There are several threads where 'full details' have been posted in the past, and even one where a user discovered that in fact the internal oscillator layout is not actually as Microchip post. It doesn't make any difference if you use the 'documented' setups, but was an interesting discovery!....
Have a good New Year.
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