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 <board.h>
|
35 |
|
|
|
36 |
|
|
//-----------------------------------------------------------------------------
|
37 |
|
|
// Defines
|
38 |
|
|
//-----------------------------------------------------------------------------
|
39 |
|
|
|
40 |
|
|
/// Keywords to write to the reset registers
|
41 |
|
|
#define RSTC_KEY_PASSWORD (0xA5 << 24)
|
42 |
|
|
|
43 |
|
|
//-----------------------------------------------------------------------------
|
44 |
|
|
// Exported functions
|
45 |
|
|
//-----------------------------------------------------------------------------
|
46 |
|
|
|
47 |
|
|
//-----------------------------------------------------------------------------
|
48 |
|
|
/// Configure the mode of the RSTC peripheral.
|
49 |
|
|
/// The configuration is computed by the lib (AT91C_RSTC_*).
|
50 |
|
|
/// \param rmr Desired mode configuration.
|
51 |
|
|
//-----------------------------------------------------------------------------
|
52 |
|
|
void RSTC_ConfigureMode(unsigned int rmr)
|
53 |
|
|
{
|
54 |
|
|
rmr &= ~AT91C_RSTC_KEY;
|
55 |
|
|
AT91C_BASE_RSTC->RSTC_RMR = rmr | RSTC_KEY_PASSWORD;
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
//-----------------------------------------------------------------------------
|
59 |
|
|
/// Enable/Disable the detection of a low level on the pin NRST as User Reset
|
60 |
|
|
/// \param enable 1 to enable & 0 to disable.
|
61 |
|
|
//-----------------------------------------------------------------------------
|
62 |
|
|
void RSTC_SetUserResetEnable(unsigned char enable)
|
63 |
|
|
{
|
64 |
|
|
unsigned int rmr = AT91C_BASE_RSTC->RSTC_RMR & (~AT91C_RSTC_KEY);
|
65 |
|
|
if (enable) {
|
66 |
|
|
|
67 |
|
|
rmr |= AT91C_RSTC_URSTEN;
|
68 |
|
|
}
|
69 |
|
|
else {
|
70 |
|
|
|
71 |
|
|
rmr &= ~AT91C_RSTC_URSTEN;
|
72 |
|
|
}
|
73 |
|
|
AT91C_BASE_RSTC->RSTC_RMR = rmr | RSTC_KEY_PASSWORD;
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
//-----------------------------------------------------------------------------
|
77 |
|
|
/// Enable/Disable the interrupt of a User Reset (USRTS bit in RSTC_RST).
|
78 |
|
|
/// \param enable 1 to enable & 0 to disable.
|
79 |
|
|
//-----------------------------------------------------------------------------
|
80 |
|
|
void RSTC_SetUserResetInterruptEnable(unsigned char enable)
|
81 |
|
|
{
|
82 |
|
|
unsigned int rmr = AT91C_BASE_RSTC->RSTC_RMR & (~AT91C_RSTC_KEY);
|
83 |
|
|
if (enable) {
|
84 |
|
|
|
85 |
|
|
rmr |= AT91C_RSTC_URSTIEN;
|
86 |
|
|
}
|
87 |
|
|
else {
|
88 |
|
|
|
89 |
|
|
rmr &= ~AT91C_RSTC_URSTIEN;
|
90 |
|
|
}
|
91 |
|
|
AT91C_BASE_RSTC->RSTC_RMR = rmr | RSTC_KEY_PASSWORD;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
//-----------------------------------------------------------------------------
|
95 |
|
|
/// Setup the external reset length. The length is asserted during a time of
|
96 |
|
|
/// pow(2, powl+1) Slow Clock(32KHz). The duration is between 60us and 2s.
|
97 |
|
|
/// \param powl Power length defined.
|
98 |
|
|
//-----------------------------------------------------------------------------
|
99 |
|
|
void RSTC_SetExtResetLength(unsigned char powl)
|
100 |
|
|
{
|
101 |
|
|
unsigned int rmr = AT91C_BASE_RSTC->RSTC_RMR;
|
102 |
|
|
rmr &= ~(AT91C_RSTC_KEY | AT91C_RSTC_ERSTL);
|
103 |
|
|
rmr |= (powl << 8) & AT91C_RSTC_ERSTL;
|
104 |
|
|
AT91C_BASE_RSTC->RSTC_RMR = rmr | RSTC_KEY_PASSWORD;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
//-----------------------------------------------------------------------------
|
109 |
|
|
/// Resets the processor.
|
110 |
|
|
//-----------------------------------------------------------------------------
|
111 |
|
|
void RSTC_ProcessorReset(void)
|
112 |
|
|
{
|
113 |
|
|
AT91C_BASE_RSTC->RSTC_RCR = AT91C_RSTC_PROCRST | RSTC_KEY_PASSWORD;
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
//-----------------------------------------------------------------------------
|
117 |
|
|
/// Resets the peripherals.
|
118 |
|
|
//-----------------------------------------------------------------------------
|
119 |
|
|
void RSTC_PeripheralReset(void)
|
120 |
|
|
{
|
121 |
|
|
AT91C_BASE_RSTC->RSTC_RCR = AT91C_RSTC_PERRST | RSTC_KEY_PASSWORD;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
//-----------------------------------------------------------------------------
|
125 |
|
|
/// Asserts the NRST pin for external resets.
|
126 |
|
|
//-----------------------------------------------------------------------------
|
127 |
|
|
void RSTC_ExtReset(void)
|
128 |
|
|
{
|
129 |
|
|
AT91C_BASE_RSTC->RSTC_RCR = AT91C_RSTC_EXTRST | RSTC_KEY_PASSWORD;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
//-----------------------------------------------------------------------------
|
133 |
|
|
/// Return NRST pin level ( 1 or 0 ).
|
134 |
|
|
//-----------------------------------------------------------------------------
|
135 |
|
|
unsigned char RSTC_GetNrstLevel(void)
|
136 |
|
|
{
|
137 |
|
|
if (AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_NRSTL) {
|
138 |
|
|
|
139 |
|
|
return 1;
|
140 |
|
|
}
|
141 |
|
|
return 0;
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
//-----------------------------------------------------------------------------
|
145 |
|
|
/// Returns 1 if at least one high-to-low transition of NRST (User Reset) has
|
146 |
|
|
/// been detected since the last read of RSTC_RSR.
|
147 |
|
|
//-----------------------------------------------------------------------------
|
148 |
|
|
unsigned char RSTC_IsUserResetDetected(void)
|
149 |
|
|
{
|
150 |
|
|
if (AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_URSTS) {
|
151 |
|
|
|
152 |
|
|
return 1;
|
153 |
|
|
}
|
154 |
|
|
return 0;
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
//-----------------------------------------------------------------------------
|
158 |
|
|
/// Return 1 if a software reset command is being performed by the reset
|
159 |
|
|
/// controller. The reset controller is busy.
|
160 |
|
|
//-----------------------------------------------------------------------------
|
161 |
|
|
unsigned char RSTC_IsBusy(void)
|
162 |
|
|
{
|
163 |
|
|
if (AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_SRCMP) {
|
164 |
|
|
|
165 |
|
|
return 1;
|
166 |
|
|
}
|
167 |
|
|
return 0;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
//-----------------------------------------------------------------------------
|
171 |
|
|
/// Get the status
|
172 |
|
|
//-----------------------------------------------------------------------------
|
173 |
|
|
unsigned char RSTC_GetStatus(void)
|
174 |
|
|
{
|
175 |
|
|
return (AT91C_BASE_RSTC->RSTC_RSR);
|
176 |
|
|
}
|