• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  *****************************************************************************************
3  *
4  * @file pmu_calibration.c
5  *
6  * @brief auto calibration function Implementation.
7  *
8  *****************************************************************************************
9  * @attention
10   #####Copyright (c) 2019 GOODIX
11   All rights reserved.
12 
13     Redistribution and use in source and binary forms, with or without
14     modification, are permitted provided that the following conditions are met:
15   * Redistributions of source code must retain the above copyright
16     notice, this list of conditions and the following disclaimer.
17   * Redistributions in binary form must reproduce the above copyright
18     notice, this list of conditions and the following disclaimer in the
19     documentation and/or other materials provided with the distribution.
20   * Neither the name of GOODIX nor the names of its contributors may be used
21     to endorse or promote products derived from this software without
22     specific prior written permission.
23 
24   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27   ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34   POSSIBILITY OF SUCH DAMAGE.
35  *****************************************************************************************
36  */
37 
38 /*
39  * INCLUDE FILES
40  *****************************************************************************************
41  */
42 #include "pmu_calibration.h"
43 #include "platform_sdk.h"
44 
45 #define INTERVAL_MIN 2000
46 #define STACKDEPTH   512
47 /*
48  * LOCAL VARIABLE DEFINITIONS
49  *****************************************************************************************
50  */
51 #ifdef ENV_USE_FREERTOS
52 static TimerHandle_t timer_handle = NULL;
53 static void system_pmu_calibration_task(void *p_arg);
54 #else
55 static app_timer_id_t s_pmu_calibration_timer_id = 0;
56 #endif
57 
58 uint32_t g_debug_temperature;
59 
60 #if CFG_LPCLK_INTERNAL_EN
61 
62 #define PMU_SMALL_INTERVAL_MS       (10*1000)
63 #define TEMPERATURN_HIGH    44
64 #define TEMPERATURN_LOW     40
65 static uint32_t pmu_interval_init = 30 * 1000;
66 static uint32_t pmu_interval_prev = 0;
67 
pmu_interval_get(uint32_t is_init)68 uint32_t pmu_interval_get(uint32_t is_init)
69 {
70     uint32_t interval = 0;
71 
72     if (g_debug_temperature > TEMPERATURN_HIGH) {
73         interval = PMU_SMALL_INTERVAL_MS;
74     } else if (g_debug_temperature >= TEMPERATURN_LOW && g_debug_temperature <= TEMPERATURN_HIGH && is_init) {
75         interval = PMU_SMALL_INTERVAL_MS;
76     } else if (g_debug_temperature < TEMPERATURN_LOW) {
77         interval = pmu_interval_init;
78     }
79 
80     return interval;
81 }
82 
pmu_timer_handler(void * p_arg)83 void pmu_timer_handler(void* p_arg)
84 {
85     pmu_calibration_handler(p_arg);
86 
87     uint32_t interval_new;
88     uint32_t interval_diff;
89 
90     interval_new = pmu_interval_get(0);
91     if (interval_new == 0) {
92         return;
93     }
94 
95     interval_diff = interval_new > pmu_interval_prev ?
96                     interval_new - pmu_interval_prev: pmu_interval_prev - interval_new;
97 
98     if (interval_diff > INTERVAL_MIN) {
99 #ifdef ENV_USE_FREERTOS
100         portBASE_TYPE xHigherPriorityTaskWoken;
101         xTimerChangePeriodFromISR(timer_handle, interval_new, &xHigherPriorityTaskWoken);
102         portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
103 #else
104         app_timer_delete(&s_pmu_calibration_timer_id);
105         app_timer_create(&s_pmu_calibration_timer_id, ATIMER_REPEAT, pmu_timer_handler);
106         app_timer_start(s_pmu_calibration_timer_id, interval_new, NULL);
107 #endif
108         pmu_interval_prev = interval_new;
109     }
110 }
111 #endif
112 
113 /*
114  * GLOBAL FUNCTION DEFINITIONS
115  *****************************************************************************************
116  */
system_pmu_calibration_init(uint32_t interval)117 void system_pmu_calibration_init(uint32_t interval)
118 {
119     if (interval) {
120         uint32_t interval_new;
121 #if CFG_LPCLK_INTERNAL_EN
122         pmu_interval_init = interval;
123         interval_new = pmu_interval_get(1);
124 #else
125         interval_new = interval;
126 #endif
127 
128 #ifdef ENV_USE_FREERTOS
129         timer_handle = xTimerCreate(NULL, interval_new, pdTRUE, NULL,
130 #if CFG_LPCLK_INTERNAL_EN
131                                     pmu_timer_handler
132 #else
133                                     pmu_calibration_handler
134 #endif // CFG_LPCLK_INTERNAL_EN
135                                    );
136 
137         xTaskCreate(system_pmu_calibration_task, "pmu_calibration_task",
138                     STACKDEPTH, NULL, configMAX_PRIORITIES - 1, NULL);
139 #else
140         app_timer_delete(&s_pmu_calibration_timer_id);
141         app_timer_create(&s_pmu_calibration_timer_id, ATIMER_REPEAT,
142 #if CFG_LPCLK_INTERNAL_EN
143                          pmu_timer_handler
144 #else
145                          pmu_calibration_handler
146 #endif  // CFG_LPCLK_INTERNAL_EN
147                         );
148         app_timer_start(s_pmu_calibration_timer_id, interval_new, NULL);
149 #endif  // ENV_USE_FREERTOS
150 
151 #if CFG_LPCLK_INTERNAL_EN
152         pmu_interval_prev = interval_new;
153 #endif
154     }
155     return;
156 }
157 
158 #ifdef ENV_USE_FREERTOS
system_pmu_calibration_start(void)159 void system_pmu_calibration_start(void)
160 {
161     if (timer_handle != NULL) {
162         if (xTimerIsTimerActive(timer_handle) == pdFALSE) {
163             xTimerStart(timer_handle, 0);
164         }
165     }
166 
167     return;
168 }
169 
system_pmu_calibration_task(void * p_arg)170 static void system_pmu_calibration_task(void *p_arg)
171 {
172     system_pmu_calibration_start();
173     vTaskDelete(NULL);
174 }
175 #endif
176 
system_pmu_calibration_stop(void)177 void system_pmu_calibration_stop(void)
178 {
179 #ifdef ENV_USE_FREERTOS
180     if (timer_handle != NULL) {
181         xTimerDelete(timer_handle, 0);
182         timer_handle = NULL;
183     }
184 #else
185     app_timer_delete(&s_pmu_calibration_timer_id);
186 #endif
187     return;
188 }
189 
190