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

[Solved] USB_Init crash PIC18F4550
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

[Solved] USB_Init crash PIC18F4550
PostPosted: Sun Dec 02, 2012 11:48 am     Reply with quote

Dear CCS/PIC expert,

I'm running into a pic crash (neither pic pin C6,C7 or A0 led light up) when i enable the usb function of the pic.

Code:

#include <18F4550.h>
#FUSES NOWDT,HSPLL,STVREN,NODEBUG,NOLVP,NOWRT,NOWRTD,NOMCLR,PLL3,CPUDIV1,USBDIV,VREGEN

#use delay(clock=48000000)

#include <usb_cdc.h>
#include <string.h>

void main()
{
usb_cdc_init();
usb_init();
output_high(pin_c7);
output_high(pin_c6);
output_high(pin_a0);

   while(1)
   {
   usb_task();
   }
}



If I comment the following lines pic will run ok (PIN_A0,C6,C7 goes HIGH)

Code:

usb_cdc_init();
usb_init();
usb_task();



I've checked the Vusb voltage it's 3.3V and steady!

Also no usb popup on windows side when connecting the usb (not even a device fail warning , or any sounds)

Btw, PCWHD 4.134 and 12 mhz quartz


Any clues?

Thank you very much!!

EDIT: 470 nF polymer cap on Vusb to ground
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!


Last edited by ELCouz on Wed Dec 05, 2012 6:07 pm; edited 1 time in total
temtronic



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

View user's profile Send private message

PostPosted: Sun Dec 02, 2012 12:49 pm     Reply with quote

1st thing...
Have you tried the CCS supplied USB example code...located in the examples folder?
Also have you installed , on the PC, the 'driver' or 'inf' file that they also supply ?

I know for a fact their code does work..but ...as the PIC USB driver code takes 1/3 of available code space I've since gone to 'regular' PICs and ttl-usb modules. Only $3 a pop,they work and NO codespace required.

hth
jay
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Sun Dec 02, 2012 1:08 pm     Reply with quote

I've tried the example first it wasn't working so that's why i've posted a more simple usb code (easier troubleshooting)

For the driver to be installed, i need first the PIC to be recognized by Windows which i can't !

Is it possible to have a fried USB Module inside the pic but the rest is okay?

- The pic is working very well except when i enable USB (freeze)

- Vusb is stable and outputting correct voltage

I really don't know what to explain more ? :(

thank you
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sun Dec 02, 2012 1:35 pm     Reply with quote

Triple check your connections. Classic one is having D+ and D- reversed.

Best Wishes
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Sun Dec 02, 2012 1:39 pm     Reply with quote

Quote:
Triple check your connections. Classic one is having D+ and D- reversed.


That's what i thought first before posting here ... i wish it was simple as that!

Looks like I have a dead USB module ... even when doing output_high(PIN_C4 & C5 which are D- /D+) i get 0v on the scope!!!

Must be why the whole thing freeze!

Should have put a pdip socket on my prototype board... lets go resolder another one !!
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sun Dec 02, 2012 1:59 pm     Reply with quote

No, the USB pins _are not_ available as standard I/O pins. Nothing will come out on then from an output_high.
Remember the examples _do_ assume PIN_USB_SENSE is connected (normally B2), and the example code requires this.

Best Wishes
rwskinner



Joined: 08 Dec 2006
Posts: 125
Location: Texas

View user's profile Send private message

PostPosted: Mon Dec 03, 2012 7:36 am     Reply with quote

On the 18F4550 I'm using, I also have problems with usb_init causing the program to stall there unless a usb cable is connected. As soon as I connect a cable and the PC bleeps stating the driver installed, then enumeration occurs, the rest of my program works as designed.

Commenting out
usb_cdc_init();
usb_init();
and the program runs fine with the exception of no USB.

Richard
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Mon Dec 03, 2012 8:45 am     Reply with quote

Of course usb_init will stall unless the cable is attached.
It waits for the USB to be connected.....

If you want to wake up without USB, but with it becoming available if the connection is made, then you need to do the following:
Code:

void main(void) {
    int1 usb_cdc_old_connected=FALSE;
    int8 itemp;
    //Other initilalisation here

    //First you must have connection sense. This is actually _required_ by the
    //USB specification, but a lot of devices that are bus powered can cheat
    //and not use it. All other devices should have it.
    //Use usb_init_cs instead of usb_init.
    usb_init_cs();
    //see comment about interrupts below.....

    //You don't need to call cdc_init. This is called automatically by init, if cdc
    //is selected.
    while {
       //main code loop
       if (usb_attached()) {
          usb_task();
          //you must call 'task', before testing 'enumerated'.
          if (usb_enumerated()){
             if (usb_cdc_carrier.dte_present) {
               if (usb_cdc_oldconnected==FALSE) {
                  printf(usb_cdc_putc,"I/F active\n\r");
                  usb_cdc_oldconnected=TRUE;
               }
               if (usb_cdc_kbhit()) {
                  itemp=usb_cdc_getc();
                  //and handler code here to do what you want
               }
             }
             else
                usb_cdc_oldconnected=FALSE;
          }
          else
             usb_cdc_oldconnected=FALSE;
       }
    }
}     

This automatically sends a message when the USB connects (I/F active). You can make this some suitable introduction to your device. But it will keep looping, and not hang if the USB is not there.
Key points are that you must use connection sense, and use the usb_init_cs, which sets of the peripheral _waiting_ for the connection to be made.

One other comment, if you are using interrupts in your code, you need to enable them. init_cs, _only_ enables the interrupts if the connection is present.

Best Wishes
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Mon Dec 03, 2012 5:56 pm     Reply with quote

hmmm after hours of troubleshooting... turns out a OSC related problem i think

Why when i touch the quartz pin the USB suddenly start (windows sound and device enumerated itself!)?

I use 22 pf caps (to ground) on both 12 mhz quartz pins...

What the hell?

Maybe an electronic guru can explain how is it possible...

thank you very much!

have a nice day!

EDIT: from the quartz casing to ground i have 1.39V is that normal?

_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Mon Dec 03, 2012 6:21 pm     Reply with quote

More information to help

Here's my usb test board



Everything is ok?
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
temtronic



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

View user's profile Send private message

PostPosted: Mon Dec 03, 2012 6:45 pm     Reply with quote

Quirky problems can come from almost anything, sorry...

If a PCB, then maybe a bad solder joint, very thin trace, flux bridge, etc.
If a 'whiteboard' (solderless), could be minor corrosion of the internal strip, not enough contact (comes from trying to put 1W resistors in the holes).

Personally I'd have 100mfd, 10V cap on the power feed. On my 4550 boards, I used 4MHz xtals (still have tons from 16C84 days), though 12MHz should be fine (be sure 'mode' is right HS, I think...)

Now I'm using 18f46k22 with the internal osc. at 16MHz on breadboards and they're fine...

You 'might' have a bad xtal, try another 12 or 8 or 4 or ?? you have.
It'll be a process of elimination...hopefully not an ordeal.
Please report back when the 'cure' is found...always nice to know why!

hth
jay
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Mon Dec 03, 2012 7:49 pm     Reply with quote

- Replaced xtal by another type (was L4 now UM1) @ 12 mhz
- Replaced 22 pf ceramic caps by 15 pf
- Grounded the xtal metal case

Code:

void main()
{
usb_cdc_init();
usb_init();

   while(1)
   {
   usb_task();
   usb_cdc_putc("#");
   delay_ms(1000);
   }
}


USB connect well and transmit but hang after a few minutes :(

UPDATE: after reading docs of the compiler

Quote:
usb_task()
If you use connection sense, and the usb_init_cs() for initialization, then you must periodically call this function to keep an eye on the connection sense pin. When the PIC is connected to the BUS, this function will then perpare the USB peripheral. When the PIC is disconnected from the BUS, it will reset the USB stack and peripheral. Will enable and use the USB interrupt.


I don't use the sense pin ...

I'll try with usb_task removed

UPDATE #2:

USB disconnect and reconnect itself ... I suspect noise in power vcc ou vusb.
I'll check with my scope
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Mon Dec 03, 2012 8:41 pm     Reply with quote





I see alot of ripple on the Vusb (cyan)

5v is yellow

Should i increase or change caps on the Vusb?
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Tue Dec 04, 2012 1:59 am     Reply with quote

Seriously, use connection sense.
CS, is actually required by the USB specs. A device is required to not enable it's transceivers, unless it sees power on the bus. It is OK to not have it, if you use a device powered by the bus (since then the presence of the 5v enables the chip and the transceivers), but otherwise CS is technically required.
Like a lot of things on USB, many devices 'get away' with this, but it causes problems later, when the CPU puts the bus to sleep, and it then doesn't wake up again, because the transceivers haven't switched off, and the devices don't re-negotiate properly....
Big capacitors on Vusb, are bad, and can damage the regulator. The spec originally suggested 220nF, but they later suggested 470nF. Something with good HF characteristics though. I'd suggest polyester. Generally polymer capacitors are better than traditional 'electrolytic', but do not have the HF responses of types like polyester. Put a 0.1uF ceramic in parallel with your polymer capacitor, to improve the HF response.

Best Wishes
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Tue Dec 04, 2012 4:21 pm     Reply with quote

Thanks Ttelmah!

I have a major problem and i think its the root of all my problems...

i've redone the prototype 2 times on 2 different veroboard. Same problem with the usb.... exactly the same prototype and components was used on a Breadboard works #1 with the usb (no random disconnect reconnect same code).

but...

when i touch the ground or any parts that are grounded the USB work #1... when i plug my scope on the ground the board works and i hear the usb connect sound

if i remove my finger from ground i get USB Unrecognized device popup in the tray... i retouch with my finger the ground, device work fine...

How to solve this ground problem ???

Everything is grounded on the GND pin (4) of USB B connector
Only power source is the 5V from the usb pin (1)

Flux has been cleaned more than once to be sure, checked solder bridge, bad joints found nothing!

Any clues?

Thanks
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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