hpm_tsns_drv.h 9.0 KB

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