aboutsummaryrefslogtreecommitdiff
path: root/KSDK_1.2.0/platform/system/inc/fsl_power_manager.h
blob: 546a3d632e3f3407a88e4634817edb1376539217 (plain)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
 * Copyright (c) 2014, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o 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.
 *
 * o Neither the name of Freescale Semiconductor, Inc. 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.
 */

#ifndef __FSL_POWER_MANAGER_H__
#define __FSL_POWER_MANAGER_H__

#include <stdint.h>
#include <stdbool.h>

#include "fsl_smc_hal.h"

/*!
 * @addtogroup power_manager
 * @{
 */


/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*!
 * @brief Power modes enumeration.
 *
 * Defines power mode. Used in the power mode configuration structure
 * (power_manager_user_config_t). From ARM core perspective, Power modes
 * can be generally divided to run modes (High speed run, Run and
 * Very low power run), sleep (Wait and Very low power wait) and deep sleep modes
 * (all Stop modes).
 * List of power modes supported by specific chip along with requirements for entering
 * and exiting of these modes can be found in chip documentation.
 * List of all supported power modes:\n
 *  \li kPowerManagerHsrun - High speed run mode. Chip-specific.
 *  \li kPowerManagerRun - Run mode. All Kinetis chips.
 *  \li kPowerManagerVlpr - Very low power run mode. All Kinetis chips.
 *  \li kPowerManagerWait - Wait mode. All Kinetis chips.
 *  \li kPowerManagerVlpw - Very low power wait mode. All Kinetis chips.
 *  \li kPowerManagerStop - Stop mode. All Kinetis chips.
 *  \li kPowerManagerVlps - Very low power stop mode. All Kinetis chips.
 *  \li kPowerManagerPstop1 - Partial stop 1 mode. Chip-specific.
 *  \li kPowerManagerPstop2 - Partial stop 2 mode. Chip-specific.
 *  \li kPowerManagerLls - Low leakage stop mode. All Kinetis chips.
 *  \li kPowerManagerLls2 - Low leakage stop 2 mode. Chip-specific.
 *  \li kPowerManagerLls3 - Low leakage stop 3 mode. Chip-specific.
 *  \li kPowerManagerVlls0 - Very low leakage stop 0 mode. Chip-specific.
 *  \li kPowerManagerVlls1 - Very low leakage stop 1 mode. All Kinetis chips.
 *  \li kPowerManagerVlls2 - Very low leakage stop 2 mode. All Kinetis chips.
 *  \li kPowerManagerVlls3 - Very low leakage stop 3 mode. All Kinetis chips.
 */
typedef enum _power_manager_modes {
#if FSL_FEATURE_SMC_HAS_HIGH_SPEED_RUN_MODE
    kPowerManagerHsrun,            /*!< High speed run mode. All Kinetis chips. @internal gui name="High speed run mode" */
#endif
    kPowerManagerRun,              /*!< Run mode. All Kinetis chips. @internal gui name="Run mode" */
    kPowerManagerVlpr,             /*!< Very low power run mode. All Kinetis chips. @internal gui name="Very low power run mode" */
    kPowerManagerWait,             /*!< Wait mode. All Kinetis chips. @internal gui name="Wait mode" */
    kPowerManagerVlpw,             /*!< Very low power wait mode. All Kinetis chips. @internal gui name="Very low power wait mode" */
    kPowerManagerStop,             /*!< Stop mode. All Kinetis chips. @internal gui name="Stop mode" */
    kPowerManagerVlps,             /*!< Very low power stop mode. All Kinetis chips. @internal gui name="Very low power stop mode" */
#if FSL_FEATURE_SMC_HAS_PSTOPO
    kPowerManagerPstop1,           /*!< Partial stop 1 mode. Chip-specific. @internal gui name="Partial stop 1 mode" */
    kPowerManagerPstop2,           /*!< Partial stop 2 mode. Chip-specific. @internal gui name="Partial stop 2 mode" */
#endif
#if FSL_FEATURE_SMC_HAS_LOW_LEAKAGE_STOP_MODE    
    kPowerManagerLls,              /*!< Low leakage stop mode. All Kinetis chips. @internal gui name="Low leakage stop mode" */
#endif    
#if FSL_FEATURE_SMC_HAS_LLS_SUBMODE
    kPowerManagerLls2,             /*!< Low leakage stop 2 mode. Chip-specific. @internal gui name="Low leakage stop 2 mode" */
    kPowerManagerLls3,             /*!< Low leakage stop 3 mode. Chip-specific. @internal gui name="Low leakage stop 3 mode" */
#endif
#if FSL_FEATURE_SMC_HAS_STOP_SUBMODE0
    kPowerManagerVlls0,            /*!< Very low leakage stop 0 mode. All Kinetis chips. @internal gui name="Very low leakage stop 0 mode" */
#endif    
    kPowerManagerVlls1,            /*!< Very low leakage stop 1 mode. All Kinetis chips. @internal gui name="Very low leakage stop 1 mode" */
#if FSL_FEATURE_SMC_HAS_STOP_SUBMODE2
    kPowerManagerVlls2,            /*!< Very low leakage stop 2 mode. All Kinetis chips. @internal gui name="Very low leakage stop 2 mode" */
#endif
    kPowerManagerVlls3,            /*!< Very low leakage stop 3 mode. All Kinetis chips. @internal gui name="Very low leakage stop 3 mode" */
    kPowerManagerMax
} power_manager_modes_t;

/*!
 * @brief Power manager success code and error codes.
 *
 * Used as return value of Power manager functions.
 */
typedef enum _power_manager_error_code {
    kPowerManagerSuccess,                  /*!< Success */
    kPowerManagerError,                    /*!< Some error occurs. */
    kPowerManagerErrorOutOfRange,          /*!< Configuration index out of range. */
    kPowerManagerErrorSwitch,              /*!< Error occurs during mode switch. */
    kPowerManagerErrorNotificationBefore,  /*!< Error occurs during send "BEFORE" notification. */
    kPowerManagerErrorNotificationAfter,   /*!< Error occurs during send "AFTER" notification.  */
    kPowerManagerErrorClock                /*!< Error occurs due to wrong clock setup for power modes */
} power_manager_error_code_t;

/*!
 * @brief Power manager policies.
 *
 * Define whether the power mode change is forced or not. Used to specify whether
 * the mode switch initiated by the POWER_SYS_SetMode() depends on the callback
 * notification results. For kPowerManagerPolicyForcible the power mode is changed
 * regardless of the results, while kPowerManagerPolicyAgreement policy is used
 * the POWER_SYS_SetMode() is exited when any of the callbacks returns error code.
 * See also POWER_SYS_SetMode() description.
 */
typedef enum _power_manager_policy {
    kPowerManagerPolicyAgreement,      /*!< POWER_SYS_SetMode() method is exited when any of the callbacks returns error code. @internal gui name="Agreement policy" */ 
    kPowerManagerPolicyForcible        /*!< Power mode is changed regardless of the results. @internal gui name="Forcible policy" */
} power_manager_policy_t;

/*! @brief The PM notification type. Used to notify registered callbacks */
typedef enum _power_manager_notify
{
    kPowerManagerNotifyRecover = 0x00U,  /*!< Notify IP to recover to previous work state.      */
    kPowerManagerNotifyBefore  = 0x01U,  /*!< Notify IP that system will change power setting.  */
    kPowerManagerNotifyAfter   = 0x02U,  /*!< Notify IP that have changed to new power setting. */
} power_manager_notify_t;

/*!
 * @brief The callback type, indicates what kinds of notification this callback handles.
 *
 * Used in the callback configuration structures (power_manager_callback_user_config_t) 
 * to specify when the registered callback will be called during power mode change initiated by 
 * POWER_SYS_SetMode().
 * Callback can be invoked in following situations:
 *  - before the power mode change (Callback return value can affect POWER_SYS_SetMode()
 *    execution. Refer to the  POWER_SYS_SetMode() and power_manager_policy_t documentation).
 *  - after entering one of the run modes or after exiting from one of the (deep) sleep power
 *    modes back to the run mode.
 *  - after unsuccessful attempt to switch power mode
 */
typedef enum _power_manager_callback_type {
    kPowerManagerCallbackBefore      = 0x01U, /*!< Before callback. @internal gui name="Before" */
    kPowerManagerCallbackAfter       = 0x02U, /*!< After callback. @internal gui name="After" */
    kPowerManagerCallbackBeforeAfter = 0x03U, /*!< Before-After callback. @internal gui name="Before-After" */
} power_manager_callback_type_t;

/*!
 * @brief Callback-specific data.
 *
 * Reference to data of this type is passed during callback registration. The reference is
 * part of the power_manager_callback_user_config_t structure and is passed to the callback during 
 * power mode change notifications.
 */
typedef void power_manager_callback_data_t;

/*!
 * @brief Power mode user configuration structure.
 *
 * This structure defines Kinetis power mode with additional power options and specifies
 * transition to and out of this mode. Application may define multiple power modes and
 * switch between them. List of defined power modes is passed to the Power manager during
 * initialization as an array of references to structures of this type (see POWER_SYS_Init()).
 * Power modes can be switched by calling POWER_SYS_SetMode() which accepts index to the list
 * of power modes passed during manager initialization. Currently used power mode can be
 * retrieved by calling POWER_SYS_GetLastMode(), which returns index of the current power mode, or
 * by POWER_SYS_GetLastModeConfig(), which returns reference to the structure of current mode.
 * List of power mode configuration structure members depends on power options available
 * for specific chip. Complete list contains:
 *  mode - Kinetis power mode. List of available modes is chip-specific. See power_manager_modes_t
 *   list of modes. This item is common for all Kinetis chips.
 *  sleepOnExitOption - Controls whether the sleep-on-exit option value is used (when set to true)
 *   or ignored (when set to false). See sleepOnExitValue. This item is common for all Kinetis chips.
 *  sleepOnExitValue - When set to true, ARM core returns to sleep (Kinetis wait modes) or deep sleep
 *   state (Kinetis stop modes) after interrupt service finishes. When set to false, core stays
 *   woken-up. This item is common for all Kinetis chips.
 *  lowPowerWakeUpOnInterruptOption - Controls whether the wake-up-on-interrupt option value is used
 *   (when set to true) or ignored (when set to false). See lowPowerWakeUpOnInterruptValue. This
 *   item is chip-specific.
 *  lowPowerWakeUpOnInterruptValue - When set to true, system exits to Run mode when any interrupt occurs while in
 *   Very low power run, Very low power wait or Very low power stop mode. This item is chip-specific.
 *  powerOnResetDetectionOption - Controls whether the power on reset detection option value is used
 *   (when set to true) or ignored (when set to false). See powerOnResetDetectionValue. This item is
 *   chip-specific.
 *  powerOnResetDetectionValue - When set to true, power on reset detection circuit is enabled in
 *   Very low leakage stop 0 mode. When set to false, circuit is disabled. This item is chip-specific.
 *  RAM2PartitionOption - Controls whether the RAM2 partition power option value is used (when set to
 *   true) or ignored (when set to false). See RAM2PartitionValue. This item is chip-specific.
 *  RAM2PartitionValue - When set to true, RAM2 partition content is retained through Very low
 *   leakage stop 2 mode. When set to false, RAM2 partition power is disabled and memory content lost.
 *   This item is chip-specific.
 *  lowPowerOscillatorOption - Controls whether the Low power oscillator power option value is used
 *   (when set to true) or ignored (when set to false). See lowPowerOscillatorValue. This item is
 *   chip-specific.
 *  lowPowerOscillatorValue - When set to true, the 1 kHz Low power oscillator is enabled in any
 *   Low leakage or Very low leakage stop mode. When set to false, oscillator is disabled in these modes.
 *   This item is chip-specific.
 * @internal gui name="Power manager configuration" id="power_managerCfg"
 */
typedef struct _power_manager_mode_user_config {
    power_manager_modes_t mode;  /*!< Power mode. @internal gui name="Power mode" id="mode" */
    bool sleepOnExitValue;       /*!< Sleep or deep sleep after interrupt service finished. @internal gui name="Sleep or deep sleep after interrupt service finished" id="sleepOnExitValue" */
#if FSL_FEATURE_SMC_HAS_LPWUI
    smc_lpwui_option_t lowPowerWakeUpOnInterruptValue;  /*!< Wake-up on interrupt from Very low power run, Very low power wait or Very low power stop mode. @internal gui name="Wake-up on interrupt from Very low power run, Very low power wait or Very low power stop mode" id="lowPowerWakeUpOnInterruptValue" */
#endif
#if FSL_FEATURE_SMC_HAS_PORPO
    smc_por_option_t powerOnResetDetectionValue;  /*!< Power on reset detection circuit is enabled in Very low leakage stop 0 mode. @internal gui name="Power on reset detection circuit is enabled in Very low leakage stop 0 mode" id="powerOnResetDetectionValue" */
#endif
#if FSL_FEATURE_SMC_HAS_RAM2_POWER_OPTION
    smc_ram2_option_t RAM2PartitionValue;  /*!< RAM2 partition content is retained through Very low leakage stop 2 mode. @internal gui name="RAM2 partition content is retained through Very low leakage stop 2 mode" id="RAM2PartitionValue" */
#endif
#if FSL_FEATURE_SMC_HAS_PSTOPO
    smc_pstop_option_t  partialStopOptionValue;  /*!< Defines Normal stop mode, or Partial Stop with both system and bus clocks disabled, or Partial Stop with system clock disabled and bus clock enabled. @internal gui name="Defines Normal stop mode, or Partial Stop with both system and bus clocks disabled, or Partial Stop with system clock disabled and bus clock enabled" id="partialStopOptionValue" */
#endif
#if FSL_FEATURE_SMC_HAS_LPOPO
    smc_lpo_option_t lowPowerOscillatorValue;  /*!< The 1 kHz Low power oscillator is enabled in any Low leakage or Very low leakage stop mode. @internal gui name="The 1 kHz Low power oscillator is enabled in any Low leakage or Very low leakage stop mode" id="lowPowerOscillatorValue" */
#endif
} power_manager_user_config_t;

/*! @brief Power notification structure passed to registered callback function. */
typedef struct _power_notify_struct
{
    uint8_t targetPowerConfigIndex;    /*!< Target power configuration index. */
    power_manager_user_config_t const *targetPowerConfigPtr; /*!< Pointer to target power configuration */
    power_manager_policy_t policy;     /*!< Clock transition policy.          */
    power_manager_notify_t notifyType; /*!< Clock notification type.          */
} power_manager_notify_struct_t;

/*!
 * @brief Callback prototype.
 *
 * Declaration of callback. It is common for registered callbacks.
 * Reference to function of this type is part of power_manager_callback_user_config_t callback 
 * configuration structure.
 * Depending on callback type, function of this prototype is called during power mode change
 * (see POWER_SYS_SetMode()) before the mode change, after it or in both cases to notify about
 * the change progress (see power_manager_callback_type_t). When called, type of the notification
 * is passed as parameter along with reference to entered power mode configuration structure
 * (see power_manager_notify_struct_t) and any data passed during the callback registration (see
 * power_manager_callback_data_t).
 * When notified before the mode change, depending on the power mode change policy (see
 * power_manager_policy_t) the callback may deny the mode change by returning any error code different
 * from kPowerManagerSuccess (see POWER_SYS_SetMode()).
 * @param notify Notification structure. 
 * @param dataPtr Callback data. Refers to the data passed during callback registration. Intended to
 *  pass any driver or application data such as internal state information.
 * @return An error code or kPowerManagerSuccess.
 */
typedef power_manager_error_code_t (* power_manager_callback_t)(
    power_manager_notify_struct_t * notify,
    power_manager_callback_data_t * dataPtr
);

/*!
 * @brief callback configuration structure
 *
 * This structure holds configuration of callbacks passed
 * to the Power manager during its initialization.
 * Callbacks of this type are expected to be statically
 * allocated.
 * This structure contains following application-defined data:
 *  callback - pointer to the callback function
 *  callbackType - specifies when the callback is called
 *  callbackData - pointer to the data passed to the callback
 */
typedef struct _power_manager_callback_user_config {
    power_manager_callback_t callback;
    power_manager_callback_type_t callbackType;
    power_manager_callback_data_t * callbackData;
} power_manager_callback_user_config_t;

/*!
 * @brief Power manager internal state structure.
 *
 * Power manager internal structure. Contains data necessary for Power manager proper
 * function. Stores references to registered power mode configurations,
 * callbacks, information about their numbers and other internal data.
 * This structure is statically allocated and initialized after POWER_SYS_Init() call.
 */
typedef struct _power_manager_state {
    power_manager_user_config_t const ** configs;   /*!< Pointer to power configure table.*/
    uint8_t configsNumber;                         /*!< Number of power configurations */
    power_manager_callback_user_config_t ** staticCallbacks; /*!< Pointer to callback table. */
    uint8_t staticCallbacksNumber;                 /*!< Max. number of callback configurations */
    uint8_t errorCallbackIndex;                    /*!< Index of callback returns error. */
    uint8_t currentConfig;                         /*!< Index of current configuration.  */  
} power_manager_state_t;

/*******************************************************************************
 * API
 ******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @brief Power manager initialization for operation.
 *
 * This function initializes the Power manager and its run-time state structure.
 * Reference to an array of Power mode configuration structures has to be passed
 * as a parameter along with a parameter specifying its size. At least one power mode
 * configuration is required. Optionally, reference to the array of predefined
 * callbacks can be passed with its size parameter.
 * For details about callbacks, refer to the power_manager_callback_user_config_t.
 * As Power manager stores only references to array of these structures, they have
 * to exist while Power manager is used.
 * It is expected that prior to the POWER_SYS_Init() call the write-once protection
 * register was configured appropriately allowing for entry to all required low power
 * modes.
 * The following is an example of how to set up two power modes and three
 * callbacks, and initialize the Power manager with structures containing their settings.
 * The example shows two possible ways the configuration structures can be stored
 * (ROM or RAM), although it is expected that they will be placed in the read-only
 * memory to save the RAM space. (Note: In the example it is assumed that the programmed chip
 * doesn't support any optional power options described in the power_manager_user_config_t)
 * :
 * @code
    const power_manager_user_config_t waitConfig = {
        kPowerManagerVlpw,
        true,
        true,
    };

    const power_manager_callback_user_config_t callbackCfg0 = {
        callback0,
        kPowerManagerCallbackBefore,
        &callback_data0
    };

    const power_manager_callback_user_config_t callbackCfg1 = {
        callback1,
        kPowerManagerCallbackAfter,
        &callback_data1
    };

    const power_manager_callback_user_config_t callbackCfg2 = {
        callback2,
        kPowerManagerCallbackBeforeAfter,
        &callback_data2
    };

    const power_manager_callback_user_config_t * const callbacks[] = {&callbackCfg0, &callbackCfg1, &callbackCfg2};

    void main(void)
    {
        power_manager_user_config_t idleConfig;
        power_manager_user_config_t *powerConfigs[] = {&idleConfig, &waitConfig};

        idleConfig.mode = kPowerManagerVlps;
        idleConfig.sleepOnExitOption = true;
        idleConfig.sleepOnExitValue = false;

        POWER_SYS_Init(&powerConfigs, 2U, &callbacks, 3U);

        POWER_SYS_SetMode(0U, kPowerManagerPolicyAgreement);

    }
 * @endcode
 *
 * @param powerConfigsPtr A pointer to an array with references to all power
 *  configurations which will be handled by Power manager.
 * @param configsNumber Number of power configurations. Size of powerConfigsPtr
 *  array.
 * @param callbacksPtr A pointer to an array with references to callback configurations.
 *  If there are no callbacks to register during Power manager initialization, use NULL value.
 * @param callbacksNumber Number of registered callbacks. Size of callbacksPtr
 *  array.
 * @return An error code or kPowerManagerSuccess.
 */
power_manager_error_code_t POWER_SYS_Init(power_manager_user_config_t const ** powerConfigsPtr,
                                          uint8_t configsNumber,
                                          power_manager_callback_user_config_t ** callbacksPtr,
                                          uint8_t callbacksNumber);

/*!
 * @brief This function deinitializes the Power manager.
 *
 * @return An error code or kPowerManagerSuccess.
 */
power_manager_error_code_t POWER_SYS_Deinit(void);

/*!
 * @brief This function configures the power mode.
 *
 * This function switches to one of the defined power modes. Requested mode number is passed
 * as an input parameter. This function notifies all registered callback functions before
 * the mode change (using  kPowerManagerCallbackBefore set as callback type parameter),
 * sets specific power options defined in the power mode configuration and enters the specified
 * mode. In case of run modes (for example, Run, Very low power run, or High speed run), this function
 * also invokes all registered callbacks after the mode change (using kPowerManagerCallbackAfter).
 * In case of sleep or deep sleep modes, if the requested mode is not exited through
 * a reset, these notifications are sent after the core wakes up.
 * Callbacks are invoked in the following order: All registered callbacks are notified
 * ordered by index in the callbacks array (see callbacksPtr parameter of POWER_SYS_Init()).
 * The same order is used for before and after switch notifications.
 * The notifications before the power mode switch can be used to obtain confirmation about
 * the change from registered callbacks. If any registered callback denies the power
 * mode change, further execution of this function depends on mode change policy: the mode
 * change is either forced (kPowerManagerPolicyForcible) or exited (kPowerManagerPolicyAgreement).
 * When mode change is forced, the result of the before switch notifications are ignored. If
 * agreement is required, if any callback returns an error code then further notifications
 * before switch notifications are cancelled and all already notified callbacks are re-invoked
 * with kPowerManagerCallbackAfter set as callback type parameter. The index of the callback
 * which returned error code during pre-switch notifications is stored (any error codes during
 * callbacks re-invocation are ignored) and POWER_SYS_GetErrorCallback() can be used to get it.
 * Regardless of the policies, if any callback returned an error code, an error code denoting in which phase
 * the error occurred is returned when POWER_SYS_SetMode() exits.
 * It is possible to enter any mode supported by the processor. Refer to the chip reference manual
 * for list of available power modes. If it is necessary to switch into intermediate power mode prior to
 * entering requested mode (for example, when switching from Run into Very low power wait through Very low
 * power run mode), then the intermediate mode is entered without invoking the callback mechanism.
 *
 * @param powerModeIndex Requested power mode represented as an index into
 *  array of user-defined power mode configurations passed to the POWER_SYS_Init().
 * @param policy Transaction policy
 * @return An error code or kPowerManagerSuccess.
 */
power_manager_error_code_t POWER_SYS_SetMode(uint8_t powerModeIndex, power_manager_policy_t policy);

/*!
 * @brief This function returns power mode set as the last one.
 *
 * This function returns index of power mode which was set using POWER_SYS_SetMode() as the last one.
 * If the power mode was entered even though some of the registered callback denied the mode change,
 * or if any of the callbacks invoked after the entering/restoring run mode failed, then the return
 * code of this function has kPowerManagerError value.
 *
 * @param powerModeIndexPtr Power mode which has been set represented as an index into array of power mode
 * configurations passed to the POWER_SYS_Init().
 * @return An error code or kPowerManagerSuccess.
 */
power_manager_error_code_t POWER_SYS_GetLastMode(uint8_t *powerModeIndexPtr);

/*!
 * @brief This function returns user configuration structure of power mode set as the last one.
 *
 * This function returns reference to configuration structure which was set using POWER_SYS_SetMode()
 * as the last one. If the current power mode was entered even though some of the registered callback denied
 * the mode change, or if any of the callbacks invoked after the entering/restoring run mode failed, then
 * the return code of this function has kPowerManagerError value.
 *
 * @param powerModePtr Pointer to power mode configuration structure of power mode set as last one.
 * @return An error code or kPowerManagerSuccess.
 */
power_manager_error_code_t POWER_SYS_GetLastModeConfig(power_manager_user_config_t const ** powerModePtr);

/*!
 * @brief This function returns currently running power mode.
 *
 * This function reads hardware settings and returns currently running power mode. Generally,
 * this function can return only kPowerManagerRun, kPowerManagerVlpr or kPowerManagerHsrun value.
 *
  * @return Currently used run power mode.
 */
power_manager_modes_t POWER_SYS_GetCurrentMode(void);

/*!
 * @brief This function returns the last failed notification callback.
 *
 * This function returns index of the last callback that failed during the power mode switch while
 * the last POWER_SYS_SetMode() was called. If the last POWER_SYS_SetMode() call ended successfully 
 * value equal to callbacks number is returned. Returned value represents index in the array of 
 * static call-backs.
 *
 * @return Callback index of last failed callback or value equal to callbacks count.
 */
uint8_t POWER_SYS_GetErrorCallbackIndex(void);

/*!
 * @brief This function returns the last failed notification callback configuration structure.
 *
 * This function returns pointer to configuration structure of the last callback that failed during 
 * the power mode switch while the last POWER_SYS_SetMode() was called. 
 * If the last POWER_SYS_SetMode() call ended successfully value NULL is returned. 
 *
 * @return Pointer to the callback configuration which returns error.
 */
power_manager_callback_user_config_t* POWER_SYS_GetErrorCallback(void);

/*!
 * @brief This function returns whether very low power mode is running.
 *
 * This function is used to detect whether very low power mode is running.
 *
 * @return Returns true if processor runs in very low power mode, otherwise false.
 */
bool POWER_SYS_GetVeryLowPowerModeStatus(void);

/*!
 * @brief This function returns whether reset was caused by low leakage wake up.
 *
 * This function is used to check that processor exited low leakage power mode
 * through reset.
 *
 * @return Returns true if processor was reset by low leakage wake up,
 *  otherwise false.
 */
bool POWER_SYS_GetLowLeakageWakeupResetStatus(void);

/*!
 * @brief Gets the acknowledge isolation flag.
 *
 * This function is used to check certain peripherals and the I/O pads are in a latched state 
 * as a result of having been in a VLLS mode.
 *
 * After recovery from VLLS, the LLWU continues to detect wake-up events until the user has 
 * acknowledged the wake-up via POWER_SYS_ClearAckIsolation()
 * 
 * @return Returns true if ACK isolation is set.
 */
bool POWER_SYS_GetAckIsolation(void);

/*!
 * @brief Clears the acknowledge isolation flag.
 *
 * This function  clears the ACK Isolation flag. Clearing releases the I/O pads and certain 
 * peripherals to their normal run mode state.
 *
 * After recovery from VLLS, the LLWU continues to detect wake-up events until the user has 
 * acknowledged the wake-up via POWER_SYS_ClearAckIsolation()
 * 
 */
void POWER_SYS_ClearAckIsolation(void);

#include "../src/power/fsl_power_manager_common.h"

#if FSL_FEATURE_SOC_LLWU_COUNT
/*!
 * The LLWU module becomes functional on entry into a low-leakage power mode. After
 * recovery from LLS, the LLWU is immediately disabled. After recovery from VLLS, the
 * LLWU continues to detect wake-up events until the user has acknowledged the wake-up
 * via POWER_SYS_ClearAckIsolation().
 * 
 * LLS modes:
 * The wake-up events due to external wake-up inputs and internal module wake-up inputs
 * result in an interrupt flow when exiting LLS. A reset event due to RESET pin assertion
 * results in a reset flow when exiting LLS.
 * 
 * VLLS modes:
 * All wakeup and reset events result in VLLS exit via a reset flow.
 * 
 * The LLWU is not active in all non-low leakage modes where detection and control logic
 * are in a static state. The LLWU registers are accessible in non-low leakage modes and are
 * available for configuring and reading status when bus transactions are possible.
 */

#if FSL_FEATURE_LLWU_HAS_INTERNAL_MODULE
/*!
 * @brief This function allows to set wake up module in low leakage wake up unit (LLWU).
 * Each of the available wake-up sources can be individually enabled or disabled.
 * 
 * The LLWU is not active in all non-low leakage modes where detection and control logic
 * are in a static state. The LLWU registers are accessible in non-low leakage modes and are
 * available for configuring and reading status when bus transactions are possible.
 *
 * After recovery from VLLS, the LLWU continues to detect wake-up events until the user has 
 * acknowledged the wake-up via POWER_SYS_ClearAckIsolation()
 *
 * @param module Wake up module name which will be set.
 * @param enable Specifies if wake up from module will be enabled (true) or disabled (false).
 */
void POWER_SYS_SetWakeupModule(power_wakeup_module_t module,bool enable);

/*!
 * @brief This function allows to get wake up module flag in LLWU.
 * 
 * For internal peripherals that are capable of running in a low-leakage power mode, such as
 * a real time clock module or CMP module, the flag from the associated peripheral is
 * accessible and returned by this function.
 * 
 * The flag will need to be cleared in the peripheral instead of clearing in LLWU.
 *
 * @param module Wake up module name.
 * @return Returns true if module flag is set.
 */
bool POWER_SYS_GetWakeupModuleFlag(power_wakeup_module_t module);

#endif
#if FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN
/*!
 * @brief This function allows to set wake up pin in low leakage wake up unit (LLWU) and allows to configure
 * pin electrical parameters if gpio pin configuration is passed as parameter.
 * Each of the available wake-up sources can be individually enabled or disabled.
 *
 * The LLWU is not active in all non-low leakage modes where detection and control logic
 * are in a static state. The LLWU registers are accessible in non-low leakage modes and are
 * available for configuring and reading status when bus transactions are possible.
 *
 * After recovery from VLLS, the LLWU continues to detect wake-up events until the user has 
 * acknowledged the wake-up via POWER_SYS_ClearAckIsolation()
 * 
 * @param pin Wake up pin name which will be set.
 * @param pinMode pin configuration mode defined in llwu_external_pin_modes_t.
 * @param config optional parameter. If passed gpio pin configuration gpio pin will be configured.
 */
void POWER_SYS_SetWakeupPin(power_wakeup_pin_t pin, llwu_external_pin_modes_t pinMode, const gpio_input_pin_t * config);

/*!
 * @brief This function allows to get wake up pin flag in low leakage wake up unit (LLWU).
 * 
 * @param pin Wake up pin name.
 * @return Returns pin wake up flag.
 */
bool POWER_SYS_GetWakeupPinFlag(power_wakeup_pin_t pin);

/*!
 * @brief This function allows to clear wake up pin flag in low leakage wake up unit (LLWU).
 * 
 * @param pin Wake up pin name
 */
void POWER_SYS_ClearWakeupPinFlag(power_wakeup_pin_t pin);

#endif
#endif

#if defined(__cplusplus)
}
#endif

/*! @}*/

#endif /* __FSL_POWER_MANAGER_H__ */
/*******************************************************************************
 * EOF
 ******************************************************************************/