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