Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 08, 2016, 07:00:22 07:00


Login with username, password and session length


Pages: [1]
Print
Author Topic: PIC32MX250F128 USB operation without crystal  (Read 2861 times)
0 Members and 1 Guest are viewing this topic.
Skarden
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 11
-Receive: 14


« on: March 07, 2014, 06:31:43 18:31 »

Hello,
please find here a nice trick permitting to use the USB on the PIC32MX2xx without using the external quartz.
The idea is to use the internal oscillator (at 8Mhz) and enable the reference clock output (divided by 2 so 4 Mhz).
Connecting this output (the REFCLKO signal) to the CLKI input using a wire, this clock can be routed to the PLL enabling it.
For sure, you still need 2 pins of the PIC (the REFCLKO and the CLKI) but you can remove the crystal.
Just for information this idea is not mine ...

Please find here the needed code to implement :

In the configuration file (ex. HardwareProfile.h)
Code:
//#define USB_USE_CRYSTAL // 8Mhz, in this case

/** CONFIGURATION **************************************************/
#pragma config UPLLEN   = ON        // USB PLL Enabled
#pragma config FPBDIV   = DIV_1         // Peripheral Clock divisor
#pragma config FWDTEN   = OFF           // Watchdog Timer
#pragma config WDTPS    = PS1           // Watchdog Timer Postscale
#pragma config OSCIOFNC = OFF           // CLKO Enable
#pragma config IESO     = OFF           // Internal/External Switch-over
#pragma config FSOSCEN  = OFF           // Secondary Oscillator Enable (KLO was off)
#pragma config CP       = OFF           // Code Protect
#pragma config BWP      = OFF           // Boot Flash Write Protect
#pragma config PWP      = OFF           // Program Flash Write Protect
#pragma config ICESEL   = ICS_PGx1      // ICE/ICD Comm Channel Select
#pragma config JTAGEN   = OFF           // JTAG Enable (JTAG Port Disabled)
#pragma config PMDL1WAY = OFF           // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
#pragma config IOL1WAY  = OFF           // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
#pragma config FUSBIDIO = OFF            // USB USID Selection (Controlled by the USB Module)
#pragma config FVBUSONIO= OFF            // USB VBUS ON Selection (Controlled by USB Module)


#if defined(USB_USE_CRYSTAL)

    #pragma config FPLLMUL  = MUL_20        // PLL Multiplier
    #pragma config UPLLIDIV = DIV_2         // USB PLL Input Divider
    #pragma config FPLLIDIV = DIV_2         // PLL Input Divider
    #pragma config FPLLODIV = DIV_2         // PLL Output Divider
    #pragma config FCKSM    = CSDCMD        // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
    #pragma config FNOSC    = PRIPLL        // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
    #pragma config POSCMOD  = HS            // Primary Oscillator Configuration (HS osc mode)
#else
    // specific setting useful to remove the crystal for the usb
    //
    #pragma config FPLLMUL = MUL_20         // PLL Multiplier
    #pragma config UPLLIDIV = DIV_1         // USB PLL Input Divider
    #pragma config FPLLIDIV = DIV_1         // PLL Input Divider
    #pragma config FPLLODIV = DIV_2         // PLL Output Divider
    #pragma config FCKSM = CSECMD           // Clock Switching and Monitor Selection (Clock Switch Enable, FSCM Disabled)
    #pragma config FNOSC = FRC              // Oscillator Selection Bits (Fast RC Osc (FRC))
    #pragma config POSCMOD  = EC            // Primary Oscillator Configuration (External clock mode)
#endif


and the code in main.c
Code:
#ifndef USB_USE_CRYSTAL
    // this setting is useful to remove the crystal
    // the usb can work without it connecting RPA4(pin12) to CLKI(pin9)
    PPSUnLock;                        // Allow PIN Mapping
        PPSOutput(3, RPA4, REFCLKO);
    PPSLock;
    int intStat, dmaSusp;
    OSCREFConfig(OSC_REFOCON_FRC, OSC_REFOCON_ON | OSC_REFOCON_OE, 1); // activate the reference clock output
    mSYSTEMUnlock(intStat, dmaSusp);
        OSCCONbits.NOSC = 0b011;    // set Primary Oscillator with PLL module (XTPLL, HSPLL or ECPLL)
        OSCCONbits.OSWEN = 1;   // initiate the oscillator switch
    mSYSTEMLock(intStat, dmaSusp);
#endif

You need to connect the pin 12 to the pin 9 to route the clock signal.

Hope this can be useful for someone of you.

« Last Edit: March 07, 2014, 06:37:06 18:37 by Skarden » Logged
gan_canny
Junior Member
**
 Warned
Offline Offline

Posts: 85

Thank You
-Given: 98
-Receive: 25


« Reply #1 on: March 07, 2014, 07:59:46 19:59 »

USB has very very very strict timing.....often the internal oscillator of a PIC is never accurate enough for reliable usb communications....The internal oscillator of the PIC often drifts off frequency as the pic chip heats up. If  reliability can be sacrificed for the cost avoidance of buying a crystal then perhaps this is idea is somewhat useful.
Logged
Magnox
V.I.P
Active Member
*****
Offline Offline

Posts: 208

Thank You
-Given: 775
-Receive: 238


Oink!


« Reply #2 on: March 07, 2014, 08:34:34 20:34 »

Hmm, nice trick!

Have you tried it, and is the resulting clock actually accurate enough for USB?

I wonder because the accuracy of the 8MHz internal oscillator is only (I think) +/- 2%. The microchip parts that do this trick internally themselves use the incoming USB signal to apply an automatic adjustment to the oscillator to get it within 0.25%.
Logged
Skarden
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 11
-Receive: 14


« Reply #3 on: March 07, 2014, 10:25:46 22:25 »

Sure, I tried and it works very well.
Logged
rsync
Newbie
*
Offline Offline

Posts: 16

Thank You
-Given: 35
-Receive: 9



« Reply #4 on: March 08, 2014, 09:54:30 09:54 »

Me too, work fine.
Logged
Gallymimu
Hero Member
*****
Offline Offline

Posts: 579

Thank You
-Given: 101
-Receive: 151


« Reply #5 on: March 09, 2014, 05:39:23 05:39 »

As  mentioned above this is a terrible idea to use an RC oscillator inside of the processor for any asynchronous timed signals (USB, UART etc.)  Sure it will work fine some times, maybe most of the time but not always.  Certainly this is only a trick suitable for a hobby application where you can afford half a day tracking down a timing error.

Think about it, don't you think that they'd dump the cost of crystals in mass produced products if it was possible to use the cheaper oscillator approach reliably?
Logged
Skarden
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 11
-Receive: 14


« Reply #6 on: March 09, 2014, 08:52:26 08:52 »

I know, but this is the fun side of the history.....
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC