temperature_sensor_ll.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*******************************************************************************
  7. * NOTICE
  8. * The hal is not public api, don't use in application code.
  9. * See readme.md in component/hal/readme.md
  10. ******************************************************************************/
  11. // The LL for temperature sensor
  12. #pragma once
  13. #include <stdbool.h>
  14. #include <stdlib.h>
  15. #include "hal/regi2c_ctrl.h"
  16. #include "soc/regi2c_saradc.h"
  17. #include "soc/apb_saradc_struct.h"
  18. #include "soc/apb_saradc_reg.h"
  19. #include "soc/soc.h"
  20. #include "soc/soc_caps.h"
  21. #include "soc/pcr_struct.h"
  22. #include "hal/temperature_sensor_types.h"
  23. #include "hal/assert.h"
  24. #include "hal/misc.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386)
  29. #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88)
  30. #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52)
  31. #define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125)
  32. #define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40)
  33. #define TEMPERATURE_SENSOR_LL_INTR_MASK APB_SARADC_APB_SARADC_TSENS_INT_ST
  34. typedef enum {
  35. TEMPERATURE_SENSOR_LL_WAKE_ABSOLUTE = 0,
  36. TEMPERATURE_SENSOR_LL_WAKE_DELTA = 1,
  37. } temperature_sensor_ll_wakeup_mode_t;
  38. /**
  39. * @brief Enable the temperature sensor power.
  40. *
  41. * @param enable true: enable the power.
  42. */
  43. static inline void temperature_sensor_ll_enable(bool enable)
  44. {
  45. APB_SARADC.saradc_apb_tsens_ctrl.saradc_tsens_pu = enable;
  46. }
  47. /**
  48. * @brief Enable the clock
  49. */
  50. static inline void temperature_sensor_ll_clk_enable(bool enable)
  51. {
  52. // clock enable duplicated with periph enable, no need to enable it again.
  53. }
  54. /**
  55. * @brief Select the clock source for temperature sensor. On ESP32-C6, temperautre sensor
  56. * can use XTAL or FOSC. To make it convenience, suggest using XTAL all the time.
  57. *
  58. * @param clk_src refer to ``temperature_sensor_clk_src_t``
  59. */
  60. static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t clk_src)
  61. {
  62. uint8_t clk_sel = 0;
  63. switch (clk_src) {
  64. case TEMPERATURE_SENSOR_CLK_SRC_XTAL:
  65. clk_sel = 1;
  66. break;
  67. case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST:
  68. clk_sel = 0;
  69. break;
  70. default:
  71. HAL_ASSERT(false);
  72. break;
  73. }
  74. PCR.tsens_clk_conf.tsens_clk_sel = clk_sel;
  75. }
  76. /**
  77. * @brief Set the hardware range, you can refer to the table ``temperature_sensor_attributes``
  78. *
  79. * @param tsens_dac ``reg_val`` in table ``temperature_sensor_attributes``
  80. */
  81. static inline void temperature_sensor_ll_set_range(uint32_t range)
  82. {
  83. REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, range);
  84. }
  85. /**
  86. * @brief Get the raw value of temperature sensor.
  87. *
  88. * @return uint32_t raw_value
  89. */
  90. __attribute__((always_inline))
  91. static inline uint32_t temperature_sensor_ll_get_raw_value(void)
  92. {
  93. return APB_SARADC.saradc_apb_tsens_ctrl.saradc_tsens_out;
  94. }
  95. /**
  96. * @brief Get the offset value of temperature sensor.
  97. *
  98. * @note This function is only used in legacy driver
  99. *
  100. * @return uint32_t offset value
  101. */
  102. static inline uint32_t temperature_sensor_ll_get_offset(void)
  103. {
  104. return REGI2C_READ_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC);
  105. }
  106. /**
  107. * @brief Get the clock division factor value.
  108. *
  109. * @note This function is only used in legacy driver
  110. *
  111. * @return uint32_t clock division factor
  112. */
  113. static inline uint32_t temperature_sensor_ll_get_clk_div(void)
  114. {
  115. return APB_SARADC.saradc_apb_tsens_ctrl.saradc_tsens_clk_div;
  116. }
  117. /**
  118. * @brief Set the clock division factor value, actually this has no impact on temperature sensor.
  119. * Suggest just keep it as default value 6.
  120. *
  121. * @note This function is only used in legacy driver
  122. *
  123. * @param clk_div clock division factor, range from 1-10
  124. */
  125. static inline void temperature_sensor_ll_set_clk_div(uint8_t clk_div)
  126. {
  127. APB_SARADC.saradc_apb_tsens_ctrl.saradc_tsens_clk_div = clk_div;
  128. }
  129. /**
  130. * @brief Choose the wake-up mode for temperature sensor
  131. *
  132. * @note ESP32-C6 does not support difference mode.
  133. *
  134. * @param mode 0: Absolute value mode. 1: Difference mode.
  135. */
  136. static inline void temperature_sensor_ll_wakeup_mode(uint8_t mode)
  137. {
  138. APB_SARADC.tsens_wake.saradc_wakeup_mode = mode;
  139. }
  140. /**
  141. * @brief Configure whether to enable temperature sensor wake up
  142. *
  143. * @param en true: enable, false: disable.
  144. */
  145. static inline void temperature_sensor_ll_wakeup_enable(bool en)
  146. {
  147. APB_SARADC.tsens_wake.saradc_wakeup_en = en;
  148. }
  149. /**
  150. * @brief Configures the low threshold for temperature sensor to wakeup
  151. *
  152. * @param th_low low threshold value.
  153. */
  154. static inline void temperature_sensor_ll_set_th_low_val(uint8_t th_low)
  155. {
  156. APB_SARADC.tsens_wake.saradc_wakeup_th_low = th_low;
  157. }
  158. /**
  159. * @brief Configures the high threshold for temperature sensor to wakeup
  160. *
  161. * @param th_high high threshold value.
  162. */
  163. static inline void temperature_sensor_ll_set_th_high_val(uint8_t th_high)
  164. {
  165. APB_SARADC.tsens_wake.saradc_wakeup_th_high = th_high;
  166. }
  167. /**
  168. * @brief Enable temperature sensor interrupt
  169. *
  170. * @param enable true: enable. false: disable
  171. */
  172. static inline void temperature_sensor_ll_enable_intr(bool enable)
  173. {
  174. APB_SARADC.saradc_int_ena.saradc_apb_saradc_tsens_int_ena = enable;
  175. }
  176. /**
  177. * @brief Clear temperature sensor interrupt
  178. */
  179. __attribute__((always_inline))
  180. static inline void temperature_sensor_ll_clear_intr(void)
  181. {
  182. APB_SARADC.saradc_int_clr.saradc_apb_saradc_tsens_int_clr = 1;
  183. }
  184. /**
  185. * @brief Get temperature sensor interrupt status.
  186. */
  187. static inline volatile void *temperature_sensor_ll_get_intr_status(void)
  188. {
  189. return &APB_SARADC.saradc_int_st;
  190. }
  191. /**
  192. * @brief Configure whether to enable hardware sampling
  193. *
  194. * @param en true: enable, false: disable
  195. */
  196. static inline void temperature_sensor_ll_sample_enable(bool en)
  197. {
  198. APB_SARADC.tsens_sample.saradc_tsens_sample_en = en;
  199. }
  200. /**
  201. * @brief Configures the hardware sampling rate
  202. *
  203. * @param rate sampling rate
  204. */
  205. static inline void temperature_sensor_ll_set_sample_rate(uint16_t rate)
  206. {
  207. HAL_FORCE_MODIFY_U32_REG_FIELD(APB_SARADC.tsens_sample, saradc_tsens_sample_rate, rate);
  208. }
  209. #ifdef __cplusplus
  210. }
  211. #endif