hdo  beta
/home/mrazik/Documents/web/old/hdo/at91lib/boards/at91sam7s-ek/board_lowlevel.c
00001 /* ----------------------------------------------------------------------------
00002  *         ATMEL Microcontroller Software Support 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2008, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 
00031 //------------------------------------------------------------------------------
00032 //         Headers
00033 //------------------------------------------------------------------------------
00034 
00035 #include "board.h"
00036 #include "board_memories.h"
00037 #include <pmc/pmc.h>
00038 
00039 //------------------------------------------------------------------------------
00040 //         Internal definitions
00041 //------------------------------------------------------------------------------
00042 // Startup time of main oscillator (in number of slow clock ticks).
00043 #define BOARD_OSCOUNT           (AT91C_CKGR_OSCOUNT & (0x40 << 8))
00044 
00045 // USB PLL divisor value to obtain a 48MHz clock.
00046 #define BOARD_USBDIV            AT91C_CKGR_USBDIV_1
00047 
00048 // PLL frequency range.
00049 #define BOARD_CKGR_PLL          AT91C_CKGR_OUT_0
00050 
00051 // PLL startup time (in number of slow clock ticks).
00052 #define BOARD_PLLCOUNT          (16 << 8)
00053 
00054 // PLL MUL value.
00055 #define BOARD_MUL               (AT91C_CKGR_MUL & (72 << 16))
00056 
00057 // PLL DIV value.
00058 #define BOARD_DIV               (AT91C_CKGR_DIV & 14)
00059 
00060 // Master clock prescaler value.
00061 #define BOARD_PRESCALER         AT91C_PMC_PRES_CLK_2
00062 
00063 //------------------------------------------------------------------------------
00064 //         Internal functions
00065 //------------------------------------------------------------------------------
00066 //------------------------------------------------------------------------------
00068 //------------------------------------------------------------------------------
00069 void defaultSpuriousHandler( void )
00070 {
00071     while (1);
00072 }
00073 
00074 //------------------------------------------------------------------------------
00076 //------------------------------------------------------------------------------
00077 void defaultFiqHandler( void )
00078 {
00079     while (1);
00080 }
00081 
00082 //------------------------------------------------------------------------------
00084 //------------------------------------------------------------------------------
00085 void defaultIrqHandler( void )
00086 {
00087     while (1);
00088 }
00089 
00090 //------------------------------------------------------------------------------
00091 //         Global Functions
00092 //------------------------------------------------------------------------------
00093 
00094 //------------------------------------------------------------------------------
00097 //------------------------------------------------------------------------------
00098 void LowLevelInit(void)
00099 {
00100     unsigned char i;
00101 
00102     // Set flash wait states in the EFC
00103     // 48MHz = 1 wait state
00104 #if defined(at91sam7s512)
00105     AT91C_BASE_EFC0->EFC_FMR = AT91C_MC_FWS_1FWS;
00106     AT91C_BASE_EFC1->EFC_FMR = AT91C_MC_FWS_1FWS;
00107 #else
00108     AT91C_BASE_MC->MC_FMR = AT91C_MC_FWS_1FWS;
00109 #endif
00110 
00111     // Initialize main oscillator
00112     AT91C_BASE_PMC->PMC_MOR = BOARD_OSCOUNT | AT91C_CKGR_MOSCEN;
00113     while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MOSCS));
00114 
00115     // Initialize PLL at 96MHz (96.109) and USB clock to 48MHz
00116     AT91C_BASE_PMC->PMC_PLLR = BOARD_USBDIV | BOARD_CKGR_PLL | BOARD_PLLCOUNT
00117                                | BOARD_MUL | BOARD_DIV;
00118     while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_LOCK));
00119 
00120     // Wait for the master clock if it was already initialized
00121     while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY));
00122 
00123     // Switch to slow clock + prescaler
00124     AT91C_BASE_PMC->PMC_MCKR = BOARD_PRESCALER;
00125     while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY));
00126 
00127     // Switch to fast clock + prescaler
00128     AT91C_BASE_PMC->PMC_MCKR |= AT91C_PMC_CSS_PLL_CLK;
00129     while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY));
00130 
00131     // Initialize AIC
00132     AT91C_BASE_AIC->AIC_IDCR = 0xFFFFFFFF;
00133     AT91C_BASE_AIC->AIC_SVR[0] = (unsigned int) defaultFiqHandler;
00134     for (i = 1; i < 31; i++) {
00135 
00136         AT91C_BASE_AIC->AIC_SVR[i] = (unsigned int) defaultIrqHandler;
00137     }
00138     AT91C_BASE_AIC->AIC_SPU = (unsigned int) defaultSpuriousHandler;
00139 
00140     // Unstack nested interrupts
00141     for (i = 0; i < 8 ; i++) {
00142 
00143         AT91C_BASE_AIC->AIC_EOICR = 0;
00144     }
00145 
00146     // Enable Debug mode
00147     AT91C_BASE_AIC->AIC_DCR = AT91C_AIC_DCR_PROT;
00148 
00149     // Watchdog initialization
00150     AT91C_BASE_WDTC->WDTC_WDMR = AT91C_WDTC_WDDIS;
00151 
00152     // Remap the internal SRAM at 0x0
00153     BOARD_RemapRam();
00154 
00155     // Disable RTT and PIT interrupts (potential problem when program A
00156     // configures RTT, then program B wants to use PIT only, interrupts
00157     // from the RTT will still occur since they both use AT91C_ID_SYS)
00158     AT91C_BASE_RTTC->RTTC_RTMR &= ~(AT91C_RTTC_ALMIEN | AT91C_RTTC_RTTINCIEN);
00159     AT91C_BASE_PITC->PITC_PIMR &= ~AT91C_PITC_PITIEN;
00160 }
00161