1 /******************************************************************************
2  *  Filename:       pmctl.h
3  *
4  *  Description:    Defines and prototypes for the PMCTL module.
5  *
6  *  Copyright (c) 2022-2024 Texas Instruments Incorporated
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  *  1) Redistributions of source code must retain the above copyright notice,
12  *     this list of conditions and the following disclaimer.
13  *
14  *  2) Redistributions in binary form must reproduce the above copyright notice,
15  *     this list of conditions and the following disclaimer in the documentation
16  *     and/or other materials provided with the distribution.
17  *
18  *  3) Neither the name of the copyright holder nor the names of its
19  *     contributors may be used to endorse or promote products derived from this
20  *     software without specific prior written permission.
21  *
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  *  POSSIBILITY OF SUCH DAMAGE.
33  *
34  ******************************************************************************/
35 
36 #ifndef __PMCTL_H__
37 #define __PMCTL_H__
38 
39 //*****************************************************************************
40 //
41 //! \addtogroup system_control_group
42 //! @{
43 //! \addtogroup pmctl_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 //*****************************************************************************
49 //
50 // If building with a C++ compiler, make all of the definitions in this header
51 // have a C binding.
52 //
53 //*****************************************************************************
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 #include <stdbool.h>
59 #include <stdint.h>
60 
61 #include "../inc/hw_types.h"
62 #include "../inc/hw_memmap.h"
63 #include "../inc/hw_pmctl.h"
64 
65 #include "../cmsis/cc23x0r5.h"
66 #include "../cmsis/core/core_cm0plus.h"
67 
68 //*****************************************************************************
69 //
70 // API Functions and prototypes
71 //
72 //*****************************************************************************
73 
74 //*****************************************************************************
75 //! \name PMCTLGetResetReason() return values
76 //@{
77 //*****************************************************************************
78 //! Device woke up from shutdown due to an IO event
79 #define PMCTL_RESET_SHUTDOWN_IO        (PMCTL_RSTSTA_SDDET | PMCTL_RSTSTA_IOWUSD)
80 //! Device woke up from shutdown due to an SWD event
81 #define PMCTL_RESET_SHUTDOWN_SWD       PMCTL_RSTSTA_SDDET
82 //! Device reset because of a watchdog timeout.
83 #define PMCTL_RESET_WATCHDOG           (PMCTL_RSTSTA_SYSSRC_WDTEV | PMCTL_RSTSTA_RESETSRC_SYSRESET)
84 //! Device reset triggered by software writing to RSTCTL.SYSRST
85 #define PMCTL_RESET_SYSTEM             (PMCTL_RSTSTA_SYSSRC_SYSRSTEV | PMCTL_RSTSTA_RESETSRC_SYSRESET)
86 //! Device reset triggered by CPU reset event
87 #define PMCTL_RESET_CPU                (PMCTL_RSTSTA_SYSSRC_CPURSTEV | PMCTL_RSTSTA_RESETSRC_SYSRESET)
88 //! Device reset triggered by CPU lockup event
89 #define PMCTL_RESET_LOCKUP             (PMCTL_RSTSTA_SYSSRC_LOCKUPEV | PMCTL_RSTSTA_RESETSRC_SYSRESET)
90 //! Device reset triggered by Analog FSM timeout event
91 #define PMCTL_RESET_ANALOG_FSM_TIMEOUT (PMCTL_RSTSTA_SYSSRC_AFSMEV | PMCTL_RSTSTA_RESETSRC_SYSRESET)
92 //! Device reset triggered by Analog Error reset event
93 #define PMCTL_RESET_ANALOG_ERROR       (PMCTL_RSTSTA_SYSSRC_AERREV | PMCTL_RSTSTA_RESETSRC_SYSRESET)
94 //! Device reset triggered by Digital Error reset event
95 #define PMCTL_RESET_DIGITAL_ERROR      (PMCTL_RSTSTA_SYSSRC_DERREV | PMCTL_RSTSTA_RESETSRC_SYSRESET)
96 //! Device reset triggered by Serial Wire Debug reset event
97 #define PMCTL_RESET_SWD                (PMCTL_RSTSTA_SYSSRC_SWDRSTEV | PMCTL_RSTSTA_RESETSRC_SYSRESET)
98 //! Device reset due to LFXT clock loss
99 #define PMCTL_RESET_LFXT               (PMCTL_RSTSTA_SYSSRC_LFLOSSEV | PMCTL_RSTSTA_RESETSRC_SYSRESET)
100 //! Device woke up from thermal shutdown after temperature drop
101 #define PMCTL_RESET_TSD                (PMCTL_RSTSTA_TSDEV | PMCTL_RSTSTA_RESETSRC_PINRESET)
102 //! Device reset due to VDDR brownout event
103 #define PMCTL_RESET_VDDR               PMCTL_RSTSTA_RESETSRC_VDDRLOSS
104 //! Device reset due to VDDS brownout event
105 #define PMCTL_RESET_VDDS               PMCTL_RSTSTA_RESETSRC_VDDSLOSS
106 //! Device reset due to pin reset
107 #define PMCTL_RESET_PIN                PMCTL_RSTSTA_RESETSRC_PINRESET
108 //! Device booted due to power on reset
109 #define PMCTL_RESET_POR                PMCTL_RSTSTA_RESETSRC_PWRON
110 //@}
111 
112 //*****************************************************************************
113 //! \name PMCTLSetVoltageRegulator() inputs and PMCTLGetVoltageRegulator() return values
114 //@{
115 //*****************************************************************************
116 //! DCDC voltage regulator
117 #define PMCTL_VOLTAGE_REGULATOR_DCDC PMCTL_VDDRCTL_SELECT_DCDC
118 //! GLDO voltage regulator
119 #define PMCTL_VOLTAGE_REGULATOR_GLDO PMCTL_VDDRCTL_SELECT_GLDO
120 //@}
121 
122 //*****************************************************************************
123 //
124 //! \brief Get the reason for the system reset.
125 //!
126 //! This function will return the reason the device reset.
127 //!
128 //! \return Returns the reset source.
129 //! - \ref PMCTL_RESET_SHUTDOWN_IO
130 //! - \ref PMCTL_RESET_SHUTDOWN_SWD
131 //! - \ref PMCTL_RESET_WATCHDOG
132 //! - \ref PMCTL_RESET_SYSTEM
133 //! - \ref PMCTL_RESET_CPU
134 //! - \ref PMCTL_RESET_LOCKUP
135 //! - \ref PMCTL_RESET_ANALOG_FSM_TIMEOUT
136 //! - \ref PMCTL_RESET_ANALOG_ERROR
137 //! - \ref PMCTL_RESET_DIGITAL_ERROR
138 //! - \ref PMCTL_RESET_SWD
139 //! - \ref PMCTL_RESET_LFXT
140 //! - \ref PMCTL_RESET_TSD
141 //! - \ref PMCTL_RESET_VDDR
142 //! - \ref PMCTL_RESET_VDDS
143 //! - \ref PMCTL_RESET_PIN
144 //! - \ref PMCTL_RESET_POR
145 //
146 //*****************************************************************************
PMCTLGetResetReason(void)147 __STATIC_INLINE uint32_t PMCTLGetResetReason(void)
148 {
149     return HWREG(PMCTL_BASE + PMCTL_O_RSTSTA);
150 }
151 
152 //*****************************************************************************
153 //
154 //! \brief Issue a system reset.
155 //!
156 //! This function issues a system reset. This will cause the device to reboot.
157 //! After rebooting, #PMCTLGetResetReason() will return #PMCTL_RESET_SYSTEM.
158 //!
159 //! \return None
160 //
161 //*****************************************************************************
PMCTLResetSystem(void)162 __STATIC_INLINE void PMCTLResetSystem(void)
163 {
164     // Disable interrupts
165     __disable_irq();
166 
167     // Write reset register
168     HWREG(PMCTL_BASE + PMCTL_O_RSTCTL) |= PMCTL_RSTCTL_SYSRST_SET;
169 
170     // Should never return from this function
171     while (1) {}
172 }
173 
174 //*****************************************************************************
175 //
176 //! \brief Set the VDDR regulator
177 //!
178 //! This function sets the VDDR regulator to use in active, idle, and standby
179 //! modes.
180 //!
181 //! \param regulator The voltage regulator to switch to.
182 //! - \ref PMCTL_VOLTAGE_REGULATOR_DCDC
183 //! - \ref PMCTL_VOLTAGE_REGULATOR_GLDO
184 //!
185 //! \return None
186 //
187 //*****************************************************************************
PMCTLSetVoltageRegulator(uint32_t regulator)188 __STATIC_INLINE void PMCTLSetVoltageRegulator(uint32_t regulator)
189 {
190     // Write to VDDR regulator register while preserving other configuration
191     // fields
192     uint32_t tmp                        = HWREG(PMCTL_BASE + PMCTL_O_VDDRCTL) & ~PMCTL_VDDRCTL_SELECT_M;
193     HWREG(PMCTL_BASE + PMCTL_O_VDDRCTL) = tmp | regulator;
194 }
195 
196 //*****************************************************************************
197 //
198 //! \brief Get the current VDDR regulator
199 //!
200 //! This function gets the VDDR regulator currently in use.
201 //!
202 //! \return Current voltage regulator
203 //! - PMCTL_VOLTAGE_REGULATOR_DCDC
204 //! - PMCTL_VOLTAGE_REGULATOR_GLDO
205 //
206 //*****************************************************************************
PMCTLGetVoltageRegulator(void)207 __STATIC_INLINE uint32_t PMCTLGetVoltageRegulator(void)
208 {
209     return (HWREG(PMCTL_BASE + PMCTL_O_VDDRCTL) & PMCTL_VDDRCTL_SELECT_M);
210 }
211 
212 //*****************************************************************************
213 //
214 // Mark the end of the C bindings section for C++ compilers.
215 //
216 //*****************************************************************************
217 #ifdef __cplusplus
218 }
219 #endif
220 
221 //*****************************************************************************
222 //
223 //! Close the Doxygen group.
224 //! @}
225 //! @}
226 //
227 //*****************************************************************************
228 
229 #endif // __PMCTL_H__
230