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 |
|
|
// Headers
|
32 |
|
|
//------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
#include "pmc.h"
|
35 |
|
|
#include <board.h>
|
36 |
|
|
#include <utility/assert.h>
|
37 |
|
|
#include <utility/trace.h>
|
38 |
|
|
|
39 |
|
|
#ifdef CP15_PRESENT
|
40 |
|
|
#include <cp15/cp15.h>
|
41 |
|
|
#endif
|
42 |
|
|
|
43 |
|
|
#define MASK_STATUS 0x3FFFFFFC
|
44 |
|
|
|
45 |
|
|
//------------------------------------------------------------------------------
|
46 |
|
|
// Global functions
|
47 |
|
|
//------------------------------------------------------------------------------
|
48 |
|
|
|
49 |
|
|
#if defined(at91sam7l64) || defined(at91sam7l128)
|
50 |
|
|
//------------------------------------------------------------------------------
|
51 |
|
|
/// Sets the fast wake-up inputs that can get the device out of Wait mode.
|
52 |
|
|
/// \param inputs Fast wake-up inputs to enable.
|
53 |
|
|
//------------------------------------------------------------------------------
|
54 |
|
|
void PMC_SetFastWakeUpInputs(unsigned int inputs)
|
55 |
|
|
{
|
56 |
|
|
SANITY_CHECK((inputs & ~0xFF) == 0);
|
57 |
|
|
AT91C_BASE_PMC->PMC_FSMR = inputs;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
#if !defined(__ICCARM__)
|
61 |
|
|
__attribute__ ((section (".ramfunc"))) // GCC
|
62 |
|
|
#endif
|
63 |
|
|
//------------------------------------------------------------------------------
|
64 |
|
|
/// Disables the main oscillator, making the device enter Wait mode.
|
65 |
|
|
//------------------------------------------------------------------------------
|
66 |
|
|
void PMC_DisableMainOscillatorForWaitMode(void)
|
67 |
|
|
{
|
68 |
|
|
AT91C_BASE_PMC->PMC_MOR = 0x37 << 16;
|
69 |
|
|
while ((AT91C_BASE_PMC->PMC_MOR & AT91C_PMC_MAINSELS) != AT91C_PMC_MAINSELS);
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
#endif
|
73 |
|
|
|
74 |
|
|
#if defined(at91sam7l)
|
75 |
|
|
//------------------------------------------------------------------------------
|
76 |
|
|
/// Disables the main oscillator when NOT running on it.
|
77 |
|
|
//------------------------------------------------------------------------------
|
78 |
|
|
void PMC_DisableMainOscillator(void)
|
79 |
|
|
{
|
80 |
|
|
AT91C_BASE_PMC->PMC_MOR = 0x37 << 16;
|
81 |
|
|
while ((AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MAINSELS) == AT91C_PMC_MAINSELS);
|
82 |
|
|
}
|
83 |
|
|
#endif
|
84 |
|
|
|
85 |
|
|
//------------------------------------------------------------------------------
|
86 |
|
|
/// Disables the processor clock
|
87 |
|
|
//------------------------------------------------------------------------------
|
88 |
|
|
void PMC_DisableProcessorClock(void)
|
89 |
|
|
{
|
90 |
|
|
AT91C_BASE_PMC->PMC_SCDR = AT91C_PMC_PCK;
|
91 |
|
|
while ((AT91C_BASE_PMC->PMC_SCSR & AT91C_PMC_PCK) != AT91C_PMC_PCK);
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
//------------------------------------------------------------------------------
|
95 |
|
|
/// Enables the clock of a peripheral. The peripheral ID (AT91C_ID_xxx) is used
|
96 |
|
|
/// to identify which peripheral is targetted.
|
97 |
|
|
/// Note that the ID must NOT be shifted (i.e. 1 << AT91C_ID_xxx).
|
98 |
|
|
/// \param id Peripheral ID (AT91C_ID_xxx).
|
99 |
|
|
//------------------------------------------------------------------------------
|
100 |
|
|
void PMC_EnablePeripheral(unsigned int id)
|
101 |
|
|
{
|
102 |
|
|
SANITY_CHECK(id < 32);
|
103 |
|
|
|
104 |
|
|
if ((AT91C_BASE_PMC->PMC_PCSR & (1 << id)) == (1 << id)) {
|
105 |
|
|
|
106 |
|
|
TRACE_INFO("PMC_EnablePeripheral: clock of peripheral"
|
107 |
|
|
" %u is already enabled\n\r",
|
108 |
|
|
id);
|
109 |
|
|
}
|
110 |
|
|
else {
|
111 |
|
|
|
112 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << id;
|
113 |
|
|
}
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
//------------------------------------------------------------------------------
|
117 |
|
|
/// Disables the clock of a peripheral. The peripheral ID (AT91C_ID_xxx) is used
|
118 |
|
|
/// to identify which peripheral is targetted.
|
119 |
|
|
/// Note that the ID must NOT be shifted (i.e. 1 << AT91C_ID_xxx).
|
120 |
|
|
/// \param id Peripheral ID (AT91C_ID_xxx).
|
121 |
|
|
//------------------------------------------------------------------------------
|
122 |
|
|
void PMC_DisablePeripheral(unsigned int id)
|
123 |
|
|
{
|
124 |
|
|
SANITY_CHECK(id < 32);
|
125 |
|
|
|
126 |
|
|
if ((AT91C_BASE_PMC->PMC_PCSR & (1 << id)) != (1 << id)) {
|
127 |
|
|
|
128 |
|
|
TRACE_INFO("PMC_DisablePeripheral: clock of peripheral"
|
129 |
|
|
" %u is not enabled\n\r",
|
130 |
|
|
id);
|
131 |
|
|
}
|
132 |
|
|
else {
|
133 |
|
|
|
134 |
|
|
AT91C_BASE_PMC->PMC_PCDR = 1 << id;
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
//------------------------------------------------------------------------------
|
139 |
|
|
/// Enable all the periph clock via PMC
|
140 |
|
|
/// (Becareful of the last 2 bits, it is not periph clock)
|
141 |
|
|
//------------------------------------------------------------------------------
|
142 |
|
|
void PMC_EnableAllPeripherals(void)
|
143 |
|
|
{
|
144 |
|
|
AT91C_BASE_PMC->PMC_PCER = MASK_STATUS;
|
145 |
|
|
while( (AT91C_BASE_PMC->PMC_PCSR & MASK_STATUS) != MASK_STATUS);
|
146 |
|
|
TRACE_INFO("Enable all periph clocks\n\r");
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
//------------------------------------------------------------------------------
|
150 |
|
|
/// Disable all the periph clock via PMC
|
151 |
|
|
/// (Becareful of the last 2 bits, it is not periph clock)
|
152 |
|
|
//------------------------------------------------------------------------------
|
153 |
|
|
void PMC_DisableAllPeripherals(void)
|
154 |
|
|
{
|
155 |
|
|
AT91C_BASE_PMC->PMC_PCDR = MASK_STATUS;
|
156 |
|
|
while((AT91C_BASE_PMC->PMC_PCSR & MASK_STATUS) != 0);
|
157 |
|
|
TRACE_INFO("Disable all periph clocks\n\r");
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
//-----------------------------------------------------------------------------
|
161 |
|
|
/// Get Periph Status
|
162 |
|
|
//-----------------------------------------------------------------------------
|
163 |
|
|
unsigned int PMC_IsAllPeriphEnabled(void)
|
164 |
|
|
{
|
165 |
|
|
return (AT91C_BASE_PMC->PMC_PCSR == MASK_STATUS);
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
//-----------------------------------------------------------------------------
|
169 |
|
|
/// Get Periph Status
|
170 |
|
|
//-----------------------------------------------------------------------------
|
171 |
|
|
unsigned int PMC_IsPeriphEnabled(unsigned int id)
|
172 |
|
|
{
|
173 |
|
|
return (AT91C_BASE_PMC->PMC_PCSR & (1 << id));
|
174 |
|
|
}
|
175 |
|
|
//------------------------------------------------------------------------------
|
176 |
|
|
/// Put the CPU in Idle Mode for lower consumption
|
177 |
|
|
//------------------------------------------------------------------------------
|
178 |
|
|
void PMC_CPUInIdleMode(void)
|
179 |
|
|
{
|
180 |
|
|
#ifndef CP15_PRESENT
|
181 |
|
|
PMC_DisableProcessorClock();
|
182 |
|
|
#else
|
183 |
|
|
AT91C_BASE_PMC->PMC_SCDR = AT91C_PMC_PCK;
|
184 |
|
|
CP15_WaitForInterrupt();
|
185 |
|
|
#endif
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
|