23 #define OSC_CLKSW_TIMEOUT 50000
64 volatile int16_t retval = 0;
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);
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)
195 volatile uint16_t _n=0;
196 volatile uint16_t err=0;
204 if(OSCCONbits.COSC != 0b000)
207 __builtin_write_OSCCONH(OSCCON_xOSC_FRC);
209 __builtin_write_OSCCONL((OSCCON & 0x7E) | 0x01);
216 if ((OSCCONbits.COSC != osc_config.osc_type) && (OSCCONbits.CLKLOCK == 0))
220 #if defined (__P33SMPS_CH__) || defined (__P33SMPS_CK__)
225 PLLDIVbits.VCODIV = osc_config.VCODIV;
226 CLKDIVbits.PLLPRE = osc_config.N1;
227 PLLFBDbits.PLLFBDIV = osc_config.M;
228 PLLDIVbits.POST1DIV = osc_config.N2;
229 PLLDIVbits.POST2DIV = osc_config.N3;
231 #elif defined (__P33SMPS_FJ__) || defined (__P33SMPS_FJA__) || defined (__P33SMPS_FJC__) || \
232 defined (__P33SMPS_EP__)
234 CLKDIVbits.PLLPRE = osc_config.N1;
235 PLLFBD = (osc_config.M - 2);
236 CLKDIVbits.PLLPOST = osc_config.N2;
241 __builtin_write_OSCCONH(osc_config.osc_type);
242 if(OSCCONbits.COSC != OSCCONbits.NOSC)
245 __builtin_write_OSCCONL((OSCCON & 0x7F) | 0x01);
249 { err = OSCERR_CSF; }
253 else if ((OSCCONbits.COSC != osc_config.osc_type) && (OSCCONbits.CLKLOCK == 1))
260 OSCCONbits.CLKLOCK = 1;
264 { err = OSCERR_PLL_LCK; }
269 return((1 - OSCCONbits.CF));
305 APLLDIV1bits.AVCODIV = aux_clock_config.AVCODIV;
308 ACLKCON1bits.APLLPRE = aux_clock_config.N1;
309 APLLFBD1bits.APLLFBDIV = aux_clock_config.M;
310 APLLDIV1bits.APOST1DIV = aux_clock_config.N2;
311 APLLDIV1bits.APOST2DIV = aux_clock_config.N3;
314 ACLKCON1bits.FRCSEL = aux_clock_config.FRCSEL;
317 ACLKCON1bits.APLLEN = aux_clock_config.APLLEN;
320 if(!aux_clock_config.APLLEN)
323 return(ACLKCON1bits.APLLEN);
354 volatile uint16_t retval = 1;
355 volatile struct AUXOSC_CONFIG_s aux_clock_config;
358 aux_clock_config.FRCSEL = PLLDIV_ACLKCON_FRCSEL_FRC;
361 aux_clock_config.AVCODIV = APLLDIV_AVCODIV_FVCO_DIV_BY_4;
364 if(afpllo_frequency <= 800) {
365 aux_clock_config.N1 = ACLKCON_APLLDIV_N1_1;
366 aux_clock_config.M = (afpllo_frequency >> 2);
367 aux_clock_config.N2 = APLLDIV_POST2DIV_N2N3_2;
368 aux_clock_config.N3 = APLLDIV_POST2DIV_N2N3_1;
376 aux_clock_config.APLLEN = ACLKCON_APLLEN_ENABLED;
420 volatile int32_t freq=0;
421 volatile uint16_t vbuf=0;
422 volatile OSCCON_xOSC_TYPE_e otype;
425 freq = (
volatile int32_t)main_osc_frequency;
428 if (main_osc_frequency > 0) {
433 otype = OSCCONbits.COSC;
441 if ((otype == OSCCON_xOSC_FRC) || (otype == OSCCON_xOSC_BFRC) || (otype == OSCCON_xOSC_FRCPLL) || (otype == OSCCON_xOSC_FRCDIVN)) {
443 freq = (
volatile int32_t)OSC_FRC_FREQ;
445 #if defined (__P33SMPS_CK__) || defined (__P33SMPS_CH_MSTR__)
447 if(otype != OSCCON_xOSC_BFRC) {
448 freq += OSC_TUN_STEP_FREQUENCY * (
volatile int32_t)(OSCTUNbits.TUN);
454 if (otype == OSCCON_xOSC_FRCDIVN) {
455 vbuf = (CLKDIVbits.FRCDIV & 0x0003);
462 else if (otype == OSCCON_xOSC_LPRC) {
463 freq = (
volatile int32_t)32000;
468 if (freq == 0)
return(0);
475 if ( (otype == OSCCON_xOSC_FRCPLL) || (otype == OSCCON_xOSC_PRIPLL) ) {
478 if (!OSCCONbits.LOCK)
return(0);
481 vbuf = (CLKDIVbits.PLLPRE & 0x000F);
482 if((vbuf > 8) || (vbuf == 0))
return (0);
486 vbuf = (PLLFBDbits.PLLFBDIV & 0x00FF);
487 if((vbuf > 200) || (vbuf < 3))
return (0);
491 vbuf = (PLLDIVbits.VCODIV & 0x0003);
492 if(vbuf > 3)
return (0);
497 vbuf = (PLLDIVbits.POST1DIV & 0x0007);
498 if((vbuf > 8) || (vbuf == 0))
return (0);
502 vbuf = (PLLDIVbits.POST2DIV & 0x0007);
503 if((vbuf > 8) || (vbuf == 0))
return (0);
520 if (CLKDIVbits.DOZEN) {
521 vbuf = (CLKDIVbits.DOZE & 0x0003);
536 if (ACLKCON1bits.APLLEN) {
543 vbuf = (ACLKCON1bits.APLLPRE & 0x000F);
544 if((vbuf > 8) || (vbuf == 0))
return (0);
548 vbuf = (APLLFBD1bits.APLLFBDIV & 0x00FF);
549 if((vbuf > 200) || (vbuf < 3))
return (0);
553 vbuf = (APLLDIV1bits.AVCODIV & 0x0003);
554 if(vbuf > 3)
return (0);
559 vbuf = (APLLDIV1bits.APOST1DIV & 0x0007);
560 if((vbuf > 8) || (vbuf == 0))
return (0);
564 vbuf = (APLLDIV1bits.APOST2DIV & 0x0007);
565 if((vbuf > 8) || (vbuf == 0))
return (0);