1 /**
2 ****************************************************************************************
3 * @file app_pwr_mgmt.c
4 * @author BLE Driver Team
5 * @brief HAL APP module driver.
6 ****************************************************************************************
7 * @attention
8 #####Copyright (c) 2019 GOODIX
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 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
18 * Neither the name of GOODIX nor the names of its contributors may be used
19 to endorse or promote products derived from this software without
20 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 COPYRIGHT HOLDERS AND 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 * INCLUDE FILES
37 *****************************************************************************************
38 */
39 #include "gr55xx_hal.h"
40 #include "gr55xx_pwr.h"
41 #include "app_pwr_mgmt.h"
42
43 /*
44 * STRUCT DEFINES
45 *****************************************************************************************
46 */
47 struct pwr_env_t {
48 app_sleep_callbacks_t *pwr_sleep_cb[APP_SLEEP_CB_MAX];
49 wakeup_priority_t wakeup_priority[APP_SLEEP_CB_MAX];
50 bool is_pwr_callback_reg;
51 };
52
53 /*
54 * LOCAL VARIABLE DEFINITIONS
55 *****************************************************************************************
56 */
57 struct pwr_env_t s_pwr_env;
58
59 /*
60 * GLOBAL FUNCTION DEFINITIONS
61 ****************************************************************************************
62 */
pwr_register_sleep_cb(const app_sleep_callbacks_t * p_cb,wakeup_priority_t wakeup_priority)63 int16_t pwr_register_sleep_cb(const app_sleep_callbacks_t *p_cb, wakeup_priority_t wakeup_priority)
64 {
65 int16_t id = -1;
66 uint8_t i = 0;
67
68 if (p_cb == NULL || wakeup_priority > WAPEUP_PRIORITY_HIGH || wakeup_priority < WAPEUP_PRIORITY_LOW) {
69 return id;
70 }
71
72 GLOBAL_EXCEPTION_DISABLE();
73 if (!s_pwr_env.is_pwr_callback_reg) {
74 pwr_mgmt_dev_init(pwr_wake_up_ind);
75 pwr_mgmt_set_callback(pwr_enter_sleep_check, NULL);
76 s_pwr_env.is_pwr_callback_reg = true;
77 }
78
79 while ((i < APP_SLEEP_CB_MAX) && (s_pwr_env.pwr_sleep_cb[i] != NULL)) {
80 i++;
81 }
82
83 if (i < APP_SLEEP_CB_MAX) {
84 s_pwr_env.pwr_sleep_cb[i] = (app_sleep_callbacks_t *)p_cb;
85 s_pwr_env.wakeup_priority[i] = wakeup_priority;
86 id = i;
87 }
88
89 GLOBAL_EXCEPTION_ENABLE();
90 return id;
91 }
92
pwr_unregister_sleep_cb(int16_t id)93 void pwr_unregister_sleep_cb(int16_t id)
94 {
95 if (id < APP_SLEEP_CB_MAX) { // Is id valid?
96 s_pwr_env.pwr_sleep_cb[id] = NULL;
97 }
98 }
99
pwr_wake_up_ind(void)100 SECTION_RAM_CODE void pwr_wake_up_ind(void)
101 {
102 uint8_t i;
103 app_sleep_callbacks_t *p_cb;
104 wakeup_priority_t priority;
105
106 for (priority = WAPEUP_PRIORITY_HIGH; priority != 0; priority--) {
107 for (i = 0; i < APP_SLEEP_CB_MAX; i++) {
108 p_cb = s_pwr_env.pwr_sleep_cb[i];
109 if ((p_cb != NULL) && (p_cb ->app_wake_up_ind != NULL) && (priority == s_pwr_env.wakeup_priority[i])) {
110 p_cb ->app_wake_up_ind();
111 }
112 }
113 }
114 }
115
pwr_enter_sleep_check(void)116 pwr_mgmt_dev_state_t pwr_enter_sleep_check(void)
117 {
118 int16_t i;
119 pwr_mgmt_dev_state_t allow_entering_sleep = DEVICE_IDLE;
120 app_sleep_callbacks_t *p_cb;
121
122 // 1. Inquiry Adapters
123 for (i = APP_SLEEP_CB_MAX - 1; i >= 0; i--) {
124 p_cb = s_pwr_env.pwr_sleep_cb[i];
125 if ((p_cb != NULL) && (p_cb->app_prepare_for_sleep != NULL)) {
126 if (!p_cb->app_prepare_for_sleep()) {
127 allow_entering_sleep = DEVICE_BUSY;
128 break;
129 }
130 }
131 }
132
133 // 2. If an Adapter rejected sleep, resume any Adapters that have already accepted it.
134 if (allow_entering_sleep == DEVICE_BUSY) {
135 i++;
136 while (i < APP_SLEEP_CB_MAX) {
137 p_cb = s_pwr_env.pwr_sleep_cb[i];
138 if ((p_cb != NULL) && (p_cb->app_sleep_canceled != NULL)) {
139 p_cb->app_sleep_canceled();
140 }
141 i++;
142 }
143 }
144
145 return allow_entering_sleep;
146 }
147
148