Evgeny Erenburg

Wrong clock settings on K70.

Discussion created by Evgeny Erenburg on Nov 2, 2016
Latest reply on Nov 3, 2016 by Kerry Zhou

I work with TWR-K70F120M.

I have an IAR project. The clocks calculated as in sysinit().

 

#elif defined (SYNCH_MODE) 
       /* Set the system dividers */
       /* NOTE: The PLL init will not configure the system clock dividers,
        * so they must be configured appropriately before calling the PLL
        * init function to ensure that clocks remain in valid ranges.
        */ 
         SIM_CLKDIV1 = ( 0
                        | SIM_CLKDIV1_OUTDIV1(0)
                        | SIM_CLKDIV1_OUTDIV2(2)
                        | SIM_CLKDIV1_OUTDIV3(2)
                        | SIM_CLKDIV1_OUTDIV4(5) );
 
       /* Initialize PLL1 */
       /* PLL1 will be the source MCGOUT and the DDR controller */  
       mcg_clk_hz = pll_init(OSCINIT, /* Don't init the osc circuit, already done */
                                 OSC_0,      /* Use CLKIN0 as the input clock */
                                 CLK0_FREQ_HZ,  /* CLKIN0 frequency */
                                 LOW_POWER,     /* Set the oscillator for low power mode */
                                 CLK0_TYPE,     /* Crystal or canned oscillator clock input */
                                 PLL_1,         /* PLL to initialize, in this case PLL1 */
                                 PLL1_PRDIV,    /* PLL predivider value */
                                 PLL1_VDIV,     /* PLL multiplier */
                                  MCGOUT);   /* Don't use the output from this PLL as the MCGOUT */

       /* Check the value returned from pll_init() to make sure there wasn't an error */
       if (mcg_clk_hz < 0x100)
         while(1);

       /* Initialize PLL0 */
       /* PLL0 is initialized, but not used as the MCGOUT */
       pll_0_clk_khz = (pll_init(NO_OSCINIT,   /* Initialize the oscillator circuit */
                             OSC_0,     /* Use CLKIN0 as the input clock */
                             CLK0_FREQ_HZ,  /* CLKIN0 frequency */
                             LOW_POWER,     /* Set the oscillator for low power mode */
                             CLK0_TYPE,     /* Crystal or canned oscillator clock input */
                             PLL_0,         /* PLL to initialize, in this case PLL0 */
                             PLL0_PRDIV,    /* PLL predivider value */
                             PLL0_VDIV,     /* PLL multiplier */
                             PLL_ONLY) / 1000);       /* Use the output from this PLL as the MCGOUT */

       /* Check the value returned from pll_init() to make sure there wasn't an error */
       if ((pll_0_clk_khz * 1000) < 0x100)
         while(1);

       pll_1_clk_khz = mcg_clk_hz / 1000;      
      
    #else
        #error "A PLL configuration for this platform is NOT defined"
    #endif
         
 /*
         * Use the value obtained from the pll_init function to define variables
  * for the core clock in kHz and also the peripheral clock. These
  * variables can be used by other functions that need awareness of the
  * system frequency.
  */
    mcg_clk_khz = mcg_clk_hz / 1000;
   core_clk_khz = mcg_clk_khz / (((SIM_CLKDIV1 & SIM_CLKDIV1_OUTDIV1_MASK) >> 28)+ 1);
   periph_clk_khz = mcg_clk_khz / (((SIM_CLKDIV1 & SIM_CLKDIV1_OUTDIV2_MASK) >> 24)+ 1);

   /* For debugging purposes, enable the trace clock and/or FB_CLK so that
    * we'll be able to monitor clocks and know the PLL is at the frequency
    * that we expect.
    */
 trace_clk_init();
   fb_clk_init();
       
        /* Initialize the DDR if the project option if defined */
        #ifdef DDR_INIT
          twr_ddr2_script_init();
        #endif

 

For TWR-K70F120M clock should be 120 Mhz. But I get 150 Mhz.

mcg_clk_khz = 150000

core_clk_khz  = 150000

periph_clk_khz  = 50000

I have another IAR project. In the second project I get the right value - 120 Mhz.

mcg_clk_khz = 120000

core_clk_khz  = 120000

periph_clk_khz  = 60000

Comparing these functions - sysinit() I didn't find any difference. What could be a problem?

Outcomes