]> git.sur5r.net Git - freertos/blob - Demo/Common/drivers/Atmel/at91lib/peripherals/rstc/rstc.c
Atmel provided hardware specifics.
[freertos] / Demo / Common / drivers / Atmel / at91lib / peripherals / rstc / rstc.c
1 /* ----------------------------------------------------------------------------\r
2  *         ATMEL Microcontroller Software Support\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2008, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\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
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\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
16  *\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
28  */\r
29 \r
30 //-----------------------------------------------------------------------------\r
31 //         Headers\r
32 //-----------------------------------------------------------------------------\r
33 \r
34 #include <board.h>\r
35 \r
36 //-----------------------------------------------------------------------------\r
37 //         Macros\r
38 //-----------------------------------------------------------------------------\r
39 \r
40 /// WRITE_RSTC: Write RSTC register\r
41 #define WRITE_RSTC(pRstc, regName, value) pRstc->regName = (value)\r
42 \r
43 /// READ_RSTC: Read RSTC registers\r
44 #define READ_RSTC(pRstc, regName) (pRstc->regName)\r
45 \r
46 //-----------------------------------------------------------------------------\r
47 //         Defines\r
48 //-----------------------------------------------------------------------------\r
49 \r
50 /// Keywords to write to the reset registers\r
51 #define RSTC_KEY_PASSWORD       (0xA5UL << 24)\r
52 \r
53 //-----------------------------------------------------------------------------\r
54 //         Exported functions\r
55 //-----------------------------------------------------------------------------\r
56 \r
57 //-----------------------------------------------------------------------------\r
58 /// Configure the mode of the RSTC peripheral.\r
59 /// The configuration is computed by the lib (AT91C_RSTC_*).\r
60 /// \param rstc  Pointer to an RSTC peripheral.\r
61 /// \param rmr Desired mode configuration.\r
62 //-----------------------------------------------------------------------------\r
63 void RSTC_ConfigureMode(AT91PS_RSTC rstc, unsigned int rmr)\r
64 {\r
65     rmr &= ~AT91C_RSTC_KEY;\r
66     WRITE_RSTC(rstc, RSTC_RMR, rmr | RSTC_KEY_PASSWORD);\r
67 }\r
68 \r
69 //-----------------------------------------------------------------------------\r
70 /// Enable/Disable the detection of a low level on the pin NRST as User Reset\r
71 /// \param rstc   Pointer to an RSTC peripheral.\r
72 /// \param enable 1 to enable & 0 to disable.\r
73 //-----------------------------------------------------------------------------\r
74 void RSTC_SetUserResetEnable(AT91PS_RSTC rstc, unsigned char enable)\r
75 {\r
76     unsigned int rmr = READ_RSTC(rstc, RSTC_RMR) & (~AT91C_RSTC_KEY);\r
77     if (enable) {\r
78 \r
79         rmr |=  AT91C_RSTC_URSTEN;\r
80     }\r
81     else {\r
82 \r
83         rmr &= ~AT91C_RSTC_URSTEN;\r
84     }\r
85     WRITE_RSTC(rstc, RSTC_RMR, rmr | RSTC_KEY_PASSWORD);\r
86 }\r
87 \r
88 //-----------------------------------------------------------------------------\r
89 /// Enable/Disable the interrupt of a User Reset (USRTS bit in RSTC_RST).\r
90 /// \param rstc   Pointer to an RSTC peripheral.\r
91 /// \param enable 1 to enable & 0 to disable.\r
92 //-----------------------------------------------------------------------------\r
93 void RSTC_SetUserResetInterruptEnable(AT91PS_RSTC rstc, unsigned char enable)\r
94 {\r
95     unsigned int rmr = READ_RSTC(rstc, RSTC_RMR) & (~AT91C_RSTC_KEY);\r
96     if (enable) {\r
97 \r
98         rmr |=  AT91C_RSTC_URSTIEN;\r
99     }\r
100     else {\r
101 \r
102         rmr &= ~AT91C_RSTC_URSTIEN;\r
103     }\r
104     WRITE_RSTC(rstc, RSTC_RMR, rmr | RSTC_KEY_PASSWORD);\r
105 }\r
106 \r
107 //-----------------------------------------------------------------------------\r
108 /// Setup the external reset length. The length is asserted during a time of\r
109 /// pow(2, powl+1) Slow Clock(32KHz). The duration is between 60us and 2s.\r
110 /// \param rstc   Pointer to an RSTC peripheral.\r
111 /// \param powl   Power length defined.\r
112 //-----------------------------------------------------------------------------\r
113 void RSTC_SetExtResetLength(AT91PS_RSTC rstc, unsigned char powl)\r
114 {\r
115     unsigned int rmr = READ_RSTC(rstc, RSTC_RMR);\r
116     rmr &= ~(AT91C_RSTC_KEY | AT91C_RSTC_ERSTL);\r
117     rmr |=  (powl << 8) & AT91C_RSTC_ERSTL;\r
118     WRITE_RSTC(rstc, RSTC_RMR, rmr | RSTC_KEY_PASSWORD);\r
119 }\r
120 \r
121 \r
122 //-----------------------------------------------------------------------------\r
123 /// Resets the processor.\r
124 /// \param rstc  Pointer to an RSTC peripheral.\r
125 //-----------------------------------------------------------------------------\r
126 void RSTC_ProcessorReset(AT91PS_RSTC rstc)\r
127 {\r
128     WRITE_RSTC(rstc, RSTC_RCR, AT91C_RSTC_PROCRST | RSTC_KEY_PASSWORD);\r
129 }\r
130 \r
131 //-----------------------------------------------------------------------------\r
132 /// Resets the peripherals.\r
133 /// \param rstc  Pointer to an RSTC peripheral.\r
134 //-----------------------------------------------------------------------------\r
135 void RSTC_PeripheralReset(AT91PS_RSTC rstc)\r
136 {\r
137     WRITE_RSTC(rstc, RSTC_RCR, AT91C_RSTC_PERRST | RSTC_KEY_PASSWORD);\r
138 }\r
139 \r
140 //-----------------------------------------------------------------------------\r
141 /// Asserts the NRST pin for external resets.\r
142 /// \param rstc  Pointer to an RSTC peripheral.\r
143 //-----------------------------------------------------------------------------\r
144 void RSTC_ExtReset(AT91PS_RSTC rstc)\r
145 {\r
146     WRITE_RSTC(rstc, RSTC_RCR, AT91C_RSTC_EXTRST | RSTC_KEY_PASSWORD);\r
147 }\r
148 \r
149 //-----------------------------------------------------------------------------\r
150 /// Return NRST pin level ( 1 or 0 ).\r
151 /// \param rstc  Pointer to an RSTC peripheral.\r
152 //-----------------------------------------------------------------------------\r
153 unsigned char RSTC_GetNrstLevel(AT91PS_RSTC rstc)\r
154 {\r
155     if (READ_RSTC(rstc, RSTC_RSR) & AT91C_RSTC_NRSTL) {\r
156 \r
157         return 1;\r
158     }\r
159     return 0;\r
160 }\r
161 \r
162 //-----------------------------------------------------------------------------\r
163 /// Returns 1 if at least one high-to-low transition of NRST (User Reset) has\r
164 /// been detected since the last read of RSTC_RSR.\r
165 /// \param rstc  Pointer to an RSTC peripheral.\r
166 //-----------------------------------------------------------------------------\r
167 unsigned char RSTC_IsUserReseetDetected(AT91PS_RSTC rstc)\r
168 {\r
169     if (READ_RSTC(rstc, RSTC_RSR) & AT91C_RSTC_URSTS) {\r
170 \r
171         return 1;\r
172     }\r
173     return 0;\r
174 }\r
175 \r
176 //-----------------------------------------------------------------------------\r
177 /// Return 1 if a software reset command is being performed by the reset\r
178 /// controller. The reset controller is busy.\r
179 /// \param rstc  Pointer to an RSTC peripheral.\r
180 //-----------------------------------------------------------------------------\r
181 unsigned char RSTC_IsBusy(AT91PS_RSTC rstc)\r
182 {\r
183     if (READ_RSTC(rstc, RSTC_RSR) & AT91C_RSTC_SRCMP) {\r
184 \r
185         return 1;\r
186     }\r
187     return 0;\r
188 }\r