blob: 340bbd609535b646f825189b6e12a3b17d0a5e9f [file] [log] [blame]
Christina Quast8be71e42014-12-02 13:06:01 +01001/* ----------------------------------------------------------------------------
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 "chip.h"
35
36#include <assert.h>
37
38//------------------------------------------------------------------------------
39// Local definitions
40//------------------------------------------------------------------------------
41
42/// Key value for the SUPC_MR register.
43#define SUPC_KEY ((uint32_t) (0xA5 << 24))
44
45//------------------------------------------------------------------------------
46// Global functions
47//------------------------------------------------------------------------------
48
49#if !defined(__ICCARM__)
50__attribute__ ((section (".ramfunc"))) // GCC
51#endif
52//------------------------------------------------------------------------------
53/// Enables the flash power supply with the given wake-up setting.
54/// \param time Wake-up time.
55//------------------------------------------------------------------------------
56void SUPC_EnableFlash( Supc* pSupc, uint32_t dwTime )
57{
58 pSupc->SUPC_FWUTR = dwTime ;
59 pSupc->SUPC_MR = SUPC_KEY | pSupc->SUPC_MR | AT91C_SUPC_FLASHON ;
60
61 while ((pSupc->SUPC_SR & AT91C_SUPC_FLASHS) != AT91C_SUPC_FLASHS) ;
62}
63
64#if !defined(__ICCARM__)
65__attribute__ ((section (".ramfunc"))) // GCC
66#endif
67//------------------------------------------------------------------------------
68/// Disables the flash power supply.
69//------------------------------------------------------------------------------
70void SUPC_DisableFlash( Supc* pSupc )
71{
72 pSupc->SUPC_MR = SUPC_KEY | (pSupc->SUPC_MR & ~AT91C_SUPC_FLASHON) ;
73
74 while ((pSupc->SUPC_SR & AT91C_SUPC_FLASHS) == AT91C_SUPC_FLASHS) ;
75}
76
77//------------------------------------------------------------------------------
78/// Sets the voltage regulator output voltage.
79/// \param voltage Voltage to set.
80//------------------------------------------------------------------------------
81void SUPC_SetVoltageOutput( Supc* pSupc, uint32_t dwVoltage )
82{
83 assert( (voltage & ~AT91C_SUPC_VRVDD) == 0 ) ;
84
85 pSupc->SUPC_MR = SUPC_KEY | (pSupc->SUPC_MR & ~AT91C_SUPC_VRVDD) | dwVoltage ;
86}
87
88//------------------------------------------------------------------------------
89/// Puts the voltage regulator in deep mode.
90//------------------------------------------------------------------------------
91void SUPC_EnableDeepMode( Supc* pSupc )
92{
93 pSupc->SUPC_MR = SUPC_KEY | pSupc->SUPC_MR | AT91C_SUPC_VRDEEP ;
94}
95
96//------------------------------------------------------------------------------
97/// Puts the voltage regulator in normal mode.
98//------------------------------------------------------------------------------
99void SUPC_DisableDeepMode( Supc* pSupc )
100{
101 pSupc->SUPC_MR = SUPC_KEY | (pSupc->SUPC_MR & ~AT91C_SUPC_VRDEEP) ;
102}
103
104//-----------------------------------------------------------------------------
105/// Enables the backup SRAM power supply, so its data is saved while the device
106/// is in backup mode.
107//-----------------------------------------------------------------------------
108void SUPC_EnableSram( Supc* pSupc )
109{
110 pSupc->SUPC_MR = SUPC_KEY | pSupc->SUPC_MR | AT91C_SUPC_SRAMON ;
111}
112
113//-----------------------------------------------------------------------------
114/// Disables the backup SRAM power supply.
115//-----------------------------------------------------------------------------
116void SUPC_DisableSram( Supc* pSupc )
117{
118 pSupc->SUPC_MR = SUPC_KEY | (pSupc->SUPC_MR & ~AT91C_SUPC_SRAMON) ;
119}
120
121//-----------------------------------------------------------------------------
122/// Enables the RTC power supply.
123//-----------------------------------------------------------------------------
124void SUPC_EnableRtc( Supc* pSupc )
125{
126 pSupc->SUPC_MR = SUPC_KEY | pSupc->SUPC_MR | AT91C_SUPC_RTCON ;
127
128 while ((pSupc->SUPC_SR & AT91C_SUPC_RTS) != AT91C_SUPC_RTS) ;
129}
130
131//-----------------------------------------------------------------------------
132/// Disables the RTC power supply.
133//-----------------------------------------------------------------------------
134void SUPC_DisableRtc( Supc* pSupc )
135{
136 pSupc->SUPC_MR = SUPC_KEY | (pSupc->SUPC_MR & ~AT91C_SUPC_RTCON) ;
137
138 while ((pSupc->SUPC_SR & AT91C_SUPC_RTS) == AT91C_SUPC_RTS);
139}
140
141//-----------------------------------------------------------------------------
142/// Sets the BOD sampling mode (or disables it).
143/// \param mode BOD sampling mode.
144//-----------------------------------------------------------------------------
145void SUPC_SetBodSampling( Supc* pSupc, uint32_t dwMode )
146{
147 assert( (dwMode & ~AT91C_SUPC_BODSMPL) == 0 ) ;
148
149 pSupc->SUPC_BOMR &= ~AT91C_SUPC_BODSMPL;
150 pSupc->SUPC_BOMR |= dwMode ;
151}
152
153//------------------------------------------------------------------------------
154/// Disables the voltage regulator, which makes the device enter backup mode.
155//------------------------------------------------------------------------------
156void SUPC_DisableVoltageRegulator( Supc* pSupc )
157{
158 pSupc->SUPC_CR = SUPC_KEY | AT91C_SUPC_VROFF ;
159
160 while ( 1 ) ;
161}
162
163//------------------------------------------------------------------------------
164/// Shuts the device down so it enters Off mode.
165//------------------------------------------------------------------------------
166void SUPC_Shutdown( Supc* pSupc )
167{
168 pSupc->SUPC_CR = SUPC_KEY | AT91C_SUPC_SHDW ;
169
170 while (1);
171}
172
173//------------------------------------------------------------------------------
174/// Sets the wake-up sources when in backup mode.
175/// \param sources Wake-up sources to enable.
176//------------------------------------------------------------------------------
177void SUPC_SetWakeUpSources( Supc* pSupc, uint32_t dwSources )
178{
179 assert( (dwSources & ~0x0000000B) == 0 ) ;
180
181 pSupc->SUPC_WUMR &= ~0x0000000B;
182 pSupc->SUPC_WUMR |= dwSources ;
183}
184
185//------------------------------------------------------------------------------
186/// Sets the wake-up inputs when in backup mode.
187/// \param inputs Wake up inputs to enable.
188//------------------------------------------------------------------------------
189void SUPC_SetWakeUpInputs( Supc* pSupc, uint32_t dwInputs )
190{
191 assert( (dwInputs & ~0xFFFF) == 0 ) ;
192
193 pSupc->SUPC_WUIR &= ~0xFFFF ;
194 pSupc->SUPC_WUIR |= dwInputs ;
195}
196