Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------------
|
| 2 | * ATMEL Microcontroller Software Support
|
| 3 | * ----------------------------------------------------------------------------
|
| 4 | * Copyright (c) 2009, Atmel Corporation
|
| 5 | *
|
| 6 | * All rights reserved.
|
| 7 | *
|
| 8 | * Redistribution and use in source and binary forms, with or without
|
| 9 | * modification, are permitted provided that the following conditions are met:
|
| 10 | *
|
| 11 | * - Redistributions of source code must retain the above copyright notice,
|
| 12 | * this list of conditions and the disclaimer below.
|
| 13 | *
|
| 14 | * Atmel's name may not be used to endorse or promote products derived from
|
| 15 | * this software without specific prior written permission.
|
| 16 | *
|
| 17 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
| 20 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
| 23 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
| 24 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
| 25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
| 26 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 27 | * ----------------------------------------------------------------------------
|
| 28 | */
|
| 29 |
|
| 30 | /**
|
| 31 | * \file
|
| 32 | *
|
| 33 | * Provides the low-level initialization function that called on chip startup.
|
| 34 | */
|
| 35 |
|
| 36 | /*----------------------------------------------------------------------------
|
| 37 | * Headers
|
| 38 | *----------------------------------------------------------------------------*/
|
| 39 |
|
| 40 | #include "board.h"
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 41 |
|
| 42 | /*----------------------------------------------------------------------------
|
| 43 | * Local definitions
|
| 44 | *----------------------------------------------------------------------------*/
|
| 45 |
|
Christina Quast | 530f208 | 2014-12-05 13:03:59 +0100 | [diff] [blame] | 46 | /* Clock settings at 48MHz for 18 MHz crystal */
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 47 | #if (BOARD_MCK == 48000000)
|
| 48 | #define BOARD_OSCOUNT (CKGR_MOR_MOSCXTST(0x8))
|
| 49 | #define BOARD_PLLAR (CKGR_PLLAR_STUCKTO1 \
|
Christina Quast | 530f208 | 2014-12-05 13:03:59 +0100 | [diff] [blame] | 50 | | CKGR_PLLAR_MULA(0xc) \
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 51 | | CKGR_PLLAR_PLLACOUNT(0x1) \
|
Christina Quast | 530f208 | 2014-12-05 13:03:59 +0100 | [diff] [blame] | 52 | | CKGR_PLLAR_DIVA(0x5))
|
| 53 | #define BOARD_MCKR (PMC_MCKR_PRES_CLK | PMC_MCKR_CSS_PLLA_CLK)
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 54 |
|
Christina Quast | 530f208 | 2014-12-05 13:03:59 +0100 | [diff] [blame] | 55 | /* Clock settings at 64MHz for 18 MHz crystal */
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 56 | #elif (BOARD_MCK == 64000000)
|
| 57 | #define BOARD_OSCOUNT (CKGR_MOR_MOSCXTST(0x8))
|
| 58 | #define BOARD_PLLAR (CKGR_PLLAR_STUCKTO1 \
|
Christina Quast | 530f208 | 2014-12-05 13:03:59 +0100 | [diff] [blame] | 59 | | CKGR_PLLAR_MULA(0x06) \
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 60 | | CKGR_PLLAR_PLLACOUNT(0x1) \
|
Christina Quast | 530f208 | 2014-12-05 13:03:59 +0100 | [diff] [blame] | 61 | | CKGR_PLLAR_DIVA(0x2))
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 62 | #define BOARD_MCKR (PMC_MCKR_PRES_CLK | PMC_MCKR_CSS_PLLA_CLK)
|
| 63 |
|
| 64 | #else
|
| 65 | #error "No settings for current BOARD_MCK."
|
| 66 | #endif
|
| 67 |
|
| 68 | /* Define clock timeout */
|
| 69 | #define CLOCK_TIMEOUT 0xFFFFFFFF
|
| 70 |
|
| 71 | /*----------------------------------------------------------------------------
|
| 72 | * Exported functions
|
| 73 | *----------------------------------------------------------------------------*/
|
| 74 |
|
| 75 | /**
|
| 76 | * \brief Performs the low-level initialization of the chip.
|
| 77 | * This includes EFC and master clock configuration.
|
| 78 | * It also enable a low level on the pin NRST triggers a user reset.
|
| 79 | */
|
Christina Quast | 8be71e4 | 2014-12-02 13:06:01 +0100 | [diff] [blame] | 80 | extern WEAK void LowLevelInit( void )
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 81 | {
|
| 82 | uint32_t timeout = 0;
|
| 83 |
|
| 84 | /* Set 3 FWS for Embedded Flash Access */
|
| 85 | EFC->EEFC_FMR = EEFC_FMR_FWS(3);
|
| 86 |
|
| 87 | /* Select external slow clock */
|
Christina Quast | 8be71e4 | 2014-12-02 13:06:01 +0100 | [diff] [blame] | 88 | /* if ((SUPC->SUPC_SR & SUPC_SR_OSCSEL) != SUPC_SR_OSCSEL_CRYST)
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 89 | {
|
| 90 | SUPC->SUPC_CR = (uint32_t)(SUPC_CR_XTALSEL_CRYSTAL_SEL | SUPC_CR_KEY(0xA5));
|
| 91 | timeout = 0;
|
| 92 | while (!(SUPC->SUPC_SR & SUPC_SR_OSCSEL_CRYST) );
|
| 93 | }
|
Christina Quast | 8be71e4 | 2014-12-02 13:06:01 +0100 | [diff] [blame] | 94 | */
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 95 |
|
| 96 | /* Initialize main oscillator */
|
Christina Quast | 8be71e4 | 2014-12-02 13:06:01 +0100 | [diff] [blame] | 97 | /* if ( !(PMC->CKGR_MOR & CKGR_MOR_MOSCSEL) )
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 98 | {
|
| 99 | PMC->CKGR_MOR = CKGR_MOR_KEY(0x37) | BOARD_OSCOUNT | CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN;
|
| 100 | timeout = 0;
|
| 101 | while (!(PMC->PMC_SR & PMC_SR_MOSCXTS) && (timeout++ < CLOCK_TIMEOUT));
|
Christina Quast | 8be71e4 | 2014-12-02 13:06:01 +0100 | [diff] [blame] | 102 | }*/
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 103 |
|
| 104 | /* Switch to 3-20MHz Xtal oscillator */
|
| 105 | PMC->CKGR_MOR = CKGR_MOR_KEY(0x37) | BOARD_OSCOUNT | CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN | CKGR_MOR_MOSCSEL;
|
| 106 | timeout = 0;
|
| 107 | while (!(PMC->PMC_SR & PMC_SR_MOSCSELS) && (timeout++ < CLOCK_TIMEOUT));
|
| 108 | PMC->PMC_MCKR = (PMC->PMC_MCKR & ~(uint32_t)PMC_MCKR_CSS_Msk) | PMC_MCKR_CSS_MAIN_CLK;
|
| 109 | for ( timeout = 0; !(PMC->PMC_SR & PMC_SR_MCKRDY) && (timeout++ < CLOCK_TIMEOUT) ; );
|
| 110 |
|
| 111 | /* Initialize PLLA */
|
| 112 | PMC->CKGR_PLLAR = BOARD_PLLAR;
|
| 113 | timeout = 0;
|
| 114 | while (!(PMC->PMC_SR & PMC_SR_LOCKA) && (timeout++ < CLOCK_TIMEOUT));
|
| 115 |
|
| 116 | /* Switch to main clock */
|
| 117 | PMC->PMC_MCKR = (BOARD_MCKR & ~PMC_MCKR_CSS_Msk) | PMC_MCKR_CSS_MAIN_CLK;
|
| 118 | for ( timeout = 0; !(PMC->PMC_SR & PMC_SR_MCKRDY) && (timeout++ < CLOCK_TIMEOUT) ; );
|
| 119 |
|
| 120 | PMC->PMC_MCKR = BOARD_MCKR ;
|
| 121 | for ( timeout = 0; !(PMC->PMC_SR & PMC_SR_MCKRDY) && (timeout++ < CLOCK_TIMEOUT) ; );
|
| 122 | }
|