1 /******************************************************************************
2 *  Filename:       pwr_ctrl.h
3 *  Revised:        2015-10-29 10:58:24 +0100 (Thu, 29 Oct 2015)
4 *  Revision:       44880
5 *
6 *  Description:    Defines and prototypes for the System Power Control.
7 *
8 *  Copyright (c) 2015, Texas Instruments Incorporated
9 *  All rights reserved.
10 *
11 *  Redistribution and use in source and binary forms, with or without
12 *  modification, are permitted provided that the following conditions are met:
13 *
14 *  1) Redistributions of source code must retain the above copyright notice,
15 *     this list of conditions and the following disclaimer.
16 *
17 *  2) Redistributions in binary form must reproduce the above copyright notice,
18 *     this list of conditions and the following disclaimer in the documentation
19 *     and/or other materials provided with the distribution.
20 *
21 *  3) Neither the name of the ORGANIZATION nor the names of its contributors may
22 *     be used to endorse or promote products derived from this software without
23 *     specific prior written permission.
24 *
25 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 *  POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************/
38 
39 //*****************************************************************************
40 //
41 //! \addtogroup system_control_group
42 //! @{
43 //! \addtogroup pwrctrl_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 #ifndef __PWR_CTRL_H__
49 #define __PWR_CTRL_H__
50 
51 //*****************************************************************************
52 //
53 // If building with a C++ compiler, make all of the definitions in this header
54 // have a C binding.
55 //
56 //*****************************************************************************
57 #ifdef __cplusplus
58 extern "C"
59 {
60 #endif
61 
62 #include <stdbool.h>
63 #include <stdint.h>
64 #include <inc/hw_types.h>
65 #include <inc/hw_memmap.h>
66 #include <inc/hw_ints.h>
67 #include <inc/hw_aon_wuc.h>
68 #include <inc/hw_aon_sysctl.h>
69 #include <inc/hw_aon_rtc.h>
70 #include <inc/hw_adi_2_refsys.h>
71 #include <driverlib/debug.h>
72 #include <driverlib/interrupt.h>
73 #include <driverlib/osc.h>
74 #include <driverlib/cpu.h>
75 #include <driverlib/prcm.h>
76 #include <driverlib/aon_wuc.h>
77 #include <driverlib/aon_ioc.h>
78 #include <driverlib/aux_wuc.h>
79 #include <driverlib/adi.h>
80 
81 //*****************************************************************************
82 //
83 // Support for DriverLib in ROM:
84 // This section renames all functions that are not "static inline", so that
85 // calling these functions will default to implementation in flash. At the end
86 // of this file a second renaming will change the defaults to implementation in
87 // ROM for available functions.
88 //
89 // To force use of the implementation in flash, e.g. for debugging:
90 // - Globally: Define DRIVERLIB_NOROM at project level
91 // - Per function: Use prefix "NOROM_" when calling the function
92 //
93 //*****************************************************************************
94 #if !defined(DOXYGEN)
95     #define PowerCtrlStateSet               NOROM_PowerCtrlStateSet
96     #define PowerCtrlSourceSet              NOROM_PowerCtrlSourceSet
97 #endif
98 
99 //*****************************************************************************
100 //
101 // Defines for the system power states
102 //
103 //*****************************************************************************
104 #define PWRCTRL_ACTIVE          0x00000001
105 #define PWRCTRL_STANDBY         0x00000002
106 #define PWRCTRL_POWER_DOWN      0x00000004
107 #define PWRCTRL_SHUTDOWN        0x00000008
108 
109 //*****************************************************************************
110 //
111 // Defines for the power configuration in the AON System Control 1.2 V
112 //
113 //*****************************************************************************
114 #define PWRCTRL_IOSEG3_ENABLE   0x00000800
115 #define PWRCTRL_IOSEG2_ENABLE   0x00000400
116 #define PWRCTRL_IOSEG3_DISABLE  0x00000200
117 #define PWRCTRL_IOSEG2_DISABLE  0x00000100
118 #define PWRCTRL_PWRSRC_DCDC     0x00000001
119 #define PWRCTRL_PWRSRC_GLDO     0x00000000
120 #define PWRCTRL_PWRSRC_ULDO     0x00000002
121 
122 //*****************************************************************************
123 //
124 // The following are defines for the various reset source for the device.
125 //
126 //*****************************************************************************
127 #define PWRCTRL_RST_POWER_ON    0x00000000  // Reset by power on
128 #define PWRCTRL_RST_PIN         0x00000001  // Pin reset
129 #define PWRCTRL_RST_VDDS_BOD    0x00000002  // VDDS Brown Out Detect
130 #define PWRCTRL_RST_VDD_BOD     0x00000003  // VDD Brown Out Detect
131 #define PWRCTRL_RST_VDDR_BOD    0x00000004  // VDDR Brown Out Detect
132 #define PWRCTRL_RST_CLK_LOSS    0x00000005  // Clock loss Reset
133 #define PWRCTRL_RST_SW_PIN      0x00000006  // SYSRESET or pin reset
134 #define PWRCTRL_RST_WARM        0x00000007  // Reset via PRCM warm reset request
135 
136 //*****************************************************************************
137 //
138 // API Functions and prototypes
139 //
140 //*****************************************************************************
141 
142 //*****************************************************************************
143 //
144 //! \brief Force the system into low power modes.
145 //!
146 //! The device has 4 main power states where \ref PWRCTRL_ACTIVE is the default
147 //! state. If the CPU is running the system is considered to be in the active
148 //! state. The three other states are:
149 //! - \ref PWRCTRL_STANDBY
150 //! - \ref PWRCTRL_POWER_DOWN
151 //! - \ref PWRCTRL_SHUTDOWN
152 //!
153 //! \note This code does not guarantee free operation of the Sensor Controller.
154 //! It is only guaranteed to flip the switches to force the desired low power
155 //! mode on the device.
156 //!
157 //! \note It is solely the programmers responsibility to properly configure an
158 //! interrupt that will enable the device to wakeup before configuring the
159 //! power mode. If not properly implemented the behavior is undefined.
160 //!
161 //! \note This function will be deprecated in future releases.
162 //!
163 //! \param ui32Powerstate defines the next power state for the system.
164 //! - \ref PWRCTRL_ACTIVE (default)
165 //! - \ref PWRCTRL_STANDBY
166 //! - \ref PWRCTRL_POWER_DOWN
167 //! - \ref PWRCTRL_SHUTDOWN
168 //!
169 //! \return None
170 //
171 //*****************************************************************************
172 extern void PowerCtrlStateSet(uint32_t ui32Powerstate);
173 
174 //*****************************************************************************
175 //
176 //! \brief Set (Request) the main power source.
177 //!
178 //! \note The system will never allow a switch to the \ref PWRCTRL_PWRSRC_ULDO
179 //! when in active mode. This is only allowed when the system is in lower power
180 //! mode where no code is executing and no peripherals are active.
181 //! Assuming that there is an external capacitor available for the
182 //! \ref PWRCTRL_PWRSRC_DCDC the system can dynamically switch back and forth
183 //! between the two when in active mode.
184 //!
185 //! \note The system will automatically switch to the GLDO / DCDC when waking
186 //! up from a low power mode.
187 //!
188 //! \param ui32PowerConfig is a bitmask indicating the target power source.
189 //! - \ref PWRCTRL_PWRSRC_DCDC
190 //! - \ref PWRCTRL_PWRSRC_GLDO
191 //! - \ref PWRCTRL_PWRSRC_ULDO
192 //!
193 //! \return None
194 //
195 //*****************************************************************************
196 extern void PowerCtrlSourceSet(uint32_t ui32PowerConfig);
197 
198 //*****************************************************************************
199 //
200 //! \brief Get the main power source.
201 //!
202 //! Use this function to retrieve the current active power source.
203 //!
204 //! When the System CPU is active it can never be powered by uLDO as this
205 //! is too weak a power source.
206 //!
207 //! \note Using the DCDC power supply requires an external inductor.
208 //!
209 //! \return Returns the main power source.
210 //! - \ref PWRCTRL_PWRSRC_DCDC
211 //! - \ref PWRCTRL_PWRSRC_GLDO
212 //
213 //*****************************************************************************
214 __STATIC_INLINE uint32_t
PowerCtrlSourceGet(void)215 PowerCtrlSourceGet(void)
216 {
217     uint32_t ui32PowerConfig;
218 
219     //
220     // Return the current power source
221     //
222     ui32PowerConfig = HWREG(AON_SYSCTL_BASE + AON_SYSCTL_O_PWRCTL);
223     if(ui32PowerConfig & AON_SYSCTL_PWRCTL_DCDC_ACTIVE)
224     {
225         return (PWRCTRL_PWRSRC_DCDC);
226     }
227     else
228     {
229         return (PWRCTRL_PWRSRC_GLDO);
230     }
231 }
232 
233 
234 //*****************************************************************************
235 //
236 //! \brief OBSOLETE: Get the last known reset source of the system.
237 //!
238 //! Recommend using function \ref SysCtrlResetSourceGet() instead of this one.
239 //! This function returns reset source but does not cover if waking up from shutdown.
240 //! This function can be seen as a subset of function \ref SysCtrlResetSourceGet()
241 //! and will be removed in a future release.
242 //!
243 //! \return Returns one of the known reset values.
244 //! The possible reset sources are:
245 //! - \ref PWRCTRL_RST_POWER_ON
246 //! - \ref PWRCTRL_RST_PIN
247 //! - \ref PWRCTRL_RST_VDDS_BOD
248 //! - \ref PWRCTRL_RST_VDD_BOD
249 //! - \ref PWRCTRL_RST_VDDR_BOD
250 //! - \ref PWRCTRL_RST_CLK_LOSS
251 //! - \ref PWRCTRL_RST_SW_PIN
252 //! - \ref PWRCTRL_RST_WARM
253 //!
254 //! \sa \ref SysCtrlResetSourceGet()
255 //
256 //*****************************************************************************
257 __STATIC_INLINE uint32_t
PowerCtrlResetSourceGet(void)258 PowerCtrlResetSourceGet(void)
259 {
260     //
261     //  Get the reset source.
262     //
263     return (( HWREG( AON_SYSCTL_BASE + AON_SYSCTL_O_RESETCTL ) &
264         AON_SYSCTL_RESETCTL_RESET_SRC_M ) >>
265         AON_SYSCTL_RESETCTL_RESET_SRC_S ) ;
266 }
267 
268 //*****************************************************************************
269 //
270 //! \brief Close the latches in the AON IOC interface and in padring.
271 //!
272 //! Use this function to unfreeze the current value retained on the IOs driven
273 //! by the device. This is required if it is desired to maintain the level of
274 //! any IO driven when going through a shutdown/powerdown cycle.
275 //!
276 //! \return None
277 //!
278 //! \sa \ref PowerCtrlIOFreezeDisable()
279 //
280 //*****************************************************************************
281 __STATIC_INLINE void
PowerCtrlIOFreezeEnable(void)282 PowerCtrlIOFreezeEnable(void)
283 {
284     //
285     // Close the IO latches at AON_IOC level and in the padring.
286     //
287     AONIOCFreezeEnable();
288     HWREG(AON_SYSCTL_BASE + AON_SYSCTL_O_SLEEPCTL) = 0;
289     HWREG(AON_RTC_BASE + AON_RTC_O_SYNC);
290 }
291 
292 //*****************************************************************************
293 //
294 //! Open the latches in the AON IOC interface and in padring.
295 //!
296 //! Use this function to unfreeze the latches that retained on the IOs driven
297 //! by the device. This function should not be called before the application
298 //! has reinitialized the IO configuration that will drive the IOs to the
299 //! desired level.
300 //!
301 //! \return None
302 //!
303 //! \sa \ref PowerCtrlIOFreezeEnable()
304 //
305 //*****************************************************************************
306 __STATIC_INLINE void
PowerCtrlIOFreezeDisable(void)307 PowerCtrlIOFreezeDisable(void)
308 {
309     //
310     // Open the IO latches at AON_IOC level and in the padring
311     //
312     AONIOCFreezeDisable();
313     HWREG(AON_SYSCTL_BASE + AON_SYSCTL_O_SLEEPCTL) = 1;
314     HWREG(AON_RTC_BASE + AON_RTC_O_SYNC);
315 }
316 
317 //*****************************************************************************
318 //
319 // Support for DriverLib in ROM:
320 // Redirect to implementation in ROM when available.
321 //
322 //*****************************************************************************
323 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
324     #include <driverlib/rom.h>
325     #ifdef ROM_PowerCtrlStateSet
326         #undef  PowerCtrlStateSet
327         #define PowerCtrlStateSet               ROM_PowerCtrlStateSet
328     #endif
329     #ifdef ROM_PowerCtrlSourceSet
330         #undef  PowerCtrlSourceSet
331         #define PowerCtrlSourceSet              ROM_PowerCtrlSourceSet
332     #endif
333 #endif
334 
335 //*****************************************************************************
336 //
337 // Mark the end of the C bindings section for C++ compilers.
338 //
339 //*****************************************************************************
340 #ifdef __cplusplus
341 }
342 #endif
343 
344 #endif // __PWR_CTRL_H__
345 
346 //*****************************************************************************
347 //
348 //! Close the Doxygen group.
349 //! @}
350 //! @}
351 //
352 //*****************************************************************************
353