• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 
9 #ifndef HPM_TSNS_DRV_H
10 #define HPM_TSNS_DRV_H
11 
12 #include "hpm_common.h"
13 #include "hpm_tsns_regs.h"
14 
15 /**
16  * @brief TSNS driver APIs
17  * @defgroup tsns_interface TSNS driver APIs
18  * @ingroup io_interfaces
19  * @{
20  *
21  */
22 
23 /***********************************************************************************************************************
24  *
25  * Definitions
26  *
27  **********************************************************************************************************************/
28 #define TSNS_TEMP_SCALE 256
29 
30 typedef enum {
31     tsns_clear_min = TSNS_FLAG_RECORD_MIN_CLR_MASK,
32     tsns_clear_max = TSNS_FLAG_RECORD_MAX_CLR_MASK,
33     tsns_clear_under_temp = TSNS_FLAG_UNDER_TEMP_MASK,
34     tsns_clear_over_temp = TSNS_FLAG_OVER_TEMP_MASK,
35     tsns_clear_irq = TSNS_FLAG_IRQ_MASK,
36 } tsns_clear_type_mask_t;
37 
38 typedef enum {
39     tsns_event_irq = 0,
40     tsns_event_reset,
41 } tsns_event_t;
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 
48 /**
49  * @brief  Enable temperature sensor
50  *
51  * @param ptr base address
52  */
tsns_enable(TSNS_Type * ptr)53 static inline void tsns_enable(TSNS_Type *ptr)
54 {
55     ptr->CONFIG |= TSNS_CONFIG_ENABLE_MASK;
56 }
57 
58 /**
59  * @brief   Disable temperature sensor
60  *
61  * @param ptr base address
62  */
tsns_disable(TSNS_Type * ptr)63 static inline void tsns_disable(TSNS_Type *ptr)
64 {
65     ptr->CONFIG &= ~TSNS_CONFIG_ENABLE_MASK;
66 }
67 
68 /**
69  * @brief   Check if current temperature value is valid or not
70  *
71  * @param ptr base address
72  *
73  * @return  true the value is valid
74  */
tsns_temperature_is_valid(TSNS_Type * ptr)75 static inline bool tsns_temperature_is_valid(TSNS_Type *ptr)
76 {
77     return ptr->STATUS & TSNS_STATUS_VALID_MASK;
78 }
79 
80 /**
81  * @brief   Get maximum measured temperature in raw
82  *
83  * @param ptr base address
84  *
85  * @return raw maximum temperature value scaled by TSNS_TEMP_SCALE
86  */
tsns_get_max_temp_raw(TSNS_Type * ptr)87 static inline int32_t tsns_get_max_temp_raw(TSNS_Type *ptr)
88 {
89     return TSNS_TMAX_T_GET(ptr->TMAX);
90 }
91 
92 /**
93  * @brief    Get minimum measured temperature in raw
94  *
95  * @param ptr base address
96  *
97  * @return  raw minimum temperature value scaled by TSNS_TEMP_SCALE
98  */
tsns_get_min_temp_raw(TSNS_Type * ptr)99 static inline int32_t tsns_get_min_temp_raw(TSNS_Type *ptr)
100 {
101     return TSNS_TMIN_T_GET(ptr->TMIN);
102 }
103 
104 /**
105  * @brief   Get current temperature in raw
106  *
107  * @param ptr base address
108  *
109  * @return  raw temperature value scaled by TSNS_TEMP_SCALE
110  */
tsns_get_current_temp_in_raw(TSNS_Type * ptr)111 static inline int32_t tsns_get_current_temp_in_raw(TSNS_Type *ptr)
112 {
113     while (!tsns_temperature_is_valid(ptr)) {
114         ;
115     }
116     return TSNS_T_T_GET(ptr->T);
117 }
118 
119 /**
120  * @brief   Get current temperature in celsius degree
121  *
122  * @param ptr base address
123  *
124  * @return  current temperature in celsius degree
125  */
tsns_get_current_temp(TSNS_Type * ptr)126 static inline float tsns_get_current_temp(TSNS_Type *ptr)
127 {
128     return (float)(((int32_t)tsns_get_current_temp_in_raw(ptr) / TSNS_TEMP_SCALE));
129 }
130 
131 /**
132  * @brief   Get temperature age
133  *
134  * @param ptr base address
135  *
136  * @return  temperature age
137  */
tsns_get_temp_age(TSNS_Type * ptr)138 static inline uint32_t tsns_get_temp_age(TSNS_Type *ptr)
139 {
140     return TSNS_AGE_AGE_GET(ptr->AGE);
141 }
142 
143 /**
144  * @brief   Set temperature high limit to trigger rest
145  *
146  * @param ptr base address
147  * @param high temperature value
148  */
tsns_set_reset_threshold_high(TSNS_Type * ptr,uint32_t high)149 static inline void tsns_set_reset_threshold_high(TSNS_Type *ptr, uint32_t high)
150 {
151     ptr->UPPER_LIM_IRQ = TSNS_UPPER_LIM_RST_T_SET(high);
152 }
153 
154 /**
155  * @brief   Set temperature low limit to trigger reset
156  *
157  * @param ptr base address
158  * @param low temperature value
159  */
tsns_set_reset_threshold_low(TSNS_Type * ptr,uint32_t low)160 static inline void tsns_set_reset_threshold_low(TSNS_Type *ptr, uint32_t low)
161 {
162     ptr->LOWER_LIM_IRQ = TSNS_LOWER_LIM_RST_T_SET(low);
163 }
164 
165 /**
166  * @brief   Enable temperature limit to trigger irq
167  *
168  * @param ptr base address
169  */
tsns_enable_limit_trigger_reset(TSNS_Type * ptr)170 static inline void tsns_enable_limit_trigger_reset(TSNS_Type *ptr)
171 {
172     ptr->CONFIG |= TSNS_CONFIG_RST_EN_MASK;
173 }
174 
175 /**
176  * @brief   Disable temperature limit to trigger irq
177  *
178  * @param ptr base address
179  */
tsns_disable_limit_trigger_irq(TSNS_Type * ptr)180 static inline void tsns_disable_limit_trigger_irq(TSNS_Type *ptr)
181 {
182     ptr->CONFIG &= ~TSNS_CONFIG_RST_EN_MASK;
183 }
184 
185 /**
186  * @brief   Set temperature high limit to trigger irq
187  *
188  * @param ptr base address
189  * @param high temperature value
190  */
tsns_set_irq_threshold_high(TSNS_Type * ptr,uint32_t high)191 static inline void tsns_set_irq_threshold_high(TSNS_Type *ptr, uint32_t high)
192 {
193     ptr->UPPER_LIM_IRQ = TSNS_UPPER_LIM_IRQ_T_SET(high);
194 }
195 
196 /**
197  * @brief   Set temperature low limit to trigger irq
198  *
199  * @param ptr base address
200  * @param low temperature value
201  */
tsns_set_irq_threshold_low(TSNS_Type * ptr,uint32_t low)202 static inline void tsns_set_irq_threshold_low(TSNS_Type *ptr, uint32_t low)
203 {
204     ptr->LOWER_LIM_IRQ = TSNS_LOWER_LIM_IRQ_T_SET(low);
205 }
206 
207 /**
208  * @brief   Enable temperature limit to trigger irq
209  *
210  * @param ptr base address
211  */
tsns_enable_limit_trigger_irq(TSNS_Type * ptr)212 static inline void tsns_enable_limit_trigger_irq(TSNS_Type *ptr)
213 {
214     ptr->CONFIG |= TSNS_CONFIG_IRQ_EN_MASK;
215 }
216 
217 /**
218  * @brief   Set validity of current measured temperature in 24Mhz clock cycles
219  *
220  * @param ptr base address
221  * @param validity  clock cycle count
222  */
tsns_set_validity(TSNS_Type * ptr,uint32_t validity)223 static inline void tsns_set_validity(TSNS_Type *ptr, uint32_t validity)
224 {
225     ptr->VALIDITY = TSNS_VALIDITY_VALIDITY_SET(validity);
226 }
227 
228 /**
229  * @brief   Set temperature limit to trigger irq
230  *
231  * @param ptr base address
232  * @param high high temperature
233  * @param low low temperature
234  */
tsns_config_irq_threshold(TSNS_Type * ptr,uint32_t high,uint32_t low)235 static inline void tsns_config_irq_threshold(TSNS_Type *ptr, uint32_t high, uint32_t low)
236 {
237     tsns_set_irq_threshold_low(ptr, low);
238     tsns_set_irq_threshold_high(ptr, high);
239 }
240 
241 /**
242  * @brief   Set temperature limit to trigger reset
243  *
244  * @param ptr base address
245  * @param high high temperature
246  * @param low low temperature
247  */
tsns_config_reset_threshold(TSNS_Type * ptr,uint32_t high,uint32_t low)248 static inline void tsns_config_reset_threshold(TSNS_Type *ptr, uint32_t high, uint32_t low)
249 {
250     tsns_set_reset_threshold_low(ptr, low);
251     tsns_set_reset_threshold_high(ptr, high);
252 }
253 
254 /**
255  * @brief   Enable compare max temperature
256  *
257  * @param ptr base address
258  */
tsns_enable_compare_max(TSNS_Type * ptr)259 static inline void tsns_enable_compare_max(TSNS_Type *ptr)
260 {
261     ptr->CONFIG |= TSNS_CONFIG_COMPARE_MAX_EN_MASK;
262 }
263 
264 /**
265  * @brief   Enable compare min temperature
266  *
267  * @param ptr base address
268  */
tsns_enable_compare_min(TSNS_Type * ptr)269 static inline void tsns_enable_compare_min(TSNS_Type *ptr)
270 {
271     ptr->CONFIG |= TSNS_CONFIG_COMPARE_MIN_EN_MASK;
272 }
273 
274 /**
275  * @brief   Disable compare max temperature
276  *
277  * @param ptr base address
278  */
tsns_disable_compare_max(TSNS_Type * ptr)279 static inline void tsns_disable_compare_max(TSNS_Type *ptr)
280 {
281     ptr->CONFIG &= ~TSNS_CONFIG_COMPARE_MAX_EN_MASK;
282 }
283 
284 /**
285  * @brief   Disable compare min temperature
286  *
287  * @param ptr base address
288  */
tsns_disable_compare_min(TSNS_Type * ptr)289 static inline void tsns_disable_compare_min(TSNS_Type *ptr)
290 {
291     ptr->CONFIG &= ~TSNS_CONFIG_COMPARE_MIN_EN_MASK;
292 }
293 
294 /**
295  * @brief   Set measurement speed
296  *
297  * @param ptr base address
298  * @param speed speed from 24-255
299  */
tsns_set_speed(TSNS_Type * ptr,uint8_t speed)300 static inline void tsns_set_speed(TSNS_Type *ptr, uint8_t speed)
301 {
302     assert(speed >= 24);
303     ptr->CONFIG = (ptr->CONFIG & TSNS_CONFIG_SPEED_MASK) | TSNS_CONFIG_SPEED_SET(speed);
304 }
305 
306 /**
307  * @brief   Set average
308  *
309  * @param ptr base address
310  * @param average range 0 - 7 (0: 2^0 = 1 means measure once and return  ... 2: 2^2 = 4 means measure 4 times and average)
311  */
tsns_set_average(TSNS_Type * ptr,uint8_t average)312 static inline void tsns_set_average(TSNS_Type *ptr, uint8_t average)
313 {
314     ptr->CONFIG = (ptr->CONFIG & TSNS_CONFIG_AVERAGE_MASK) | TSNS_CONFIG_AVERAGE_SET(average);
315 }
316 
317 /**
318  * @brief   Enable Async mode
319  *
320  * @param ptr base address
321  */
tsns_enable_async_mode(TSNS_Type * ptr)322 static inline void tsns_enable_async_mode(TSNS_Type *ptr)
323 {
324     ptr->CONFIG |= TSNS_CONFIG_ASYNC_MASK;
325 }
326 
327 /**
328  * @brief   Disable Async mode and switch to active mode
329  *
330  * @param ptr base address
331  */
tsns_disable_async_mode(TSNS_Type * ptr)332 static inline void tsns_disable_async_mode(TSNS_Type *ptr)
333 {
334     ptr->CONFIG &= ~TSNS_CONFIG_ASYNC_MASK;
335 }
336 
337 /**
338  * @brief   Enable trigger mode
339  *
340  * @param ptr base address
341  */
tsns_enable_trigger_mode(TSNS_Type * ptr)342 static inline void tsns_enable_trigger_mode(TSNS_Type *ptr)
343 {
344     ptr->CONFIG &= ~TSNS_CONFIG_CONTINUOUS_MASK;
345 }
346 
347 /**
348  * @brief   Enable continuous mode
349  *
350  * @param ptr base address
351  */
tsns_enable_continuous_mode(TSNS_Type * ptr)352 static inline void tsns_enable_continuous_mode(TSNS_Type *ptr)
353 {
354     ptr->CONFIG |= TSNS_CONFIG_CONTINUOUS_MASK;
355 }
356 
357 /**
358  * @brief   trigger measurement
359  *
360  * @param ptr base address
361  */
tsns_trigger_measurement(TSNS_Type * ptr)362 static inline void tsns_trigger_measurement(TSNS_Type *ptr)
363 {
364     uint32_t tmp = ptr->CONFIG;
365     ptr->CONFIG &= ~TSNS_CONFIG_CONTINUOUS_MASK;
366     ptr->STATUS |= TSNS_STATUS_TRIGGER_MASK;
367     ptr->CONFIG = tmp;
368 }
369 
370 /**
371  * @brief   clear tsns flag or recorded data
372  *
373  * @param ptr base address
374  * @param mask flag or data to be cleared
375  */
tsns_clear_with_mask(TSNS_Type * ptr,tsns_clear_type_mask_t mask)376 static inline void tsns_clear_with_mask(TSNS_Type *ptr, tsns_clear_type_mask_t mask)
377 {
378     ptr->FLAG |= mask;
379 }
380 
381 
382 /**
383  * @brief   configure low temperature limite to trigger event
384  *
385  * @param ptr base address
386  * @param low temperature value
387  * @param e event type, tsns_event_irq or tsns_event_reset
388  */
389 void tsns_configure_low_limit_event(TSNS_Type *ptr, int32_t low, tsns_event_t e);
390 
391 /**
392  * @brief   configure high temperature limite to trigger event
393  *
394  * @param ptr base address
395  * @param high temperature value
396  * @param e event type, tsns_event_irq or tsns_event_reset
397  */
398 void tsns_configure_high_limit_event(TSNS_Type *ptr, int32_t high, tsns_event_t e);
399 
400 /**
401  * @brief   configure temperature limite to trigger event
402  *
403  * @param ptr base address
404  * @param high temperature value
405  * @param low temperature value
406  * @param e event type, tsns_event_irq or tsns_event_reset
407  */
408 void tsns_configure_limit_event(TSNS_Type *ptr, int32_t high, int32_t low, tsns_event_t e);
409 
410 #ifdef __cplusplus
411 }
412 #endif
413 /**
414  * @}
415  *
416  */
417 #endif /* HPM_TSNS_DRV_H */
418