11 years, 6 months ago.

Enerergy and KL25Z

Hello everyone and congratulations for your work.

I work on the KL25 and I think it has great potential with mbed

I found some problems with this uC, I think this is the most complicated in the world uc to change its clock, I stumble and I can not do it, I would and I must not be the only to switch from 48Mhz to 32K768 to its internal clock in order to mount the battery power supply.

Fresscale always speak with acronimes : FLL, FEI, FEE, FBE, FBI, PEE, PBE, BLPI,BLPE, and i do not arrive to change from one mode to an other with "mcg.c" from freescale.

I try somme function like :

mcg_clk_hz = fbe_fbi(4000000, 1); mcg_clk_hz = fbi_blpi(32768, 1);

All return an error.

I do not know wich mode clock the library put the uC : FEE, FBE, BLPE, FPE, PEE ?

After in case of succes i know than you must call : SystemCoreClockUpdate ();

i succefuly write two smal funtions to put the uC in wait or sleep for the KEIL compiler :

void wait() { SCB_SCR &= SCB_SCR_SLEEPDEEP_MASK; wfi(); }

void stop() { SCB_SCR |= SCB_SCR_SLEEPDEEP_MASK; wfi(); }

Please, if somebody have more informations, or want to work we me to the problem, i thinks it is a good functions to put in a new library.

Thanks you so much for your help

Best regards

Hello, what is your default clocks? KL25Z in mbed is by default in PEE mode as you can see here: https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/targets/cmsis/TARGET_Freescale/TARGET_KL25Z/system_MKL25Z4.c You used wrong functions. What is your target mode? Please check KL25Z reference manual, table MCGOUTCLK Frequency Calculation Options. What frequency is required for your application? I would say BLPI (Bypassed low power internal). There is even an example in RM how to do it : 24.5.3.2 Example 2: Moving from PEE to BLPI mode: MCGOUTCLK frequency =32 kHz.

Regards, c0170

posted by Martin Kojtal 04 Jul 2013

2 Answers

11 years, 3 months ago.

I've just gotten the KL25Z to run at 48 MHz from the internal 32.786 kHz oscillator. Here's what I did:

I imported the mbed-src. Then I navigated to: mbed-src/targets/cmsis/TARGET_Freescale/TARGET_KL25Z/system_MKL25Z4.c

I changed the the line where CLOCK_SETUP is defined to:

#define CLOCK_SETUP     0 

Then down in the function SystemInit, I changed the default CLOCK_SETUP==0 block as follows:

#if (CLOCK_SETUP == 0)
  /* SIM->CLKDIV1: OUTDIV1=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,OUTDIV4=1,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */
  SIM->CLKDIV1 = (uint32_t)0x00010000UL; /* Update system prescalers */
  /* Switch to FEI Mode */
  /* MCG->C1: CLKS=0,FRDIV=0,IREFS=1,IRCLKEN=1,IREFSTEN=0 */
  MCG->C1 = (uint8_t)0x06U;
  /* MCG_C2: LOCRE0=0,??=0,RANGE0=0,HGO0=0,EREFS0=0,LP=0,IRCS=0 */
  MCG->C2 = (uint8_t)0x00U;
  /* MCG->C4: DMX32=1,DRST_DRS=1 [32k up to 48 MHz ] */
  MCG->C4 = (uint8_t)((MCG->C4 
            & (uint8_t)~(uint8_t)( MCG_C4_DRST_DRS(0x02))) 
            | (uint8_t)( MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS(0x01)));                    

  /* OSC0->CR: ERCLKEN=1,??=0,EREFSTEN=0,??=0,SC2P=0,SC4P=0,SC8P=0,SC16P=0 */
  OSC0->CR = (uint8_t)0x80U;
  /* MCG->C5: ??=0,PLLCLKEN0=0,PLLSTEN0=0,PRDIV0=0 */
  MCG->C5 = (uint8_t)0x00U;
  /* MCG->C6: LOLIE0=0,PLLS=0,CME0=0,VDIV0=0 */
  MCG->C6 = (uint8_t)0x00U;
  while((MCG->S & MCG_S_IREFST_MASK) == 0x00U) { /* Check that the source of the FLL reference clock is the internal reference clock. */
  }
  while((MCG->S & 0x0CU) != 0x00U) {    /* Wait until output of the FLL is selected */
  }

I verified on an oscilloscope that the frequency a pin wiggles is the same frequency as it does when CLOCK_SETUP is set to 1. At first it was running at 41 MHz, so I had to finesse the code above to run at the full 48 MHz.

Thanks again for your code

If I put the CLOCK_SETUP 0, the ticker do not work, the cod never go in interrupt finction. I just replace by CLOCK_SETUP 1 and the ticker work fine. Do you test with ticker ? Do you have any suggestion ?

Best regards

posted by Michel DURAND 05 Nov 2013

Where did you make the modification of CLOCK_SETUP? Did you import the copy of the mbed library I modified (search for mbed-src-FLL48) or did you do it from scratch on your end?

I just tried the ticker example (https://mbed.org/users/mbed_official/code/FRDM_Ticker/) with the copy of mbed-src-FLL48 I published on a FRDM board and the light blinks.

The true test would be to load it onto one of the boards I made that has no external crystal, but I don't have one with me at the moment. I can do that later today.

posted by david dicarlo 05 Nov 2013

Ok, I tested on a board with no crystal using my modified library and it indeed does not work the same as when using the crystal. I was mistaken above, it was actually not working but I did not realize that until I ran it with the default library and saw different behaviour of the flashing LEDs than when running off the internal oscillator only.

There seems to be some inconsistency between the internal oscillator settings and the default external settings. I've not been able to get to the root of it because of lack of time, but even the Serial connection through the OpenSDA debugger does not work when operating on the FLL.

posted by david dicarlo 05 Nov 2013

Thanks for your test.

I thinks than the uart and the ticker is cloked by OSCERCLK (only extern oscilator) and not by the MCGOUTCLK, but i do not know how modify the library to change the clocking. Best regards

posted by Michel DURAND 07 Nov 2013

I created a demo project showing the issue and submitted it to the Freescale technical support team. I will update here once I get more info.

posted by david dicarlo 07 Nov 2013

Hello there,

can you share that demo with some description? Is there an issue with mbed libraries or just KL25Z? Leave a comment with a link here, or pm me.

Regards,
0xc0170

posted by Martin Kojtal 07 Nov 2013
11 years, 5 months ago.

Thanks you so much for your answer.

Yes i want to run from 48Mhz (Mbed library) to 32k768 from internal oscilator

I try this code and i thinks that the oscilator go down

Moving from PEE to BLPI first move from PEE to PBE MCG_C1 |= MCG_C1_CLKS(2); select external reference clock as MCG_OUT Wait for clock status bits to update indicating clock has switched while (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0x2){} now move to FBE mode make sure the FRDIV is configured to keep the FLL reference within spec. MCG_C1 &= MCG_C1_FRDIV_MASK; clear FRDIV field MCG_C1 |= MCG_C1_FRDIV(3); set FLL ref divider to 256 MCG_C6 &= MCG_C6_PLLS_MASK; clear PLLS to select the FLL while (MCG_S & MCG_S_PLLST_MASK){} Wait for PLLST status bit to clear to indicate switch to FLL output now move to FBI mode MCG_C2 |= MCG_C2_IRCS_MASK; set the IRCS bit to select the fast IRC set CLKS to 1 to select the internal reference clock keep FRDIV at existing value to keep FLL ref clock in spec. set IREFS to 1 to select internal reference clock

MCG_C1 = MCG_C1_CLKS(1) | MCG_C1_FRDIV(3) | MCG_C1_IREFS_MASK; wait for internal reference to be selected while (!(MCG_S & MCG_S_IREFST_MASK)){} wait for fast internal reference to be selected while (!(MCG_S & MCG_S_IRCST_MASK)){} wait for clock to switch to IRC while (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0x1){} now move to BLPI MCG_C2 |= MCG_C2_LP_MASK; set the LP bit to enter BLPI

SIM_CLKDIV1 = ( 0
SIM_CLKDIV1_OUTDIV1(0)
SIM_CLKDIV1_OUTDIV4(1) );

SystemCoreClockUpdate ();

But I do not konw if the external oscilator if off (for the energy) and the SystemCoreClockUpdate (); do not update the calcul of the mbed library.

For example i must divide by 1468 the ticker to obtain 100mS : cent_milisecondes.attach(&ticker, (.5)/1468 ); 10 Hz

The 1468 is 48 000 000 Hz / 32768 Hz

Thanks a lot

Best regards