chipKIT® Development Platform

Inspired by Arduino™

How to set up external clock on uC32 with MPLABX?

Created Sat, 11 Mar 2017 20:16:32 +0000 by riceman0


riceman0

Sat, 11 Mar 2017 20:16:32 +0000

Hello, I am writing code for my chipkit uC32 with MPLABX and a programmer.

I am writing my "hello world" program and I think the default settings for this chip are to use the fast internal oscillator, which is 8MHZ.

However the board specs say that the dev board has a 80MHZ speed, so I assume there is an oscillator on the board. I also assume that the CPU is typically configured to use the oscillator by the bootloader (?).

I'm not using the bootloader, just writing my own C code; so I was wondering, do you (or does anyone) have some sample C code that configures this CPU to use the external oscillator. It looks like this involves a few registers, and I'm not 100% confident I'll get this right. Some sample code would save me a ton of time and also educate me.

Appreciate any help.

*** Quick update... I did try this:

#pragma config FNOSC = PRI

and it seemed to speed up my chip slightly (judging by my LEDs blinking), but it was not the 10x (8MHZ to 80MHZ) jump I expected. So, yeah like I said I could use some help. :)


majenko

Sat, 11 Mar 2017 20:51:20 +0000

You need to set up both the oscillator and the PLL. The oscillator is an 8MHz crystal, so you need to get the PLL to multiply by 10.

The settings used in the bootloader are:

//*    Oscillator Settings
    #pragma config FNOSC    = PRIPLL                        // Oscillator selection
    #pragma config POSCMOD  = XT                            // Primary oscillator mode
    #pragma config FPLLIDIV = DIV_2                         // PLL input divider
    #pragma config FPLLMUL  = MUL_20                        // PLL multiplier
    #pragma config FPLLODIV = DIV_1                         // PLL output divider
    #pragma config FPBDIV   = DIV_1                         // Peripheral bus clock divider
    #pragma config FSOSCEN  = ON                            // Secondary oscillator enable

    //*    Clock control settings
    #pragma config IESO     = OFF                           // Internal/external clock switchover
    #pragma config FCKSM    = CSDCMD                        // Clock switching (CSx)/Clock monitor (CMx)
    #pragma config OSCIOFNC = OFF                           // Clock output on OSCO pin enable

    //*    Other Peripheral Device settings
    #pragma config FWDTEN   = OFF                           // Watchdog timer enable
    #pragma config WDTPS    = PS1024                        // Watchdog timer postscaler

    //*    Code Protection settings
    #pragma config CP       = OFF                           // Code protection
    #pragma config BWP      = OFF                           // Boot flash write protect
    #pragma config PWP      = OFF                           // Program flash write protect

    //*    Debug settings
    #pragma config ICESEL   = ICS_PGx2                      // ICE pin selection

It multiplies the clock by 20, then divides it by 2, yielding an 80MHz clock from an 8MHz crystal.


riceman0

Sat, 11 Mar 2017 23:25:33 +0000

Awesome, on first pass that looks like it did the trick! thanks!

Question... I omitted the watchdog settings, the code protection settings, and the ICE pin selection.

Do I need (1) the "secondary oscillator" and (2) the "clock output on OSC0" and clock switching/switchover?

I'm not sure what those do, so not sure if they're important.

Also... is the entire bootloader source publically available? There's not much sample code specifically for this CPU so I thought I'd learn a thing or two. I was looking for it but just found the hex.

Thanks again!


majenko

Sun, 12 Mar 2017 12:18:19 +0000

The secondary oscillator is only useful if you attach a 32768Hz crystal to the SOSCI and SOSCO pins for use with the internal RTCC. Pointless otherwise.

Clock output on OSCO can't be used with a crystal, only the internal FRC/LPRC or an external clock module. It outputs the internal clock on the OSCO pin so you can drive other things at the same frequency (or use it for debugging clock problems). Leave it off.

Clock switching is used for selecting different clock sources at runtime. Good for reducing power while you're not needing high speed, but complex to manage.

The bootloader we use is here: https://github.com/chipKIT32/PIC32-avrdude-bootloader - however it's not the bootloader that comes pre-programmed because Digilent want to use their own version. It is available somewhere, but I forget where.


EmbeddedMan

Sun, 12 Mar 2017 16:13:29 +0000

All Digilent bootloaders are available here (in source and hex form) : https://github.com/chipKIT32/chipKIT-digiboot

*Brian