adc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // Copyright 2016-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. #include "sdkconfig.h"
  17. #include "esp_types.h"
  18. #include "esp_log.h"
  19. #include "sys/lock.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/xtensa_api.h"
  22. #include "freertos/semphr.h"
  23. #include "freertos/timers.h"
  24. #include "esp_pm.h"
  25. #include "esp_intr_alloc.h"
  26. #include "driver/periph_ctrl.h"
  27. #include "driver/rtc_io.h"
  28. #include "driver/rtc_cntl.h"
  29. #include "driver/gpio.h"
  30. #include "driver/adc.h"
  31. #include "esp32s2/esp_efuse_rtc_table.h"
  32. #include "hal/adc_types.h"
  33. #include "hal/adc_hal.h"
  34. #define ADC_CHECK_RET(fun_ret) ({ \
  35. if (fun_ret != ESP_OK) { \
  36. ESP_LOGE(ADC_TAG,"%s:%d\n",__FUNCTION__,__LINE__); \
  37. return ESP_FAIL; \
  38. } \
  39. })
  40. static const char *ADC_TAG = "ADC";
  41. #define ADC_CHECK(a, str, ret_val) ({ \
  42. if (!(a)) { \
  43. ESP_LOGE(ADC_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  44. return (ret_val); \
  45. } \
  46. })
  47. #define ADC_GET_IO_NUM(periph, channel) (adc_channel_io_map[periph][channel])
  48. #define ADC_CHANNEL_CHECK(periph, channel) ADC_CHECK(channel < SOC_ADC_CHANNEL_NUM(periph), "ADC"#periph" channel error", ESP_ERR_INVALID_ARG)
  49. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  50. #define ADC_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  51. #define ADC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  52. #ifdef CONFIG_PM_ENABLE
  53. static esp_pm_lock_handle_t s_adc_digi_arbiter_lock = NULL;
  54. #endif //CONFIG_PM_ENABLE
  55. esp_err_t adc_cal_offset(adc_ll_num_t adc_n, adc_channel_t channel, adc_atten_t atten);
  56. /*---------------------------------------------------------------
  57. Digital controller setting
  58. ---------------------------------------------------------------*/
  59. esp_err_t adc_digi_init(void)
  60. {
  61. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  62. ADC_ENTER_CRITICAL();
  63. adc_hal_init();
  64. adc_hal_arbiter_config(&config);
  65. ADC_EXIT_CRITICAL();
  66. adc_hal_calibration_init(ADC_NUM_1);
  67. adc_hal_calibration_init(ADC_NUM_2);
  68. return ESP_OK;
  69. }
  70. esp_err_t adc_digi_deinit(void)
  71. {
  72. #ifdef CONFIG_PM_ENABLE
  73. if (s_adc_digi_arbiter_lock) {
  74. esp_pm_lock_delete(s_adc_digi_arbiter_lock);
  75. s_adc_digi_arbiter_lock = NULL;
  76. }
  77. #endif
  78. adc_power_release();
  79. ADC_ENTER_CRITICAL();
  80. adc_hal_digi_deinit();
  81. ADC_EXIT_CRITICAL();
  82. return ESP_OK;
  83. }
  84. esp_err_t adc_digi_controller_config(const adc_digi_config_t *config)
  85. {
  86. #ifdef CONFIG_PM_ENABLE
  87. esp_err_t err;
  88. if (s_adc_digi_arbiter_lock == NULL) {
  89. if (config->dig_clk.use_apll) {
  90. err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "adc_dma", &s_adc_digi_arbiter_lock);
  91. } else {
  92. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "adc_dma", &s_adc_digi_arbiter_lock);
  93. }
  94. if (err != ESP_OK) {
  95. s_adc_digi_arbiter_lock = NULL;
  96. ESP_LOGE(ADC_TAG, "ADC-DMA pm lock error");
  97. return err;
  98. }
  99. }
  100. #endif //CONFIG_PM_ENABLE
  101. if (config->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  102. for (int i = 0; i < config->adc1_pattern_len; i++) {
  103. adc_cal_offset(ADC_NUM_1, config->adc1_pattern[i].channel, config->adc1_pattern[i].atten);
  104. }
  105. }
  106. if (config->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  107. for (int i = 0; i < config->adc2_pattern_len; i++) {
  108. adc_cal_offset(ADC_NUM_2, config->adc2_pattern[i].channel, config->adc2_pattern[i].atten);
  109. }
  110. }
  111. /* If enable digtal controller, adc xpd should always on. */
  112. adc_power_acquire();
  113. ADC_ENTER_CRITICAL();
  114. adc_hal_digi_controller_config(config);
  115. ADC_EXIT_CRITICAL();
  116. return ESP_OK;
  117. }
  118. esp_err_t adc_arbiter_config(adc_unit_t adc_unit, adc_arbiter_t *config)
  119. {
  120. if (adc_unit & ADC_UNIT_1) {
  121. return ESP_ERR_NOT_SUPPORTED;
  122. }
  123. ADC_ENTER_CRITICAL();
  124. adc_hal_arbiter_config(config);
  125. ADC_EXIT_CRITICAL();
  126. return ESP_OK;
  127. }
  128. /**
  129. * @brief Set ADC module controller.
  130. * There are five SAR ADC controllers:
  131. * Two digital controller: Continuous conversion mode (DMA). High performance with multiple channel scan modes;
  132. * Two RTC controller: Single conversion modes (Polling). For low power purpose working during deep sleep;
  133. * the other is dedicated for Power detect (PWDET / PKDET), Only support ADC2.
  134. *
  135. * @note Only ADC2 support arbiter to switch controllers automatically. Access to the ADC is based on the priority of the controller.
  136. * @note For ADC1, Controller access is mutually exclusive.
  137. *
  138. * @param adc_unit ADC unit.
  139. * @param ctrl ADC controller, Refer to `adc_ll_controller_t`.
  140. *
  141. * @return
  142. * - ESP_OK Success
  143. */
  144. esp_err_t adc_set_controller(adc_unit_t adc_unit, adc_ll_controller_t ctrl)
  145. {
  146. adc_arbiter_t config = {0};
  147. adc_arbiter_t cfg = ADC_ARBITER_CONFIG_DEFAULT();
  148. if (adc_unit & ADC_UNIT_1) {
  149. adc_hal_set_controller(ADC_NUM_1, ctrl);
  150. }
  151. if (adc_unit & ADC_UNIT_2) {
  152. adc_hal_set_controller(ADC_NUM_2, ctrl);
  153. switch (ctrl) {
  154. case ADC2_CTRL_FORCE_PWDET:
  155. config.pwdet_pri = 2;
  156. config.mode = ADC_ARB_MODE_SHIELD;
  157. adc_hal_arbiter_config(&config);
  158. adc_hal_set_controller(ADC_NUM_2, ADC2_CTRL_PWDET);
  159. break;
  160. case ADC2_CTRL_FORCE_RTC:
  161. config.rtc_pri = 2;
  162. config.mode = ADC_ARB_MODE_SHIELD;
  163. adc_hal_arbiter_config(&config);
  164. adc_hal_set_controller(ADC_NUM_2, ADC_CTRL_RTC);
  165. break;
  166. case ADC2_CTRL_FORCE_ULP:
  167. config.rtc_pri = 2;
  168. config.mode = ADC_ARB_MODE_SHIELD;
  169. adc_hal_arbiter_config(&config);
  170. adc_hal_set_controller(ADC_NUM_2, ADC_CTRL_ULP);
  171. break;
  172. case ADC2_CTRL_FORCE_DIG:
  173. config.dig_pri = 2;
  174. config.mode = ADC_ARB_MODE_SHIELD;
  175. adc_hal_arbiter_config(&config);
  176. adc_hal_set_controller(ADC_NUM_2, ADC_CTRL_DIG);
  177. break;
  178. default:
  179. adc_hal_arbiter_config(&cfg);
  180. break;
  181. }
  182. }
  183. return ESP_OK;
  184. }
  185. esp_err_t adc_digi_start(void)
  186. {
  187. #ifdef CONFIG_PM_ENABLE
  188. ADC_CHECK((s_adc_digi_arbiter_lock), "Should start after call `adc_digi_controller_config`", ESP_FAIL);
  189. esp_pm_lock_acquire(s_adc_digi_arbiter_lock);
  190. #endif
  191. ADC_ENTER_CRITICAL();
  192. adc_hal_digi_enable();
  193. ADC_EXIT_CRITICAL();
  194. return ESP_OK;
  195. }
  196. esp_err_t adc_digi_stop(void)
  197. {
  198. #ifdef CONFIG_PM_ENABLE
  199. if (s_adc_digi_arbiter_lock) {
  200. esp_pm_lock_release(s_adc_digi_arbiter_lock);
  201. }
  202. #endif
  203. ADC_ENTER_CRITICAL();
  204. adc_hal_digi_disable();
  205. ADC_EXIT_CRITICAL();
  206. return ESP_OK;
  207. }
  208. /**
  209. * @brief Reset FSM of adc digital controller.
  210. *
  211. * @return
  212. * - ESP_OK Success
  213. */
  214. esp_err_t adc_digi_reset(void)
  215. {
  216. ADC_ENTER_CRITICAL();
  217. adc_hal_digi_reset();
  218. adc_hal_digi_clear_pattern_table(ADC_NUM_1);
  219. adc_hal_digi_clear_pattern_table(ADC_NUM_2);
  220. ADC_EXIT_CRITICAL();
  221. return ESP_OK;
  222. }
  223. /*************************************/
  224. /* Digital controller filter setting */
  225. /*************************************/
  226. esp_err_t adc_digi_filter_reset(adc_digi_filter_idx_t idx)
  227. {
  228. ADC_ENTER_CRITICAL();
  229. if (idx == ADC_DIGI_FILTER_IDX0) {
  230. adc_hal_digi_filter_reset(ADC_NUM_1);
  231. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  232. adc_hal_digi_filter_reset(ADC_NUM_2);
  233. }
  234. ADC_EXIT_CRITICAL();
  235. return ESP_OK;
  236. }
  237. esp_err_t adc_digi_filter_set_config(adc_digi_filter_idx_t idx, adc_digi_filter_t *config)
  238. {
  239. ADC_ENTER_CRITICAL();
  240. if (idx == ADC_DIGI_FILTER_IDX0) {
  241. adc_hal_digi_filter_set_factor(ADC_NUM_1, config->mode);
  242. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  243. adc_hal_digi_filter_set_factor(ADC_NUM_2, config->mode);
  244. }
  245. ADC_EXIT_CRITICAL();
  246. return ESP_OK;
  247. }
  248. esp_err_t adc_digi_filter_get_config(adc_digi_filter_idx_t idx, adc_digi_filter_t *config)
  249. {
  250. ADC_ENTER_CRITICAL();
  251. if (idx == ADC_DIGI_FILTER_IDX0) {
  252. config->adc_unit = ADC_UNIT_1;
  253. config->channel = ADC_CHANNEL_MAX;
  254. adc_hal_digi_filter_get_factor(ADC_NUM_1, &config->mode);
  255. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  256. config->adc_unit = ADC_UNIT_2;
  257. config->channel = ADC_CHANNEL_MAX;
  258. adc_hal_digi_filter_get_factor(ADC_NUM_2, &config->mode);
  259. }
  260. ADC_EXIT_CRITICAL();
  261. return ESP_OK;
  262. }
  263. esp_err_t adc_digi_filter_enable(adc_digi_filter_idx_t idx, bool enable)
  264. {
  265. ADC_ENTER_CRITICAL();
  266. if (idx == ADC_DIGI_FILTER_IDX0) {
  267. adc_hal_digi_filter_enable(ADC_NUM_1, enable);
  268. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  269. adc_hal_digi_filter_enable(ADC_NUM_2, enable);
  270. }
  271. ADC_EXIT_CRITICAL();
  272. return ESP_OK;
  273. }
  274. /**
  275. * @brief Get the filtered data of adc digital controller filter. For debug.
  276. * The data after each measurement and filtering is updated to the DMA by the digital controller. But it can also be obtained manually through this API.
  277. *
  278. * @note For ESP32S2, The filter will filter all the enabled channel data of the each ADC unit at the same time.
  279. * @param idx Filter index.
  280. * @return Filtered data. if <0, the read data invalid.
  281. */
  282. int adc_digi_filter_read_data(adc_digi_filter_idx_t idx)
  283. {
  284. if (idx == ADC_DIGI_FILTER_IDX0) {
  285. return adc_hal_digi_filter_read_data(ADC_NUM_1);
  286. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  287. return adc_hal_digi_filter_read_data(ADC_NUM_2);
  288. } else {
  289. return -1;
  290. }
  291. }
  292. /**************************************/
  293. /* Digital controller monitor setting */
  294. /**************************************/
  295. esp_err_t adc_digi_monitor_set_config(adc_digi_monitor_idx_t idx, adc_digi_monitor_t *config)
  296. {
  297. ADC_ENTER_CRITICAL();
  298. if (idx == ADC_DIGI_MONITOR_IDX0) {
  299. adc_hal_digi_monitor_config(ADC_NUM_1, config);
  300. } else if (idx == ADC_DIGI_MONITOR_IDX1) {
  301. adc_hal_digi_monitor_config(ADC_NUM_2, config);
  302. }
  303. ADC_EXIT_CRITICAL();
  304. return ESP_OK;
  305. }
  306. esp_err_t adc_digi_monitor_enable(adc_digi_monitor_idx_t idx, bool enable)
  307. {
  308. ADC_ENTER_CRITICAL();
  309. if (idx == ADC_DIGI_MONITOR_IDX0) {
  310. adc_hal_digi_monitor_enable(ADC_NUM_1, enable);
  311. } else if (idx == ADC_DIGI_MONITOR_IDX1) {
  312. adc_hal_digi_monitor_enable(ADC_NUM_2, enable);
  313. }
  314. ADC_EXIT_CRITICAL();
  315. return ESP_OK;
  316. }
  317. /**************************************/
  318. /* Digital controller intr setting */
  319. /**************************************/
  320. esp_err_t adc_digi_intr_enable(adc_unit_t adc_unit, adc_digi_intr_t intr_mask)
  321. {
  322. ADC_ENTER_CRITICAL();
  323. if (adc_unit & ADC_UNIT_1) {
  324. adc_hal_digi_intr_enable(ADC_NUM_1, intr_mask);
  325. }
  326. if (adc_unit & ADC_UNIT_2) {
  327. adc_hal_digi_intr_enable(ADC_NUM_2, intr_mask);
  328. }
  329. ADC_EXIT_CRITICAL();
  330. return ESP_OK;
  331. }
  332. esp_err_t adc_digi_intr_disable(adc_unit_t adc_unit, adc_digi_intr_t intr_mask)
  333. {
  334. ADC_ENTER_CRITICAL();
  335. if (adc_unit & ADC_UNIT_1) {
  336. adc_hal_digi_intr_disable(ADC_NUM_1, intr_mask);
  337. }
  338. if (adc_unit & ADC_UNIT_2) {
  339. adc_hal_digi_intr_disable(ADC_NUM_2, intr_mask);
  340. }
  341. ADC_EXIT_CRITICAL();
  342. return ESP_OK;
  343. }
  344. esp_err_t adc_digi_intr_clear(adc_unit_t adc_unit, adc_digi_intr_t intr_mask)
  345. {
  346. ADC_ENTER_CRITICAL();
  347. if (adc_unit & ADC_UNIT_1) {
  348. adc_hal_digi_intr_clear(ADC_NUM_1, intr_mask);
  349. }
  350. if (adc_unit & ADC_UNIT_2) {
  351. adc_hal_digi_intr_clear(ADC_NUM_2, intr_mask);
  352. }
  353. ADC_EXIT_CRITICAL();
  354. return ESP_OK;
  355. }
  356. uint32_t adc_digi_intr_get_status(adc_unit_t adc_unit)
  357. {
  358. uint32_t ret = 0;
  359. ADC_ENTER_CRITICAL();
  360. if (adc_unit & ADC_UNIT_1) {
  361. ret = adc_hal_digi_get_intr_status(ADC_NUM_1);
  362. }
  363. if (adc_unit & ADC_UNIT_2) {
  364. ret = adc_hal_digi_get_intr_status(ADC_NUM_2);
  365. }
  366. ADC_EXIT_CRITICAL();
  367. return ret;
  368. }
  369. static uint8_t s_isr_registered = 0;
  370. static intr_handle_t s_adc_isr_handle = NULL;
  371. esp_err_t adc_digi_isr_register(void (*fn)(void *), void *arg, int intr_alloc_flags)
  372. {
  373. ADC_CHECK((fn != NULL), "Parameter error", ESP_ERR_INVALID_ARG);
  374. ADC_CHECK(s_isr_registered == 0, "ADC ISR have installed, can not install again", ESP_FAIL);
  375. esp_err_t ret = esp_intr_alloc(ETS_APB_ADC_INTR_SOURCE, intr_alloc_flags, fn, arg, &s_adc_isr_handle);
  376. if (ret == ESP_OK) {
  377. s_isr_registered = 1;
  378. }
  379. return ret;
  380. }
  381. esp_err_t adc_digi_isr_deregister(void)
  382. {
  383. esp_err_t ret = ESP_FAIL;
  384. if (s_isr_registered) {
  385. ret = esp_intr_free(s_adc_isr_handle);
  386. if (ret == ESP_OK) {
  387. s_isr_registered = 0;
  388. }
  389. }
  390. return ret;
  391. }
  392. /*---------------------------------------------------------------
  393. RTC controller setting
  394. ---------------------------------------------------------------*/
  395. /*---------------------------------------------------------------
  396. Calibration
  397. ---------------------------------------------------------------*/
  398. static uint16_t s_adc_cali_param[ADC_NUM_MAX][ADC_ATTEN_MAX] = { {0}, {0} };
  399. //NOTE: according to calibration version, different types of lock may be taken during the process:
  400. // 1. Semaphore when reading efuse
  401. // 2. Spinlock when actually doing ADC calibration
  402. //This function shoudn't be called inside critical section or ISR
  403. uint32_t adc_get_calibration_offset(adc_ll_num_t adc_n, adc_channel_t channel, adc_atten_t atten, bool no_cal)
  404. {
  405. #ifdef CONFIG_IDF_ENV_FPGA
  406. return 0;
  407. #endif
  408. if (s_adc_cali_param[adc_n][atten]) {
  409. ESP_LOGV(ADC_TAG, "Use calibrated val ADC%d atten=%d: %04X", adc_n, atten, s_adc_cali_param[adc_n][atten]);
  410. return (uint32_t)s_adc_cali_param[adc_n][atten];
  411. }
  412. if (no_cal) {
  413. return 0; //indicating failure
  414. }
  415. uint32_t dout = 0;
  416. // check if we can fetch the values from eFuse.
  417. int version = esp_efuse_rtc_table_read_calib_version();
  418. if (version == 2) {
  419. int tag = esp_efuse_rtc_table_get_tag(version, adc_n + 1, atten, RTCCALIB_V2_PARAM_VINIT);
  420. dout = esp_efuse_rtc_table_get_parsed_efuse_value(tag, false);
  421. } else {
  422. adc_power_acquire();
  423. ADC_ENTER_CRITICAL();
  424. const bool internal_gnd = true;
  425. dout = adc_hal_self_calibration(adc_n, channel, atten, internal_gnd);
  426. ADC_EXIT_CRITICAL();
  427. adc_power_release();
  428. }
  429. ESP_LOGD(ADC_TAG, "Calib(V%d) ADC%d atten=%d: %04X", version, adc_n, atten, dout);
  430. s_adc_cali_param[adc_n][atten] = (uint16_t)dout;
  431. return dout;
  432. }
  433. esp_err_t adc_cal_offset(adc_ll_num_t adc_n, adc_channel_t channel, adc_atten_t atten)
  434. {
  435. adc_hal_calibration_init(adc_n);
  436. uint32_t cal_val = adc_get_calibration_offset(adc_n, channel, atten, false);
  437. ADC_ENTER_CRITICAL();
  438. adc_hal_set_calibration_param(adc_n, cal_val);
  439. ADC_EXIT_CRITICAL();
  440. return ESP_OK;
  441. }