-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsys_ctrl.c
426 lines (377 loc) · 16.7 KB
/
sys_ctrl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/******************************************************************************
* Filename: sys_ctrl.c
* Revised: 2017-11-07 11:56:04 +0100 (Tue, 07 Nov 2017)
* Revision: 50203
*
* Description: Driver for the System Control.
*
* Copyright (c) 2015 - 2017, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3) Neither the name of the ORGANIZATION nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
// Hardware headers
#include "../inc/hw_types.h"
#include "../inc/hw_ccfg.h"
// Driverlib headers
#include "aon_batmon.h"
#include "setup_rom.h"
#include "sys_ctrl.h"
//*****************************************************************************
//
// Handle support for DriverLib in ROM:
// This section will undo prototype renaming made in the header file
//
//*****************************************************************************
#if !defined(DOXYGEN)
#undef SysCtrlSetRechargeBeforePowerDown
#define SysCtrlSetRechargeBeforePowerDown NOROM_SysCtrlSetRechargeBeforePowerDown
#undef SysCtrlAdjustRechargeAfterPowerDown
#define SysCtrlAdjustRechargeAfterPowerDown NOROM_SysCtrlAdjustRechargeAfterPowerDown
#undef SysCtrl_DCDC_VoltageConditionalControl
#define SysCtrl_DCDC_VoltageConditionalControl NOROM_SysCtrl_DCDC_VoltageConditionalControl
#undef SysCtrlResetSourceGet
#define SysCtrlResetSourceGet NOROM_SysCtrlResetSourceGet
#endif
//*****************************************************************************
//
// Recharge calculator defines and globals
//
//*****************************************************************************
#define PD_STATE_CACHE_RET 1
#define PD_STATE_RFMEM_RET 2
#define PD_STATE_XOSC_LPM 4
#define PD_STATE_EXT_REG_MODE 8
typedef struct {
uint32_t pdTime ;
uint16_t pdRechargePeriod ;
uint8_t pdState ;
int8_t pdTemp ;
} PowerQualGlobals_t;
static PowerQualGlobals_t powerQualGlobals;
//*****************************************************************************
//
// SysCtrlSetRechargeBeforePowerDown( xoscPowerMode )
//
//*****************************************************************************
void
SysCtrlSetRechargeBeforePowerDown( uint32_t xoscPowerMode )
{
int32_t curTemp ;
int32_t shiftedTemp ;
int32_t deltaVddrSleepTrim ;
int32_t vddrTrimSleep ;
int32_t vddrTrimActve ;
int32_t diffVddrActiveSleep ;
uint32_t ccfg_ModeConfReg ;
uint32_t curState ;
uint32_t prcmRamRetention ;
uint32_t di ;
uint32_t dii ;
uint32_t ti ;
uint32_t cd ;
uint32_t cl ;
uint32_t load ;
uint32_t k ;
uint32_t vddrCap ;
uint32_t newRechargePeriod ;
uint32_t perE ;
uint32_t perM ;
const uint32_t * pLookupTable ;
// If external regulator mode we shall:
// - Disable adaptive recharge (bit[31]=0) in AON_WUC_O_RECHARGECFG
// - Set recharge period to approximately 500 mS (perM=31, perE=5 => 0xFD)
// - Make sure you get a recalculation if leaving external regulator mode by setting powerQualGlobals.pdState accordingly
if ( HWREG( AON_SYSCTL_BASE + AON_SYSCTL_O_PWRCTL ) & AON_SYSCTL_PWRCTL_EXT_REG_MODE ) {
powerQualGlobals.pdState = PD_STATE_EXT_REG_MODE;
HWREG( AON_WUC_BASE + AON_WUC_O_RECHARGECFG ) = 0x00A4FDFD;
return;
}
//--- Spec. point 1 ---
curTemp = AONBatMonTemperatureGetDegC();
curState = 0;
// read the MODE_CONF register in CCFG
ccfg_ModeConfReg = HWREG( CCFG_BASE + CCFG_O_MODE_CONF );
// Get VDDR_TRIM_SLEEP_DELTA + 1 (sign extended)
deltaVddrSleepTrim = ((((int32_t) ccfg_ModeConfReg )
<< ( 32 - CCFG_MODE_CONF_VDDR_TRIM_SLEEP_DELTA_W - CCFG_MODE_CONF_VDDR_TRIM_SLEEP_DELTA_S ))
>> ( 32 - CCFG_MODE_CONF_VDDR_TRIM_SLEEP_DELTA_W )) + 1;
// Do temperature compensation if enabled
if (( ccfg_ModeConfReg & CCFG_MODE_CONF_VDDR_TRIM_SLEEP_TC ) == 0 ) {
int32_t tcDelta = ( 62 - curTemp ) >> 3;
if ( tcDelta > 8 ) tcDelta = 8;
if ( tcDelta > deltaVddrSleepTrim ) deltaVddrSleepTrim = tcDelta;
}
{
vddrTrimSleep = SetupSignExtendVddrTrimValue((
HWREG( FCFG1_BASE + FCFG1_O_LDO_TRIM ) &
FCFG1_LDO_TRIM_VDDR_TRIM_SLEEP_M ) >>
FCFG1_LDO_TRIM_VDDR_TRIM_SLEEP_S ) ;
vddrTrimActve = SetupSignExtendVddrTrimValue((
HWREG( FCFG1_BASE + FCFG1_O_SHDW_ANA_TRIM ) &
FCFG1_SHDW_ANA_TRIM_VDDR_TRIM_M ) >>
FCFG1_SHDW_ANA_TRIM_VDDR_TRIM_S ) ;
}
vddrTrimSleep += deltaVddrSleepTrim;
if ( vddrTrimSleep > 21 ) vddrTrimSleep = 21;
if ( vddrTrimSleep < -10 ) vddrTrimSleep = -10;
// Write adjusted value using MASKED write (MASK8)
HWREGH( ADI3_BASE + ADI_O_MASK8B + ( ADI_3_REFSYS_O_DCDCCTL1 * 2 )) = (( ADI_3_REFSYS_DCDCCTL1_VDDR_TRIM_SLEEP_M << 8 ) |
(( vddrTrimSleep << ADI_3_REFSYS_DCDCCTL1_VDDR_TRIM_SLEEP_S ) & ADI_3_REFSYS_DCDCCTL1_VDDR_TRIM_SLEEP_M ));
prcmRamRetention = HWREG( PRCM_BASE + PRCM_O_RAMRETEN );
if ( prcmRamRetention & PRCM_RAMRETEN_VIMS_M ) {
curState |= PD_STATE_CACHE_RET;
}
if ( prcmRamRetention & PRCM_RAMRETEN_RFC ) {
curState |= PD_STATE_RFMEM_RET;
}
if ( xoscPowerMode != XOSC_IN_HIGH_POWER_MODE ) {
curState |= PD_STATE_XOSC_LPM;
}
//--- Spec. point 2 ---
if ((( curTemp - powerQualGlobals.pdTemp ) >= 5 ) || ( curState != powerQualGlobals.pdState )) {
//--- Spec. point 3 ---
shiftedTemp = curTemp - 15;
//--- Spec point 4 ---
//4. Check for external VDDR load option (may not be supported): ext_load = (VDDR_EXT_LOAD=0 in CCFG)
// Currently not implementing external load handling
// if ( __ccfg.ulModeConfig & MODE_CONF_VDDR_EXT_LOAD ) {
// }
pLookupTable = (uint32_t *)( FCFG1_BASE + FCFG1_O_PWD_CURR_20C );
//--- Spec point 5 ---
di = 0;
ti = 0;
if ( shiftedTemp >= 0 ) {
//--- Spec point 5.a ---
shiftedTemp += ( shiftedTemp << 4 );
//--- Spec point 5.b ---
ti = ( shiftedTemp >> 8 );
if ( ti > 7 ) {
ti = 7;
}
dii = ti;
if ( dii > 6 ) {
dii = 6;
}
//--- Spec point 5.c ---
cd = pLookupTable[ dii + 1 ] - pLookupTable[ dii ];
//--- Spec point 5.d ---
di = cd & 0xFF;
//--- Spec point 5.e ---
if ( curState & PD_STATE_XOSC_LPM ) {
di += (( cd >> 8 ) & 0xFF );
}
if ( curState & PD_STATE_RFMEM_RET ) {
di += (( cd >> 16 ) & 0xFF );
}
if ( curState & PD_STATE_CACHE_RET ) {
di += (( cd >> 24 ) & 0xFF );
}
//--- Spec point 5.f ---
// Currently not implementing external load handling
}
//--- Spec. point 6 ---
cl = pLookupTable[ ti ];
//--- Spec. point 7 ---
load = cl & 0xFF;
//--- Spec. point 8 ---
if ( curState & PD_STATE_XOSC_LPM ) {
load += (( cl >> 8 ) & 0xFF );
}
if ( curState & PD_STATE_RFMEM_RET ) {
load += (( cl >> 16 ) & 0xFF );
}
if ( curState & PD_STATE_CACHE_RET ) {
load += (( cl >> 24 ) & 0xFF );
}
//--- Spec. point 9 ---
load += ((( di * ( shiftedTemp - ( ti << 8 ))) + 128 ) >> 8 );
// Currently not implementing external load handling
// if ( __ccfg.ulModeConfig & MODE_CONF_VDDR_EXT_LOAD ) {
//--- Spec. point 10 ---
// } else {
//--- Spec. point 11 ---
diffVddrActiveSleep = ( vddrTrimActve - vddrTrimSleep );
if ( diffVddrActiveSleep < 1 ) diffVddrActiveSleep = 1;
k = ( diffVddrActiveSleep * 52 );
// }
//--- Spec. point 12 ---
vddrCap = ( ccfg_ModeConfReg & CCFG_MODE_CONF_VDDR_CAP_M ) >> CCFG_MODE_CONF_VDDR_CAP_S;
newRechargePeriod = ( vddrCap * k ) / load;
if ( newRechargePeriod > 0xFFFF ) {
newRechargePeriod = 0xFFFF;
}
powerQualGlobals.pdRechargePeriod = newRechargePeriod;
//--- Spec. point 13 ---
if ( curTemp > 127 ) curTemp = 127;
if ( curTemp < -128 ) curTemp = -128;
powerQualGlobals.pdTemp = curTemp;
powerQualGlobals.pdState = curState;
}
powerQualGlobals.pdTime = HWREG( AON_RTC_BASE + AON_RTC_O_SEC );
// Calculate PER_E and PER_M (based on powerQualGlobals.pdRechargePeriod)
// Round downwards but make sure PER_E=0 and PER_M=1 is the minimum possible setting.
// (assuming that powerQualGlobals.pdRechargePeriod always are <= 0xFFFF)
perE = 0;
perM = powerQualGlobals.pdRechargePeriod;
if ( perM < 31 ) {
perM = 31;
powerQualGlobals.pdRechargePeriod = 31;
}
while ( perM > 511 ) {
perM >>= 1;
perE += 1;
}
perM = ( perM - 15 ) >> 4;
HWREG( AON_WUC_BASE + AON_WUC_O_RECHARGECFG ) =
( 0x80A4E700 ) |
( perM << AON_WUC_RECHARGECFG_PER_M_S ) |
( perE << AON_WUC_RECHARGECFG_PER_E_S ) ;
HWREG( AON_WUC_BASE + AON_WUC_O_RECHARGESTAT ) = 0;
}
//*****************************************************************************
//
// SysCtrlAdjustRechargeAfterPowerDown()
//
//*****************************************************************************
void
SysCtrlAdjustRechargeAfterPowerDown( uint32_t vddrRechargeMargin )
{
int32_t curTemp ;
uint32_t longestRechargePeriod ;
uint32_t deltaTime ;
uint32_t newRechargePeriod ;
//--- Spec. point 2 ---
longestRechargePeriod = ( HWREG( AON_WUC_BASE + AON_WUC_O_RECHARGESTAT ) &
AON_WUC_RECHARGESTAT_MAX_USED_PER_M ) >>
AON_WUC_RECHARGESTAT_MAX_USED_PER_S ;
if ( longestRechargePeriod != 0 ) {
//--- Spec. changed (originally point 1) ---
curTemp = AONBatMonTemperatureGetDegC();
if ( curTemp < powerQualGlobals.pdTemp ) {
if ( curTemp < -128 ) {
curTemp = -128;
}
powerQualGlobals.pdTemp = curTemp;
}
// Add some margin between the longest previous recharge period and the
// next initial recharge period. Since it is a fixed margin, it will have a
// higher impact as a fraction of the converged recharge period at higher temperatures
// where it is needed more due to higher leakage.
if (longestRechargePeriod > vddrRechargeMargin) {
longestRechargePeriod -= vddrRechargeMargin;
}
else {
longestRechargePeriod = 1;
}
//--- Spec. point 4 ---
if ( longestRechargePeriod < powerQualGlobals.pdRechargePeriod ) {
powerQualGlobals.pdRechargePeriod = longestRechargePeriod;
} else {
//--- Spec. point 5 ---
deltaTime = HWREG( AON_RTC_BASE + AON_RTC_O_SEC ) - powerQualGlobals.pdTime + 2;
if ( deltaTime > 31 ) {
deltaTime = 31;
}
newRechargePeriod = powerQualGlobals.pdRechargePeriod + (( longestRechargePeriod - powerQualGlobals.pdRechargePeriod ) >> (deltaTime>>1));
if ( newRechargePeriod > 0xFFFF ) {
newRechargePeriod = 0xFFFF;
}
powerQualGlobals.pdRechargePeriod = newRechargePeriod;
}
}
}
//*****************************************************************************
//
// SysCtrl_DCDC_VoltageConditionalControl()
//
//*****************************************************************************
void
SysCtrl_DCDC_VoltageConditionalControl( void )
{
uint32_t batThreshold ; // Fractional format with 8 fractional bits.
uint32_t aonBatmonBat ; // Fractional format with 8 fractional bits.
uint32_t ccfg_ModeConfReg ; // Holds a copy of the CCFG_O_MODE_CONF register.
uint32_t aonSysctlPwrctl ; // Reflect whats read/written to the AON_SYSCTL_O_PWRCTL register.
// We could potentially call this function before any battery voltage measurement
// is made/available. In that case we must make sure that we do not turn off the DCDC.
// This can be done by doing nothing as long as the battery voltage is 0 (Since the
// reset value of the battery voltage register is 0).
aonBatmonBat = HWREG( AON_BATMON_BASE + AON_BATMON_O_BAT );
if ( aonBatmonBat != 0 ) {
// Check if Voltage Conditional Control is enabled
// It is enabled if all the following are true:
// - DCDC in use (either in active or recharge mode), (in use if one of the corresponding CCFG bits are zero).
// - Alternative DCDC settings are enabled ( DIS_ALT_DCDC_SETTING == 0 )
// - Not in external regulator mode ( EXT_REG_MODE == 0 )
ccfg_ModeConfReg = HWREG( CCFG_BASE + CCFG_O_MODE_CONF );
if (((( ccfg_ModeConfReg & CCFG_MODE_CONF_DCDC_RECHARGE_M ) == 0 ) ||
(( ccfg_ModeConfReg & CCFG_MODE_CONF_DCDC_ACTIVE_M ) == 0 ) ) &&
(( HWREG( AON_SYSCTL_BASE + AON_SYSCTL_O_PWRCTL ) & AON_SYSCTL_PWRCTL_EXT_REG_MODE ) == 0 ) &&
(( HWREG( CCFG_BASE + CCFG_O_SIZE_AND_DIS_FLAGS ) & CCFG_SIZE_AND_DIS_FLAGS_DIS_ALT_DCDC_SETTING ) == 0 ) )
{
aonSysctlPwrctl = HWREG( AON_SYSCTL_BASE + AON_SYSCTL_O_PWRCTL );
batThreshold = (((( HWREG( CCFG_BASE + CCFG_O_MODE_CONF_1 ) &
CCFG_MODE_CONF_1_ALT_DCDC_VMIN_M ) >>
CCFG_MODE_CONF_1_ALT_DCDC_VMIN_S ) + 28 ) << 4 );
if ( aonSysctlPwrctl & ( AON_SYSCTL_PWRCTL_DCDC_EN_M | AON_SYSCTL_PWRCTL_DCDC_ACTIVE_M )) {
// DCDC is ON, check if it should be switched off
if ( aonBatmonBat < batThreshold ) {
aonSysctlPwrctl &= ~( AON_SYSCTL_PWRCTL_DCDC_EN_M | AON_SYSCTL_PWRCTL_DCDC_ACTIVE_M );
HWREG( AON_SYSCTL_BASE + AON_SYSCTL_O_PWRCTL ) = aonSysctlPwrctl;
}
} else {
// DCDC is OFF, check if it should be switched on
if ( aonBatmonBat > batThreshold ) {
if (( ccfg_ModeConfReg & CCFG_MODE_CONF_DCDC_RECHARGE_M ) == 0 ) aonSysctlPwrctl |= AON_SYSCTL_PWRCTL_DCDC_EN_M ;
if (( ccfg_ModeConfReg & CCFG_MODE_CONF_DCDC_ACTIVE_M ) == 0 ) aonSysctlPwrctl |= AON_SYSCTL_PWRCTL_DCDC_ACTIVE_M ;
HWREG( AON_SYSCTL_BASE + AON_SYSCTL_O_PWRCTL ) = aonSysctlPwrctl;
}
}
}
}
}
//*****************************************************************************
//
// SysCtrlResetSourceGet()
//
//*****************************************************************************
uint32_t
SysCtrlResetSourceGet( void )
{
uint32_t aonSysctlResetCtl = HWREG( AON_SYSCTL_BASE + AON_SYSCTL_O_RESETCTL );
if ( aonSysctlResetCtl & AON_SYSCTL_RESETCTL_WU_FROM_SD_M ) {
if ( aonSysctlResetCtl & AON_SYSCTL_RESETCTL_GPIO_WU_FROM_SD_M ) {
return ( RSTSRC_WAKEUP_FROM_SHUTDOWN );
} else {
return ( RSTSRC_WAKEUP_FROM_TCK_NOISE );
}
} else {
return (( aonSysctlResetCtl & AON_SYSCTL_RESETCTL_RESET_SRC_M ) >> AON_SYSCTL_RESETCTL_RESET_SRC_S ) ;
}
}