]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MB9A310_IAR_Keil/Fujitu_source/system_mb9af31x.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / CORTEX_MB9A310_IAR_Keil / Fujitu_source / system_mb9af31x.c
1 /************************************************************************/\r
2 /*               (C) Fujitsu Semiconductor Europe GmbH (FSEU)           */\r
3 /*                                                                      */\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
7 /* alterations.                                                         */\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
11 /*                                                                      */\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
15 /* deliverable.                                                         */\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
21 /*                                                                      */\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
26 /*                                                                      */\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
30 /*                                                                      */\r
31 /* (V1.5)                                                               */\r
32 /************************************************************************/\r
33 \r
34 #include "mcu.h"\r
35 \r
36 /** \file system_mb9af31x.c\r
37  **\r
38  ** FM3 system initialization functions\r
39  ** All adjustments can be done in belonging header file.\r
40  **\r
41  ** History:\r
42  ** 2011-05-16 V1.0 MWi original version\r
43  ******************************************************************************/\r
44 \r
45 /**\r
46  ******************************************************************************\r
47  ** System Clock Frequency (Core Clock) Variable according CMSIS\r
48  ******************************************************************************/\r
49 uint32_t SystemCoreClock = __HCLK;\r
50 \r
51 /**\r
52  ******************************************************************************\r
53  ** \brief  Update the System Core Clock with current core Clock retrieved from\r
54  ** cpu registers.\r
55  ** \param  none\r
56  ** \return none\r
57  ******************************************************************************/\r
58 void SystemCoreClockUpdate (void) {\r
59   uint32_t masterClk;\r
60   uint32_t u32RegisterRead; // Workaround variable for MISRA C rule conformance\r
61 \r
62   switch ((FM3_CRG->SCM_CTL >> 5) & 0x07) {\r
63     case 0:                                 /* internal High-speed Cr osc.    */\r
64       masterClk = __CLKHC;\r
65       break;\r
66 \r
67     case 1:                                 /* external main osc.             */\r
68       masterClk = __CLKMO;\r
69       break;\r
70 \r
71     case 2:                                 /* PLL clock                      */\r
72   // Workaround for preventing MISRA C:1998 Rule 46 (MISRA C:2004 Rule 12.2)\r
73   // violation:\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
77       break;\r
78 \r
79     case 4:                                 /* internal Low-speed CR osc.     */\r
80       masterClk = __CLKLC;\r
81       break;\r
82 \r
83     case 5:                                 /* external Sub osc.              */\r
84       masterClk = __CLKSO;\r
85       break;\r
86 \r
87     default:\r
88       masterClk = 0Ul;\r
89       break;\r
90   }\r
91 \r
92   switch (FM3_CRG->BSC_PSR & 0x07) {\r
93     case 0:\r
94       SystemCoreClock = masterClk;\r
95       break;\r
96 \r
97     case 1:\r
98       SystemCoreClock = masterClk / 2;\r
99       break;\r
100 \r
101     case 2:\r
102       SystemCoreClock = masterClk / 3;\r
103       break;\r
104 \r
105     case 3:\r
106       SystemCoreClock = masterClk / 4;\r
107       break;\r
108 \r
109     case 4:\r
110       SystemCoreClock = masterClk / 6;\r
111       break;\r
112 \r
113     case 5:\r
114       SystemCoreClock = masterClk /8;\r
115       break;\r
116 \r
117     case 6:\r
118       SystemCoreClock = masterClk /16;\r
119       break;\r
120 \r
121     default:\r
122       SystemCoreClock = 0Ul;\r
123       break;\r
124   }\r
125 \r
126 }\r
127 \r
128 /**\r
129  ******************************************************************************\r
130  ** \brief  Setup the microcontroller system. Initialize the System and update\r
131  ** the SystemCoreClock variable.\r
132  **\r
133  ** \param  none\r
134  ** \return none\r
135  ******************************************************************************/\r
136 void SystemInit (void) {\r
137 \r
138   static uint32_t u32IoRegisterRead;  // Workaround variable for MISRA C rule conformance\r
139   \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
144 #endif\r
145 \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
153 \r
154   FM3_CRG->CSW_TMR   = CSW_TMR_Val;                /* set oscillation stabilization wait time */\r
155   \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
159   }\r
160   \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
164   }\r
165 \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
169   \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
173   }\r
174 \r
175   FM3_CRG->SCM_CTL  |= (SCM_CTL_Val & 0xE0);       /* Set Master Clock switch */ \r
176   \r
177   // Workaround for preventing MISRA C:1998 Rule 46 (MISRA C:2004 Rule 12.2)\r
178   // violations:\r
179   //   "Unordered reads and writes to or from same location" and\r
180   //   "Unordered accesses to a volatile location"\r
181   do                                              \r
182   {                                               \r
183     u32IoRegisterRead = (FM3_CRG->SCM_CTL & 0xE0); \r
184   }while ((FM3_CRG->SCM_STR & 0xE0) != u32IoRegisterRead);\r
185 #endif // (CLOCK_SETUP)\r
186   \r
187 #if (CR_TRIM_SETUP)\r
188   /* CR Trimming Data  */\r
189   if( 0x000003FF != (FM3_FLASH_IF->CRTRMM & 0x000003FF) )\r
190   {\r
191     /* UnLock (MCR_FTRM) */\r
192     FM3_CRTRIM->MCR_RLR = 0x1ACCE554;\r
193     /* Set MCR_FTRM */\r
194     FM3_CRTRIM->MCR_FTRM = FM3_FLASH_IF->CRTRMM;\r
195     /* Lock (MCR_FTRM) */\r
196     FM3_CRTRIM->MCR_RLR = 0x00000000;\r
197   }\r
198 #endif // (CR_TRIM_SETUP)\r
199 }\r
200 \r
201 \r
202 \r