23 #define OSC_CLKSW_TIMEOUT 50000 62 volatile uint16_t p33c_OscFrc_DefaultInitialize(
volatile enum CPU_SPEED_DEFAULTS_e cpu_speed)
64 volatile int16_t retval = 1;
65 volatile struct OSC_CONFIG_s osc;
67 osc.osc_type = OSCCON_xOSC_FRCPLL;
68 osc.N1 = CLKDIV_PLLDIV_N1_1;
72 case CPU_SPEED_20_MIPS:
73 osc.M = PLLFBD_PLLFBDIV_M_20;
75 case CPU_SPEED_30_MIPS:
76 osc.M = PLLFBD_PLLFBDIV_M_30;
78 case CPU_SPEED_40_MIPS:
79 osc.M = PLLFBD_PLLFBDIV_M_40;
81 case CPU_SPEED_50_MIPS:
82 osc.M = PLLFBD_PLLFBDIV_M_50;
84 case CPU_SPEED_60_MIPS:
85 osc.M = PLLFBD_PLLFBDIV_M_60;
87 case CPU_SPEED_70_MIPS:
88 osc.M = PLLFBD_PLLFBDIV_M_70;
90 case CPU_SPEED_80_MIPS:
91 osc.M = PLLFBD_PLLFBDIV_M_80;
93 case CPU_SPEED_90_MIPS:
94 osc.M = PLLFBD_PLLFBDIV_M_90;
96 case CPU_SPEED_100_MIPS:
97 osc.M = PLLFBD_PLLFBDIV_M_100;
103 osc.N2 = PLLDIV_POST2DIV_N2N3_2;
104 osc.N3 = PLLDIV_POST2DIV_N2N3_1;
106 retval &= p33c_Osc_Initialize(osc);
138 volatile uint16_t p33c_OscFrc_Initialize(
volatile enum CLKDIV_FRCDIVN_e frc_div,
volatile enum OSCTUN_TUN_e frc_tune)
140 #if defined (__P33SMPS_CH_MSTR__) || defined (__P33SMPS_CK__) 144 volatile uint16_t err=0;
149 OSCTUNbits.TUN = frc_tune;
152 CLKDIVbits.FRCDIV = frc_div;
192 volatile uint16_t p33c_Osc_Initialize(
volatile struct OSC_CONFIG_s osc_config)
194 volatile uint16_t retval=0;
195 volatile uint16_t _n=0;
203 if(OSCCONbits.COSC != 0b000)
206 __builtin_write_OSCCONH(OSCCON_xOSC_FRC);
208 __builtin_write_OSCCONL((OSCCON & 0x7E) | 0x01);
215 if ((OSCCONbits.COSC != osc_config.osc_type) && (OSCCONbits.CLKLOCK == 0))
219 #if defined (__P33SMPS_CH__) || defined (__P33SMPS_CK__) 224 PLLDIVbits.VCODIV = osc_config.VCODIV;
225 CLKDIVbits.PLLPRE = osc_config.N1;
226 PLLFBDbits.PLLFBDIV = osc_config.M;
227 PLLDIVbits.POST1DIV = osc_config.N2;
228 PLLDIVbits.POST2DIV = osc_config.N3;
230 #elif defined (__P33SMPS_FJ__) || defined (__P33SMPS_FJA__) || defined (__P33SMPS_FJC__) || \ 231 defined (__P33SMPS_EP__) 233 CLKDIVbits.PLLPRE = osc_config.N1;
234 PLLFBD = (osc_config.M - 2);
235 CLKDIVbits.PLLPOST = osc_config.N2;
240 __builtin_write_OSCCONH(osc_config.osc_type);
241 if(OSCCONbits.COSC != OSCCONbits.NOSC)
244 __builtin_write_OSCCONL((OSCCON & 0x7F) | 0x01);
248 { retval = OSCERR_CSF; }
252 else if ((OSCCONbits.COSC != osc_config.osc_type) && (OSCCONbits.CLKLOCK == 1))
259 OSCCONbits.CLKLOCK = 1;
263 { retval = OSCERR_PLL_LCK; }
267 return((1 - OSCCONbits.CF));
299 volatile uint16_t p33c_OscAuxClk_Initialize(
volatile struct AUXOSC_CONFIG_s aux_clock_config)
303 APLLDIV1bits.AVCODIV = aux_clock_config.AVCODIV;
306 ACLKCON1bits.APLLPRE = aux_clock_config.N1;
307 APLLFBD1bits.APLLFBDIV = aux_clock_config.M;
308 APLLDIV1bits.APOST1DIV = aux_clock_config.N2;
309 APLLDIV1bits.APOST2DIV = aux_clock_config.N3;
312 ACLKCON1bits.FRCSEL = aux_clock_config.FRCSEL;
315 ACLKCON1bits.APLLEN = aux_clock_config.APLLEN;
318 if(!aux_clock_config.APLLEN)
321 return(ACLKCON1bits.APLLEN);
350 volatile uint16_t p33c_OscAuxClk_DefaultInitialize(
volatile enum AUX_PLL_DEFAULTS_e afpllo_frequency)
352 volatile uint16_t retval = 1;
353 volatile struct AUXOSC_CONFIG_s aux_clock_config;
356 aux_clock_config.FRCSEL = PLLDIV_ACLKCON_FRCSEL_FRC;
359 aux_clock_config.AVCODIV = APLLDIV_AVCODIV_FVCO_DIV_BY_4;
362 if(afpllo_frequency <= 800) {
363 aux_clock_config.N1 = ACLKCON_APLLDIV_N1_1;
364 aux_clock_config.M = (afpllo_frequency >> 2);
365 aux_clock_config.N2 = APLLDIV_POST2DIV_N2N3_2;
366 aux_clock_config.N3 = APLLDIV_POST2DIV_N2N3_1;
374 aux_clock_config.APLLEN = ACLKCON_APLLEN_ENABLED;
377 retval &= p33c_OscAuxClk_Initialize(aux_clock_config);
414 volatile uint16_t p33c_Osc_SetExtFrequency(
volatile int32_t ext_osc_frequency)
416 volatile uint16_t retval=1;
418 SystemFrequencies.
fpri = ext_osc_frequency;
461 volatile uint16_t p33c_Osc_GetFrequencies(
void)
463 volatile int32_t freq=0;
464 volatile uint16_t vbuf=0;
465 volatile enum OSCCON_xOSC_TYPE_e otype;
468 otype = OSCCONbits.COSC;
471 SystemFrequencies.
frc = (
volatile int32_t)OSC_FRC_FREQ;
476 if ((otype == OSCCON_xOSC_FRC) || (otype == OSCCON_xOSC_BFRC) || (otype == OSCCON_xOSC_FRCPLL) || (otype == OSCCON_xOSC_FRCDIVN)) {
478 freq = (
volatile int32_t)OSC_FRC_FREQ;
480 #if defined (__P33SMPS_CK__) || defined (__P33SMPS_CH_MSTR__) 482 if(otype != OSCCON_xOSC_BFRC) {
483 freq += OSC_TUN_STEP_FREQUENCY * (
volatile int32_t)(OSCTUNbits.TUN);
484 SystemFrequencies.
frc = freq;
489 if (otype == OSCCON_xOSC_FRCDIVN) {
490 vbuf = (CLKDIVbits.FRCDIV & 0x0003);
497 else if (otype == OSCCON_xOSC_LPRC) {
498 freq = (
volatile int32_t)32000;
503 if (freq == 0)
return(0);
507 SystemFrequencies.
fclk = (
volatile uint32_t)freq;
510 if ( (otype == OSCCON_xOSC_FRCPLL) || (otype == OSCCON_xOSC_PRIPLL) ) {
513 if (!OSCCONbits.LOCK)
return(0);
516 vbuf = (CLKDIVbits.PLLPRE & 0x000F);
517 if((vbuf > 8) || (vbuf == 0))
return (0);
521 vbuf = (PLLFBDbits.PLLFBDIV & 0x00FF);
522 if((vbuf > 200) || (vbuf < 3))
return (0);
526 vbuf = (PLLDIVbits.VCODIV & 0x0003);
527 if(vbuf > 3)
return (0);
529 SystemFrequencies.
fvco = (
volatile uint32_t)(freq/vbuf);
532 vbuf = (PLLDIVbits.POST1DIV & 0x0007);
533 if((vbuf > 8) || (vbuf == 0))
return (0);
537 vbuf = (PLLDIVbits.POST2DIV & 0x0007);
538 if((vbuf > 8) || (vbuf == 0))
return (0);
544 SystemFrequencies.
fpllo = (
volatile uint32_t)freq;
548 SystemFrequencies.
fosc = (
volatile uint32_t)freq;
552 SystemFrequencies.
fp = (
volatile uint32_t)freq;
555 if (CLKDIVbits.DOZEN) {
556 vbuf = (CLKDIVbits.DOZE & 0x0003);
559 SystemFrequencies.
fcy = (
volatile uint32_t)freq;
562 SystemFrequencies.
tcy = 1.0/((float)SystemFrequencies.
fcy);
565 SystemFrequencies.
tp = 1.0/((float)SystemFrequencies.
fp);
571 if (ACLKCON1bits.APLLEN) {
574 if(ACLKCON1bits.FRCSEL) { freq = SystemFrequencies.
frc; }
575 else { freq = SystemFrequencies.
fpri; }
578 vbuf = (ACLKCON1bits.APLLPRE & 0x000F);
579 if((vbuf > 8) || (vbuf == 0))
return (0);
583 vbuf = (APLLFBD1bits.APLLFBDIV & 0x00FF);
584 if((vbuf > 200) || (vbuf < 3))
return (0);
588 vbuf = (APLLDIV1bits.AVCODIV & 0x0003);
589 if(vbuf > 3)
return (0);
591 SystemFrequencies.
afvco = (
volatile uint32_t)(freq/vbuf);
594 vbuf = (APLLDIV1bits.APOST1DIV & 0x0007);
595 if((vbuf > 8) || (vbuf == 0))
return (0);
599 vbuf = (APLLDIV1bits.APOST2DIV & 0x0007);
600 if((vbuf > 8) || (vbuf == 0))
return (0);
602 SystemFrequencies.
afpllo = freq;
606 SystemFrequencies.
afpllo = 0;
607 SystemFrequencies.
afvco = 0;
#define OSC_CLKSW_TIMEOUT
value to set the timeout for clock switching operations