1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * Description: Provides tsensor driver source \n
16 *
17 * History: \n
18 * 2022-09-16, Create file. \n
19 */
20 #include "common_def.h"
21 #include "hal_tsensor.h"
22 #include "tsensor.h"
23 /**
24 * @if Eng
25 * @brief Definition of the Tsensor work mode.
26 * @else
27 * @brief Tsensor工作模式定义。
28 * @endif
29 */
30 typedef enum tsensor_work_mode {
31 TSENSOR_WORK_MODE_INQUIRE, /*!< @if Eng Tsensor mode: inquire mode.
32 @else Tsensor模式:查询模式。 @endif */
33 TSENSOR_WORK_MODE_INTERRUPT, /*!< @if Eng Tsensor mode: interrupt mode.
34 @else Tsensor模式:中断模式。 @endif */
35 TSENSOR_WORK_MODE_MAX_NUM,
36 TSENSOR_WORK_MODE_NONE = TSENSOR_WORK_MODE_MAX_NUM
37 } tsensor_work_mode_t;
38
39 static bool g_tsensor_inited = false;
40 static hal_tsensor_funcs_t *g_hal_funcs = NULL;
41 static tsensor_work_mode_t g_tsensor_work_mode = TSENSOR_WORK_MODE_NONE;
42
43 #define TSENSOR_TEMP_THRESHOLD_L_MAX (HAL_TSENSOR_TEMP_THRESHOLD_L_MAX)
44 #define TSENSOR_TEMP_THRESHOLD_H_MAX (HAL_TSENSOR_TEMP_THRESHOLD_H_MAX)
45
uapi_tsensor_temp_check(int16_t temp)46 static errcode_t uapi_tsensor_temp_check(int16_t temp)
47 {
48 if ((temp < TSENSOR_TEMP_THRESHOLD_L_MAX) ||
49 (temp > TSENSOR_TEMP_THRESHOLD_H_MAX)) {
50 return ERRCODE_TSENSOR_INVALID_PARAMETER;
51 }
52
53 return ERRCODE_SUCC;
54 }
55
56
uapi_tsensor_check(int8_t temp_threshold_low,int8_t temp_threshold_high)57 static errcode_t uapi_tsensor_check(int8_t temp_threshold_low,
58 int8_t temp_threshold_high)
59 {
60 errcode_t ret = ERRCODE_SUCC;
61 ret = uapi_tsensor_temp_check(temp_threshold_low);
62 if (ret != ERRCODE_SUCC) {
63 return ret;
64 }
65 ret = uapi_tsensor_temp_check(temp_threshold_high);
66 if (ret != ERRCODE_SUCC) {
67 return ret;
68 }
69
70 if (temp_threshold_low > temp_threshold_high) {
71 return ERRCODE_TSENSOR_INVALID_PARAMETER;
72 }
73
74 return ERRCODE_SUCC;
75 }
76
uapi_tsensor_init(void)77 errcode_t uapi_tsensor_init(void)
78 {
79 if (unlikely(g_tsensor_inited == true)) {
80 return ERRCODE_FAIL;
81 }
82
83 tsensor_port_register_hal_funcs();
84 g_hal_funcs = hal_tsensor_get_funcs();
85 errcode_t ret = g_hal_funcs->init();
86 if (ret != ERRCODE_SUCC) {
87 return ret;
88 }
89 #if defined(CONFIG_TSENSOR_USING_V150)
90 tsensor_port_power_on_and_fre_div(true);
91 #endif /* CONFIG_TSENSOR_USING_V150 */
92 tsensor_port_register_irq();
93 g_tsensor_inited = true;
94
95 return ret;
96 }
97
uapi_tsensor_deinit(void)98 errcode_t uapi_tsensor_deinit(void)
99 {
100 if (unlikely(g_tsensor_inited == false)) {
101 return ERRCODE_TSENSOR_NOT_INIT;
102 }
103
104 #if defined(CONFIG_TSENSOR_USING_V150)
105 tsensor_port_power_on_and_fre_div(false);
106 #endif /* CONFIG_TSENSOR_USING_V150 */
107 tsensor_port_unregister_irq();
108 g_hal_funcs->deinit();
109 tsensor_port_unregister_hal_funcs();
110 g_tsensor_inited = false;
111
112 return ERRCODE_SUCC;
113 }
114
uapi_tsensor_start_inquire_mode(tsensor_samp_mode_t mode,uint32_t period)115 errcode_t uapi_tsensor_start_inquire_mode(tsensor_samp_mode_t mode, uint32_t period)
116 {
117 if (unlikely(g_tsensor_inited == false)) {
118 return ERRCODE_TSENSOR_NOT_INIT;
119 }
120
121 if (mode >= TSENSOR_SAMP_MODE_NONE) {
122 return ERRCODE_TSENSOR_INVALID_PARAMETER;
123 }
124
125 uint32_t flag = tsensor_port_irq_lock();
126 g_hal_funcs->set_samp_mode((hal_tsensor_samp_mode_t)mode, period);
127 g_tsensor_work_mode = TSENSOR_WORK_MODE_INQUIRE;
128 tsensor_port_irq_unlock(flag);
129
130 return ERRCODE_SUCC;
131 }
132
uapi_tsensor_get_current_temp(int8_t * temp)133 errcode_t uapi_tsensor_get_current_temp(int8_t *temp)
134 {
135 if (unlikely(g_tsensor_inited == false)) {
136 return ERRCODE_TSENSOR_NOT_INIT;
137 }
138
139 if (temp == NULL) {
140 return ERRCODE_TSENSOR_INVALID_PARAMETER;
141 }
142
143 g_hal_funcs->refresh_temp();
144
145 if (!g_hal_funcs->get_temp((volatile int8_t *)temp)) {
146 return ERRCODE_TSENSOR_GET_TEMP_INVALID;
147 }
148
149 return ERRCODE_SUCC;
150 }
151
uapi_tsensor_enable_outtemp_interrupt(hal_tsensor_callback_t callback,int8_t temp_threshold_low,int8_t temp_threshold_high)152 errcode_t uapi_tsensor_enable_outtemp_interrupt(hal_tsensor_callback_t callback,
153 int8_t temp_threshold_low,
154 int8_t temp_threshold_high)
155 {
156 if (unlikely(g_tsensor_inited == false)) {
157 return ERRCODE_TSENSOR_NOT_INIT;
158 }
159
160 errcode_t ret = uapi_tsensor_check(temp_threshold_low, temp_threshold_high);
161 if (ret != ERRCODE_SUCC) {
162 return ret;
163 }
164 uint32_t flag = tsensor_port_irq_lock();
165 g_hal_funcs->set_temp_threshold(TSENSOR_SET_LOW_TEMP, temp_threshold_low);
166 g_hal_funcs->set_temp_threshold(TSENSOR_SET_HIGH_TEMP, temp_threshold_high);
167 g_hal_funcs->set_callback(HAL_TSENSOR_INTERRIPT_TYPE_OUT_THRESH, callback);
168 g_hal_funcs->set_interrupt(HAL_TSENSOR_INTERRIPT_TYPE_OUT_THRESH, true);
169 g_tsensor_work_mode = TSENSOR_WORK_MODE_INTERRUPT;
170 tsensor_port_irq_unlock(flag);
171
172 return ERRCODE_SUCC;
173 }
174
uapi_tsensor_enable_overtemp_interrupt(hal_tsensor_callback_t callback,int8_t overtemp)175 errcode_t uapi_tsensor_enable_overtemp_interrupt(hal_tsensor_callback_t callback, int8_t overtemp)
176 {
177 if (unlikely(g_tsensor_inited == false)) {
178 return ERRCODE_TSENSOR_NOT_INIT;
179 }
180
181 errcode_t ret = uapi_tsensor_temp_check(overtemp);
182 if (ret != ERRCODE_SUCC) {
183 return ret;
184 }
185
186 uint32_t flag = tsensor_port_irq_lock();
187 g_hal_funcs->set_temp_threshold(TSENSOR_SET_OVER_TEMP, overtemp);
188 g_hal_funcs->set_callback(HAL_TSENSOR_INTERRIPT_TYPE_OVERTEMP, callback);
189 g_hal_funcs->set_interrupt(HAL_TSENSOR_INTERRIPT_TYPE_OVERTEMP, true);
190 g_tsensor_work_mode = TSENSOR_WORK_MODE_INTERRUPT;
191 tsensor_port_irq_unlock(flag);
192
193 return ERRCODE_SUCC;
194 }
195
uapi_tsensor_enable_done_interrupt(hal_tsensor_callback_t callback)196 errcode_t uapi_tsensor_enable_done_interrupt(hal_tsensor_callback_t callback)
197 {
198 if (unlikely(g_tsensor_inited == false)) {
199 return ERRCODE_TSENSOR_NOT_INIT;
200 }
201
202 uint32_t flag = tsensor_port_irq_lock();
203 g_hal_funcs->set_callback(HAL_TSENSOR_INTERRIPT_TYPE_DONE, callback);
204 g_hal_funcs->set_interrupt(HAL_TSENSOR_INTERRIPT_TYPE_DONE, true);
205 g_tsensor_work_mode = TSENSOR_WORK_MODE_INTERRUPT;
206 tsensor_port_irq_unlock(flag);
207
208 return ERRCODE_SUCC;
209 }
210
211 #if defined(CONFIG_TSENSOR_TEMP_COMPENSATION)
uapi_tsensor_set_calibration_single_point(tsensor_calibration_point_t * point)212 void uapi_tsensor_set_calibration_single_point(tsensor_calibration_point_t *point)
213 {
214 if (unlikely(g_tsensor_inited == false)) {
215 return;
216 }
217
218 g_hal_funcs->enable_calibration((hal_tsensor_calibration_point_t *)point, HAL_TSENSOR_CALIBRATION_NUM_SINGLE);
219 }
220
uapi_tsensor_set_calibration_two_points(const tsensor_calibration_point_t * point_first,const tsensor_calibration_point_t * point_second)221 void uapi_tsensor_set_calibration_two_points(const tsensor_calibration_point_t *point_first,
222 const tsensor_calibration_point_t *point_second)
223 {
224 if (unlikely(g_tsensor_inited == false)) {
225 return;
226 }
227
228 tsensor_calibration_point_t point[HAL_TSENSOR_CALIBRATION_NUM_TWO];
229 point[0] = *point_first;
230 point[1] = *point_second;
231
232 g_hal_funcs->enable_calibration((hal_tsensor_calibration_point_t *)point, HAL_TSENSOR_CALIBRATION_NUM_TWO);
233 }
234 #endif /* CONFIG_TSENSOR_TEMP_COMPENSATION */
235
236 #if defined(CONFIG_TSENSOR_MULTILEVEL)
uapi_tsensor_set_multilevel_threshold_value(tsensor_multilevel_value_t level,int16_t temp)237 errcode_t uapi_tsensor_set_multilevel_threshold_value(tsensor_multilevel_value_t level, int16_t temp)
238 {
239 if (unlikely(g_tsensor_inited == false)) {
240 return ERRCODE_TSENSOR_NOT_INIT;
241 }
242
243 if (level >= TSENSOR_MULTILEVEL_VAL_MAX) {
244 return ERRCODE_TSENSOR_INVALID_PARAMETER;
245 }
246
247 errcode_t ret = uapi_tsensor_temp_check(temp);
248 if (ret != ERRCODE_SUCC) {
249 return ret;
250 }
251
252 return g_hal_funcs->set_multilevel_value(level, temp);
253 }
254
uapi_tsensor_set_multilevel_threshold_en(tsensor_multilevel_en_t level,hal_tsensor_callback_t callback)255 errcode_t uapi_tsensor_set_multilevel_threshold_en(tsensor_multilevel_en_t level,
256 hal_tsensor_callback_t callback)
257 {
258 if (unlikely(g_tsensor_inited == false)) {
259 return ERRCODE_TSENSOR_NOT_INIT;
260 }
261
262 if (level >= TSENSOR_MULTILEVEL_EN_MAX) {
263 return ERRCODE_TSENSOR_INVALID_PARAMETER;
264 }
265 return g_hal_funcs->set_multilevel_en(level, callback);
266 }
267 #endif /* CONFIG_TSENSOR_MULTILEVEL */