1 /************************************************************************/
\r
2 /* (C) Fujitsu Semiconductor Europe GmbH (FSEU) */
\r
4 /* The following software deliverable is intended for and must only be */
\r
5 /* used for reference and in an evaluation laboratory environment. */
\r
6 /* It is provided on an as-is basis without charge and is subject to */
\r
8 /* It is the user's obligation to fully test the software in its */
\r
9 /* environment and to ensure proper functionality, qualification and */
\r
10 /* compliance with component specifications. */
\r
12 /* In the event the software deliverable includes the use of open */
\r
13 /* source components, the provisions of the governing open source */
\r
14 /* license agreement shall apply with respect to such software */
\r
16 /* FSEU does not warrant that the deliverables do not infringe any */
\r
17 /* third party intellectual property right (IPR). In the event that */
\r
18 /* the deliverables infringe a third party IPR it is the sole */
\r
19 /* responsibility of the customer to obtain necessary licenses to */
\r
20 /* continue the usage of the deliverable. */
\r
22 /* To the maximum extent permitted by applicable law FSEU disclaims all */
\r
23 /* warranties, whether express or implied, in particular, but not */
\r
24 /* limited to, warranties of merchantability and fitness for a */
\r
25 /* particular purpose for which the deliverable is not designated. */
\r
27 /* To the maximum extent permitted by applicable law, FSEU's liability */
\r
28 /* is restricted to intentional misconduct and gross negligence. */
\r
29 /* FSEU is not liable for consequential damages. */
\r
32 /************************************************************************/
\r
36 /** \file system_mb9af31x.c
\r
38 ** FM3 system initialization functions
\r
39 ** All adjustments can be done in belonging header file.
\r
42 ** 2011-05-16 V1.0 MWi original version
\r
43 ******************************************************************************/
\r
46 ******************************************************************************
\r
47 ** System Clock Frequency (Core Clock) Variable according CMSIS
\r
48 ******************************************************************************/
\r
49 uint32_t SystemCoreClock = __HCLK;
\r
52 ******************************************************************************
\r
53 ** \brief Update the System Core Clock with current core Clock retrieved from
\r
57 ******************************************************************************/
\r
58 void SystemCoreClockUpdate (void) {
\r
60 uint32_t u32RegisterRead; // Workaround variable for MISRA C rule conformance
\r
62 switch ((FM3_CRG->SCM_CTL >> 5) & 0x07) {
\r
63 case 0: /* internal High-speed Cr osc. */
\r
64 masterClk = __CLKHC;
\r
67 case 1: /* external main osc. */
\r
68 masterClk = __CLKMO;
\r
71 case 2: /* PLL clock */
\r
72 // Workaround for preventing MISRA C:1998 Rule 46 (MISRA C:2004 Rule 12.2)
\r
74 // "Unordered accesses to a volatile location"
\r
75 u32RegisterRead = (__CLKMO * (((FM3_CRG->PLL_CTL2) & 0x1F) + 1));
\r
76 masterClk = (u32RegisterRead / (((FM3_CRG->PLL_CTL1 >> 4) & 0x0F) + 1));
\r
79 case 4: /* internal Low-speed CR osc. */
\r
80 masterClk = __CLKLC;
\r
83 case 5: /* external Sub osc. */
\r
84 masterClk = __CLKSO;
\r
92 switch (FM3_CRG->BSC_PSR & 0x07) {
\r
94 SystemCoreClock = masterClk;
\r
98 SystemCoreClock = masterClk / 2;
\r
102 SystemCoreClock = masterClk / 3;
\r
106 SystemCoreClock = masterClk / 4;
\r
110 SystemCoreClock = masterClk / 6;
\r
114 SystemCoreClock = masterClk /8;
\r
118 SystemCoreClock = masterClk /16;
\r
122 SystemCoreClock = 0Ul;
\r
129 ******************************************************************************
\r
130 ** \brief Setup the microcontroller system. Initialize the System and update
\r
131 ** the SystemCoreClock variable.
\r
135 ******************************************************************************/
\r
136 void SystemInit (void) {
\r
138 static uint32_t u32IoRegisterRead; // Workaround variable for MISRA C rule conformance
\r
140 #if (HWWD_DISABLE) /* HW Watchdog Disable */
\r
141 FM3_HWWDT->WDG_LCK = 0x1ACCE551; /* HW Watchdog Unlock */
\r
142 FM3_HWWDT->WDG_LCK = 0xE5331AAE;
\r
143 FM3_HWWDT->WDG_CTL = 0; /* HW Watchdog stop */
\r
146 #if (CLOCK_SETUP) /* Clock Setup */
\r
147 FM3_CRG->BSC_PSR = BSC_PSR_Val; /* set System Clock presacaler */
\r
148 FM3_CRG->APBC0_PSR = APBC0_PSR_Val; /* set APB0 presacaler */
\r
149 FM3_CRG->APBC1_PSR = APBC1_PSR_Val; /* set APB1 presacaler */
\r
150 FM3_CRG->APBC2_PSR = APBC2_PSR_Val; /* set APB2 presacaler */
\r
151 FM3_CRG->SWC_PSR = SWC_PSR_Val | (1UL << 7); /* set SW Watchdog presacaler */
\r
152 FM3_CRG->TTC_PSR = TTC_PSR_Val; /* set Trace Clock presacaler */
\r
154 FM3_CRG->CSW_TMR = CSW_TMR_Val; /* set oscillation stabilization wait time */
\r
156 if (SCM_CTL_Val & (1UL << 1)) { /* Main clock oscillator enabled ? */
\r
157 FM3_CRG->SCM_CTL |= (1UL << 1); /* enable main oscillator */
\r
158 while (!(FM3_CRG->SCM_STR & (1UL << 1))); /* wait for Main clock oscillation stable */
\r
161 if (SCM_CTL_Val & (1UL << 3)) { /* Sub clock oscillator enabled ? */
\r
162 FM3_CRG->SCM_CTL |= (1UL << 3); /* enable sub oscillator */
\r
163 while (!(FM3_CRG->SCM_STR & (1UL << 3))); /* wait for Sub clock oscillation stable */
\r
166 FM3_CRG->PSW_TMR = PSW_TMR_Val; /* set PLL stabilization wait time */
\r
167 FM3_CRG->PLL_CTL1 = PLL_CTL1_Val; /* set PLLM and PLLK */
\r
168 FM3_CRG->PLL_CTL2 = PLL_CTL2_Val; /* set PLLN */
\r
170 if (SCM_CTL_Val & (1UL << 4)) { /* PLL enabled ? */
\r
171 FM3_CRG->SCM_CTL |= (1UL << 4); /* enable PLL */
\r
172 while (!(FM3_CRG->SCM_STR & (1UL << 4))); /* wait for PLL stable */
\r
175 FM3_CRG->SCM_CTL |= (SCM_CTL_Val & 0xE0); /* Set Master Clock switch */
\r
177 // Workaround for preventing MISRA C:1998 Rule 46 (MISRA C:2004 Rule 12.2)
\r
179 // "Unordered reads and writes to or from same location" and
\r
180 // "Unordered accesses to a volatile location"
\r
183 u32IoRegisterRead = (FM3_CRG->SCM_CTL & 0xE0);
\r
184 }while ((FM3_CRG->SCM_STR & 0xE0) != u32IoRegisterRead);
\r
185 #endif // (CLOCK_SETUP)
\r
187 #if (CR_TRIM_SETUP)
\r
188 /* CR Trimming Data */
\r
189 if( 0x000003FF != (FM3_FLASH_IF->CRTRMM & 0x000003FF) )
\r
191 /* UnLock (MCR_FTRM) */
\r
192 FM3_CRTRIM->MCR_RLR = 0x1ACCE554;
\r
194 FM3_CRTRIM->MCR_FTRM = FM3_FLASH_IF->CRTRMM;
\r
195 /* Lock (MCR_FTRM) */
\r
196 FM3_CRTRIM->MCR_RLR = 0x00000000;
\r
198 #endif // (CR_TRIM_SETUP)
\r