Hi Philip,
I am trying to run @ VLPR, using external crystal (32768 Hz). Using KL05, Freedom board, codewarrior IDE, debugging yet.
My application is very simple (1 pulse per revolution enconder, max 400 RPM, 2 channels, numeric display). At initialization, I try to use VLPR, and if no user action after 20s, it goes to VLLS0, reseting (LWU), when user push button.
At the datasheet, I found example moving from FEI to BLPE but using 2MHz crystal. (But it should work!)
At the AN4503, you advise to change internal SLOW 32k clock to FAST internal clock (2 MHz). But how is minimun crystal clock, using external reference?
Using debug codewarrior IDE, after write MCG_C1, and wait for MCG_S, debug goes off (can't read registers, run, pause,stop, anything!)
I wounder if it is a very low frequency to use, or if is only a FRDM board limitation, or anything else.
Example:
void sysinit (void)
{
unsigned char frdiv_val;
unsigned char temp_reg;
short i;
/* Enable all of the port clocks. These have to be enabled to configure
* pin muxing options, so most code will need all of these on anyway.
*/
SIM_SCGC5 |= (SIM_SCGC5_PORTA_MASK
| SIM_SCGC5_PORTB_MASK
);
if (PMC_REGSC & PMC_REGSC_ACKISO_MASK)
PMC_REGSC |= PMC_REGSC_ACKISO_MASK;
/* Ramp up the system clock */
/* Set the system dividers */
SIM_CLKDIV1 = ( 0
| SIM_CLKDIV1_OUTDIV1(0)
| SIM_CLKDIV1_OUTDIV4(1) );
// configure the MCG_C2 register
// the RANGE value is determined by the external frequency. Since the RANGE parameter affects the FRDIV divide value
// it still needs to be set correctly even if the oscillator is not being used
temp_reg = MCG_C2;
temp_reg &= ~(MCG_C2_RANGE0_MASK | MCG_C2_HGO0_MASK | MCG_C2_EREFS0_MASK); // clear fields before writing new values
temp_reg |= (MCG_C2_RANGE0(0) | (LOW_POWER << MCG_C2_HGO0_SHIFT) | (CLK0_TYPE << MCG_C2_EREFS0_SHIFT));
MCG_C2 = temp_reg;
// Select external oscilator and Reference Divider and clear IREFS to start ext osc
// If IRCLK is required it must be enabled outside of this driver, existing state will be maintained
// CLKS=2, FRDIV=frdiv_val, IREFS=0, IRCLKEN=0, IREFSTEN=0
temp_reg = MCG_C1;
temp_reg &= ~(MCG_C1_CLKS_MASK | MCG_C1_FRDIV_MASK | MCG_C1_IREFS_MASK); // Clear values in these fields
temp_reg |= (MCG_C1_CLKS(2) | MCG_C1_FRDIV(frdiv_val)); // Set the required CLKS and FRDIV values
MCG_C1 = temp_reg;
// if the external oscillator is used need to wait for OSCINIT to set
for (i = 0 ; i < 10000 ; i++)
{
if (MCG_S & MCG_S_OSCINIT0_MASK) break; // jump out early if OSCINIT sets before loop finishes
}
if (!(MCG_S & MCG_S_OSCINIT0_MASK)) while(1); // check bit is really set and return with error if not set
// wait for Reference clock Status bit to clear
for (i = 0 ; i < 2000 ; i++)
{
if (!(MCG_S & MCG_S_IREFST_MASK)) break; // jump out early if IREFST clears before loop finishes
}
if (MCG_S & MCG_S_IREFST_MASK) while(1); // check bit is really clear and return with error if not set
// Wait for clock status bits to show clock source is ext ref clk
for (i = 0 ; i < 2000 ; i++)
{
if (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) == 0x2) break; // jump out early if CLKST shows EXT CLK slected before loop finishes
}
if (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0x2) while(1); // check EXT CLK is really selected and return with error if not
// Now in FBE
// It is recommended that the clock monitor is enabled when using an external clock as the clock source/reference.
// It is enabled here but can be removed if this is not required.
// MCG_C6 |= MCG_C6_CME0_MASK;
//MCG_C6 |= MCG_C6_CME0_MASK;
// continue...
Thank you,
Rodrigo