1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2008, Atmel Corporation
\r
6 * All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions are met:
\r
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\r
14 * Atmel's name may not be used to endorse or promote products derived from
\r
15 * this software without specific prior written permission.
\r
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
27 * ----------------------------------------------------------------------------
\r
30 //------------------------------------------------------------------------------
\r
32 //------------------------------------------------------------------------------
\r
36 #include <utility/assert.h>
\r
38 //------------------------------------------------------------------------------
\r
39 // Local definitions
\r
40 //------------------------------------------------------------------------------
\r
42 /// Key value for the SUPC_MR register.
\r
43 #define SUPC_KEY ((unsigned int) (0xA5 << 24))
\r
45 //------------------------------------------------------------------------------
\r
47 //------------------------------------------------------------------------------
\r
49 //------------------------------------------------------------------------------
\r
50 /// Enables the SLCD power supply.
\r
51 /// \param internal If 1, the power supply is configured as internal; otherwise
\r
52 /// it is set at external.
\r
53 //------------------------------------------------------------------------------
\r
54 void SUPC_EnableSlcd(unsigned char internal)
\r
58 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | (AT91C_BASE_SUPC->SUPC_MR & ~AT91C_SUPC_LCDMODE) | AT91C_SUPC_LCDMODE_INTERNAL;
\r
62 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | (AT91C_BASE_SUPC->SUPC_MR & ~AT91C_SUPC_LCDMODE) | AT91C_SUPC_LCDMODE_EXTERNAL;
\r
64 while ((AT91C_BASE_SUPC->SUPC_SR & AT91C_SUPC_LCDS) != AT91C_SUPC_LCDS);
\r
67 //------------------------------------------------------------------------------
\r
68 /// Disables the SLCD power supply.
\r
69 //------------------------------------------------------------------------------
\r
70 void SUPC_DisableSlcd(void)
\r
72 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | (AT91C_BASE_SUPC->SUPC_MR & ~AT91C_SUPC_LCDMODE);
\r
73 while ((AT91C_BASE_SUPC->SUPC_SR & AT91C_SUPC_LCDS) == AT91C_SUPC_LCDS);
\r
76 //------------------------------------------------------------------------------
\r
77 /// Sets the output voltage of the SLCD charge pump.
\r
78 /// \param voltage Output voltage.
\r
79 //------------------------------------------------------------------------------
\r
80 void SUPC_SetSlcdVoltage(unsigned int voltage)
\r
82 SANITY_CHECK((voltage & ~AT91C_SUPC_LCDOUT) == 0);
\r
83 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | (AT91C_BASE_SUPC->SUPC_MR & ~AT91C_SUPC_LCDOUT) | voltage;
\r
86 #if !defined(__ICCARM__)
\r
87 __attribute__ ((section (".ramfunc"))) // GCC
\r
89 //------------------------------------------------------------------------------
\r
90 /// Enables the flash power supply with the given wake-up setting.
\r
91 /// \param time Wake-up time.
\r
92 //------------------------------------------------------------------------------
\r
93 void SUPC_EnableFlash(unsigned int time)
\r
95 AT91C_BASE_SUPC->SUPC_FWUTR = time;
\r
96 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | AT91C_BASE_SUPC->SUPC_MR | AT91C_SUPC_FLASHON;
\r
97 while ((AT91C_BASE_SUPC->SUPC_SR & AT91C_SUPC_FLASHS) != AT91C_SUPC_FLASHS);
\r
100 #if !defined(__ICCARM__)
\r
101 __attribute__ ((section (".ramfunc"))) // GCC
\r
103 //------------------------------------------------------------------------------
\r
104 /// Disables the flash power supply.
\r
105 //------------------------------------------------------------------------------
\r
106 void SUPC_DisableFlash(void)
\r
108 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | (AT91C_BASE_SUPC->SUPC_MR & ~AT91C_SUPC_FLASHON);
\r
109 while ((AT91C_BASE_SUPC->SUPC_SR & AT91C_SUPC_FLASHS) == AT91C_SUPC_FLASHS);
\r
112 //------------------------------------------------------------------------------
\r
113 /// Sets the voltage regulator output voltage.
\r
114 /// \param voltage Voltage to set.
\r
115 //------------------------------------------------------------------------------
\r
116 void SUPC_SetVoltageOutput(unsigned int voltage)
\r
118 SANITY_CHECK((voltage & ~AT91C_SUPC_VRVDD) == 0);
\r
119 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | (AT91C_BASE_SUPC->SUPC_MR & ~AT91C_SUPC_VRVDD) | voltage;
\r
122 //------------------------------------------------------------------------------
\r
123 /// Puts the voltage regulator in deep mode.
\r
124 //------------------------------------------------------------------------------
\r
125 void SUPC_EnableDeepMode(void)
\r
127 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | AT91C_BASE_SUPC->SUPC_MR | AT91C_SUPC_VRDEEP;
\r
130 //------------------------------------------------------------------------------
\r
131 /// Puts the voltage regulator in normal mode.
\r
132 //------------------------------------------------------------------------------
\r
133 void SUPC_DisableDeepMode(void)
\r
135 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | (AT91C_BASE_SUPC->SUPC_MR & ~AT91C_SUPC_VRDEEP);
\r
138 //-----------------------------------------------------------------------------
\r
139 /// Enables the backup SRAM power supply, so its data is saved while the device
\r
140 /// is in backup mode.
\r
141 //-----------------------------------------------------------------------------
\r
142 void SUPC_EnableSram(void)
\r
144 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | AT91C_BASE_SUPC->SUPC_MR | AT91C_SUPC_SRAMON;
\r
147 //-----------------------------------------------------------------------------
\r
148 /// Disables the backup SRAM power supply.
\r
149 //-----------------------------------------------------------------------------
\r
150 void SUPC_DisableSram(void)
\r
152 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | (AT91C_BASE_SUPC->SUPC_MR & ~AT91C_SUPC_SRAMON);
\r
155 //-----------------------------------------------------------------------------
\r
156 /// Enables the RTC power supply.
\r
157 //-----------------------------------------------------------------------------
\r
158 void SUPC_EnableRtc(void)
\r
160 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | AT91C_BASE_SUPC->SUPC_MR | AT91C_SUPC_RTCON;
\r
161 while ((AT91C_BASE_SUPC->SUPC_SR & AT91C_SUPC_RTS) != AT91C_SUPC_RTS);
\r
164 //-----------------------------------------------------------------------------
\r
165 /// Disables the RTC power supply.
\r
166 //-----------------------------------------------------------------------------
\r
167 void SUPC_DisableRtc(void)
\r
169 AT91C_BASE_SUPC->SUPC_MR = SUPC_KEY | (AT91C_BASE_SUPC->SUPC_MR & ~AT91C_SUPC_RTCON);
\r
170 while ((AT91C_BASE_SUPC->SUPC_SR & AT91C_SUPC_RTS) == AT91C_SUPC_RTS);
\r
173 //-----------------------------------------------------------------------------
\r
174 /// Sets the BOD sampling mode (or disables it).
\r
175 /// \param mode BOD sampling mode.
\r
176 //-----------------------------------------------------------------------------
\r
177 void SUPC_SetBodSampling(unsigned int mode)
\r
179 SANITY_CHECK((mode & ~AT91C_SUPC_BODSMPL) == 0);
\r
180 AT91C_BASE_SUPC->SUPC_BOMR &= ~AT91C_SUPC_BODSMPL;
\r
181 AT91C_BASE_SUPC->SUPC_BOMR |= mode;
\r
184 //------------------------------------------------------------------------------
\r
185 /// Disables the voltage regulator, which makes the device enter backup mode.
\r
186 //------------------------------------------------------------------------------
\r
187 void SUPC_DisableVoltageRegulator(void)
\r
189 AT91C_BASE_SUPC->SUPC_CR = SUPC_KEY | AT91C_SUPC_VROFF;
\r
193 //------------------------------------------------------------------------------
\r
194 /// Shuts the device down so it enters Off mode.
\r
195 //------------------------------------------------------------------------------
\r
196 void SUPC_Shutdown(void)
\r
198 AT91C_BASE_SUPC->SUPC_CR = SUPC_KEY | AT91C_SUPC_SHDW;
\r
202 //------------------------------------------------------------------------------
\r
203 /// Sets the wake-up sources when in backup mode.
\r
204 /// \param sources Wake-up sources to enable.
\r
205 //------------------------------------------------------------------------------
\r
206 void SUPC_SetWakeUpSources(unsigned int sources)
\r
208 SANITY_CHECK((sources & ~0x0000000B) == 0);
\r
209 AT91C_BASE_SUPC->SUPC_WUMR &= ~0x0000000B;
\r
210 AT91C_BASE_SUPC->SUPC_WUMR |= sources;
\r
213 //------------------------------------------------------------------------------
\r
214 /// Sets the wake-up inputs when in backup mode.
\r
215 /// \param inputs Wake up inputs to enable.
\r
216 //------------------------------------------------------------------------------
\r
217 void SUPC_SetWakeUpInputs(unsigned int inputs)
\r
219 SANITY_CHECK((inputs & ~0xFFFF) == 0);
\r
220 AT91C_BASE_SUPC->SUPC_WUIR &= ~0xFFFF;
\r
221 AT91C_BASE_SUPC->SUPC_WUIR |= inputs;
\r