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 #if CFG_LPCLK_INTERNAL_EN
59 #error "INTERNAL LPCLK is not supported by OHOS"
60 #endif
61
62 /*
63 * GLOBAL FUNCTION DEFINITIONS
64 *****************************************************************************************
65 */
system_pmu_calibration_init(uint32_t interval)66 void system_pmu_calibration_init(uint32_t interval)
67 {
68 if (interval) {
69 uint32_t interval_new;
70 #if CFG_LPCLK_INTERNAL_EN
71 pmu_interval_init = interval;
72 interval_new = pmu_interval_get(1);
73 #else
74 interval_new = interval;
75 #endif
76
77 #ifdef ENV_USE_FREERTOS
78 timer_handle = xTimerCreate(NULL, interval_new, pdTRUE, NULL,
79 #if CFG_LPCLK_INTERNAL_EN
80 pmu_timer_handler
81 #else
82 pmu_calibration_handler
83 #endif // CFG_LPCLK_INTERNAL_EN
84 );
85
86 xTaskCreate(system_pmu_calibration_task, "pmu_calibration_task",
87 STACKDEPTH, NULL, configMAX_PRIORITIES - 1, NULL);
88 #else
89 app_timer_delete(&s_pmu_calibration_timer_id);
90 app_timer_create(&s_pmu_calibration_timer_id, ATIMER_REPEAT,
91 #if CFG_LPCLK_INTERNAL_EN
92 pmu_timer_handler
93 #else
94 pmu_calibration_handler
95 #endif // CFG_LPCLK_INTERNAL_EN
96 );
97 app_timer_start(s_pmu_calibration_timer_id, interval_new, NULL);
98 #endif // ENV_USE_FREERTOS
99
100 #if CFG_LPCLK_INTERNAL_EN
101 pmu_interval_prev = interval_new;
102 #endif
103 }
104 return;
105 }
106
107 #ifdef ENV_USE_FREERTOS
system_pmu_calibration_start(void)108 void system_pmu_calibration_start(void)
109 {
110 if (timer_handle != NULL) {
111 if (xTimerIsTimerActive(timer_handle) == pdFALSE) {
112 xTimerStart(timer_handle, 0);
113 }
114 }
115
116 return;
117 }
118
system_pmu_calibration_task(void * p_arg)119 static void system_pmu_calibration_task(void *p_arg)
120 {
121 system_pmu_calibration_start();
122 vTaskDelete(NULL);
123 }
124 #endif
125
system_pmu_calibration_stop(void)126 void system_pmu_calibration_stop(void)
127 {
128 #ifdef ENV_USE_FREERTOS
129 if (timer_handle != NULL) {
130 xTimerDelete(timer_handle, 0);
131 timer_handle = NULL;
132 }
133 #else
134 app_timer_delete(&s_pmu_calibration_timer_id);
135 #endif
136 return;
137 }
138
139