4 * \brief Provides the low-level initialization functions that called
\r
7 * Copyright (c) 2011 - 2012 Atmel Corporation. All rights reserved.
\r
13 * Redistribution and use in source and binary forms, with or without
\r
14 * modification, are permitted provided that the following conditions are met:
\r
16 * 1. Redistributions of source code must retain the above copyright notice,
\r
17 * this list of conditions and the following disclaimer.
\r
19 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
20 * this list of conditions and the following disclaimer in the documentation
\r
21 * and/or other materials provided with the distribution.
\r
23 * 3. The name of Atmel may not be used to endorse or promote products derived
\r
24 * from this software without specific prior written permission.
\r
26 * 4. This software may only be redistributed and used in connection with an
\r
27 * Atmel microcontroller product.
\r
29 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
\r
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
32 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
\r
33 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
\r
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
\r
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
\r
38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
39 * POSSIBILITY OF SUCH DAMAGE.
\r
55 /* Clock Settings (120MHz) */
\r
56 #define SYS_BOARD_OSCOUNT (CKGR_MOR_MOSCXTST(0x8U))
\r
57 #define SYS_BOARD_PLLAR (CKGR_PLLAR_ONE \
\r
58 | CKGR_PLLAR_MULA(0x13U) \
\r
59 | CKGR_PLLAR_PLLACOUNT(0x3fU) \
\r
60 | CKGR_PLLAR_DIVA(0x1U))
\r
61 #define SYS_BOARD_MCKR (PMC_MCKR_PRES_CLK_2 | PMC_MCKR_CSS_PLLA_CLK)
\r
63 #define SYS_CKGR_MOR_KEY_VALUE CKGR_MOR_KEY(0x37) /* Key to unlock MOR register */
\r
65 uint32_t SystemCoreClock = CHIP_FREQ_MAINCK_RC_4MHZ;
\r
68 * \brief Setup the microcontroller system.
\r
69 * Initialize the System and update the SystemFrequency variable.
\r
71 void SystemInit( void )
\r
73 /* Set FWS according to SYS_BOARD_MCKR configuration */
\r
74 EFC->EEFC_FMR = EEFC_FMR_FWS(5);
\r
76 /* Initialize main oscillator */
\r
77 if ( !(PMC->CKGR_MOR & CKGR_MOR_MOSCSEL) ) {
\r
78 PMC->CKGR_MOR = SYS_CKGR_MOR_KEY_VALUE | SYS_BOARD_OSCOUNT |
\r
79 CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN;
\r
81 while ( !(PMC->PMC_SR & PMC_SR_MOSCXTS) ) {
\r
85 /* Switch to 3-20MHz Xtal oscillator */
\r
86 PMC->CKGR_MOR = SYS_CKGR_MOR_KEY_VALUE | SYS_BOARD_OSCOUNT |
\r
87 CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN |
\r
90 while ( !(PMC->PMC_SR & PMC_SR_MOSCSELS) ) {
\r
93 PMC->PMC_MCKR = (PMC->PMC_MCKR & ~(uint32_t)PMC_MCKR_CSS_Msk) |
\r
94 PMC_MCKR_CSS_MAIN_CLK;
\r
96 while ( !(PMC->PMC_SR & PMC_SR_MCKRDY) ) {
\r
99 /* Initialize PLLA */
\r
100 PMC->CKGR_PLLAR = SYS_BOARD_PLLAR;
\r
101 while ( !(PMC->PMC_SR & PMC_SR_LOCKA) ) {
\r
104 /* Switch to main clock */
\r
105 PMC->PMC_MCKR = (SYS_BOARD_MCKR & ~PMC_MCKR_CSS_Msk) |
\r
106 PMC_MCKR_CSS_MAIN_CLK;
\r
107 while ( !(PMC->PMC_SR & PMC_SR_MCKRDY) ) {
\r
110 /* Switch to PLLA */
\r
111 PMC->PMC_MCKR = SYS_BOARD_MCKR;
\r
112 while ( !(PMC->PMC_SR & PMC_SR_MCKRDY) ) {
\r
115 SystemCoreClock = CHIP_FREQ_CPU_MAX;
\r
118 void SystemCoreClockUpdate( void )
\r
120 /* Determine clock frequency according to clock register values */
\r
121 switch (PMC->PMC_MCKR & (uint32_t) PMC_MCKR_CSS_Msk) {
\r
122 case PMC_MCKR_CSS_SLOW_CLK: /* Slow clock */
\r
123 if ( SUPC->SUPC_SR & SUPC_SR_OSCSEL ) {
\r
124 SystemCoreClock = CHIP_FREQ_XTAL_32K;
\r
126 SystemCoreClock = CHIP_FREQ_SLCK_RC;
\r
130 case PMC_MCKR_CSS_MAIN_CLK: /* Main clock */
\r
131 if ( PMC->CKGR_MOR & CKGR_MOR_MOSCSEL ) {
\r
132 SystemCoreClock = CHIP_FREQ_XTAL_12M;
\r
134 SystemCoreClock = CHIP_FREQ_MAINCK_RC_4MHZ;
\r
136 switch ( PMC->CKGR_MOR & CKGR_MOR_MOSCRCF_Msk ) {
\r
137 case CKGR_MOR_MOSCRCF_4_MHz:
\r
140 case CKGR_MOR_MOSCRCF_8_MHz:
\r
141 SystemCoreClock *= 2U;
\r
144 case CKGR_MOR_MOSCRCF_12_MHz:
\r
145 SystemCoreClock *= 3U;
\r
154 case PMC_MCKR_CSS_PLLA_CLK: /* PLLA clock */
\r
155 if ( PMC->CKGR_MOR & CKGR_MOR_MOSCSEL ) {
\r
156 SystemCoreClock = CHIP_FREQ_XTAL_12M ;
\r
158 SystemCoreClock = CHIP_FREQ_MAINCK_RC_4MHZ;
\r
160 switch ( PMC->CKGR_MOR & CKGR_MOR_MOSCRCF_Msk ) {
\r
161 case CKGR_MOR_MOSCRCF_4_MHz:
\r
164 case CKGR_MOR_MOSCRCF_8_MHz:
\r
165 SystemCoreClock *= 2U;
\r
168 case CKGR_MOR_MOSCRCF_12_MHz:
\r
169 SystemCoreClock *= 3U;
\r
177 if ((uint32_t) (PMC->PMC_MCKR & (uint32_t) PMC_MCKR_CSS_Msk) == PMC_MCKR_CSS_PLLA_CLK)
\r
179 SystemCoreClock *= ((((PMC->CKGR_PLLAR) & CKGR_PLLAR_MULA_Msk) >> CKGR_PLLAR_MULA_Pos) + 1U);
\r
180 SystemCoreClock /= ((((PMC->CKGR_PLLAR) & CKGR_PLLAR_DIVA_Msk) >> CKGR_PLLAR_DIVA_Pos));
\r
188 if ((PMC->PMC_MCKR & PMC_MCKR_PRES_Msk) == PMC_MCKR_PRES_CLK_3) {
\r
189 SystemCoreClock /= 3U;
\r
191 SystemCoreClock >>= ((PMC->PMC_MCKR & PMC_MCKR_PRES_Msk) >>
\r
192 PMC_MCKR_PRES_Pos);
\r
197 * Initialize flash.
\r
199 void system_init_flash( uint32_t ul_clk )
\r
201 /* Set FWS for embedded Flash access according to operating frequency */
\r
202 if ( ul_clk < CHIP_FREQ_FWS_0 ) {
\r
203 EFC->EEFC_FMR = EEFC_FMR_FWS(0);
\r
205 if (ul_clk < CHIP_FREQ_FWS_1) {
\r
206 EFC->EEFC_FMR = EEFC_FMR_FWS(1);
\r
208 if (ul_clk < CHIP_FREQ_FWS_2) {
\r
209 EFC->EEFC_FMR = EEFC_FMR_FWS(2);
\r
211 if ( ul_clk < CHIP_FREQ_FWS_3 ) {
\r
212 EFC->EEFC_FMR = EEFC_FMR_FWS(3);
\r
214 if ( ul_clk < CHIP_FREQ_FWS_4 ) {
\r
215 EFC->EEFC_FMR = EEFC_FMR_FWS(4);
\r
217 EFC->EEFC_FMR = EEFC_FMR_FWS(5);
\r