| 1 | //----------------------------------------------------------------------------- |
| 2 | // ATMEL Microcontroller Software Support - ROUSSET - |
| 3 | //----------------------------------------------------------------------------- |
| 4 | // DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 5 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 6 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 7 | // DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 8 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 9 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 10 | // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 11 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 12 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 13 | // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 14 | //----------------------------------------------------------------------------- |
| 15 | // File Name : Cstartup_SAM7.c |
| 16 | // Object : Low level initialisations written in C for Tools |
| 17 | // For AT91SAM7X256 with 2 flash plane |
| 18 | // Creation : JPP 14-Sep-2006 |
| 19 | //----------------------------------------------------------------------------- |
| 20 | |
| 21 | #include "project.h" |
| 22 | |
| 23 | |
| 24 | // The following functions must be write in ARM mode this function called |
| 25 | // directly by exception vector |
| 26 | extern void AT91F_Spurious_handler(void); |
| 27 | extern void AT91F_Default_IRQ_handler(void); |
| 28 | extern void AT91F_Default_FIQ_handler(void); |
| 29 | |
| 30 | //*---------------------------------------------------------------------------- |
| 31 | //* \fn AT91F_LowLevelInit |
| 32 | //* \brief This function performs very low level HW initialization |
| 33 | //* this function can use a Stack, depending the compilation |
| 34 | //* optimization mode |
| 35 | //*---------------------------------------------------------------------------- |
| 36 | void AT91F_LowLevelInit(void) @ "ICODE" |
| 37 | { |
| 38 | unsigned char i; |
| 39 | /////////////////////////////////////////////////////////////////////////// |
| 40 | // EFC Init |
| 41 | /////////////////////////////////////////////////////////////////////////// |
| 42 | AT91C_BASE_MC->MC_FMR = AT91C_MC_FWS_1FWS ; |
| 43 | |
| 44 | /////////////////////////////////////////////////////////////////////////// |
| 45 | // Init PMC Step 1. Enable Main Oscillator |
| 46 | // Main Oscillator startup time is board specific: |
| 47 | // Main Oscillator Startup Time worst case (3MHz) corresponds to 15ms |
| 48 | // (0x40 for AT91C_CKGR_OSCOUNT field) |
| 49 | /////////////////////////////////////////////////////////////////////////// |
| 50 | AT91C_BASE_PMC->PMC_MOR = (( AT91C_CKGR_OSCOUNT & (0x40 <<8) | AT91C_CKGR_MOSCEN )); |
| 51 | // Wait Main Oscillator stabilization |
| 52 | while(!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MOSCS)); |
| 53 | |
| 54 | /////////////////////////////////////////////////////////////////////////// |
| 55 | // Init PMC Step 2. |
| 56 | // Set PLL to 96MHz (96,109MHz) and UDP Clock to 48MHz |
| 57 | // PLL Startup time depends on PLL RC filter: worst case is choosen |
| 58 | // UDP Clock (48,058MHz) is compliant with the Universal Serial Bus |
| 59 | // Specification (+/- 0.25% for full speed) |
| 60 | /////////////////////////////////////////////////////////////////////////// |
| 61 | AT91C_BASE_PMC->PMC_PLLR = AT91C_CKGR_USBDIV_1 | |
| 62 | (16 << 8) | |
| 63 | (AT91C_CKGR_MUL & (72 << 16)) | |
| 64 | (AT91C_CKGR_DIV & 14); |
| 65 | // Wait for PLL stabilization |
| 66 | while( !(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_LOCK) ); |
| 67 | // Wait until the master clock is established for the case we already |
| 68 | // turn on the PLL |
| 69 | while( !(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY) ); |
| 70 | |
| 71 | /////////////////////////////////////////////////////////////////////////// |
| 72 | // Init PMC Step 3. |
| 73 | // Selection of Master Clock MCK equal to (Processor Clock PCK) PLL/2=48MHz |
| 74 | // The PMC_MCKR register must not be programmed in a single write operation |
| 75 | // (see. Product Errata Sheet) |
| 76 | /////////////////////////////////////////////////////////////////////////// |
| 77 | AT91C_BASE_PMC->PMC_MCKR = AT91C_PMC_PRES_CLK_2; |
| 78 | // Wait until the master clock is established |
| 79 | while( !(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY) ); |
| 80 | |
| 81 | AT91C_BASE_PMC->PMC_MCKR |= AT91C_PMC_CSS_PLL_CLK; |
| 82 | // Wait until the master clock is established |
| 83 | while( !(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY) ); |
| 84 | |
| 85 | /////////////////////////////////////////////////////////////////////////// |
| 86 | // Disable Watchdog (write once register) |
| 87 | /////////////////////////////////////////////////////////////////////////// |
| 88 | AT91C_BASE_WDTC->WDTC_WDMR = AT91C_WDTC_WDDIS; |
| 89 | |
| 90 | /////////////////////////////////////////////////////////////////////////// |
| 91 | // Init AIC: assign corresponding handler for each interrupt source |
| 92 | /////////////////////////////////////////////////////////////////////////// |
| 93 | AT91C_BASE_AIC->AIC_SVR[0] = (int) AT91F_Default_FIQ_handler ; |
| 94 | for (i = 1; i < 31; i++) { |
| 95 | AT91C_BASE_AIC->AIC_SVR[i] = (int) AT91F_Default_IRQ_handler ; |
| 96 | } |
| 97 | AT91C_BASE_AIC->AIC_SPU = (unsigned int) AT91F_Spurious_handler; |
| 98 | } |