temperature_sensor_ll.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 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 "hal/regi2c_ctrl.h"
  15. #include "soc/regi2c_saradc.h"
  16. #include "soc/apb_saradc_struct.h"
  17. #include "soc/rtc_cntl_reg.h"
  18. #include "soc/sens_struct.h"
  19. #include "hal/temperature_sensor_types.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386)
  24. #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88)
  25. #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52)
  26. #define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125)
  27. #define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40)
  28. /**
  29. * @brief Enable the temperature sensor power.
  30. *
  31. * @param enable true: enable the power.
  32. */
  33. static inline void temperature_sensor_ll_enable(bool enable)
  34. {
  35. SENS.sar_tctrl.tsens_power_up_force = enable;
  36. SENS.sar_tctrl2.tsens_xpd_force = enable;
  37. SENS.sar_tctrl.tsens_power_up = enable;
  38. }
  39. /**
  40. * @brief Enable the clock
  41. */
  42. static inline void temperature_sensor_ll_clk_enable(bool enable)
  43. {
  44. SENS.sar_tctrl2.tsens_clkgate_en = enable;
  45. }
  46. /**
  47. * @brief Choose the clock. No need to choose the clock source on ESP32-S2. ESP32-S2
  48. * can use RTC clock.
  49. */
  50. static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t clk_src)
  51. {
  52. // No need to select the temperature sensor clock on esp32s2.
  53. }
  54. /**
  55. * @brief Set the hardware range, you can refer to the table ``temperature_sensor_attributes``
  56. *
  57. * @param tsens_dac ``reg_val`` in table ``temperature_sensor_attributes``
  58. */
  59. static inline void temperature_sensor_ll_set_range(uint32_t tsens_dac)
  60. {
  61. REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, tsens_dac);
  62. }
  63. /**
  64. * @brief Get the raw value of temperature sensor.
  65. *
  66. * @return uint32_t raw_value
  67. */
  68. static inline uint32_t temperature_sensor_ll_get_raw_value(void)
  69. {
  70. SENS.sar_tctrl.tsens_dump_out = 1;
  71. while (!SENS.sar_tctrl.tsens_ready) {
  72. }
  73. SENS.sar_tctrl.tsens_dump_out = 0;
  74. return SENS.sar_tctrl.tsens_out;
  75. }
  76. /**
  77. * @brief Get the offset value of temperature sensor.
  78. *
  79. * @note This function is only used in legacy driver
  80. *
  81. * @return uint32_t offset value
  82. */
  83. static inline uint32_t temperature_sensor_ll_get_offset(void)
  84. {
  85. return REGI2C_READ_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC);
  86. }
  87. /**
  88. * @brief Get the clock division factor value.
  89. *
  90. * @note This function is only used in legacy driver
  91. *
  92. * @return uint32_t clock division factor
  93. */
  94. static inline uint32_t temperature_sensor_ll_get_clk_div(void)
  95. {
  96. return SENS.sar_tctrl.tsens_clk_div;
  97. }
  98. /**
  99. * @brief Set the clock division factor value, actually this has no impact on temperature sensor.
  100. * Suggest just keep it as default value 6.
  101. *
  102. * @note This function is only used in legacy driver
  103. *
  104. * @param clk_div clock division factor, range from 1-10
  105. */
  106. static inline void temperature_sensor_ll_set_clk_div(uint8_t clk_div)
  107. {
  108. SENS.sar_tctrl.tsens_clk_div = clk_div;
  109. }
  110. #ifdef __cplusplus
  111. }
  112. #endif