]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained_IAR_Keil/libchip_samv7/source/rstc.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained_IAR_Keil / libchip_samv7 / source / rstc.c
1 /* ----------------------------------------------------------------------------\r
2  *         SAM Software Package License \r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2011, 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 /** \file */\r
31 /*---------------------------------------------------------------------------\r
32  *         Headers\r
33  *---------------------------------------------------------------------------*/\r
34 \r
35 #include <chip.h>\r
36 \r
37 /*---------------------------------------------------------------------------\r
38  *         Defines\r
39  *---------------------------------------------------------------------------*/\r
40 \r
41 /** Keywords to write to the reset registers */\r
42 #define RSTC_KEY_PASSWORD           RSTC_MR_KEY(0xA5U)\r
43 \r
44 #define RSTC_MR_URSTEN              (1ul     )\r
45 #define RSTC_MR_URSTIEN             (1ul << 4)\r
46 \r
47 /*---------------------------------------------------------------------------\r
48  *         Exported functions\r
49  *---------------------------------------------------------------------------*/\r
50 \r
51 /**\r
52  * Configure the mode of the RSTC peripheral.\r
53  * The configuration is computed by the lib (RSTC_RMR_*).\r
54  * \param mr Desired mode configuration.\r
55  */\r
56 void RSTC_ConfigureMode(uint32_t mr)\r
57 {\r
58     Rstc *pHw = RSTC;\r
59     mr &= ~RSTC_MR_KEY_Msk;\r
60     pHw->RSTC_MR = mr | RSTC_KEY_PASSWORD;\r
61 }\r
62 \r
63 /**\r
64  * Enable/Disable the detection of a low level on the pin NRST as User Reset\r
65  * \param enable 1 to enable & 0 to disable.\r
66  */\r
67 void RSTC_SetUserResetEnable(uint8_t enable)\r
68 {\r
69     Rstc *pHw = RSTC;\r
70     uint32_t mr = pHw->RSTC_MR & (~RSTC_MR_KEY_Msk);\r
71     if (enable)\r
72     {\r
73         mr |=  RSTC_MR_URSTEN;\r
74     }\r
75     else\r
76     {\r
77         mr &= ~RSTC_MR_URSTEN;\r
78     }\r
79     pHw->RSTC_MR = mr | RSTC_KEY_PASSWORD;\r
80 }\r
81 \r
82 /**\r
83  * Enable/Disable the interrupt of a User Reset (USRTS bit in RSTC_RST).\r
84  * \param enable 1 to enable & 0 to disable.\r
85  */\r
86 void RSTC_SetUserResetInterruptEnable(uint8_t enable)\r
87 {\r
88     Rstc *pHw = RSTC;\r
89     uint32_t mr = pHw->RSTC_MR & (~RSTC_MR_KEY_Msk);\r
90     if (enable)\r
91     {\r
92         mr |=  RSTC_MR_URSTIEN;\r
93     }\r
94     else {\r
95 \r
96         mr &= ~RSTC_MR_URSTIEN;\r
97     }\r
98     pHw->RSTC_MR = mr | RSTC_KEY_PASSWORD;\r
99 }\r
100 \r
101 /**\r
102  * Setup the external reset length. The length is asserted during a time of\r
103  * pow(2, powl+1) Slow Clock(32KHz). The duration is between 60us and 2s.\r
104  * \param powl   Power length defined.\r
105  */\r
106 void RSTC_SetExtResetLength(uint8_t powl)\r
107 {\r
108     Rstc *pHw = RSTC;\r
109     uint32_t mr = pHw->RSTC_MR;\r
110     mr &= ~(RSTC_MR_KEY_Msk | RSTC_MR_ERSTL_Msk);\r
111     mr |=  RSTC_MR_ERSTL(powl);\r
112     pHw->RSTC_MR = mr | RSTC_KEY_PASSWORD;\r
113 }\r
114 \r
115 \r
116 /**\r
117  * Resets the processor.\r
118  */\r
119 void RSTC_ProcessorReset(void)\r
120 {\r
121     Rstc *pHw = RSTC;\r
122     pHw->RSTC_CR = RSTC_CR_PROCRST | RSTC_KEY_PASSWORD;\r
123 }\r
124 \r
125 /**\r
126  * Resets the peripherals.\r
127  */\r
128 void RSTC_PeripheralReset(void)\r
129 {\r
130     Rstc *pHw = RSTC;\r
131     pHw->RSTC_CR = RSTC_CR_PERRST | RSTC_KEY_PASSWORD;\r
132 }\r
133 \r
134 /**\r
135  * Asserts the NRST pin for external resets.\r
136  */\r
137 void RSTC_ExtReset(void)\r
138 {\r
139     Rstc *pHw = RSTC;\r
140     pHw->RSTC_CR = RSTC_CR_EXTRST | RSTC_KEY_PASSWORD;\r
141 }\r
142 \r
143 /**\r
144  * Return NRST pin level ( 1 or 0 ).\r
145  */\r
146 uint8_t RSTC_GetNrstLevel(void)\r
147 {\r
148     Rstc *pHw = RSTC;\r
149     return ((pHw->RSTC_SR & RSTC_SR_NRSTL) > 0);\r
150 }\r
151 \r
152 /**\r
153  * Returns 1 if at least one high-to-low transition of NRST (User Reset) has\r
154  * been detected since the last read of RSTC_RSR.\r
155  */\r
156 uint8_t RSTC_IsUserResetDetected(void)\r
157 {\r
158     Rstc *pHw = RSTC;\r
159     if (pHw->RSTC_SR & RSTC_SR_URSTS)\r
160     {\r
161         return 1;\r
162     }\r
163     return 0;\r
164 }\r
165 \r
166 /**\r
167  * Return 1 if a software reset command is being performed by the reset\r
168  * controller. The reset controller is busy.\r
169  */\r
170 uint8_t RSTC_IsBusy(void)\r
171 {\r
172     Rstc *pHw = RSTC;\r
173     if (pHw->RSTC_SR & RSTC_SR_SRCMP)\r
174     {\r
175         return 1;\r
176     }\r
177     return 0;\r
178 }\r
179 \r
180 /**\r
181  * Get the status\r
182  */\r
183 uint32_t RSTC_GetStatus(void)\r
184 {\r
185     Rstc *pHw = RSTC;\r
186     return (pHw->RSTC_SR);\r
187 }\r
188 \r