xref: /btstack/port/msp432p401lp-cc256x/system_msp432p401r.c (revision 142ae15a423624edf2dd1e70e2cd0a47dd644b3e)
15fd0122aSMatthias Ringwald /******************************************************************************
2f7529f1dSMatthias Ringwald * @file     system_msp432p401r.c
3f7529f1dSMatthias Ringwald * @brief    CMSIS Cortex-M4F Device Peripheral Access Layer Source File for
4f7529f1dSMatthias Ringwald *           MSP432P401R
55fd0122aSMatthias Ringwald * @version  3.231
65fd0122aSMatthias Ringwald * @date     01/26/18
7f7529f1dSMatthias Ringwald *
8f7529f1dSMatthias Ringwald * @note     View configuration instructions embedded in comments
9f7529f1dSMatthias Ringwald *
10f7529f1dSMatthias Ringwald ******************************************************************************/
11f7529f1dSMatthias Ringwald //*****************************************************************************
12f7529f1dSMatthias Ringwald //
135fd0122aSMatthias Ringwald // Copyright (C) 2015 - 2018 Texas Instruments Incorporated - http://www.ti.com/
14f7529f1dSMatthias Ringwald //
15f7529f1dSMatthias Ringwald // Redistribution and use in source and binary forms, with or without
16f7529f1dSMatthias Ringwald // modification, are permitted provided that the following conditions
17f7529f1dSMatthias Ringwald // are met:
18f7529f1dSMatthias Ringwald //
19f7529f1dSMatthias Ringwald //  Redistributions of source code must retain the above copyright
20f7529f1dSMatthias Ringwald //  notice, this list of conditions and the following disclaimer.
21f7529f1dSMatthias Ringwald //
22f7529f1dSMatthias Ringwald //  Redistributions in binary form must reproduce the above copyright
23f7529f1dSMatthias Ringwald //  notice, this list of conditions and the following disclaimer in the
24f7529f1dSMatthias Ringwald //  documentation and/or other materials provided with the
25f7529f1dSMatthias Ringwald //  distribution.
26f7529f1dSMatthias Ringwald //
27f7529f1dSMatthias Ringwald //  Neither the name of Texas Instruments Incorporated nor the names of
28f7529f1dSMatthias Ringwald //  its contributors may be used to endorse or promote products derived
29f7529f1dSMatthias Ringwald //  from this software without specific prior written permission.
30f7529f1dSMatthias Ringwald //
31f7529f1dSMatthias Ringwald // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32f7529f1dSMatthias Ringwald // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33f7529f1dSMatthias Ringwald // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34f7529f1dSMatthias Ringwald // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35f7529f1dSMatthias Ringwald // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36f7529f1dSMatthias Ringwald // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37f7529f1dSMatthias Ringwald // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38f7529f1dSMatthias Ringwald // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39f7529f1dSMatthias Ringwald // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40f7529f1dSMatthias Ringwald // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41f7529f1dSMatthias Ringwald // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42f7529f1dSMatthias Ringwald //
43f7529f1dSMatthias Ringwald //*****************************************************************************
44f7529f1dSMatthias Ringwald 
45f7529f1dSMatthias Ringwald #include <stdint.h>
465fd0122aSMatthias Ringwald #include <ti/devices/msp432p4xx/inc/msp.h>
47f7529f1dSMatthias Ringwald 
48f7529f1dSMatthias Ringwald /*--------------------- Configuration Instructions ----------------------------
49f7529f1dSMatthias Ringwald    1. If you prefer to halt the Watchdog Timer, set __HALT_WDT to 1:
50f7529f1dSMatthias Ringwald    #define __HALT_WDT       1
51f7529f1dSMatthias Ringwald    2. Insert your desired CPU frequency in Hz at:
525fd0122aSMatthias Ringwald    #define __SYSTEM_CLOCK   12000000
53f7529f1dSMatthias Ringwald    3. If you prefer the DC-DC power regulator (more efficient at higher
54f7529f1dSMatthias Ringwald        frequencies), set the __REGULATOR to 1:
55f7529f1dSMatthias Ringwald    #define __REGULATOR      1
56f7529f1dSMatthias Ringwald  *---------------------------------------------------------------------------*/
57f7529f1dSMatthias Ringwald 
58f7529f1dSMatthias Ringwald /*--------------------- Watchdog Timer Configuration ------------------------*/
59f7529f1dSMatthias Ringwald //  Halt the Watchdog Timer
60f7529f1dSMatthias Ringwald //     <0> Do not halt the WDT
61f7529f1dSMatthias Ringwald //     <1> Halt the WDT
62f7529f1dSMatthias Ringwald #define __HALT_WDT         1
63f7529f1dSMatthias Ringwald 
64f7529f1dSMatthias Ringwald /*--------------------- CPU Frequency Configuration -------------------------*/
65f7529f1dSMatthias Ringwald //  CPU Frequency
66f7529f1dSMatthias Ringwald //     <1500000> 1.5 MHz
67f7529f1dSMatthias Ringwald //     <3000000> 3 MHz
68f7529f1dSMatthias Ringwald //     <12000000> 12 MHz
69f7529f1dSMatthias Ringwald //     <24000000> 24 MHz
70f7529f1dSMatthias Ringwald //     <48000000> 48 MHz
715fd0122aSMatthias Ringwald #define  __SYSTEM_CLOCK    48000000
72f7529f1dSMatthias Ringwald 
73f7529f1dSMatthias Ringwald /*--------------------- Power Regulator Configuration -----------------------*/
74f7529f1dSMatthias Ringwald //  Power Regulator Mode
75f7529f1dSMatthias Ringwald //     <0> LDO
76f7529f1dSMatthias Ringwald //     <1> DC-DC
775fd0122aSMatthias Ringwald #define __REGULATOR        0
78f7529f1dSMatthias Ringwald 
79f7529f1dSMatthias Ringwald /*----------------------------------------------------------------------------
80f7529f1dSMatthias Ringwald    Define clocks, used for SystemCoreClockUpdate()
81f7529f1dSMatthias Ringwald  *---------------------------------------------------------------------------*/
82f7529f1dSMatthias Ringwald #define __VLOCLK           10000
83f7529f1dSMatthias Ringwald #define __MODCLK           24000000
84f7529f1dSMatthias Ringwald #define __LFXT             32768
85f7529f1dSMatthias Ringwald #define __HFXT             48000000
86f7529f1dSMatthias Ringwald 
87f7529f1dSMatthias Ringwald /*----------------------------------------------------------------------------
88f7529f1dSMatthias Ringwald    Clock Variable definitions
89f7529f1dSMatthias Ringwald  *---------------------------------------------------------------------------*/
90f7529f1dSMatthias Ringwald uint32_t SystemCoreClock = __SYSTEM_CLOCK;  /*!< System Clock Frequency (Core Clock)*/
91f7529f1dSMatthias Ringwald 
92f7529f1dSMatthias Ringwald /**
93f7529f1dSMatthias Ringwald  * Update SystemCoreClock variable
94f7529f1dSMatthias Ringwald  *
95f7529f1dSMatthias Ringwald  * @param  none
96f7529f1dSMatthias Ringwald  * @return none
97f7529f1dSMatthias Ringwald  *
98f7529f1dSMatthias Ringwald  * @brief  Updates the SystemCoreClock with current core Clock
99f7529f1dSMatthias Ringwald  *         retrieved from cpu registers.
100f7529f1dSMatthias Ringwald  */
SystemCoreClockUpdate(void)101f7529f1dSMatthias Ringwald void SystemCoreClockUpdate(void)
102f7529f1dSMatthias Ringwald {
1035fd0122aSMatthias Ringwald     uint32_t source = 0, divider = 0, dividerValue = 0, centeredFreq = 0, calVal = 0;
1045fd0122aSMatthias Ringwald     int16_t dcoTune = 0;
1055fd0122aSMatthias Ringwald     float dcoConst = 0.0;
106f7529f1dSMatthias Ringwald 
107f7529f1dSMatthias Ringwald     divider = (CS->CTL1 & CS_CTL1_DIVM_MASK) >> CS_CTL1_DIVM_OFS;
108f7529f1dSMatthias Ringwald     dividerValue = 1 << divider;
109f7529f1dSMatthias Ringwald     source = CS->CTL1 & CS_CTL1_SELM_MASK;
110f7529f1dSMatthias Ringwald 
111f7529f1dSMatthias Ringwald     switch(source)
112f7529f1dSMatthias Ringwald     {
113f7529f1dSMatthias Ringwald     case CS_CTL1_SELM__LFXTCLK:
114f7529f1dSMatthias Ringwald         if(BITBAND_PERI(CS->IFG, CS_IFG_LFXTIFG_OFS))
115f7529f1dSMatthias Ringwald         {
116f7529f1dSMatthias Ringwald             // Clear interrupt flag
117f7529f1dSMatthias Ringwald             CS->KEY = CS_KEY_VAL;
118f7529f1dSMatthias Ringwald             CS->CLRIFG |= CS_CLRIFG_CLR_LFXTIFG;
119f7529f1dSMatthias Ringwald             CS->KEY = 1;
120f7529f1dSMatthias Ringwald 
121f7529f1dSMatthias Ringwald             if(BITBAND_PERI(CS->IFG, CS_IFG_LFXTIFG_OFS))
122f7529f1dSMatthias Ringwald             {
123f7529f1dSMatthias Ringwald                 if(BITBAND_PERI(CS->CLKEN, CS_CLKEN_REFOFSEL_OFS))
124f7529f1dSMatthias Ringwald                 {
125f7529f1dSMatthias Ringwald                     SystemCoreClock = (128000 / dividerValue);
126f7529f1dSMatthias Ringwald                 }
127f7529f1dSMatthias Ringwald                 else
128f7529f1dSMatthias Ringwald                 {
129f7529f1dSMatthias Ringwald                     SystemCoreClock = (32000 / dividerValue);
130f7529f1dSMatthias Ringwald                 }
131f7529f1dSMatthias Ringwald             }
132f7529f1dSMatthias Ringwald             else
133f7529f1dSMatthias Ringwald             {
134f7529f1dSMatthias Ringwald                 SystemCoreClock = __LFXT / dividerValue;
135f7529f1dSMatthias Ringwald             }
136f7529f1dSMatthias Ringwald         }
137f7529f1dSMatthias Ringwald         else
138f7529f1dSMatthias Ringwald         {
139f7529f1dSMatthias Ringwald             SystemCoreClock = __LFXT / dividerValue;
140f7529f1dSMatthias Ringwald         }
141f7529f1dSMatthias Ringwald         break;
142f7529f1dSMatthias Ringwald     case CS_CTL1_SELM__VLOCLK:
143f7529f1dSMatthias Ringwald         SystemCoreClock = __VLOCLK / dividerValue;
144f7529f1dSMatthias Ringwald         break;
145f7529f1dSMatthias Ringwald     case CS_CTL1_SELM__REFOCLK:
146f7529f1dSMatthias Ringwald         if (BITBAND_PERI(CS->CLKEN, CS_CLKEN_REFOFSEL_OFS))
147f7529f1dSMatthias Ringwald         {
148f7529f1dSMatthias Ringwald             SystemCoreClock = (128000 / dividerValue);
149f7529f1dSMatthias Ringwald         }
150f7529f1dSMatthias Ringwald         else
151f7529f1dSMatthias Ringwald         {
152f7529f1dSMatthias Ringwald             SystemCoreClock = (32000 / dividerValue);
153f7529f1dSMatthias Ringwald         }
154f7529f1dSMatthias Ringwald         break;
155f7529f1dSMatthias Ringwald     case CS_CTL1_SELM__DCOCLK:
156f7529f1dSMatthias Ringwald         dcoTune = (CS->CTL0 & CS_CTL0_DCOTUNE_MASK) >> CS_CTL0_DCOTUNE_OFS;
157f7529f1dSMatthias Ringwald 
158f7529f1dSMatthias Ringwald         switch(CS->CTL0 & CS_CTL0_DCORSEL_MASK)
159f7529f1dSMatthias Ringwald         {
160f7529f1dSMatthias Ringwald         case CS_CTL0_DCORSEL_0:
161f7529f1dSMatthias Ringwald             centeredFreq = 1500000;
162f7529f1dSMatthias Ringwald             break;
163f7529f1dSMatthias Ringwald         case CS_CTL0_DCORSEL_1:
164f7529f1dSMatthias Ringwald             centeredFreq = 3000000;
165f7529f1dSMatthias Ringwald             break;
166f7529f1dSMatthias Ringwald         case CS_CTL0_DCORSEL_2:
167f7529f1dSMatthias Ringwald             centeredFreq = 6000000;
168f7529f1dSMatthias Ringwald             break;
169f7529f1dSMatthias Ringwald         case CS_CTL0_DCORSEL_3:
170f7529f1dSMatthias Ringwald             centeredFreq = 12000000;
171f7529f1dSMatthias Ringwald             break;
172f7529f1dSMatthias Ringwald         case CS_CTL0_DCORSEL_4:
173f7529f1dSMatthias Ringwald             centeredFreq = 24000000;
174f7529f1dSMatthias Ringwald             break;
175f7529f1dSMatthias Ringwald         case CS_CTL0_DCORSEL_5:
176f7529f1dSMatthias Ringwald             centeredFreq = 48000000;
177f7529f1dSMatthias Ringwald             break;
178f7529f1dSMatthias Ringwald         }
179f7529f1dSMatthias Ringwald 
180f7529f1dSMatthias Ringwald         if(dcoTune == 0)
181f7529f1dSMatthias Ringwald         {
182f7529f1dSMatthias Ringwald             SystemCoreClock = centeredFreq;
183f7529f1dSMatthias Ringwald         }
184f7529f1dSMatthias Ringwald         else
185f7529f1dSMatthias Ringwald         {
186f7529f1dSMatthias Ringwald 
187f7529f1dSMatthias Ringwald             if(dcoTune & 0x1000)
188f7529f1dSMatthias Ringwald             {
189f7529f1dSMatthias Ringwald                 dcoTune = dcoTune | 0xF000;
190f7529f1dSMatthias Ringwald             }
191f7529f1dSMatthias Ringwald 
192f7529f1dSMatthias Ringwald             if (BITBAND_PERI(CS->CTL0, CS_CTL0_DCORES_OFS))
193f7529f1dSMatthias Ringwald             {
1945fd0122aSMatthias Ringwald                 dcoConst = *((volatile const float *) &TLV->DCOER_CONSTK_RSEL04);
195f7529f1dSMatthias Ringwald                 calVal = TLV->DCOER_FCAL_RSEL04;
196f7529f1dSMatthias Ringwald             }
197f7529f1dSMatthias Ringwald             /* Internal Resistor */
198f7529f1dSMatthias Ringwald             else
199f7529f1dSMatthias Ringwald             {
2005fd0122aSMatthias Ringwald                 dcoConst = *((volatile const float *) &TLV->DCOIR_CONSTK_RSEL04);
201f7529f1dSMatthias Ringwald                 calVal = TLV->DCOIR_FCAL_RSEL04;
202f7529f1dSMatthias Ringwald             }
203f7529f1dSMatthias Ringwald 
204f7529f1dSMatthias Ringwald             SystemCoreClock = (uint32_t) ((centeredFreq)
205f7529f1dSMatthias Ringwald                                / (1
206f7529f1dSMatthias Ringwald                                     - ((dcoConst * dcoTune)
207f7529f1dSMatthias Ringwald                                             / (8 * (1 + dcoConst * (768 - calVal))))));
208f7529f1dSMatthias Ringwald         }
209f7529f1dSMatthias Ringwald         break;
210f7529f1dSMatthias Ringwald     case CS_CTL1_SELM__MODOSC:
211f7529f1dSMatthias Ringwald         SystemCoreClock = __MODCLK / dividerValue;
212f7529f1dSMatthias Ringwald         break;
213f7529f1dSMatthias Ringwald     case CS_CTL1_SELM__HFXTCLK:
214f7529f1dSMatthias Ringwald         if(BITBAND_PERI(CS->IFG, CS_IFG_HFXTIFG_OFS))
215f7529f1dSMatthias Ringwald         {
216f7529f1dSMatthias Ringwald             // Clear interrupt flag
217f7529f1dSMatthias Ringwald             CS->KEY = CS_KEY_VAL;
218f7529f1dSMatthias Ringwald             CS->CLRIFG |= CS_CLRIFG_CLR_HFXTIFG;
219f7529f1dSMatthias Ringwald             CS->KEY = 1;
220f7529f1dSMatthias Ringwald 
221f7529f1dSMatthias Ringwald             if(BITBAND_PERI(CS->IFG, CS_IFG_HFXTIFG_OFS))
222f7529f1dSMatthias Ringwald             {
223f7529f1dSMatthias Ringwald                 if(BITBAND_PERI(CS->CLKEN, CS_CLKEN_REFOFSEL_OFS))
224f7529f1dSMatthias Ringwald                 {
225f7529f1dSMatthias Ringwald                     SystemCoreClock = (128000 / dividerValue);
226f7529f1dSMatthias Ringwald                 }
227f7529f1dSMatthias Ringwald                 else
228f7529f1dSMatthias Ringwald                 {
229f7529f1dSMatthias Ringwald                     SystemCoreClock = (32000 / dividerValue);
230f7529f1dSMatthias Ringwald                 }
231f7529f1dSMatthias Ringwald             }
232f7529f1dSMatthias Ringwald             else
233f7529f1dSMatthias Ringwald             {
234f7529f1dSMatthias Ringwald                 SystemCoreClock = __HFXT / dividerValue;
235f7529f1dSMatthias Ringwald             }
236f7529f1dSMatthias Ringwald         }
237f7529f1dSMatthias Ringwald         else
238f7529f1dSMatthias Ringwald         {
239f7529f1dSMatthias Ringwald             SystemCoreClock = __HFXT / dividerValue;
240f7529f1dSMatthias Ringwald         }
241f7529f1dSMatthias Ringwald         break;
242f7529f1dSMatthias Ringwald     }
243f7529f1dSMatthias Ringwald }
244f7529f1dSMatthias Ringwald 
245f7529f1dSMatthias Ringwald /**
246f7529f1dSMatthias Ringwald  * Initialize the system
247f7529f1dSMatthias Ringwald  *
248f7529f1dSMatthias Ringwald  * @param  none
249f7529f1dSMatthias Ringwald  * @return none
250f7529f1dSMatthias Ringwald  *
251f7529f1dSMatthias Ringwald  * @brief  Setup the microcontroller system.
252f7529f1dSMatthias Ringwald  *
253f7529f1dSMatthias Ringwald  * Performs the following initialization steps:
254f7529f1dSMatthias Ringwald  *     1. Enables the FPU
255f7529f1dSMatthias Ringwald  *     2. Halts the WDT if requested
256f7529f1dSMatthias Ringwald  *     3. Enables all SRAM banks
257f7529f1dSMatthias Ringwald  *     4. Sets up power regulator and VCORE
258f7529f1dSMatthias Ringwald  *     5. Enable Flash wait states if needed
259f7529f1dSMatthias Ringwald  *     6. Change MCLK to desired frequency
260f7529f1dSMatthias Ringwald  *     7. Enable Flash read buffering
261f7529f1dSMatthias Ringwald  */
SystemInit(void)262f7529f1dSMatthias Ringwald void SystemInit(void)
263f7529f1dSMatthias Ringwald {
264f7529f1dSMatthias Ringwald     // Enable FPU if used
2655fd0122aSMatthias Ringwald     #if (__FPU_USED == 1)                                  // __FPU_USED is defined in core_cm4.h
2665fd0122aSMatthias Ringwald     SCB->CPACR |= ((3UL << 10 * 2) |                       // Set CP10 Full Access
2675fd0122aSMatthias Ringwald                    (3UL << 11 * 2));                       // Set CP11 Full Access
268f7529f1dSMatthias Ringwald     #endif
269f7529f1dSMatthias Ringwald 
270f7529f1dSMatthias Ringwald     #if (__HALT_WDT == 1)
271f7529f1dSMatthias Ringwald     WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;            // Halt the WDT
272f7529f1dSMatthias Ringwald     #endif
273f7529f1dSMatthias Ringwald 
274f7529f1dSMatthias Ringwald     SYSCTL->SRAM_BANKEN = SYSCTL_SRAM_BANKEN_BNK7_EN;      // Enable all SRAM banks
275f7529f1dSMatthias Ringwald 
276f7529f1dSMatthias Ringwald     #if (__SYSTEM_CLOCK == 1500000)                        // 1.5 MHz
277f7529f1dSMatthias Ringwald     // Default VCORE is LDO VCORE0 so no change necessary
278f7529f1dSMatthias Ringwald 
279f7529f1dSMatthias Ringwald     // Switches LDO VCORE0 to DCDC VCORE0 if requested
280f7529f1dSMatthias Ringwald     #if __REGULATOR
281f7529f1dSMatthias Ringwald     while((PCM->CTL1 & PCM_CTL1_PMR_BUSY));
282f7529f1dSMatthias Ringwald     PCM->CTL0 = PCM_CTL0_KEY_VAL | PCM_CTL0_AMR_4;
283f7529f1dSMatthias Ringwald     while((PCM->CTL1 & PCM_CTL1_PMR_BUSY));
284f7529f1dSMatthias Ringwald     #endif
285f7529f1dSMatthias Ringwald 
286f7529f1dSMatthias Ringwald     // No flash wait states necessary
287f7529f1dSMatthias Ringwald 
288f7529f1dSMatthias Ringwald     // DCO = 1.5 MHz; MCLK = source
289f7529f1dSMatthias Ringwald     CS->KEY = CS_KEY_VAL;                                  // Unlock CS module for register access
290f7529f1dSMatthias Ringwald     CS->CTL0 = CS_CTL0_DCORSEL_0;                          // Set DCO to 1.5MHz
2915fd0122aSMatthias Ringwald     CS->CTL1 = (CS->CTL1 & ~(CS_CTL1_SELM_MASK | CS_CTL1_DIVM_MASK)) | CS_CTL1_SELM__DCOCLK;
2925fd0122aSMatthias Ringwald 	                                                       // Select MCLK as DCO source
293f7529f1dSMatthias Ringwald     CS->KEY = 0;
294f7529f1dSMatthias Ringwald 
295f7529f1dSMatthias Ringwald     // Set Flash Bank read buffering
2965fd0122aSMatthias Ringwald     FLCTL->BANK0_RDCTL = FLCTL->BANK0_RDCTL & ~(FLCTL_BANK0_RDCTL_BUFD | FLCTL_BANK0_RDCTL_BUFI);
2975fd0122aSMatthias Ringwald     FLCTL->BANK1_RDCTL = FLCTL->BANK1_RDCTL & ~(FLCTL_BANK1_RDCTL_BUFD | FLCTL_BANK1_RDCTL_BUFI);
298f7529f1dSMatthias Ringwald 
299f7529f1dSMatthias Ringwald     #elif (__SYSTEM_CLOCK == 3000000)                      // 3 MHz
300f7529f1dSMatthias Ringwald     // Default VCORE is LDO VCORE0 so no change necessary
301f7529f1dSMatthias Ringwald 
302f7529f1dSMatthias Ringwald     // Switches LDO VCORE0 to DCDC VCORE0 if requested
303f7529f1dSMatthias Ringwald     #if __REGULATOR
304f7529f1dSMatthias Ringwald     while(PCM->CTL1 & PCM_CTL1_PMR_BUSY);
305f7529f1dSMatthias Ringwald     PCM->CTL0 = PCM_CTL0_KEY_VAL | PCM_CTL0_AMR_4;
306f7529f1dSMatthias Ringwald     while(PCM->CTL1 & PCM_CTL1_PMR_BUSY);
307f7529f1dSMatthias Ringwald     #endif
308f7529f1dSMatthias Ringwald 
309f7529f1dSMatthias Ringwald     // No flash wait states necessary
310f7529f1dSMatthias Ringwald 
311f7529f1dSMatthias Ringwald     // DCO = 3 MHz; MCLK = source
312f7529f1dSMatthias Ringwald     CS->KEY = CS_KEY_VAL;                                  // Unlock CS module for register access
313f7529f1dSMatthias Ringwald     CS->CTL0 = CS_CTL0_DCORSEL_1;                          // Set DCO to 1.5MHz
3145fd0122aSMatthias Ringwald     CS->CTL1 = (CS->CTL1 & ~(CS_CTL1_SELM_MASK | CS_CTL1_DIVM_MASK)) | CS_CTL1_SELM__DCOCLK;
3155fd0122aSMatthias Ringwald 	                                                       // Select MCLK as DCO source
316f7529f1dSMatthias Ringwald     CS->KEY = 0;
317f7529f1dSMatthias Ringwald 
318f7529f1dSMatthias Ringwald     // Set Flash Bank read buffering
3195fd0122aSMatthias Ringwald     FLCTL->BANK0_RDCTL = FLCTL->BANK0_RDCTL & ~(FLCTL_BANK0_RDCTL_BUFD | FLCTL_BANK0_RDCTL_BUFI);
3205fd0122aSMatthias Ringwald     FLCTL->BANK1_RDCTL = FLCTL->BANK1_RDCTL & ~(FLCTL_BANK1_RDCTL_BUFD | FLCTL_BANK1_RDCTL_BUFI);
321f7529f1dSMatthias Ringwald 
322f7529f1dSMatthias Ringwald     #elif (__SYSTEM_CLOCK == 12000000)                     // 12 MHz
323f7529f1dSMatthias Ringwald     // Default VCORE is LDO VCORE0 so no change necessary
324f7529f1dSMatthias Ringwald 
325f7529f1dSMatthias Ringwald     // Switches LDO VCORE0 to DCDC VCORE0 if requested
326f7529f1dSMatthias Ringwald     #if __REGULATOR
327f7529f1dSMatthias Ringwald     while((PCM->CTL1 & PCM_CTL1_PMR_BUSY));
328f7529f1dSMatthias Ringwald     PCM->CTL0 = PCM_CTL0_KEY_VAL | PCM_CTL0_AMR_4;
329f7529f1dSMatthias Ringwald     while((PCM->CTL1 & PCM_CTL1_PMR_BUSY));
330f7529f1dSMatthias Ringwald     #endif
331f7529f1dSMatthias Ringwald 
332f7529f1dSMatthias Ringwald     // No flash wait states necessary
333f7529f1dSMatthias Ringwald 
334f7529f1dSMatthias Ringwald     // DCO = 12 MHz; MCLK = source
335f7529f1dSMatthias Ringwald     CS->KEY = CS_KEY_VAL;                                  // Unlock CS module for register access
336f7529f1dSMatthias Ringwald     CS->CTL0 = CS_CTL0_DCORSEL_3;                          // Set DCO to 12MHz
3375fd0122aSMatthias Ringwald     CS->CTL1 = (CS->CTL1 & ~(CS_CTL1_SELM_MASK | CS_CTL1_DIVM_MASK)) | CS_CTL1_SELM__DCOCLK;
3385fd0122aSMatthias Ringwald 	                                                       // Select MCLK as DCO source
339f7529f1dSMatthias Ringwald     CS->KEY = 0;
340f7529f1dSMatthias Ringwald 
341f7529f1dSMatthias Ringwald     // Set Flash Bank read buffering
3425fd0122aSMatthias Ringwald     FLCTL->BANK0_RDCTL = FLCTL->BANK0_RDCTL & ~(FLCTL_BANK0_RDCTL_BUFD | FLCTL_BANK0_RDCTL_BUFI);
3435fd0122aSMatthias Ringwald     FLCTL->BANK1_RDCTL = FLCTL->BANK1_RDCTL & ~(FLCTL_BANK1_RDCTL_BUFD | FLCTL_BANK1_RDCTL_BUFI);
344f7529f1dSMatthias Ringwald 
345f7529f1dSMatthias Ringwald     #elif (__SYSTEM_CLOCK == 24000000)                     // 24 MHz
346f7529f1dSMatthias Ringwald     // Default VCORE is LDO VCORE0 so no change necessary
347f7529f1dSMatthias Ringwald 
348f7529f1dSMatthias Ringwald     // Switches LDO VCORE0 to DCDC VCORE0 if requested
349f7529f1dSMatthias Ringwald     #if __REGULATOR
350f7529f1dSMatthias Ringwald     while((PCM->CTL1 & PCM_CTL1_PMR_BUSY));
351f7529f1dSMatthias Ringwald     PCM->CTL0 = PCM_CTL0_KEY_VAL | PCM_CTL0_AMR_4;
352f7529f1dSMatthias Ringwald     while((PCM->CTL1 & PCM_CTL1_PMR_BUSY));
353f7529f1dSMatthias Ringwald     #endif
354f7529f1dSMatthias Ringwald 
355f7529f1dSMatthias Ringwald     // 1 flash wait state (BANK0 VCORE0 max is 12 MHz)
3565fd0122aSMatthias Ringwald     FLCTL->BANK0_RDCTL = (FLCTL->BANK0_RDCTL & ~FLCTL_BANK0_RDCTL_WAIT_MASK) | FLCTL_BANK0_RDCTL_WAIT_1;
3575fd0122aSMatthias Ringwald     FLCTL->BANK1_RDCTL = (FLCTL->BANK1_RDCTL & ~FLCTL_BANK1_RDCTL_WAIT_MASK) | FLCTL_BANK1_RDCTL_WAIT_1;
358f7529f1dSMatthias Ringwald 
359f7529f1dSMatthias Ringwald     // DCO = 24 MHz; MCLK = source
360f7529f1dSMatthias Ringwald     CS->KEY = CS_KEY_VAL;                                  // Unlock CS module for register access
361f7529f1dSMatthias Ringwald     CS->CTL0 = CS_CTL0_DCORSEL_4;                          // Set DCO to 24MHz
3625fd0122aSMatthias Ringwald     CS->CTL1 = (CS->CTL1 & ~(CS_CTL1_SELM_MASK | CS_CTL1_DIVM_MASK)) | CS_CTL1_SELM__DCOCLK;
3635fd0122aSMatthias Ringwald 	                                                       // Select MCLK as DCO source
364f7529f1dSMatthias Ringwald     CS->KEY = 0;
365f7529f1dSMatthias Ringwald 
366f7529f1dSMatthias Ringwald     // Set Flash Bank read buffering
3675fd0122aSMatthias Ringwald     FLCTL->BANK0_RDCTL = FLCTL->BANK0_RDCTL | (FLCTL_BANK0_RDCTL_BUFD | FLCTL_BANK0_RDCTL_BUFI);
3685fd0122aSMatthias Ringwald     FLCTL->BANK1_RDCTL = FLCTL->BANK1_RDCTL & ~(FLCTL_BANK1_RDCTL_BUFD | FLCTL_BANK1_RDCTL_BUFI);
369f7529f1dSMatthias Ringwald 
370f7529f1dSMatthias Ringwald     #elif (__SYSTEM_CLOCK == 48000000)                     // 48 MHz
371f7529f1dSMatthias Ringwald     // Switches LDO VCORE0 to LDO VCORE1; mandatory for 48 MHz setting
372f7529f1dSMatthias Ringwald     while((PCM->CTL1 & PCM_CTL1_PMR_BUSY));
373f7529f1dSMatthias Ringwald     PCM->CTL0 = PCM_CTL0_KEY_VAL | PCM_CTL0_AMR_1;
374f7529f1dSMatthias Ringwald     while((PCM->CTL1 & PCM_CTL1_PMR_BUSY));
375f7529f1dSMatthias Ringwald 
376f7529f1dSMatthias Ringwald     // Switches LDO VCORE1 to DCDC VCORE1 if requested
377f7529f1dSMatthias Ringwald     #if __REGULATOR
378f7529f1dSMatthias Ringwald     while((PCM->CTL1 & PCM_CTL1_PMR_BUSY));
379f7529f1dSMatthias Ringwald     PCM->CTL0 = PCM_CTL0_KEY_VAL | PCM_CTL0_AMR_5;
380f7529f1dSMatthias Ringwald     while((PCM->CTL1 & PCM_CTL1_PMR_BUSY));
381f7529f1dSMatthias Ringwald     #endif
382f7529f1dSMatthias Ringwald 
3835fd0122aSMatthias Ringwald     // 1 flash wait states (BANK0 VCORE1 max is 16 MHz, BANK1 VCORE1 max is 32 MHz)
3845fd0122aSMatthias Ringwald     FLCTL->BANK0_RDCTL = (FLCTL->BANK0_RDCTL & ~FLCTL_BANK0_RDCTL_WAIT_MASK) | FLCTL_BANK0_RDCTL_WAIT_1;
3855fd0122aSMatthias Ringwald     FLCTL->BANK1_RDCTL = (FLCTL->BANK1_RDCTL & ~FLCTL_BANK1_RDCTL_WAIT_MASK) | FLCTL_BANK1_RDCTL_WAIT_1;
386f7529f1dSMatthias Ringwald 
387f7529f1dSMatthias Ringwald     // DCO = 48 MHz; MCLK = source
388f7529f1dSMatthias Ringwald     CS->KEY = CS_KEY_VAL;                                  // Unlock CS module for register access
389f7529f1dSMatthias Ringwald     CS->CTL0 = CS_CTL0_DCORSEL_5;                          // Set DCO to 48MHz
390*142ae15aSMatthias Ringwald //    CS->CTL1 = (CS->CTL1 & ~(CS_CTL1_SELM_MASK | CS_CTL1_DIVM_MASK)) | CS_CTL1_SELM__DCOCLK;
391*142ae15aSMatthias Ringwald //	                                                       // Select MCLK as DCO source
392*142ae15aSMatthias Ringwald     CS->CTL1 = 0x00000033;   // reset value (SMCLK, HSMCLK, MCLK source DCO)
393f7529f1dSMatthias Ringwald     CS->KEY = 0;
394f7529f1dSMatthias Ringwald 
395f7529f1dSMatthias Ringwald     // Set Flash Bank read buffering
3965fd0122aSMatthias Ringwald     FLCTL->BANK0_RDCTL = FLCTL->BANK0_RDCTL | (FLCTL_BANK0_RDCTL_BUFD | FLCTL_BANK0_RDCTL_BUFI);
3975fd0122aSMatthias Ringwald     FLCTL->BANK1_RDCTL = FLCTL->BANK1_RDCTL | (FLCTL_BANK1_RDCTL_BUFD | FLCTL_BANK1_RDCTL_BUFI);
398f7529f1dSMatthias Ringwald     #endif
399f7529f1dSMatthias Ringwald 
400f7529f1dSMatthias Ringwald }
4015fd0122aSMatthias Ringwald 
4025fd0122aSMatthias Ringwald 
403