Hi everyone, I hope you can help me to solve my doubts. They are silly doubts for you, for sure, but I started with the kinetis MCU some weeks ago and I don't know how to change it yet.
This is my case, I have a FRDMKL02Z kit and IAR as software development tool. I would like to change the clock configuration of my project.
I started with the "Hardware Timer Demo" example supplied by NXP, I have seen the NXP documentation and I know there are two different clock modules (MCG and OSC) and so on. But in the practical way I wanted to change my program for another clock configuration (I would like to have an interrupt every 10 microseconds and also enough time for signal processing between interrupts happen, if possible).
There is a "BOARD_ClockInit" function and also a "BOARD_InitOsc0" function to configure the OSC module, but how can I change these functions to configure this demo example for a desired frequency?
Thanks a lot in advance.
Solved! Go to Solution.
Hi Andres
The function BOARD_InitOsc0 is only configuring the system oscillator. OSC frequency, MCG range, enable capacitor. You have an external OSC and the frequency is 32.768 KHz.
// OSC0 configuration.
osc_user_config_t osc0Config =
{
.freq = OSC0_XTAL_FREQ,
.hgo = MCG_HGO0,
.range = MCG_RANGE0,
.erefs = MCG_EREFS0,
.enableCapacitor2p = OSC0_SC2P_ENABLE_CONFIG,
.enableCapacitor4p = OSC0_SC4P_ENABLE_CONFIG,
.enableCapacitor8p = OSC0_SC8P_ENABLE_CONFIG,
.enableCapacitor16p = OSC0_SC16P_ENABLE_CONFIG,
};
Now you have the frequency of the OSC and you will selected the configuration for the different states of the MCG.
CLOCK_SetBootConfig(&g_defaultClockConfigRun);
The next structure shows the configuration for enter run mode
const clock_manager_user_config_t g_defaultClockConfigRun =
{
.mcgConfig =
{
.mcg_mode = kMcgModeFEE, // Work in FEE mode.
.irclkEnable = true, // MCGIRCLK enable.
.irclkEnableInStop = false, // MCGIRCLK disable in STOP mode.
.ircs = kMcgIrcSlow, // Select IRC32k.
.fcrdiv = 0U, // FCRDIV is 0.
.frdiv = 0U,
.drs = kMcgDcoRangeSelMid, // Mid frequency range
.dmx32 = kMcgDmx32Fine, // DCO has a default range of 25%
},
.simConfig =
{
.outdiv1 = 0U,
.outdiv4 = 1U,
},
.oscerConfig =
{
.enable = true, // OSCERCLK enable.
.enableInStop = false, // OSCERCLK disable in STOP mode.
}
};
As you can see the mcg configuration is working in FEE mode, and selecting the OSCCLK. If you want to know or to modify the frequency, you should modified the values of drs and dmx.
The select values are drs = 1 (kMcgDcoRangeSelMid ) and dmx32 = 1 (kMcgDmx32Fine) and with this you have a frequency of 48MHz. The core is working with 48MHz.
DCO RANGE = 1464 * 32.768KHz = 47.972352 Mhz.
If you have problems with the frequency of MGC let me know
Have a great day
Mario
Hi Andres,
Did you run this code:C:\Freescale\KSDK_1.3.0\examples\frdmkl02z\demo_apps\hwtimer_demo\iar ?
This code is using the systick hardware timer, the max interrupt period is about 5.59s, when core is 48Mhz, and divide-by-16 of the core clock as the systick clock source, as you know, systick is a 24bit timer.
If you want to get the 10s period, you can configure the HWTIMER_PERIOD = 5000000
#define HWTIMER_PERIOD | 5000000 |
Then, the interrupt will enter in every 5 seconds, you can count the interrupt times.
Eg.
volatile unsigned char hwcnt=0;
void hwtimer_callback(void* data)
{
// PRINTF(".");
if ((HWTIMER_SYS_GetTicks(&hwtimer) % HWTIMER_DOTS_PER_LINE) == 0)
{
PRINTF("\r\n");
}
if ((HWTIMER_SYS_GetTicks(&hwtimer) % (HWTIMER_LINES_COUNT * HWTIMER_DOTS_PER_LINE)) == 0)
{
if (kHwtimerSuccess != HWTIMER_SYS_Stop(&hwtimer))
{
PRINTF("\r\nError: hwtimer stop.\r\n");
}
PRINTF("End\r\n");
}
hwcnt++;
if((hwcnt) == 2)
{
hwcnt=0;
PRINTF(".");
}
}
When hwcnt=2, it means, the 10s is reached, and it will printf a point.
You can try it.
Wish it helps you!
If you still have question, please let me know!
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi Jingjing,
Thank you very much for your time and help.
Answering to your first question, YES, I have run the hwtimer_demo code. But in my case, I want a timer interrupt period of 10 microseconds (NOT 10 seconds), so I configure the HWTIMER_PERIOD = 10:
#define HWTIMER_PERIOD | 10 |
Although I configure the HWTIMER_PERIOD = 10, it seems the interrupt does not occur at 10 microseconds, but I will try to understand it later, but my first issue is that I have used the default configuration of the hwtimer_demo example (I don't know if 48 Mhz core clock). How or in which function can I verify and/or change the clock configuration?
Thank you very much for your help Jingjing. I hope you can guide me on it.
Best regards
Hi Andres,
Sorry for the 10 seconds, if you use the 10ms, you should define it like this:
#defind HWRIMER_PERIOD 10000
Because the default period unit is us, you can't define it as 10.
If you never modify the KSDK code, the core is 47972352U, just as mario has told you.
Wish it helps you!
If you still have question, please contact me!
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Thank you for your support Jingjing. Fortunatelly I just fix the problem, and I understand it better with Mario explanations.
Have a great day.
Andres.
Hi Andres
The function BOARD_InitOsc0 is only configuring the system oscillator. OSC frequency, MCG range, enable capacitor. You have an external OSC and the frequency is 32.768 KHz.
// OSC0 configuration.
osc_user_config_t osc0Config =
{
.freq = OSC0_XTAL_FREQ,
.hgo = MCG_HGO0,
.range = MCG_RANGE0,
.erefs = MCG_EREFS0,
.enableCapacitor2p = OSC0_SC2P_ENABLE_CONFIG,
.enableCapacitor4p = OSC0_SC4P_ENABLE_CONFIG,
.enableCapacitor8p = OSC0_SC8P_ENABLE_CONFIG,
.enableCapacitor16p = OSC0_SC16P_ENABLE_CONFIG,
};
Now you have the frequency of the OSC and you will selected the configuration for the different states of the MCG.
CLOCK_SetBootConfig(&g_defaultClockConfigRun);
The next structure shows the configuration for enter run mode
const clock_manager_user_config_t g_defaultClockConfigRun =
{
.mcgConfig =
{
.mcg_mode = kMcgModeFEE, // Work in FEE mode.
.irclkEnable = true, // MCGIRCLK enable.
.irclkEnableInStop = false, // MCGIRCLK disable in STOP mode.
.ircs = kMcgIrcSlow, // Select IRC32k.
.fcrdiv = 0U, // FCRDIV is 0.
.frdiv = 0U,
.drs = kMcgDcoRangeSelMid, // Mid frequency range
.dmx32 = kMcgDmx32Fine, // DCO has a default range of 25%
},
.simConfig =
{
.outdiv1 = 0U,
.outdiv4 = 1U,
},
.oscerConfig =
{
.enable = true, // OSCERCLK enable.
.enableInStop = false, // OSCERCLK disable in STOP mode.
}
};
As you can see the mcg configuration is working in FEE mode, and selecting the OSCCLK. If you want to know or to modify the frequency, you should modified the values of drs and dmx.
The select values are drs = 1 (kMcgDcoRangeSelMid ) and dmx32 = 1 (kMcgDmx32Fine) and with this you have a frequency of 48MHz. The core is working with 48MHz.
DCO RANGE = 1464 * 32.768KHz = 47.972352 Mhz.
If you have problems with the frequency of MGC let me know
Have a great day
Mario
Thank you very much Mario. It has been very helpful for me.
Have a great day.
Andres.