1 /*
2  * Copyright (c) 2017-2020, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQueueNTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /** ============================================================================
33  *  @file       PowerCC26X2.h
34  *
35  *  @brief      Power manager interface for CC26X2
36  *
37  *  The Power header file should be included in an application as follows:
38  *  @code
39  *  #include <ti/drivers/Power.h>
40  *  #include <ti/drivers/power/PowerCC26X2.h>
41  *  @endcode
42  *
43  *  Refer to @ref Power.h for a complete description of APIs.
44  *
45  *  ## Implementation #
46  *  This header file defines the power resources, constraints, events, sleep
47  *  states and transition latencies for CC26X2.
48  *
49  *  ============================================================================
50  */
51 
52 #ifndef ti_drivers_power_PowerCC26X2_
53 #define ti_drivers_power_PowerCC26X2_
54 
55 #include <ti/drivers/dpl/HwiP.h>
56 #include <ti/drivers/dpl/ClockP.h>
57 #include <ti/drivers/Power.h>
58 #include <ti/drivers/power/PowerCC26XX.h>
59 #include <ti/drivers/Temperature.h>
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 /*! The latency to reserve for resume from STANDBY (usec). */
66 #define PowerCC26X2_RESUMETIMESTANDBY  750
67 
68 /*! The total latency to reserve for entry to and exit from STANDBY (usec). */
69 #define PowerCC26X2_TOTALTIMESTANDBY   1000
70 
71 /*! The initial delay when waking from STANDBY (usec). */
72 #define PowerCC26X2_WAKEDELAYSTANDBY    240
73 
74 /*! The initial wait time (usec) before checking if RCOSC_LF is stable. */
75 #define PowerCC26X2_INITIALWAITRCOSC_LF 1000
76 
77 /*! The retry wait time (usec) when checking to see if RCOSC_LF is stable. */
78 #define PowerCC26X2_RETRYWAITRCOSC_LF   1000
79 
80 /*! The initial wait time (usec) before checking if XOSC_HF is stable. */
81 #define PowerCC26X2_INITIALWAITXOSC_HF  50
82 
83 /*! The retry wait time (usec) when checking to see if XOSC_HF is stable. */
84 #define PowerCC26X2_RETRYWAITXOSC_HF    50
85 
86 /*! The initial wait time (usec) before checking if XOSC_LF is stable. */
87 #define PowerCC26X2_INITIALWAITXOSC_LF  10000
88 
89 /*! The retry wait time (usec) when checking to see if XOSC_LF is stable. */
90 #define PowerCC26X2_RETRYWAITXOSC_LF    5000
91 
92 #define PowerCC26X2_PERIPH_PKA          PowerCC26XX_NUMRESOURCES /*!< Resource ID: PKA Module */
93 
94 #define PowerCC26X2_PERIPH_UART1        PowerCC26XX_NUMRESOURCES + 1 /*!< Resource ID: UART1 */
95 
96 /*! The temperature delta in degrees C before the RTC is re-compensated when
97  *  SCLK_LF is derived from SCLK_HF and SCLK_HF is supplied by HPOSC.
98  */
99 #define PowerCC26X2_HPOSC_RTC_COMPENSATION_DELTA 3
100 
101 /* \cond */
102 #define PowerCC26X2_NUMRESOURCES   (PowerCC26XX_NUMRESOURCES + 2) /* Number of resources in database */
103 /* \endcond */
104 
105 /* \cond */
106 #define PowerCC26X2_NUMCONSTRAINTS (PowerCC26XX_NUMCONSTRAINTS + 0) /* Number of constraints supported */
107 /* \endcond */
108 
109 /* \cond */
110 /*
111  *  Calibration stages
112  */
113 #define PowerCC26X2_SETUP_CALIBRATE     1
114 #define PowerCC26X2_INITIATE_CALIBRATE  2
115 #define PowerCC26X2_DO_CALIBRATE        3
116 /* \endcond */
117 
118 
119 /*! @brief Global configuration structure */
120 typedef struct {
121     /*!
122      *  @brief The Power Policy's initialization function
123      *
124      *  If the policy does not have an initialization function, 'NULL'
125      *  should be specified.
126      */
127     Power_PolicyInitFxn policyInitFxn;
128     /*!
129      *  @brief The Power Policy function
130      *
131      *  When enabled, this function is invoked in the idle loop, to
132      *  opportunistically select and activate sleep states.
133      *
134      *  Two reference policies are provided:
135      *
136      *    PowerCC26X2_doWFI() - a simple policy that invokes CPU wait for
137      *    interrupt (WFI)
138      *
139      *    PowerCC26X2_standbyPolicy() - an agressive policy that considers
140      *    constraints, time until next scheduled work, and sleep state
141      *    latencies, and optionally puts the device into the STANDBY state,
142      *    the IDLE state, or as a minimum, WFI.
143      *
144      *  Custom policies can be written, and specified via this function pointer.
145      *
146      *  In addition to this static selection, the Power Policy can be
147      *  dynamically changed at runtime, via the Power_setPolicy() API.
148      */
149     Power_PolicyFxn policyFxn;
150     /*!
151      *  @brief The function to be used for activating RC Oscillator (RCOSC)
152      *  calibration
153      *
154      *  Calibration is normally enabled, via specification of the function
155      *  PowerCC26X2_calibrate().  This enables high accuracy operation, and
156      *  faster high frequency crystal oscillator (XOSC_HF) startups.
157      *
158      *  To disable RCOSC calibration, the function PowerCC26X2_noCalibrate()
159      *  should be specified.
160      */
161     bool (*calibrateFxn)(unsigned int);
162     /*!
163      *  @brief Boolean specifying if the Power Policy function is enabled
164      *
165      *  If 'true', the policy function will be invoked once for each pass
166      *  of the idle loop.
167      *
168      *  If 'false', the policy will not be invoked.
169      *
170      *  In addition to this static setting, the power policy can be dynamically
171      *  enabled and disabled at runtime, via the Power_enablePolicy() and
172      *  Power_disablePolicy() functions, respectively.
173      */
174     bool enablePolicy;
175     /*!
176      *  @brief Boolean specifying whether the low frequency RC oscillator
177      * (RCOSC_LF) should be calibrated.
178      *
179      *  If RCOSC calibration is enabled (above, via specification of
180      *  an appropriate calibrateFxn), this Boolean specifies whether
181      *  RCOSC_LF should be calibrated.
182      */
183     bool calibrateRCOSC_LF;
184     /*!
185      *  @brief Boolean specifying whether the high frequency RC oscillator
186      * (RCOSC_HF) should be calibrated.
187      *
188      *  If RCOSC calibration is enabled (above, via specification of
189      *  an appropriate calibrateFxn), this Boolean specifies whether
190      *  RCOSC_HF should be calibrated.
191      */
192     bool calibrateRCOSC_HF;
193     /*!
194      *  @brief The function to be used for enabling or disabling the TCXO
195      *
196      *  If TCXO is configured to be enabled in CCFG this function will
197      *  enable or disable the TCXO by asserting or deasserting power to it.
198      */
199     void (*enableTCXOFxn)(bool);
200 } PowerCC26X2_Config;
201 
202 /*!
203  *  @brief  PowerCC26X2_ModuleState
204  *
205  *  Power manager state structure. The application must not access any members
206  *  of this structure!
207  */
208 typedef struct {
209     List_List notifyList;           /*!< Event notification list */
210     uint32_t constraintMask;        /*!< Aggregate constraints mask */
211     ClockP_Struct clockObj;         /*!< Clock object for scheduling wakeups */
212     ClockP_Struct calibrationClock; /*!< Clock object for scheduling wakeups */
213     ClockP_Struct tcxoEnableClock;  /*!< Clock object for TCXO startup time */
214     HwiP_Struct oscHwi;             /*!< Hwi object for oscillator stabilisation */
215     HwiP_Struct tdcHwi;             /*!< Hwi object for RCOSC calibration */
216     Temperature_NotifyObj hposcRtcCompNotifyObj;
217     /*! Temperature notification to compensate the RTC when SCLK_LF is derived
218      *  from SCLK_HF when SCLK_HF is configured as HPOSC.
219      */
220     int32_t nDeltaFreqCurr;         /*!< RCOSC calibration variable */
221     int32_t nCtrimCurr;             /*!< RCOSC calibration variable */
222     int32_t nCtrimFractCurr;        /*!< RCOSC calibration variable */
223     int32_t nCtrimNew;              /*!< RCOSC calibration variable */
224     int32_t nCtrimFractNew;         /*!< RCOSC calibration variable */
225     int32_t nRtrimNew;              /*!< RCOSC calibration variable */
226     int32_t nRtrimCurr;             /*!< RCOSC calibration variable */
227     int32_t nDeltaFreqNew;          /*!< RCOSC calibration variable */
228     bool bRefine;                   /*!< RCOSC calibration variable */
229     uint32_t state;                 /*!< Current transition state */
230     bool xoscPending;               /*!< Is XOSC_HF activation in progress? */
231     bool calLF;                     /*!< Calibrate RCOSC_LF? */
232     uint8_t auxHwiState;            /*!< The AUX ISR calibration state */
233     bool busyCal;                   /*!< Already busy calibrating? */
234     uint32_t calStep;               /*!< The current calibration step */
235     bool firstLF;                   /*!< Is this the first LF calibration? */
236     bool enablePolicy;              /*!< Is the Power policy enabled? */
237     bool initialized;               /*!< Has Power_init() been called? */
238     uint8_t constraintCounts[PowerCC26X2_NUMCONSTRAINTS];
239     /*!< Array to maintain constraint reference counts */
240     uint8_t resourceCounts[PowerCC26X2_NUMRESOURCES];
241     /*!< Array to maintain resource dependency reference counts */
242     unsigned int (*resourceHandlers[3])(unsigned int);
243     /*!< Array of special dependency handler functions */
244     Power_PolicyFxn policyFxn;   /*!< The Power policy function */
245 } PowerCC26X2_ModuleState;
246 
247 /*!
248  *  @brief Enable RTC compensation when SCLK_LF is derived from HPOSC
249  *
250  *  Enables automatic compensation for temperature based clock drift of the RTC
251  *  when SCLK_LF is derived from HPOSC.
252  *
253  *  It only needs to be called once after the system boots.
254  *
255  *  This function should only be called when SCLK_LF is configured to be drived
256  *  from HPOSC.
257  */
258 void PowerCC26X2_enableHposcRtcCompensation(void);
259 
260 #ifdef __cplusplus
261 }
262 #endif
263 
264 #endif /* POWER_CC26X2_ */
265