1 /******************************************************************************
2 *  Filename:       sys_ctrl.h
3 *  Revised:        2015-11-09 11:32:42 +0100 (Mon, 09 Nov 2015)
4 *  Revision:       45004
5 *
6 *  Description:    Defines and prototypes for the System Controller.
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 sysctrl_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 #ifndef __SYSCTRL_H__
49 #define __SYSCTRL_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_sysctl.h>
68 #include <inc/hw_prcm.h>
69 #include <inc/hw_nvic.h>
70 #include <inc/hw_aon_wuc.h>
71 #include <inc/hw_aux_wuc.h>
72 #include <inc/hw_aon_ioc.h>
73 #include <inc/hw_ddi_0_osc.h>
74 #include <inc/hw_rfc_pwr.h>
75 #include <inc/hw_prcm.h>
76 #include <inc/hw_adi_3_refsys.h>
77 #include <inc/hw_aon_sysctl.h>
78 #include <inc/hw_aon_rtc.h>
79 #include <inc/hw_fcfg1.h>
80 #include <driverlib/interrupt.h>
81 #include <driverlib/debug.h>
82 #include <driverlib/pwr_ctrl.h>
83 #include <driverlib/osc.h>
84 #include <driverlib/prcm.h>
85 #include <driverlib/aux_wuc.h>
86 #include <driverlib/aon_wuc.h>
87 #include <driverlib/adi.h>
88 #include <driverlib/ddi.h>
89 #include <driverlib/cpu.h>
90 #include <driverlib/vims.h>
91 
92 //*****************************************************************************
93 //
94 // Support for DriverLib in ROM:
95 // This section renames all functions that are not "static inline", so that
96 // calling these functions will default to implementation in flash. At the end
97 // of this file a second renaming will change the defaults to implementation in
98 // ROM for available functions.
99 //
100 // To force use of the implementation in flash, e.g. for debugging:
101 // - Globally: Define DRIVERLIB_NOROM at project level
102 // - Per function: Use prefix "NOROM_" when calling the function
103 //
104 //*****************************************************************************
105 #if !defined(DOXYGEN)
106     #define SysCtrlPowerEverything          NOROM_SysCtrlPowerEverything
107     #define SysCtrlStandby                  NOROM_SysCtrlStandby
108     #define SysCtrlPowerdown                NOROM_SysCtrlPowerdown
109     #define SysCtrlShutdown                 NOROM_SysCtrlShutdown
110     #define SysCtrlResetSourceGet           NOROM_SysCtrlResetSourceGet
111 #endif
112 
113 //*****************************************************************************
114 //
115 // Defines for the settings of the main XOSC
116 //
117 //*****************************************************************************
118 #define SYSCTRL_SYSBUS_ON       0x00000001
119 #define SYSCTRL_SYSBUS_OFF      0x00000000
120 
121 //*****************************************************************************
122 //
123 // Defines for the different power modes of the System CPU
124 //
125 //*****************************************************************************
126 #define CPU_RUN                 0x00000000
127 #define CPU_SLEEP               0x00000001
128 #define CPU_DEEP_SLEEP          0x00000002
129 
130 //*****************************************************************************
131 //
132 // Defines for SysCtrlSetRechargeBeforePowerDown
133 //
134 //*****************************************************************************
135 #define XOSC_IN_HIGH_POWER_MODE 0 // When xosc_hf is in HIGH_POWER_XOSC
136 #define XOSC_IN_LOW_POWER_MODE  1 // When xosc_hf is in LOW_POWER_XOSC
137 
138 //*****************************************************************************
139 //
140 // API Functions and prototypes
141 //
142 //*****************************************************************************
143 
144 //*****************************************************************************
145 //
146 //! \brief Power up everything.
147 //!
148 //! \note The sequencing in this function is not necessarily how you would
149 //! want to sequence active mode in a real application. There might be
150 //! application specific prerequisites or hardware restrictions you would want
151 //! to consider which deviate from this specific implementation.
152 //!
153 //! \note This function might be deprecated in future releases
154 //!
155 //! \return None
156 //
157 //*****************************************************************************
158 extern void SysCtrlPowerEverything(void);
159 
160 //*****************************************************************************
161 //
162 //! \brief Force the system into standby mode.
163 //!
164 //! \note The sequencing in this function is not necessarily how you would
165 //! want to sequence the standby in a real application. There might be
166 //! application specific prerequisites you would want to do before entering
167 //! standby which deviate from this specific implementation.
168 //!
169 //! \return None
170 //
171 //*****************************************************************************
172 extern void SysCtrlStandby(void);
173 
174 //*****************************************************************************
175 //
176 //! \brief Force the system into power down.
177 //!
178 //! \note The sequencing in this function is not necessarily how you would
179 //! want to sequence the powerdown in a real application. There might be
180 //! application specific prerequisites you would want to do before entering
181 //! powerdown which deviate from this specific implementation.
182 //!
183 //! \return None
184 //
185 //*****************************************************************************
186 extern void SysCtrlPowerdown(void);
187 
188 //*****************************************************************************
189 //
190 //! \brief Force the system into shutdown.
191 //!
192 //! \note It is solely the programmer's responsibility to properly configure an
193 //! interrupt that will enable the device to wakeup from the shutdown mode,
194 //! before calling this function. In shutdown the only possible wakeup action
195 //! is an IO interrupt.
196 //!
197 //! \note The sequencing in this function is not necessarily how you would
198 //! want to sequence the shutdown in a real application. There might be
199 //! application specific prerequisites you would want to do before entering
200 //! shutdown which deviate from this specific implementation.
201 //!
202 //! \return This function does \b not return.
203 //
204 //*****************************************************************************
205 extern void SysCtrlShutdown(void);
206 
207 //*****************************************************************************
208 //
209 //! \brief Get the CPU core clock frequency.
210 //!
211 //! Use this function to get the current clock frequency for the CPU.
212 //!
213 //! The CPU can run from 48 MHz and down to 750kHz. The frequency is defined
214 //! by the combined division factor of the SYSBUS and the CPU clock divider.
215 //!
216 //! \return Returns the current CPU core clock frequency.
217 //
218 //*****************************************************************************
219 __STATIC_INLINE uint32_t
SysCtrlClockGet(void)220 SysCtrlClockGet( void )
221 {
222     //
223     // Return fixed clock speed
224     //
225     return( GET_MCU_CLOCK );
226 }
227 
228 
229 //*****************************************************************************
230 //
231 //! \brief Sync all accesses to the AON register interface.
232 //!
233 //! When this function returns, all writes to the AON register interface are
234 //! guaranteed to have propagated to hardware. The function will return
235 //! immediately if no AON writes are pending; otherwise, it will wait for the next
236 //! AON clock before returning.
237 //!
238 //! \return None
239 //!
240 //! \sa \ref SysCtrlAonUpdate()
241 //
242 //*****************************************************************************
243 __STATIC_INLINE void
SysCtrlAonSync(void)244 SysCtrlAonSync(void)
245 {
246     //
247     // Sync the AON interface
248     //
249     HWREG(AON_RTC_BASE + AON_RTC_O_SYNC);
250 }
251 
252 //*****************************************************************************
253 //
254 //! \brief Update all interfaces to AON.
255 //!
256 //! When this function returns, at least 1 clock cycle has progressed on the
257 //! AON domain, so that any outstanding updates to and from the AON interface
258 //! is guaranteed to be in sync.
259 //!
260 //! \note This function should primarily be used after wakeup from sleep modes,
261 //! as it will guarantee that all shadow registers on the AON interface are updated
262 //! before reading any AON registers from the MCU domain. If a write has been
263 //! done to the AON interface it is sufficient to call the \ref SysCtrlAonSync().
264 //!
265 //! \return None
266 //!
267 //! \sa \ref SysCtrlAonSync()
268 //
269 //*****************************************************************************
270 __STATIC_INLINE void
SysCtrlAonUpdate(void)271 SysCtrlAonUpdate(void)
272 {
273     //
274     // Force a clock cycle on the AON interface to guarantee all registers are
275     // in sync.
276     //
277     HWREG(AON_RTC_BASE + AON_RTC_O_SYNC) = 1;
278     HWREG(AON_RTC_BASE + AON_RTC_O_SYNC);
279 }
280 
281 
282 //*****************************************************************************
283 //
284 //! \brief Set Recharge values before entering Power Down.
285 //!
286 //! This function shall be called just before entering Power Down.
287 //! It calculates an optimal and safe recharge setting of the adaptive recharge
288 //! controller. The results of previous setting are also taken into account.
289 //!
290 //! \note In order to make sure that the register writes are completed, \ref SysCtrlAonSync()
291 //! must be called before entering standby/power down. This is not done internally
292 //! in this function due to two reasons:
293 //! - 1) There might be other register writes that must be synchronized as well.
294 //! - 2) It is possible to save some time by doing other things before calling
295 //! \ref SysCtrlAonSync() since this call will not return before there are no
296 //! outstanding write requests between MCU and AON.
297 //!
298 //! \param xoscPowerMode (typically running in XOSC_IN_HIGH_POWER_MODE all the time).
299 //! - \ref XOSC_IN_HIGH_POWER_MODE : When xosc_hf is in HIGH_POWER_XOSC.
300 //! - \ref XOSC_IN_LOW_POWER_MODE  : When xosc_hf is in LOW_POWER_XOSC.
301 //!
302 //! \return None
303 //
304 //*****************************************************************************
305 void
306 SysCtrlSetRechargeBeforePowerDown( uint32_t xoscPowerMode );
307 
308 
309 //*****************************************************************************
310 //
311 //! \brief Adjust Recharge calculations to be used next.
312 //!
313 //! This function shall be called just after returning from Power Down.
314 //!
315 //! Reads the results from the adaptive recharge controller and current chip
316 //! temperature. This is used as additional information when calculating
317 //! optimal recharge controller settings next time (When
318 //! \ref SysCtrlSetRechargeBeforePowerDown() is called next time).
319 //!
320 //! \note
321 //! Special care must be taken to make sure that the AON registers read are
322 //! updated after the wakeup. Writing to an AON register and then calling
323 //! \ref SysCtrlAonSync() will handle this. Typically this must be done anyway,
324 //! for example by calling \ref AONWUCAuxWakeupEvent() and then later on calling
325 //! \ref SysCtrlAonSync() just before calling \ref SysCtrlSetRechargeBeforePowerDown().
326 //!
327 //! \return None
328 //
329 //*****************************************************************************
330 void
331 SysCtrlAdjustRechargeAfterPowerDown( void );
332 
333 
334 //*****************************************************************************
335 //
336 //! \brief Turns DCDC on or off depending of what is considered to be optimal usage.
337 //!
338 //! This function controls the DCDC only if both the following CCFG settings are \c true:
339 //! - DCDC is configured to be used.
340 //! - Alternative DCDC settings are defined and enabled.
341 //!
342 //! The DCDC is configured in accordance to the CCFG settings when turned on.
343 //!
344 //! This function should be called periodically.
345 //!
346 //! \return None
347 //
348 //*****************************************************************************
349 void
350 SysCtrl_DCDC_VoltageConditionalControl( void );
351 
352 
353 //*****************************************************************************
354 // \name Return values from calling SysCtrlResetSourceGet()
355 //@{
356 //*****************************************************************************
357 #define RSTSRC_PWR_ON               (( AON_SYSCTL_RESETCTL_RESET_SRC_PWR_ON    ) >> ( AON_SYSCTL_RESETCTL_RESET_SRC_S ))
358 #define RSTSRC_PIN_RESET            (( AON_SYSCTL_RESETCTL_RESET_SRC_PIN_RESET ) >> ( AON_SYSCTL_RESETCTL_RESET_SRC_S ))
359 #define RSTSRC_VDDS_LOSS            (( AON_SYSCTL_RESETCTL_RESET_SRC_VDDS_LOSS ) >> ( AON_SYSCTL_RESETCTL_RESET_SRC_S ))
360 #define RSTSRC_VDD_LOSS             (( AON_SYSCTL_RESETCTL_RESET_SRC_VDD_LOSS  ) >> ( AON_SYSCTL_RESETCTL_RESET_SRC_S ))
361 #define RSTSRC_VDDR_LOSS            (( AON_SYSCTL_RESETCTL_RESET_SRC_VDDR_LOSS ) >> ( AON_SYSCTL_RESETCTL_RESET_SRC_S ))
362 #define RSTSRC_CLK_LOSS             (( AON_SYSCTL_RESETCTL_RESET_SRC_CLK_LOSS  ) >> ( AON_SYSCTL_RESETCTL_RESET_SRC_S ))
363 #define RSTSRC_SYSRESET             (( AON_SYSCTL_RESETCTL_RESET_SRC_SYSRESET  ) >> ( AON_SYSCTL_RESETCTL_RESET_SRC_S ))
364 #define RSTSRC_WARMRESET            (( AON_SYSCTL_RESETCTL_RESET_SRC_WARMRESET ) >> ( AON_SYSCTL_RESETCTL_RESET_SRC_S ))
365 #define RSTSRC_WAKEUP_FROM_SHUTDOWN ((( AON_SYSCTL_RESETCTL_RESET_SRC_M        ) >> ( AON_SYSCTL_RESETCTL_RESET_SRC_S )) + 1 )
366 //@}
367 
368 //*****************************************************************************
369 //
370 //! \brief Returns last reset source (including "wakeup from shutdown").
371 //!
372 //! \return Returns one of the RSTSRC_ defines.
373 //
374 //*****************************************************************************
375 extern uint32_t SysCtrlResetSourceGet( void );
376 
377 //*****************************************************************************
378 //
379 //! \brief Perform a full system reset.
380 //!
381 //! \return The chip will reset and hence never return from this call.
382 //
383 //*****************************************************************************
384 __STATIC_INLINE void
SysCtrlSystemReset(void)385 SysCtrlSystemReset( void )
386 {
387    // Disable CPU interrupts
388    CPUcpsid();
389    // Write reset register
390    HWREGBITW( AON_SYSCTL_BASE + AON_SYSCTL_O_RESETCTL, AON_SYSCTL_RESETCTL_SYSRESET_BITN ) = 1;
391    // Finally, wait until the above write propagates
392    while ( 1 ) {
393       // Do nothing, just wait for the reset (and never return from here)
394    }
395 }
396 
397 //*****************************************************************************
398 //
399 //! \brief Enables reset if OSC clock loss event is asserted.
400 //!
401 //! Clock loss circuit in analog domain must be enabled as well in order to
402 //! actually enable for a clock loss reset to occur
403 //! \ref OSCClockLossEventEnable().
404 //!
405 //! \note This function shall typically not be called because the clock loss
406 //! reset functionality is controlled by the boot code (a factory configuration
407 //! defines whether it is set or not).
408 //!
409 //! \return None
410 //!
411 //! \sa \ref SysCtrlClockLossResetDisable(), \ref OSCClockLossEventEnable()
412 //
413 //*****************************************************************************
414 __STATIC_INLINE void
SysCtrlClockLossResetEnable(void)415 SysCtrlClockLossResetEnable(void)
416 {
417     //
418     // Set clock loss enable bit in AON_SYSCTRL using bit banding
419     //
420     HWREGBITW(AON_SYSCTL_BASE + AON_SYSCTL_O_RESETCTL, AON_SYSCTL_RESETCTL_CLK_LOSS_EN_BITN) = 1;
421 }
422 
423 //*****************************************************************************
424 //
425 //! \brief Disables reset due to OSC clock loss event.
426 //!
427 //! \note This function shall typically not be called because the clock loss
428 //! reset functionality is controlled by the boot code (a factory configuration
429 //! defines whether it is set or not).
430 //!
431 //! \return None
432 //!
433 //! \sa \ref SysCtrlClockLossResetEnable()
434 //
435 //*****************************************************************************
436 __STATIC_INLINE void
SysCtrlClockLossResetDisable(void)437 SysCtrlClockLossResetDisable(void)
438 {
439     //
440     // Clear clock loss enable bit in AON_SYSCTRL using bit banding
441     //
442     HWREGBITW(AON_SYSCTL_BASE + AON_SYSCTL_O_RESETCTL, AON_SYSCTL_RESETCTL_CLK_LOSS_EN_BITN) = 0;
443 }
444 
445 //*****************************************************************************
446 //
447 // Support for DriverLib in ROM:
448 // Redirect to implementation in ROM when available.
449 //
450 //*****************************************************************************
451 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
452     #include <driverlib/rom.h>
453     #ifdef ROM_SysCtrlPowerEverything
454         #undef  SysCtrlPowerEverything
455         #define SysCtrlPowerEverything          ROM_SysCtrlPowerEverything
456     #endif
457     #ifdef ROM_SysCtrlStandby
458         #undef  SysCtrlStandby
459         #define SysCtrlStandby                  ROM_SysCtrlStandby
460     #endif
461     #ifdef ROM_SysCtrlPowerdown
462         #undef  SysCtrlPowerdown
463         #define SysCtrlPowerdown                ROM_SysCtrlPowerdown
464     #endif
465     #ifdef ROM_SysCtrlShutdown
466         #undef  SysCtrlShutdown
467         #define SysCtrlShutdown                 ROM_SysCtrlShutdown
468     #endif
469     #ifdef ROM_SysCtrlResetSourceGet
470         #undef  SysCtrlResetSourceGet
471         #define SysCtrlResetSourceGet           ROM_SysCtrlResetSourceGet
472     #endif
473 #endif
474 
475 //*****************************************************************************
476 //
477 // Mark the end of the C bindings section for C++ compilers.
478 //
479 //*****************************************************************************
480 #ifdef __cplusplus
481 }
482 #endif
483 
484 #endif //  __SYSCTRL_H__
485 
486 //*****************************************************************************
487 //
488 //! Close the Doxygen group.
489 //! @}
490 //! @}
491 //
492 //*****************************************************************************
493