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 v151 hal tsensor \n
16 *
17 * History: \n
18 * 2023-02-28, Create file. \n
19 */
20 #include "common_def.h"
21 #include "hal_tsensor.h"
22 #include "hal_tsensor_v151.h"
23
24 static hal_tsensor_callback_t g_hal_tsensor_callbacks[HAL_TSENSOR_INTERRIPT_TYPE_MAX_NUM] = { NULL };
25 static hal_tsensor_calibration_point_t g_hal_tsensor_points[HAL_TSENSOR_CALIBRATION_NUM_MAX] = {0};
26 static int g_hal_tsensor_calibration_type = 0;
27
hal_tsensor_reg_tempcode_transto_temp(uint16_t temp_code)28 static inline int8_t hal_tsensor_reg_tempcode_transto_temp(uint16_t temp_code)
29 {
30 /* temperature(℃) = (temp_code - 114) / (896 - 114) * [125 - (-40)] + (-40) */
31 return (int8_t)((temp_code - TEMP_CODE_MIN) * (TEMP_VAL_MAX - TEMP_VAL_MIN) / (TEMP_CODE_MAX - TEMP_CODE_MIN) +
32 TEMP_VAL_MIN);
33 }
34
hal_tsensor_reg_temp_transto_tempcode(int8_t temp)35 static inline uint16_t hal_tsensor_reg_temp_transto_tempcode(int8_t temp)
36 {
37 /* temperature(℃) = (temp_code - 114) / (896 - 114) * [125 - (-40)] + (-40) */
38 return (uint16_t)((((temp) - TEMP_VAL_MIN) * (TEMP_CODE_MAX - TEMP_CODE_MIN) / (TEMP_VAL_MAX - TEMP_VAL_MIN)) +
39 TEMP_CODE_MIN);
40 }
41
tsensor_v151_calibration_juction_to_environment(int16_t * temperature)42 static void tsensor_v151_calibration_juction_to_environment(int16_t *temperature)
43 {
44 int16_t ref_d_value = 0;
45 int16_t temp_d_value = 0;
46
47 switch (g_hal_tsensor_calibration_type) {
48 case HAL_TSENSOR_CALIBRATION_NUM_SINGLE:
49 *temperature += g_hal_tsensor_points[0].environment_temp - g_hal_tsensor_points[0].tsensor_temp;
50 break;
51 case HAL_TSENSOR_CALIBRATION_NUM_TWO:
52 ref_d_value = g_hal_tsensor_points[1].environment_temp - g_hal_tsensor_points[0].environment_temp;
53 temp_d_value = g_hal_tsensor_points[1].tsensor_temp - g_hal_tsensor_points[0].tsensor_temp;
54
55 if (temp_d_value == 0 || ref_d_value == 0) {
56 *temperature += g_hal_tsensor_points[0].environment_temp - g_hal_tsensor_points[0].tsensor_temp;
57 } else {
58 *temperature = (*temperature - g_hal_tsensor_points[0].tsensor_temp) * ref_d_value /
59 temp_d_value + g_hal_tsensor_points[0].environment_temp;
60 }
61 break;
62 default:
63 break;
64 }
65 }
66
tsensor_v151_calibration_environment_to_juction(int16_t * temperature)67 static void tsensor_v151_calibration_environment_to_juction(int16_t *temperature)
68 {
69 int16_t ref_d_value = 0;
70 int16_t temp_d_value = 0;
71
72 switch (g_hal_tsensor_calibration_type) {
73 case HAL_TSENSOR_CALIBRATION_NUM_SINGLE:
74 *temperature += g_hal_tsensor_points[0].tsensor_temp - g_hal_tsensor_points[0].environment_temp;
75 break;
76 case HAL_TSENSOR_CALIBRATION_NUM_TWO:
77 ref_d_value = g_hal_tsensor_points[1].tsensor_temp - g_hal_tsensor_points[0].tsensor_temp;
78 temp_d_value = g_hal_tsensor_points[1].environment_temp - g_hal_tsensor_points[0].environment_temp;
79
80 if (temp_d_value == 0 || ref_d_value == 0) {
81 *temperature += g_hal_tsensor_points[0].tsensor_temp - g_hal_tsensor_points[0].environment_temp;
82 } else {
83 *temperature = (*temperature - g_hal_tsensor_points[0].environment_temp) * ref_d_value /
84 temp_d_value + g_hal_tsensor_points[0].tsensor_temp;
85 }
86 break;
87 default:
88 break;
89 }
90 }
91
hal_tsensor_v151_enable_calibration(hal_tsensor_calibration_point_t * point_data,int8_t point_num)92 static void hal_tsensor_v151_enable_calibration(hal_tsensor_calibration_point_t *point_data, int8_t point_num)
93 {
94 int8_t i = 0;
95 hal_tsensor_v151_reg_set_calib_enable();
96 for (; i < point_num; i++) {
97 g_hal_tsensor_points[i] = point_data[i];
98 }
99
100 g_hal_tsensor_calibration_type = point_num;
101 }
102
hal_tsensor_v151_init(void)103 static errcode_t hal_tsensor_v151_init(void)
104 {
105 if (hal_tsensor_regs_init() != ERRCODE_SUCC) {
106 return ERRCODE_TSENSOR_REG_ADDR_INVALID;
107 }
108 hal_tsensor_v151_reg_set_auto_refresh_enable(false);
109 hal_tsensor_v151_reg_set_calib_enable();
110
111 return ERRCODE_SUCC;
112 }
113
hal_tsensor_v151_deinit(void)114 static void hal_tsensor_v151_deinit(void)
115 {
116 hal_tsensor_v151_reg_clear_sts();
117 hal_tsensor_v151_reg_clr_temp_intr();
118 hal_tsensor_v151_reg_set_auto_refresh_enable(false);
119
120 hal_tsensor_v151_reg_set_tsensor_done_intr_enable(0);
121 hal_tsensor_v151_reg_set_out_temp_threshold_intr_enable(0);
122 hal_tsensor_v151_reg_set_overtemp_intr_enable(0);
123 hal_tsensor_v151_reg_set_tsensor_disable();
124 hal_tsensor_regs_deinit();
125 }
126
hal_tsensor_v151_set_samp_mode(hal_tsensor_samp_mode_t mode,uint32_t period)127 static void hal_tsensor_v151_set_samp_mode(hal_tsensor_samp_mode_t mode, uint32_t period)
128 {
129 hal_tsensor_v151_reg_set_auto_refresh_enable(false);
130 hal_tsensor_v151_reg_set_tsensor_mode(mode);
131 hal_tsensor_v151_reg_set_auto_refresh_period(period);
132 hal_tsensor_v151_reg_set_tsensor_enable();
133 }
134
hal_tsensor_v151_temp_threshold_set_low(hal_tsensor_set_temp_id_t id,int8_t temp)135 static void hal_tsensor_v151_temp_threshold_set_low(hal_tsensor_set_temp_id_t id, int8_t temp)
136 {
137 unused(id);
138 uint16_t temp_code;
139
140 temp_code = hal_tsensor_reg_temp_transto_tempcode(temp);
141 hal_tsensor_v151_reg_set_temp_low_limit(temp_code);
142 }
143
hal_tsensor_v151_temp_threshold_set_high(hal_tsensor_set_temp_id_t id,int8_t temp)144 static void hal_tsensor_v151_temp_threshold_set_high(hal_tsensor_set_temp_id_t id, int8_t temp)
145 {
146 unused(id);
147 uint16_t temp_code;
148
149 temp_code = hal_tsensor_reg_temp_transto_tempcode(temp);
150 hal_tsensor_v151_reg_set_temp_high_limit(temp_code);
151 }
152
hal_tsensor_v151_temp_threshold_set_over(hal_tsensor_set_temp_id_t id,int8_t temp)153 static void hal_tsensor_v151_temp_threshold_set_over(hal_tsensor_set_temp_id_t id, int8_t temp)
154 {
155 unused(id);
156 uint16_t temp_code;
157
158 temp_code = hal_tsensor_reg_temp_transto_tempcode(temp);
159 hal_tsensor_v151_reg_set_overtemp_threshold(temp_code);
160 }
161
162 static hal_tsensor_set_temp_threshold_t g_hal_tsensor_set_func_array[TSENSOR_SET_TEMP_MAX] = {
163 hal_tsensor_v151_temp_threshold_set_low,
164 hal_tsensor_v151_temp_threshold_set_high,
165 hal_tsensor_v151_temp_threshold_set_over,
166 };
167
hal_tsensor_v151_set_temp_threshold(hal_tsensor_set_temp_id_t id,int8_t temp)168 static void hal_tsensor_v151_set_temp_threshold(hal_tsensor_set_temp_id_t id, int8_t temp)
169 {
170 int16_t temp_d = temp;
171 tsensor_v151_calibration_environment_to_juction((int16_t *)&temp_d);
172 if (temp_d < HAL_TSENSOR_TEMP_THRESHOLD_L_MAX) {
173 temp_d = HAL_TSENSOR_TEMP_THRESHOLD_L_MAX;
174 }
175 if (temp_d > HAL_TSENSOR_TEMP_THRESHOLD_H_MAX) {
176 temp_d = HAL_TSENSOR_TEMP_THRESHOLD_H_MAX;
177 }
178
179 g_hal_tsensor_set_func_array[id](id, temp_d);
180 }
181
hal_tsensor_v151_set_interrupt(hal_tsensor_interript_type_t interrupt_type,bool value)182 static void hal_tsensor_v151_set_interrupt(hal_tsensor_interript_type_t interrupt_type, bool value)
183 {
184 hal_tsensor_v151_reg_set_auto_refresh_enable(true);
185 if (interrupt_type == HAL_TSENSOR_INTERRIPT_TYPE_DONE) {
186 hal_tsensor_v151_reg_set_tsensor_done_intr_enable(value);
187 } else if (interrupt_type == HAL_TSENSOR_INTERRIPT_TYPE_OVERTEMP) {
188 hal_tsensor_v151_reg_set_out_temp_threshold_intr_enable(value);
189 } else if (interrupt_type == HAL_TSENSOR_INTERRIPT_TYPE_OUT_THRESH) {
190 hal_tsensor_v151_reg_set_overtemp_intr_enable(value);
191 }
192 }
193
hal_tsensor_v151_set_callback(hal_tsensor_interript_type_t interrupt_type,hal_tsensor_callback_t tsensor_callback)194 static void hal_tsensor_v151_set_callback(hal_tsensor_interript_type_t interrupt_type,
195 hal_tsensor_callback_t tsensor_callback)
196 {
197 g_hal_tsensor_callbacks[interrupt_type] = tsensor_callback;
198 }
199
hal_tsensor_v151_refresh_temp(void)200 static void hal_tsensor_v151_refresh_temp(void)
201 {
202 hal_tsensor_v151_reg_set_start();
203 }
204
hal_tsensor_v151_get_temp(volatile int8_t * data)205 static bool hal_tsensor_v151_get_temp(volatile int8_t *data)
206 {
207 int16_t temp;
208 uint16_t temp_code;
209
210 /* wait for temperature valid flag */
211 uint64_t start_time = tsensor_port_get_ms();
212 while (hal_tsensor_v151_reg_is_tsensor_ready() == 0) {
213 if ((tsensor_port_get_ms() - start_time) > CONFIG_TSENSOR_WAIT_TIME_MS) {
214 return false;
215 }
216 }
217
218 /* get temp_code and tranfer temp_code to tepmerature */
219 temp_code = hal_tsensor_v151_reg_get_data();
220 temp = hal_tsensor_reg_tempcode_transto_temp(temp_code);
221
222 tsensor_v151_calibration_juction_to_environment((int16_t *)&temp);
223
224 if (temp < HAL_TSENSOR_TEMP_THRESHOLD_L_MAX) {
225 *data = HAL_TSENSOR_TEMP_THRESHOLD_L_MAX;
226 return false;
227 }
228 if (temp > HAL_TSENSOR_TEMP_THRESHOLD_H_MAX) {
229 *data = HAL_TSENSOR_TEMP_THRESHOLD_H_MAX;
230 return false;
231 }
232
233 *data = temp;
234 return true;
235 }
236
237 static hal_tsensor_funcs_t g_hal_tsensor_v151_funcs = {
238 .init = hal_tsensor_v151_init,
239 .deinit = hal_tsensor_v151_deinit,
240 .set_samp_mode = hal_tsensor_v151_set_samp_mode,
241 .set_temp_threshold = hal_tsensor_v151_set_temp_threshold,
242 .set_interrupt = hal_tsensor_v151_set_interrupt,
243 .set_callback = hal_tsensor_v151_set_callback,
244 #if defined(CONFIG_TSENSOR_MULTILEVEL)
245 .set_multilevel_value = NULL,
246 .set_multilevel_en = NULL,
247 #endif /* CONFIG_TSENSOR_MULTILEVEL */
248 .enable_calibration = hal_tsensor_v151_enable_calibration,
249 .refresh_temp = hal_tsensor_v151_refresh_temp,
250 .get_temp = hal_tsensor_v151_get_temp,
251 };
252
hal_tsensor_v151_funcs_get(void)253 hal_tsensor_funcs_t *hal_tsensor_v151_funcs_get(void)
254 {
255 return &g_hal_tsensor_v151_funcs;
256 }
257
hal_tsensor_notify_int_callback(hal_tsensor_interript_type_t interrupt_type,int8_t temp)258 static void hal_tsensor_notify_int_callback(hal_tsensor_interript_type_t interrupt_type, int8_t temp)
259 {
260 if (g_hal_tsensor_callbacks[interrupt_type]) {
261 g_hal_tsensor_callbacks[interrupt_type](temp);
262 }
263 }
264
hal_tsensor_irq_handler(void)265 void hal_tsensor_irq_handler(void)
266 {
267 int8_t temp;
268 uint32_t interrupt_sts = hal_tsensor_v151_reg_get_temp_intr_status();
269 if ((uint16_t)interrupt_sts & hal_tsensor_v151_reg_get_temp_intr_enable_status()) {
270 hal_tsensor_v151_get_temp(&temp);
271 }
272
273 if (interrupt_sts & (0x1 << HAL_TSENSOR_INTERRIPT_TYPE_DONE)) {
274 hal_tsensor_notify_int_callback(HAL_TSENSOR_INTERRIPT_TYPE_DONE, temp);
275 }
276 if (interrupt_sts & (0x1 << HAL_TSENSOR_INTERRIPT_TYPE_OUT_THRESH)) {
277 hal_tsensor_notify_int_callback(HAL_TSENSOR_INTERRIPT_TYPE_OUT_THRESH, temp);
278 }
279 if (interrupt_sts & (0x1 << HAL_TSENSOR_INTERRIPT_TYPE_OVERTEMP)) {
280 hal_tsensor_notify_int_callback(HAL_TSENSOR_INTERRIPT_TYPE_OVERTEMP, temp);
281 }
282
283 hal_tsensor_v151_reg_set_auto_refresh_enable(true);
284 hal_tsensor_v151_reg_clear_sts(); /* 清除16点平均单次上报模式或16点平均循环上报模式的状态 */
285 hal_tsensor_v151_reg_clr_temp_intr();
286 }
287