【RTD400 MCAL 3】 K312 MCU clock system configuration
1. Abstract
This document is talking about how to configure the clock system in the MCU of the K3 chip MCAL. This topic was always disdainful to talk about when I was doing LLD before, because the clock system of K3 is too simple, with internal fast and slow clock sources, external fast and slow clock sources, a PLL multiplier, and then various core peripherals to share. K3's RM even made a few options to frame the rules. From the perspective of LLD, especially the perspective of S32DS CT configuration, it is even more concise and clear. Here is a CT picture to show it:
Fig 1
Fig 2
With such a clock system, you can generate code with just a few taps and pokes. However, LLD is too free, and MCAL often encounters problems. Therefore, I decided to spend some time to understand the entire clock system of this MCAL MCU. This article takes K312 as an example to explain. Other K3 series are similar.
2. Clock system theory and configuration
2.1 K312 clock system
From the clock chapter of RM, you can see the whole system block diagram:
Fig 3
This block diagram clearly shows the situation of each part. There are four clock sources:
Internal fast clock FIRC: 48MHz, +/-5% error, maximum startup time 25us
Internal slow clock SIRC: 32KHz, +/-10% error, maximum startup time 3ms
External fast clock FXOSC: 8-40MHz, startup stabilization time FXOSC_CTRL[EOCV] × 128
External slow clock SXOSC: 32.768KHz, startup stabilization time SXOSC_CTRL[EOCV] x 128
One PLL: input 8-40MHZ, VOC output 640M-1280Mhz, PLL_PHIn_CLK output 25-480MHz.
MUX_0: Output CORE_CLK, AIPS_PLAT_CLK, AIPS_SLOW_CLK, HSE_CLK, DCM_CLK
MUX_1: Output system timer STM0_CLK
MUX_3: Output FLEXCAN0-2 clock
MUX_4: Output FLEXCAN3-5 clock
MUX_5: Output CLKOUT_STANDBY
MUX_6: Output CLKOUT_RUN
MUX_11: Output TRACE_CLK
RTC_CLK: RTC clock
2.1.1 PLL
From the PLL perspective, we need to know which values the frequency multiplier is related to, which can be calculated using the following formula:
Fig 4
If it is an integer, the red box in the above figure is the common method, and this article will also use the above method to configure.
PLL_PHI is the clock output by the final PLL, which is provided to the MC corresponding to other MUXs for selection.
2.1.2 MUX_0 System
The MUX_0 system with details can be seen from RM:
Fig 5
As you can see, the clock source of MUX_0 can be two types: PLL or internal FIRC.
Then the core clock can be generated later, AIPS_PLAT_CLK, AIPS_SLOW_CLK, HSE_CLK, DCM_CLK.
So what is the specific frequency of the generated clock? In principle, it can meet the maximum clock corresponding to each module, but the K3 series also makes some option recommendations. For example, K312 recommends using option B mode when RUN, especially the HSE clock, which usually needs to strictly meet the option recommendation.
2.1.3 MUX_6 Clock output
In order to check the corresponding clock situation in the chip, the corresponding clock can be output through the CLKOUT pin. The CLKOUT pin can correspond to the selection of multiple clock sources. The specific situation is as follows:
Fig 6
The yellow content in the figure is what K312's CLKOUT_RUN can support. After the clock is configured, the corresponding clock will be selected to test whether the output is consistent with the configuration.
2.1.4 option B Recommended Solution
In this article, K312 will configure the clock of option B in EB.
Fig 7
2.2 EB configuration
First, create a new K312 EB project. For the specific creation method, please refer to the previous article:
[S32K3 Tools Part] How to port RTD's existing MCAL demo to other K3 chips
This article will focus on the clock configuration corresponding to the MCU module based on RTD400 MCAL. For MCU configuration, two documents need to be consulted as reference books:
C:\NXP\SW32K3_S32M27x_RTD_R21-11_4.0.0\eclipse\plugins\Mcu_TS_T40D34M40I0R0\doc: RTD_MCU_UM.pdf and RTD_MCU_IM.pdf
If you don’t know how to configure, just follow the default values recommended by the document.
The following figure is an overview of the MCU. The main configured modules have the following three components:
General, McuClockSettingConfig, McuModeSettingConf
Fig 8
2.2.1 General configuration
In addition to Figure 8, you need to turn on the internal and external fast and slow clock control and PLL control, and add the corresponding API, as well as the crystal oscillator frequency. If this is not turned on, the corresponding configuration later will not be able to be configured.
Fig 9
2.2.2 McuClockSettingConfig configuration
This is the core area of MCU clock configuration, which includes clock source, PLL, and various MUX conditions.
First, you need to add a clock configuration:
Fig 10
Click in and there will be detailed configuration:
Fig 11
There are 17 items in total. You can keep the default configuration for options 1 and 6. Since the board does not connect to the external slow crystal oscillator 5, it is not configured. The rest should be configured according to the actual situation. The following explains them one by one:
2.2.2.1 McuFIRC configuration
Internal fast clock, 48MHz:
Fig 12
2.2.2.2 McuSIRC configuration
Internal slow clock 32Khz
Fig 13
2.2.2.3 McuFXOSC configuration
External crystal oscillator 16MHZ, fill in according to the actual connection situation.
Fig 14
2.2.2.4 McuCgm0ClockMux0 configuration
Mux0 configuration, here are configured core clock, AIPS_PLAT_CLK, AIPS_SLOW_CLK, HSE, DCM_CLK, is to meet the optionB requirements, and the clock comes from PLL_PHI0_CLK. When actually configuring, first configure the PLL clock to output the correct PLL_PHI0_CLK, PLL_PHI1_CLK clock.
Fig 15
2.2.2.5 McuCgm0ClockMux1 configuration
Fig 16
It can be configured according to the clock source required by the actual module.
2.2.2.6 McuCgm0ClockMux3 configuration
Configure the clock source of the FLEXCAN0-2 module:
Fig 17
2.2.2.7 McuCgm0ClockMux4 configuration
Configure the clock source of the FLEXCAN3-5 module:
Fig 18
2.2.2.8 McuCgm0ClockMux5 configuration
Configure the clock source of the CLKOUT_STANBY module:
Fig 19
2.2.2.9 McuCgm0ClockMux6 configuration
Configure the clock source of the CLKOUT_RUN module
Fig 20
2.2.2.10 McuCgm0ClockMux11 configuration
Configure the clock source of the TRACE_CLK module
Fig 21
2.2.2.11 McuRtcClockSelect configuration
Configure the clock source of the RTC module
Fig 22
2.2.2.12 McuPLL configuration
Configure the clock source of the PLL module
Fig 23
2.2.2.13 McuClockReferencePoint configuration
Configure the reference clock and the clock source selection interface of the peripheral modules.
Fig 24
At this point, the clock configuration is complete.
For verification, you can use the CLKOUT_RUN output to output the corresponding clock to pin PTD10 for viewing.
2.2.3 McuModeSettingConf configuration
In Mcu's McuModeSettingConf->McuPeripheral, you need to turn on the peripherals you want to use:
Fig 25
2.2.4 PORT configuration
Because the internal clock needs to be output to CLKOUT_RUN, K312's PTD10 MSCR106 is checked, so the PORT pin is added as follows:
Fig 26
3. Test Result
Next, on the S32K312-EVB board, we modify the clock source of EB's CLKOUT_RUN to test whether the clock matches the configuration.
Commonly used MCU-related drivers are as follows:
Fig 27
The calling sequence of system startup MCU initialization is as follows:
1). Mcu_Init()
2). Mcu_InitClock()
3). Mcu_GetPllStatus() - Till PLL is locked.
4). Mcu_DistributePllClock()
5). Mcu_SetMode()
6). Mcu_InitRamSection() - If required
The corresponding main code is as follows:
#include "Mcu.h"
#include "Mcu_Cfg.h"
#include "Port.h"
#include "Dio.h"
#include "Port_Cfg.h"
#include "Platform.h"
void TestDelay(uint32 delay);
void TestDelay(uint32 delay)
{
static volatile uint32 DelayTimer = 0;
while(DelayTimer < delay)
{
DelayTimer++;
}
DelayTimer = 0;
}
/**
* @brief Main function of the example
* @details Initialize the used drivers and uses the Icu
* and Dio drivers to toggle a LED on a push button
*/
int main(void)
{
uint8 count = 0U;
uint8 u8TimeOut = 100U;
/* Initialize the Mcu driver */
#if (MCU_PRECOMPILE_SUPPORT == STD_ON)
Mcu_Init(NULL_PTR);
#elif (MCU_PRECOMPILE_SUPPORT == STD_OFF)
Mcu_Init(&Mcu_Config_VS_0);
#endif /* (MCU_PRECOMPILE_SUPPORT == STD_ON) */
/* Initialize the clock tree and apply PLL as system clock */
Mcu_InitClock(McuClockSettingConfig_0);
#if (MCU_NO_PLL == STD_OFF)
while ( MCU_PLL_LOCKED != Mcu_GetPllStatus() )
{
}
Mcu_DistributePllClock();
#endif
/* Apply a mode configuration */
Mcu_SetMode(McuModeSettingConf_0);
/* Initialize all pins using the Port driver */
Port_Init(NULL_PTR);
/* Initialize Platform driver */
Platform_Init(NULL_PTR);
while (count++ < 10)
{
Dio_WriteChannel(DioConf_DioChannel_Digital_Output_LED_Q172, STD_HIGH);
Dio_WriteChannel(DioConf_DioChannel_Digital_Output_LED_Q257, STD_HIGH);
TestDelay(5000000);
Dio_WriteChannel(DioConf_DioChannel_Digital_Output_LED_Q172, STD_LOW);
Dio_WriteChannel(DioConf_DioChannel_Digital_Output_LED_Q257, STD_LOW);
TestDelay(5000000);
}
// Exit_Example(TRUE);
return (0U);
}
#ifdef __cplusplus
}
#endif
3.1 CLKOUT FIRC_CLK DIV2
Fig 28
It can be seen that the original 48Mhz clock of FIRC is divided by 2 and the clock waveform of 24Mhz is obtained, which is correct!
3.2 CLKOUT SIRC_CLK DIV2
Fig 29
It can be seen that the original 32Khz clock of SIRC is divided by 2 and the clock waveform of 16khz is obtained, which is correct!
3.3 CLKOUT FXOSC_CLK DIV10
Fig 30
It can be seen that the original 16Mhz clock of FXOSC is divided by 10 and the clock waveform of 1.6Mhz is obtained.
3.4 CLKOUT PLLPH0 CLK DIV10
Fig 31
It can be seen that the original 120Mhz clock of PLLPH0 is divided by 10 and the 12Mhz clock waveform is obtained, which is correct.
3.5 CLKOUT CORE CLK DIV10
Fig 32
It can be seen that the original 120Mhz clock of CORE is divided by 10 and the 12Mhz clock waveform is obtained, which is correct.
3.6 CLKOUT PLLPH1 CLK DIV4
Fig 33
It can be seen that the original 48Mhz clock of PLLPH1 is divided by 4 and the 12Mhz clock waveform is obtained.
3.7 CLKOUT HSE CLK DIV10
Fig 34
It can be seen that the original 60Mhz clock of HSE is divided by 10 and the clock waveform of 6Mhz is obtained, which is correct.
3.8 CLKOUT AIPS_PLAT CLK DIV10
Fig 35
It can be seen that the original 60Mhz clock of AIPS_PLAT_CLK is divided by 10 and the clock waveform of 6Mhz is obtained, which is correct.
3.9 CLKOUT AIPS_SLOW CLK DIV10
Fig 36
It can be seen that the original 30Mhz clock of AIPS_SLOW_CLK is divided by 10 and the clock waveform of 3Mhz is obtained, which is correct.
記事全体を表示