1 |
580 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
2 |
|
|
* ATMEL Microcontroller Software Support
|
3 |
|
|
* ----------------------------------------------------------------------------
|
4 |
|
|
* Copyright (c) 2008, 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 |
|
|
/// \unit
|
32 |
|
|
///
|
33 |
|
|
/// !Purpose
|
34 |
|
|
///
|
35 |
|
|
/// Provides the low-level initialization function that gets called on chip
|
36 |
|
|
/// startup.
|
37 |
|
|
///
|
38 |
|
|
/// !Usage
|
39 |
|
|
///
|
40 |
|
|
/// LowLevelInit() is called in #board_cstartup_xxx.c#.
|
41 |
|
|
//------------------------------------------------------------------------------
|
42 |
|
|
|
43 |
|
|
//------------------------------------------------------------------------------
|
44 |
|
|
// Headers
|
45 |
|
|
//------------------------------------------------------------------------------
|
46 |
|
|
|
47 |
|
|
#include "board.h"
|
48 |
|
|
#include "board_memories.h"
|
49 |
|
|
#include "board_lowlevel.h"
|
50 |
|
|
#include <pio/pio.h>
|
51 |
|
|
|
52 |
|
|
//------------------------------------------------------------------------------
|
53 |
|
|
// Local definitions
|
54 |
|
|
//------------------------------------------------------------------------------
|
55 |
|
|
// Settings at 48/48MHz
|
56 |
|
|
#define AT91C_CKGR_MUL_SHIFT 16
|
57 |
|
|
#define AT91C_CKGR_OUT_SHIFT 14
|
58 |
|
|
#define AT91C_CKGR_PLLCOUNT_SHIFT 8
|
59 |
|
|
#define AT91C_CKGR_DIV_SHIFT 0
|
60 |
|
|
|
61 |
|
|
#define BOARD_OSCOUNT (AT91C_CKGR_MOSCXTST & (0x3F << 8))
|
62 |
|
|
#define BOARD_PLLR ((1 << 29) | (0x7 << AT91C_CKGR_MUL_SHIFT) \
|
63 |
|
|
| (0x0 << AT91C_CKGR_OUT_SHIFT) |(0x3f << AT91C_CKGR_PLLCOUNT_SHIFT) \
|
64 |
|
|
| (0x1 << AT91C_CKGR_DIV_SHIFT))
|
65 |
|
|
#define BOARD_MCKR ( AT91C_PMC_PRES_CLK_2 | AT91C_PMC_CSS_PLLA_CLK)
|
66 |
|
|
|
67 |
|
|
// Define clock timeout
|
68 |
|
|
#define CLOCK_TIMEOUT 0xFFFFFFFF
|
69 |
|
|
|
70 |
|
|
#define AT91C_SUPC_SR_OSCSEL_CRYST 0x80UL
|
71 |
|
|
#define AT91C_SUPC_CR_XTALSEL_CRYSTAL_SEL 0x08UL
|
72 |
|
|
|
73 |
|
|
void SetDefaultMaster(unsigned char enable);
|
74 |
|
|
|
75 |
|
|
//------------------------------------------------------------------------------
|
76 |
|
|
// Local variables
|
77 |
|
|
//------------------------------------------------------------------------------
|
78 |
|
|
|
79 |
|
|
//------------------------------------------------------------------------------
|
80 |
|
|
// Local functions
|
81 |
|
|
//------------------------------------------------------------------------------
|
82 |
|
|
|
83 |
|
|
//------------------------------------------------------------------------------
|
84 |
|
|
// Exported functions
|
85 |
|
|
//------------------------------------------------------------------------------
|
86 |
|
|
|
87 |
|
|
//------------------------------------------------------------------------------
|
88 |
|
|
/// After POR, at91sam3u device is running on 4MHz internal RC
|
89 |
|
|
/// At the end of the LowLevelInit procedure MCK = 48MHz PLLA = 96 CPU=48MHz
|
90 |
|
|
/// Performs the low-level initialization of the chip. This includes EFC, master
|
91 |
|
|
/// clock, IRQ & watchdog configuration.
|
92 |
|
|
//------------------------------------------------------------------------------
|
93 |
|
|
void LowLevelInit(void)
|
94 |
|
|
{
|
95 |
|
|
unsigned int timeout = 0;
|
96 |
|
|
|
97 |
|
|
/* Set 2 WS for Embedded Flash Access
|
98 |
|
|
************************************/
|
99 |
|
|
AT91C_BASE_EFC0->EFC_FMR = AT91C_EFC_FWS_2WS;
|
100 |
|
|
AT91C_BASE_EFC1->EFC_FMR = AT91C_EFC_FWS_2WS;
|
101 |
|
|
|
102 |
|
|
/* Watchdog initialization
|
103 |
|
|
*************************/
|
104 |
|
|
AT91C_BASE_WDTC->WDTC_WDMR = AT91C_WDTC_WDDIS;
|
105 |
|
|
|
106 |
|
|
/* Select external slow clock
|
107 |
|
|
****************************/
|
108 |
|
|
if ((AT91C_BASE_SUPC->SUPC_SR & AT91C_SUPC_SR_OSCSEL_CRYST) != AT91C_SUPC_SR_OSCSEL_CRYST) {
|
109 |
|
|
AT91C_BASE_SUPC->SUPC_CR = AT91C_SUPC_CR_XTALSEL_CRYSTAL_SEL | (0xA5UL << 24UL);
|
110 |
|
|
timeout = 0;
|
111 |
|
|
while (!(AT91C_BASE_SUPC->SUPC_SR & AT91C_SUPC_SR_OSCSEL_CRYST) && (timeout++ < CLOCK_TIMEOUT));
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
/* Initialize main oscillator
|
115 |
|
|
****************************/
|
116 |
|
|
|
117 |
|
|
if(!(AT91C_BASE_PMC->PMC_MOR & AT91C_CKGR_MOSCSEL))
|
118 |
|
|
{
|
119 |
|
|
AT91C_BASE_PMC->PMC_MOR = (0x37 << 16) | BOARD_OSCOUNT | AT91C_CKGR_MOSCRCEN | AT91C_CKGR_MOSCXTEN;
|
120 |
|
|
timeout = 0;
|
121 |
|
|
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MOSCXTS) && (timeout++ < CLOCK_TIMEOUT));
|
122 |
|
|
}
|
123 |
|
|
else
|
124 |
|
|
{
|
125 |
|
|
AT91C_BASE_PMC->PMC_MOR = (0x37 << 16) | BOARD_OSCOUNT | AT91C_CKGR_MOSCRCEN | AT91C_CKGR_MOSCXTEN | AT91C_CKGR_MOSCSEL;
|
126 |
|
|
timeout = 0;
|
127 |
|
|
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MOSCRCS) && (timeout++ < CLOCK_TIMEOUT));
|
128 |
|
|
AT91C_BASE_PMC->PMC_MOR = (0x37 << 16) | BOARD_OSCOUNT | AT91C_CKGR_MOSCRCEN | AT91C_CKGR_MOSCXTEN;
|
129 |
|
|
timeout = 0;
|
130 |
|
|
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MOSCSELS) && (timeout++ < CLOCK_TIMEOUT));
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
/* Switch to moscsel */
|
134 |
|
|
AT91C_BASE_PMC->PMC_MOR = (0x37 << 16) | BOARD_OSCOUNT | AT91C_CKGR_MOSCRCEN | AT91C_CKGR_MOSCXTEN | AT91C_CKGR_MOSCSEL;
|
135 |
|
|
timeout = 0;
|
136 |
|
|
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MOSCSELS) && (timeout++ < CLOCK_TIMEOUT));
|
137 |
|
|
AT91C_BASE_PMC->PMC_MCKR = (AT91C_BASE_PMC->PMC_MCKR & ~AT91C_PMC_CSS) | AT91C_PMC_CSS_MAIN_CLK;
|
138 |
|
|
timeout = 0;
|
139 |
|
|
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY) && (timeout++ < CLOCK_TIMEOUT));
|
140 |
|
|
|
141 |
|
|
/* Initialize PLLA */
|
142 |
|
|
AT91C_BASE_PMC->PMC_PLLAR = BOARD_PLLR;
|
143 |
|
|
timeout = 0;
|
144 |
|
|
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_LOCKA) && (timeout++ < CLOCK_TIMEOUT));
|
145 |
|
|
|
146 |
|
|
/* Initialize UTMI for USB usage */
|
147 |
|
|
AT91C_BASE_CKGR->CKGR_UCKR |= (AT91C_CKGR_UPLLCOUNT & (3 << 20)) | AT91C_CKGR_UPLLEN;
|
148 |
|
|
timeout = 0;
|
149 |
|
|
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_LOCKU) && (timeout++ < CLOCK_TIMEOUT));
|
150 |
|
|
|
151 |
|
|
/* Switch to fast clock
|
152 |
|
|
**********************/
|
153 |
|
|
AT91C_BASE_PMC->PMC_MCKR = (BOARD_MCKR & ~AT91C_PMC_CSS) | AT91C_PMC_CSS_MAIN_CLK;
|
154 |
|
|
timeout = 0;
|
155 |
|
|
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY) && (timeout++ < CLOCK_TIMEOUT));
|
156 |
|
|
|
157 |
|
|
AT91C_BASE_PMC->PMC_MCKR = BOARD_MCKR;
|
158 |
|
|
timeout = 0;
|
159 |
|
|
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY) && (timeout++ < CLOCK_TIMEOUT));
|
160 |
|
|
|
161 |
|
|
/* Enable clock for UART
|
162 |
|
|
************************/
|
163 |
|
|
AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_DBGU);
|
164 |
|
|
|
165 |
|
|
/* Optimize CPU setting for speed */
|
166 |
|
|
SetDefaultMaster(1);
|
167 |
|
|
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
//------------------------------------------------------------------------------
|
171 |
|
|
/// Enable or disable default master access
|
172 |
|
|
/// \param enalbe 1 enable defaultMaster settings, 0 disable it.
|
173 |
|
|
//------------------------------------------------------------------------------
|
174 |
|
|
void SetDefaultMaster(unsigned char enable)
|
175 |
|
|
{
|
176 |
|
|
AT91PS_HMATRIX2 pMatrix = AT91C_BASE_MATRIX;
|
177 |
|
|
|
178 |
|
|
// Set default master
|
179 |
|
|
if (enable == 1) {
|
180 |
|
|
|
181 |
|
|
// Set default master: SRAM0 -> Cortex-M3 System
|
182 |
|
|
pMatrix->HMATRIX2_SCFG0 |= AT91C_MATRIX_FIXED_DEFMSTR_SCFG0_ARMS |
|
183 |
|
|
AT91C_MATRIX_DEFMSTR_TYPE_FIXED_DEFMSTR;
|
184 |
|
|
|
185 |
|
|
// Set default master: SRAM1 -> Cortex-M3 System
|
186 |
|
|
pMatrix->HMATRIX2_SCFG1 |= AT91C_MATRIX_FIXED_DEFMSTR_SCFG1_ARMS |
|
187 |
|
|
AT91C_MATRIX_DEFMSTR_TYPE_FIXED_DEFMSTR;
|
188 |
|
|
|
189 |
|
|
// Set default master: Internal flash0 -> Cortex-M3 Instruction/Data
|
190 |
|
|
pMatrix->HMATRIX2_SCFG3 |= AT91C_MATRIX_FIXED_DEFMSTR_SCFG3_ARMC |
|
191 |
|
|
AT91C_MATRIX_DEFMSTR_TYPE_FIXED_DEFMSTR;
|
192 |
|
|
} else {
|
193 |
|
|
|
194 |
|
|
// Clear default master: SRAM0 -> Cortex-M3 System
|
195 |
|
|
pMatrix->HMATRIX2_SCFG0 &= (~AT91C_MATRIX_DEFMSTR_TYPE);
|
196 |
|
|
|
197 |
|
|
// Clear default master: SRAM1 -> Cortex-M3 System
|
198 |
|
|
pMatrix->HMATRIX2_SCFG1 &= (~AT91C_MATRIX_DEFMSTR_TYPE);
|
199 |
|
|
|
200 |
|
|
// Clear default master: Internal flash0 -> Cortex-M3 Instruction/Data
|
201 |
|
|
pMatrix->HMATRIX2_SCFG3 &= (~AT91C_MATRIX_DEFMSTR_TYPE);
|
202 |
|
|
}
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
//------------------------------------------------------------------------------
|
206 |
|
|
/// Set flash wait state
|
207 |
|
|
/// \param ws Value of flash wait state
|
208 |
|
|
//------------------------------------------------------------------------------
|
209 |
|
|
void SetFlashWaitState(unsigned char ws)
|
210 |
|
|
{
|
211 |
|
|
// Set Wait State for Embedded Flash Access
|
212 |
|
|
AT91C_BASE_EFC0->EFC_FMR = ((ws << 8) & AT91C_EFC_FWS);
|
213 |
|
|
AT91C_BASE_EFC1->EFC_FMR = ((ws << 8) & AT91C_EFC_FWS);
|
214 |
|
|
}
|
215 |
|
|
|