rtc_module.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835
  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 <esp_types.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. #include "esp_log.h"
  18. #include "soc/rtc_periph.h"
  19. #include "soc/sens_periph.h"
  20. #include "soc/syscon_periph.h"
  21. #include "soc/rtc.h"
  22. #include "soc/periph_defs.h"
  23. #include "rtc_io.h"
  24. #include "touch_pad.h"
  25. #include "adc.h"
  26. #include "dac.h"
  27. #include "freertos/FreeRTOS.h"
  28. #include "freertos/xtensa_api.h"
  29. #include "freertos/semphr.h"
  30. #include "freertos/timers.h"
  31. #include "esp_intr_alloc.h"
  32. #include "sys/lock.h"
  33. #include "driver/rtc_cntl.h"
  34. #include "driver/gpio.h"
  35. #include "driver/rtc_io.h"
  36. #include "adc1_i2s_private.h"
  37. #include "sdkconfig.h"
  38. #if CONFIG_IDF_TARGET_ESP32
  39. #include "esp32/rom/ets_sys.h"
  40. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  41. #include "esp32s2beta/rom/ets_sys.h"
  42. #endif
  43. #include "hal/dac_hal.h"
  44. #ifndef NDEBUG
  45. // Enable built-in checks in queue.h in debug builds
  46. #define INVARIANTS
  47. #endif
  48. #include "sys/queue.h"
  49. #define ADC_FSM_RSTB_WAIT_DEFAULT (8)
  50. #define ADC_FSM_START_WAIT_DEFAULT (5)
  51. #define ADC_FSM_STANDBY_WAIT_DEFAULT (100)
  52. #define ADC_FSM_TIME_KEEP (-1)
  53. #define ADC_MAX_MEAS_NUM_DEFAULT (255)
  54. #define ADC_MEAS_NUM_LIM_DEFAULT (1)
  55. #define SAR_ADC_CLK_DIV_DEFAULT (2)
  56. #define ADC_PATT_LEN_MAX (16)
  57. #define TOUCH_PAD_FILTER_FACTOR_DEFAULT (4) // IIR filter coefficient.
  58. #define TOUCH_PAD_SHIFT_DEFAULT (4) // Increase computing accuracy.
  59. #define TOUCH_PAD_SHIFT_ROUND_DEFAULT (8) // ROUND = 2^(n-1); rounding off for fractional.
  60. static const char *RTC_MODULE_TAG = "RTC_MODULE";
  61. #define RTC_MODULE_CHECK(a, str, ret_val) if (!(a)) { \
  62. ESP_LOGE(RTC_MODULE_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
  63. return (ret_val); \
  64. }
  65. #define RTC_RES_CHECK(res, ret_val) if ( (a) != ESP_OK) { \
  66. ESP_LOGE(RTC_MODULE_TAG,"%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__); \
  67. return (ret_val); \
  68. }
  69. #define ADC_CHECK_UNIT(unit) RTC_MODULE_CHECK(adc_unit < ADC_UNIT_2, "ADC unit error, only support ADC1 for now", ESP_ERR_INVALID_ARG)
  70. #define ADC1_CHECK_FUNCTION_RET(fun_ret) if(fun_ret!=ESP_OK){\
  71. ESP_LOGE(RTC_MODULE_TAG,"%s:%d\n",__FUNCTION__,__LINE__);\
  72. return ESP_FAIL;\
  73. }
  74. #define ADC2_CHECK_FUNCTION_RET(fun_ret) do { if(fun_ret!=ESP_OK){\
  75. ESP_LOGE(RTC_MODULE_TAG,"%s:%d\n",__FUNCTION__,__LINE__);\
  76. return ESP_FAIL;\
  77. } }while (0)
  78. portMUX_TYPE rtc_spinlock = portMUX_INITIALIZER_UNLOCKED;
  79. #if CONFIG_IDF_TARGET_ESP32
  80. static SemaphoreHandle_t rtc_touch_mux = NULL;
  81. #endif
  82. /*
  83. In ADC2, there're two locks used for different cases:
  84. 1. lock shared with app and WIFI:
  85. when wifi using the ADC2, we assume it will never stop,
  86. so app checks the lock and returns immediately if failed.
  87. 2. lock shared between tasks:
  88. when several tasks sharing the ADC2, we want to guarantee
  89. all the requests will be handled.
  90. Since conversions are short (about 31us), app returns the lock very soon,
  91. we use a spinlock to stand there waiting to do conversions one by one.
  92. adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock.
  93. */
  94. //prevent ADC2 being used by wifi and other tasks at the same time.
  95. static _lock_t adc2_wifi_lock;
  96. //prevent ADC2 being used by tasks (regardless of WIFI)
  97. portMUX_TYPE adc2_spinlock = portMUX_INITIALIZER_UNLOCKED;
  98. //prevent ADC1 being used by I2S dma and other tasks at the same time.
  99. static _lock_t adc1_i2s_lock;
  100. #if CONFIG_IDF_TARGET_ESP32
  101. typedef struct {
  102. TimerHandle_t timer;
  103. uint16_t filtered_val[TOUCH_PAD_MAX];
  104. uint16_t raw_val[TOUCH_PAD_MAX];
  105. uint32_t filter_period;
  106. uint32_t period;
  107. bool enable;
  108. } touch_pad_filter_t;
  109. static touch_pad_filter_t *s_touch_pad_filter = NULL;
  110. // check if touch pad be inited.
  111. static uint16_t s_touch_pad_init_bit = 0x0000;
  112. static filter_cb_t s_filter_cb = NULL;
  113. #endif
  114. typedef enum {
  115. ADC_CTRL_RTC = 0,
  116. ADC_CTRL_ULP = 1,
  117. ADC_CTRL_DIG = 2,
  118. ADC2_CTRL_PWDET = 3,
  119. } adc_controller_t ;
  120. static const char TAG[] = "adc";
  121. static inline void adc1_hall_enable(bool enable);
  122. #if CONFIG_IDF_TARGET_ESP32
  123. /*---------------------------------------------------------------
  124. Touch Pad
  125. ---------------------------------------------------------------*/
  126. //Some register bits of touch sensor 8 and 9 are mismatched, we need to swap the bits.
  127. #define BITSWAP(data, n, m) (((data >> n) & 0x1) == ((data >> m) & 0x1) ? (data) : ((data) ^ ((0x1 <<n) | (0x1 << m))))
  128. #define TOUCH_BITS_SWAP(v) BITSWAP(v, TOUCH_PAD_NUM8, TOUCH_PAD_NUM9)
  129. static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, touch_fsm_mode_t mode);
  130. //Some registers of touch sensor 8 and 9 are mismatched, we need to swap register index
  131. inline static touch_pad_t touch_pad_num_wrap(touch_pad_t touch_num)
  132. {
  133. if (touch_num == TOUCH_PAD_NUM8) {
  134. return TOUCH_PAD_NUM9;
  135. } else if (touch_num == TOUCH_PAD_NUM9) {
  136. return TOUCH_PAD_NUM8;
  137. }
  138. return touch_num;
  139. }
  140. esp_err_t touch_pad_isr_handler_register(void (*fn)(void *), void *arg, int no_use, intr_handle_t *handle_no_use)
  141. {
  142. RTC_MODULE_CHECK(fn, "Touch_Pad ISR null", ESP_ERR_INVALID_ARG);
  143. #if CONFIG_IDF_TARGET_ESP32
  144. return rtc_isr_register(fn, arg, RTC_CNTL_TOUCH_INT_ST_M);
  145. #else
  146. return ESP_FAIL;
  147. #endif
  148. }
  149. esp_err_t touch_pad_isr_register(intr_handler_t fn, void* arg)
  150. {
  151. RTC_MODULE_CHECK(fn, "Touch_Pad ISR null", ESP_ERR_INVALID_ARG);
  152. #if CONFIG_IDF_TARGET_ESP32
  153. return rtc_isr_register(fn, arg, RTC_CNTL_TOUCH_INT_ST_M);
  154. #else
  155. return ESP_FAIL;
  156. #endif
  157. }
  158. esp_err_t touch_pad_isr_deregister(intr_handler_t fn, void *arg)
  159. {
  160. return rtc_isr_deregister(fn, arg);
  161. }
  162. static esp_err_t touch_pad_get_io_num(touch_pad_t touch_num, gpio_num_t *gpio_num)
  163. {
  164. switch (touch_num) {
  165. case TOUCH_PAD_NUM0:
  166. *gpio_num = TOUCH_PAD_NUM0_GPIO_NUM;
  167. break;
  168. case TOUCH_PAD_NUM1:
  169. *gpio_num = TOUCH_PAD_NUM1_GPIO_NUM;
  170. break;
  171. case TOUCH_PAD_NUM2:
  172. *gpio_num = TOUCH_PAD_NUM2_GPIO_NUM;
  173. break;
  174. case TOUCH_PAD_NUM3:
  175. *gpio_num = TOUCH_PAD_NUM3_GPIO_NUM;
  176. break;
  177. case TOUCH_PAD_NUM4:
  178. *gpio_num = TOUCH_PAD_NUM4_GPIO_NUM;
  179. break;
  180. case TOUCH_PAD_NUM5:
  181. *gpio_num = TOUCH_PAD_NUM5_GPIO_NUM;
  182. break;
  183. case TOUCH_PAD_NUM6:
  184. *gpio_num = TOUCH_PAD_NUM6_GPIO_NUM;
  185. break;
  186. case TOUCH_PAD_NUM7:
  187. *gpio_num = TOUCH_PAD_NUM7_GPIO_NUM;
  188. break;
  189. case TOUCH_PAD_NUM8:
  190. *gpio_num = TOUCH_PAD_NUM8_GPIO_NUM;
  191. break;
  192. case TOUCH_PAD_NUM9:
  193. *gpio_num = TOUCH_PAD_NUM9_GPIO_NUM;
  194. break;
  195. default:
  196. return ESP_ERR_INVALID_ARG;
  197. }
  198. return ESP_OK;
  199. }
  200. static uint32_t _touch_filter_iir(uint32_t in_now, uint32_t out_last, uint32_t k)
  201. {
  202. if (k == 0) {
  203. return in_now;
  204. } else {
  205. uint32_t out_now = (in_now + (k - 1) * out_last) / k;
  206. return out_now;
  207. }
  208. }
  209. esp_err_t touch_pad_set_filter_read_cb(filter_cb_t read_cb)
  210. {
  211. s_filter_cb = read_cb;
  212. return ESP_OK;
  213. }
  214. static void touch_pad_filter_cb(void *arg)
  215. {
  216. static uint32_t s_filtered_temp[TOUCH_PAD_MAX] = {0};
  217. if (s_touch_pad_filter == NULL || rtc_touch_mux == NULL) {
  218. return;
  219. }
  220. uint16_t val = 0;
  221. touch_fsm_mode_t mode;
  222. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  223. touch_pad_get_fsm_mode(&mode);
  224. for (int i = 0; i < TOUCH_PAD_MAX; i++) {
  225. if ((s_touch_pad_init_bit >> i) & 0x1) {
  226. _touch_pad_read(i, &val, mode);
  227. s_touch_pad_filter->raw_val[i] = val;
  228. s_filtered_temp[i] = s_filtered_temp[i] == 0 ? ((uint32_t)val << TOUCH_PAD_SHIFT_DEFAULT) : s_filtered_temp[i];
  229. s_filtered_temp[i] = _touch_filter_iir((val << TOUCH_PAD_SHIFT_DEFAULT),
  230. s_filtered_temp[i], TOUCH_PAD_FILTER_FACTOR_DEFAULT);
  231. s_touch_pad_filter->filtered_val[i] = (s_filtered_temp[i] + TOUCH_PAD_SHIFT_ROUND_DEFAULT) >> TOUCH_PAD_SHIFT_DEFAULT;
  232. }
  233. }
  234. xTimerReset(s_touch_pad_filter->timer, portMAX_DELAY);
  235. xSemaphoreGive(rtc_touch_mux);
  236. if(s_filter_cb != NULL) {
  237. //return the raw data and filtered data.
  238. s_filter_cb(s_touch_pad_filter->raw_val, s_touch_pad_filter->filtered_val);
  239. }
  240. }
  241. esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_cycle)
  242. {
  243. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  244. portENTER_CRITICAL(&rtc_spinlock);
  245. //touch sensor sleep cycle Time = sleep_cycle / RTC_SLOW_CLK( can be 150k or 32k depending on the options)
  246. SENS.sar_touch_ctrl2.touch_sleep_cycles = sleep_cycle;
  247. //touch sensor measure time= meas_cycle / 8Mhz
  248. SENS.sar_touch_ctrl1.touch_meas_delay = meas_cycle;
  249. //the waiting cycles (in 8MHz) between TOUCH_START and TOUCH_XPD
  250. SENS.sar_touch_ctrl1.touch_xpd_wait = TOUCH_PAD_MEASURE_WAIT_DEFAULT;
  251. portEXIT_CRITICAL(&rtc_spinlock);
  252. xSemaphoreGive(rtc_touch_mux);
  253. return ESP_OK;
  254. }
  255. esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_cycle)
  256. {
  257. portENTER_CRITICAL(&rtc_spinlock);
  258. if (sleep_cycle) {
  259. *sleep_cycle = SENS.sar_touch_ctrl2.touch_sleep_cycles;
  260. }
  261. if (meas_cycle) {
  262. *meas_cycle = SENS.sar_touch_ctrl1.touch_meas_delay;
  263. }
  264. portEXIT_CRITICAL(&rtc_spinlock);
  265. return ESP_OK;
  266. }
  267. esp_err_t touch_pad_set_voltage(touch_high_volt_t refh, touch_low_volt_t refl, touch_volt_atten_t atten)
  268. {
  269. RTC_MODULE_CHECK(((refh < TOUCH_HVOLT_MAX) && (refh >= (int )TOUCH_HVOLT_KEEP)), "touch refh error",
  270. ESP_ERR_INVALID_ARG);
  271. RTC_MODULE_CHECK(((refl < TOUCH_LVOLT_MAX) && (refh >= (int )TOUCH_LVOLT_KEEP)), "touch refl error",
  272. ESP_ERR_INVALID_ARG);
  273. RTC_MODULE_CHECK(((atten < TOUCH_HVOLT_ATTEN_MAX) && (refh >= (int )TOUCH_HVOLT_ATTEN_KEEP)), "touch atten error",
  274. ESP_ERR_INVALID_ARG);
  275. #if CONFIG_IDF_TARGET_ESP32
  276. portENTER_CRITICAL(&rtc_spinlock);
  277. if (refh > TOUCH_HVOLT_KEEP) {
  278. RTCIO.touch_cfg.drefh = refh;
  279. }
  280. if (refl > TOUCH_LVOLT_KEEP) {
  281. RTCIO.touch_cfg.drefl = refl;
  282. }
  283. if (atten > TOUCH_HVOLT_ATTEN_KEEP) {
  284. RTCIO.touch_cfg.drange = atten;
  285. }
  286. portEXIT_CRITICAL(&rtc_spinlock);
  287. #endif
  288. return ESP_OK;
  289. }
  290. esp_err_t touch_pad_get_voltage(touch_high_volt_t *refh, touch_low_volt_t *refl, touch_volt_atten_t *atten)
  291. {
  292. #if CONFIG_IDF_TARGET_ESP32
  293. portENTER_CRITICAL(&rtc_spinlock);
  294. if (refh) {
  295. *refh = RTCIO.touch_cfg.drefh;
  296. }
  297. if (refl) {
  298. *refl = RTCIO.touch_cfg.drefl;
  299. }
  300. if (atten) {
  301. *atten = RTCIO.touch_cfg.drange;
  302. }
  303. portEXIT_CRITICAL(&rtc_spinlock);
  304. #endif
  305. return ESP_OK;
  306. }
  307. esp_err_t touch_pad_set_cnt_mode(touch_pad_t touch_num, touch_cnt_slope_t slope, touch_tie_opt_t opt)
  308. {
  309. RTC_MODULE_CHECK((slope < TOUCH_PAD_SLOPE_MAX), "touch slope error", ESP_ERR_INVALID_ARG);
  310. RTC_MODULE_CHECK((opt < TOUCH_PAD_TIE_OPT_MAX), "touch opt error", ESP_ERR_INVALID_ARG);
  311. touch_pad_t touch_pad_wrap = touch_pad_num_wrap(touch_num);
  312. portENTER_CRITICAL(&rtc_spinlock);
  313. RTCIO.touch_pad[touch_pad_wrap].tie_opt = opt;
  314. RTCIO.touch_pad[touch_num].dac = slope;
  315. portEXIT_CRITICAL(&rtc_spinlock);
  316. return ESP_OK;
  317. }
  318. esp_err_t touch_pad_get_cnt_mode(touch_pad_t touch_num, touch_cnt_slope_t *slope, touch_tie_opt_t *opt)
  319. {
  320. RTC_MODULE_CHECK((touch_num < TOUCH_PAD_MAX), "touch IO error", ESP_ERR_INVALID_ARG);
  321. touch_pad_t touch_pad_wrap = touch_pad_num_wrap(touch_num);
  322. portENTER_CRITICAL(&rtc_spinlock);
  323. if(opt) {
  324. *opt = RTCIO.touch_pad[touch_pad_wrap].tie_opt;
  325. }
  326. if(slope) {
  327. *slope = RTCIO.touch_pad[touch_num].dac;
  328. }
  329. portEXIT_CRITICAL(&rtc_spinlock);
  330. return ESP_OK;
  331. }
  332. esp_err_t touch_pad_io_init(touch_pad_t touch_num)
  333. {
  334. RTC_MODULE_CHECK((touch_num < TOUCH_PAD_MAX), "touch IO error", ESP_ERR_INVALID_ARG);
  335. gpio_num_t gpio_num = GPIO_NUM_0;
  336. touch_pad_get_io_num(touch_num, &gpio_num);
  337. rtc_gpio_init(gpio_num);
  338. rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED);
  339. rtc_gpio_pulldown_dis(gpio_num);
  340. rtc_gpio_pullup_dis(gpio_num);
  341. return ESP_OK;
  342. }
  343. esp_err_t touch_pad_set_fsm_mode(touch_fsm_mode_t mode)
  344. {
  345. RTC_MODULE_CHECK((mode < TOUCH_FSM_MODE_MAX), "touch fsm mode error", ESP_ERR_INVALID_ARG);
  346. portENTER_CRITICAL(&rtc_spinlock);
  347. SENS.sar_touch_ctrl2.touch_start_en = 0;
  348. SENS.sar_touch_ctrl2.touch_start_force = mode;
  349. RTCCNTL.state0.touch_slp_timer_en = (mode == TOUCH_FSM_MODE_TIMER ? 1 : 0);
  350. portEXIT_CRITICAL(&rtc_spinlock);
  351. return ESP_OK;
  352. }
  353. esp_err_t touch_pad_get_fsm_mode(touch_fsm_mode_t *mode)
  354. {
  355. if (mode) {
  356. *mode = SENS.sar_touch_ctrl2.touch_start_force;
  357. }
  358. return ESP_OK;
  359. }
  360. esp_err_t touch_pad_sw_start(void)
  361. {
  362. portENTER_CRITICAL(&rtc_spinlock);
  363. SENS.sar_touch_ctrl2.touch_start_en = 0;
  364. SENS.sar_touch_ctrl2.touch_start_en = 1;
  365. portEXIT_CRITICAL(&rtc_spinlock);
  366. return ESP_OK;
  367. }
  368. esp_err_t touch_pad_set_thresh(touch_pad_t touch_num, uint16_t threshold)
  369. {
  370. RTC_MODULE_CHECK((touch_num < TOUCH_PAD_MAX), "touch IO error", ESP_ERR_INVALID_ARG);
  371. touch_pad_t tp_wrap = touch_pad_num_wrap(touch_num);
  372. portENTER_CRITICAL(&rtc_spinlock);
  373. if (tp_wrap & 0x1) {
  374. SENS.touch_thresh[tp_wrap / 2].l_thresh = threshold;
  375. } else {
  376. SENS.touch_thresh[tp_wrap / 2].h_thresh = threshold;
  377. }
  378. portEXIT_CRITICAL(&rtc_spinlock);
  379. return ESP_OK;
  380. }
  381. esp_err_t touch_pad_get_thresh(touch_pad_t touch_num, uint16_t *threshold)
  382. {
  383. RTC_MODULE_CHECK((touch_num < TOUCH_PAD_MAX), "touch IO error", ESP_ERR_INVALID_ARG);
  384. touch_pad_t tp_wrap = touch_pad_num_wrap(touch_num);
  385. if (threshold) {
  386. *threshold = (tp_wrap & 0x1 )? \
  387. SENS.touch_thresh[tp_wrap / 2].l_thresh : \
  388. SENS.touch_thresh[tp_wrap / 2].h_thresh;
  389. }
  390. return ESP_OK;
  391. }
  392. esp_err_t touch_pad_set_trigger_mode(touch_trigger_mode_t mode)
  393. {
  394. RTC_MODULE_CHECK((mode < TOUCH_TRIGGER_MAX), "touch trigger mode error", ESP_ERR_INVALID_ARG);
  395. portENTER_CRITICAL(&rtc_spinlock);
  396. SENS.sar_touch_ctrl1.touch_out_sel = mode;
  397. portEXIT_CRITICAL(&rtc_spinlock);
  398. return ESP_OK;
  399. }
  400. esp_err_t touch_pad_get_trigger_mode(touch_trigger_mode_t *mode)
  401. {
  402. if (mode) {
  403. *mode = SENS.sar_touch_ctrl1.touch_out_sel;
  404. }
  405. return ESP_OK;
  406. }
  407. esp_err_t touch_pad_set_trigger_source(touch_trigger_src_t src)
  408. {
  409. RTC_MODULE_CHECK((src < TOUCH_TRIGGER_SOURCE_MAX), "touch trigger source error", ESP_ERR_INVALID_ARG);
  410. portENTER_CRITICAL(&rtc_spinlock);
  411. SENS.sar_touch_ctrl1.touch_out_1en = src;
  412. portEXIT_CRITICAL(&rtc_spinlock);
  413. return ESP_OK;
  414. }
  415. esp_err_t touch_pad_get_trigger_source(touch_trigger_src_t *src)
  416. {
  417. if (src) {
  418. *src = SENS.sar_touch_ctrl1.touch_out_1en;
  419. }
  420. return ESP_OK;
  421. }
  422. esp_err_t touch_pad_set_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask)
  423. {
  424. RTC_MODULE_CHECK((set1_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch set1 bitmask error", ESP_ERR_INVALID_ARG);
  425. RTC_MODULE_CHECK((set2_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch set2 bitmask error", ESP_ERR_INVALID_ARG);
  426. RTC_MODULE_CHECK((en_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch work_en bitmask error", ESP_ERR_INVALID_ARG);
  427. portENTER_CRITICAL(&rtc_spinlock);
  428. SENS.sar_touch_enable.touch_pad_outen1 |= TOUCH_BITS_SWAP(set1_mask);
  429. SENS.sar_touch_enable.touch_pad_outen2 |= TOUCH_BITS_SWAP(set2_mask);
  430. SENS.sar_touch_enable.touch_pad_worken |= TOUCH_BITS_SWAP(en_mask);
  431. portEXIT_CRITICAL(&rtc_spinlock);
  432. return ESP_OK;
  433. }
  434. esp_err_t touch_pad_get_group_mask(uint16_t *set1_mask, uint16_t *set2_mask, uint16_t *en_mask)
  435. {
  436. portENTER_CRITICAL(&rtc_spinlock);
  437. if (set1_mask) {
  438. *set1_mask = TOUCH_BITS_SWAP(SENS.sar_touch_enable.touch_pad_outen1);
  439. }
  440. if (set2_mask) {
  441. *set2_mask = TOUCH_BITS_SWAP(SENS.sar_touch_enable.touch_pad_outen2);
  442. }
  443. if (en_mask) {
  444. *en_mask = TOUCH_BITS_SWAP(SENS.sar_touch_enable.touch_pad_worken);
  445. }
  446. portEXIT_CRITICAL(&rtc_spinlock);
  447. return ESP_OK;
  448. }
  449. esp_err_t touch_pad_clear_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask)
  450. {
  451. RTC_MODULE_CHECK((set1_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch set1 bitmask error", ESP_ERR_INVALID_ARG);
  452. RTC_MODULE_CHECK((set2_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch set2 bitmask error", ESP_ERR_INVALID_ARG);
  453. RTC_MODULE_CHECK((en_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch work_en bitmask error", ESP_ERR_INVALID_ARG);
  454. portENTER_CRITICAL(&rtc_spinlock);
  455. SENS.sar_touch_enable.touch_pad_outen1 &= TOUCH_BITS_SWAP(~set1_mask);
  456. SENS.sar_touch_enable.touch_pad_outen2 &= TOUCH_BITS_SWAP(~set2_mask);
  457. SENS.sar_touch_enable.touch_pad_worken &= TOUCH_BITS_SWAP(~en_mask);
  458. portEXIT_CRITICAL(&rtc_spinlock);
  459. return ESP_OK;
  460. }
  461. uint32_t IRAM_ATTR touch_pad_get_status(void)
  462. {
  463. uint32_t status = SENS.sar_touch_ctrl2.touch_meas_en;
  464. return TOUCH_BITS_SWAP(status);
  465. }
  466. esp_err_t IRAM_ATTR touch_pad_clear_status(void)
  467. {
  468. SENS.sar_touch_ctrl2.touch_meas_en_clr = 1;
  469. return ESP_OK;
  470. }
  471. esp_err_t touch_pad_intr_enable(void)
  472. {
  473. portENTER_CRITICAL(&rtc_spinlock);
  474. RTCCNTL.int_ena.rtc_touch = 1;
  475. portEXIT_CRITICAL(&rtc_spinlock);
  476. return ESP_OK;
  477. }
  478. esp_err_t touch_pad_intr_disable(void)
  479. {
  480. portENTER_CRITICAL(&rtc_spinlock);
  481. RTCCNTL.int_ena.rtc_touch = 0;
  482. portEXIT_CRITICAL(&rtc_spinlock);
  483. return ESP_OK;
  484. }
  485. esp_err_t touch_pad_config(touch_pad_t touch_num, uint16_t threshold)
  486. {
  487. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  488. RTC_MODULE_CHECK(touch_num < TOUCH_PAD_MAX, "Touch_Pad Num Err", ESP_ERR_INVALID_ARG);
  489. touch_fsm_mode_t mode;
  490. touch_pad_set_thresh(touch_num, threshold);
  491. touch_pad_io_init(touch_num);
  492. touch_pad_set_cnt_mode(touch_num, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_LOW);
  493. touch_pad_get_fsm_mode(&mode);
  494. if (TOUCH_FSM_MODE_SW == mode) {
  495. touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  496. s_touch_pad_init_bit |= (1 << touch_num);
  497. } else if (TOUCH_FSM_MODE_TIMER == mode){
  498. uint16_t sleep_time = 0;
  499. uint16_t meas_cycle = 0;
  500. uint32_t wait_time_ms = 0;
  501. uint32_t wait_tick = 0;
  502. uint32_t rtc_clk = rtc_clk_slow_freq_get_hz();
  503. touch_pad_set_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  504. touch_pad_get_meas_time(&sleep_time, &meas_cycle);
  505. //If the FSM mode is 'TOUCH_FSM_MODE_TIMER', The data will be ready after one measurement cycle
  506. //after this function is executed, otherwise, the "touch_value" by "touch_pad_read" is 0.
  507. wait_time_ms = sleep_time/(rtc_clk/1000) + meas_cycle/(RTC_FAST_CLK_FREQ_APPROX/1000);
  508. wait_tick = wait_time_ms/portTICK_RATE_MS;
  509. vTaskDelay(wait_tick ? wait_tick : 1);
  510. s_touch_pad_init_bit |= (1 << touch_num);
  511. } else {
  512. return ESP_FAIL;
  513. }
  514. return ESP_OK;
  515. }
  516. esp_err_t touch_pad_init(void)
  517. {
  518. if (rtc_touch_mux == NULL) {
  519. rtc_touch_mux = xSemaphoreCreateMutex();
  520. }
  521. if (rtc_touch_mux == NULL) {
  522. return ESP_FAIL;
  523. }
  524. touch_pad_intr_disable();
  525. touch_pad_clear_group_mask(TOUCH_PAD_BIT_MASK_MAX, TOUCH_PAD_BIT_MASK_MAX, TOUCH_PAD_BIT_MASK_MAX);
  526. touch_pad_set_trigger_mode(TOUCH_TRIGGER_MODE_DEFAULT);
  527. touch_pad_set_trigger_source(TOUCH_TRIGGER_SOURCE_DEFAULT);
  528. touch_pad_clear_status();
  529. touch_pad_set_meas_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT, TOUCH_PAD_MEASURE_CYCLE_DEFAULT);
  530. touch_pad_set_fsm_mode(TOUCH_FSM_MODE_DEFAULT);
  531. return ESP_OK;
  532. }
  533. esp_err_t touch_pad_deinit(void)
  534. {
  535. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  536. if (s_touch_pad_filter != NULL) {
  537. touch_pad_filter_stop();
  538. touch_pad_filter_delete();
  539. }
  540. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  541. s_touch_pad_init_bit = 0x0000;
  542. touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW);
  543. touch_pad_clear_status();
  544. touch_pad_intr_disable();
  545. xSemaphoreGive(rtc_touch_mux);
  546. vSemaphoreDelete(rtc_touch_mux);
  547. rtc_touch_mux = NULL;
  548. return ESP_OK;
  549. }
  550. static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, touch_fsm_mode_t mode)
  551. {
  552. esp_err_t res = ESP_OK;
  553. touch_pad_t tp_wrap = touch_pad_num_wrap(touch_num);
  554. if (TOUCH_FSM_MODE_SW == mode) {
  555. touch_pad_set_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  556. touch_pad_sw_start();
  557. while (SENS.sar_touch_ctrl2.touch_meas_done == 0) {};
  558. *touch_value = (tp_wrap & 0x1) ? \
  559. SENS.touch_meas[tp_wrap / 2].l_val: \
  560. SENS.touch_meas[tp_wrap / 2].h_val;
  561. touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  562. } else if (TOUCH_FSM_MODE_TIMER == mode) {
  563. while (SENS.sar_touch_ctrl2.touch_meas_done == 0) {};
  564. *touch_value = (tp_wrap & 0x1) ? \
  565. SENS.touch_meas[tp_wrap / 2].l_val: \
  566. SENS.touch_meas[tp_wrap / 2].h_val;
  567. } else {
  568. res = ESP_FAIL;
  569. }
  570. if (*touch_value == 0) {
  571. res = ESP_ERR_INVALID_STATE;
  572. }
  573. return res;
  574. }
  575. esp_err_t touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value)
  576. {
  577. RTC_MODULE_CHECK(touch_num < TOUCH_PAD_MAX, "Touch_Pad Num Err", ESP_ERR_INVALID_ARG);
  578. RTC_MODULE_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
  579. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  580. esp_err_t res = ESP_OK;
  581. touch_fsm_mode_t mode;
  582. touch_pad_get_fsm_mode(&mode);
  583. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  584. res = _touch_pad_read(touch_num, touch_value, mode);
  585. xSemaphoreGive(rtc_touch_mux);
  586. return res;
  587. }
  588. IRAM_ATTR esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint16_t *touch_value)
  589. {
  590. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  591. RTC_MODULE_CHECK(touch_num < TOUCH_PAD_MAX, "Touch_Pad Num Err", ESP_ERR_INVALID_ARG);
  592. RTC_MODULE_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
  593. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_FAIL);
  594. *touch_value = s_touch_pad_filter->raw_val[touch_num];
  595. if (*touch_value == 0) {
  596. return ESP_ERR_INVALID_STATE;
  597. }
  598. return ESP_OK;
  599. }
  600. IRAM_ATTR esp_err_t touch_pad_read_filtered(touch_pad_t touch_num, uint16_t *touch_value)
  601. {
  602. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  603. RTC_MODULE_CHECK(touch_num < TOUCH_PAD_MAX, "Touch_Pad Num Err", ESP_ERR_INVALID_ARG);
  604. RTC_MODULE_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
  605. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_FAIL);
  606. *touch_value = (s_touch_pad_filter->filtered_val[touch_num]);
  607. if (*touch_value == 0) {
  608. return ESP_ERR_INVALID_STATE;
  609. }
  610. return ESP_OK;
  611. }
  612. esp_err_t touch_pad_set_filter_period(uint32_t new_period_ms)
  613. {
  614. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  615. RTC_MODULE_CHECK(new_period_ms > 0, "Touch pad filter period error", ESP_ERR_INVALID_ARG);
  616. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  617. esp_err_t ret = ESP_OK;
  618. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  619. if (s_touch_pad_filter != NULL) {
  620. xTimerChangePeriod(s_touch_pad_filter->timer, new_period_ms / portTICK_PERIOD_MS, portMAX_DELAY);
  621. s_touch_pad_filter->period = new_period_ms;
  622. } else {
  623. ESP_LOGE(RTC_MODULE_TAG, "Touch pad filter deleted");
  624. ret = ESP_ERR_INVALID_STATE;
  625. }
  626. xSemaphoreGive(rtc_touch_mux);
  627. return ret;
  628. }
  629. esp_err_t touch_pad_get_filter_period(uint32_t* p_period_ms)
  630. {
  631. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  632. RTC_MODULE_CHECK(p_period_ms != NULL, "Touch pad period pointer error", ESP_ERR_INVALID_ARG);
  633. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  634. esp_err_t ret = ESP_OK;
  635. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  636. if (s_touch_pad_filter != NULL) {
  637. *p_period_ms = s_touch_pad_filter->period;
  638. } else {
  639. ESP_LOGE(RTC_MODULE_TAG, "Touch pad filter deleted");
  640. ret = ESP_ERR_INVALID_STATE;
  641. }
  642. xSemaphoreGive(rtc_touch_mux);
  643. return ret;
  644. }
  645. esp_err_t touch_pad_filter_start(uint32_t filter_period_ms)
  646. {
  647. RTC_MODULE_CHECK(filter_period_ms >= portTICK_PERIOD_MS, "Touch pad filter period error", ESP_ERR_INVALID_ARG);
  648. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  649. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  650. if (s_touch_pad_filter == NULL) {
  651. s_touch_pad_filter = (touch_pad_filter_t *) calloc(1, sizeof(touch_pad_filter_t));
  652. if (s_touch_pad_filter == NULL) {
  653. goto err_no_mem;
  654. }
  655. }
  656. if (s_touch_pad_filter->timer == NULL) {
  657. s_touch_pad_filter->timer = xTimerCreate("filter_tmr", filter_period_ms / portTICK_PERIOD_MS, pdFALSE,
  658. NULL, (void(*)(TimerHandle_t))touch_pad_filter_cb);
  659. if (s_touch_pad_filter->timer == NULL) {
  660. free(s_touch_pad_filter);
  661. s_touch_pad_filter = NULL;
  662. goto err_no_mem;
  663. }
  664. s_touch_pad_filter->period = filter_period_ms;
  665. }
  666. xSemaphoreGive(rtc_touch_mux);
  667. touch_pad_filter_cb(NULL);
  668. return ESP_OK;
  669. err_no_mem:
  670. xSemaphoreGive(rtc_touch_mux);
  671. return ESP_ERR_NO_MEM;
  672. }
  673. esp_err_t touch_pad_filter_stop(void)
  674. {
  675. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  676. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  677. esp_err_t ret = ESP_OK;
  678. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  679. if (s_touch_pad_filter != NULL) {
  680. xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY);
  681. } else {
  682. ESP_LOGE(RTC_MODULE_TAG, "Touch pad filter deleted");
  683. ret = ESP_ERR_INVALID_STATE;
  684. }
  685. xSemaphoreGive(rtc_touch_mux);
  686. return ret;
  687. }
  688. esp_err_t touch_pad_filter_delete(void)
  689. {
  690. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  691. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  692. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  693. if (s_touch_pad_filter != NULL) {
  694. if (s_touch_pad_filter->timer != NULL) {
  695. xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY);
  696. xTimerDelete(s_touch_pad_filter->timer, portMAX_DELAY);
  697. s_touch_pad_filter->timer = NULL;
  698. }
  699. free(s_touch_pad_filter);
  700. s_touch_pad_filter = NULL;
  701. }
  702. xSemaphoreGive(rtc_touch_mux);
  703. return ESP_OK;
  704. }
  705. esp_err_t touch_pad_get_wakeup_status(touch_pad_t *pad_num)
  706. {
  707. uint32_t touch_mask = SENS.sar_touch_ctrl2.touch_meas_en;
  708. if(touch_mask == 0) {
  709. return ESP_FAIL;
  710. }
  711. *pad_num = touch_pad_num_wrap((touch_pad_t)(__builtin_ffs(touch_mask) - 1));
  712. return ESP_OK;
  713. }
  714. #endif
  715. /*---------------------------------------------------------------
  716. ADC Common
  717. ---------------------------------------------------------------*/
  718. #if CONFIG_IDF_TARGET_ESP32S2BETA
  719. #define SENS_FORCE_XPD_AMP_FSM 0 // Use FSM to control power down
  720. #define SENS_FORCE_XPD_AMP_PD 2 // Force power down
  721. #define SENS_FORCE_XPD_AMP_PU 3 // Force power up
  722. #define SENS_SAR1_ATTEN_VAL_MASK 0x3
  723. #define SENS_SAR2_ATTEN_VAL_MASK 0x3
  724. #define SENS_FORCE_XPD_SAR_SW_M (BIT(1))
  725. #define SENS_FORCE_XPD_SAR_FSM 0 // Use FSM to control power down
  726. #define SENS_FORCE_XPD_SAR_PD 2 // Force power down
  727. #define SENS_FORCE_XPD_SAR_PU 3 // Force power up
  728. #endif
  729. static esp_err_t adc_set_fsm_time(int rst_wait, int start_wait, int standby_wait, int sample_cycle)
  730. {
  731. portENTER_CRITICAL(&rtc_spinlock);
  732. #if CONFIG_IDF_TARGET_ESP32
  733. // Internal FSM reset wait time
  734. if (rst_wait >= 0) {
  735. SYSCON.saradc_fsm.rstb_wait = rst_wait;
  736. }
  737. // Internal FSM start wait time
  738. if (start_wait >= 0) {
  739. SYSCON.saradc_fsm.start_wait = start_wait;
  740. }
  741. // Internal FSM standby wait time
  742. if (standby_wait >= 0) {
  743. SYSCON.saradc_fsm.standby_wait = standby_wait;
  744. }
  745. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  746. // Internal FSM reset wait time
  747. if (rst_wait >= 0) {
  748. SYSCON.saradc_fsm_wait.rstb_wait = rst_wait;
  749. }
  750. // Internal FSM start wait time
  751. if (start_wait >= 0) {
  752. SYSCON.saradc_fsm_wait.xpd_wait = start_wait;
  753. }
  754. // Internal FSM standby wait time
  755. if (standby_wait >= 0) {
  756. SYSCON.saradc_fsm_wait.standby_wait = standby_wait;
  757. }
  758. #endif
  759. // Internal FSM standby sample cycle
  760. if (sample_cycle >= 0) {
  761. SYSCON.saradc_fsm.sample_cycle = sample_cycle;
  762. }
  763. portEXIT_CRITICAL(&rtc_spinlock);
  764. return ESP_OK;
  765. }
  766. static esp_err_t adc_set_data_format(adc_i2s_encode_t mode)
  767. {
  768. portENTER_CRITICAL(&rtc_spinlock);
  769. //data format:
  770. //0: ADC_ENCODE_12BIT [15:12]-channel [11:0]-12 bits ADC data
  771. //1: ADC_ENCODE_11BIT [15]-1 [14:11]-channel [10:0]-11 bits ADC data, the resolution should not be larger than 11 bits in this case.
  772. SYSCON.saradc_ctrl.data_sar_sel = mode;
  773. portEXIT_CRITICAL(&rtc_spinlock);
  774. return ESP_OK;
  775. }
  776. static esp_err_t adc_set_measure_limit(uint8_t meas_num, bool lim_en)
  777. {
  778. portENTER_CRITICAL(&rtc_spinlock);
  779. // Set max measure number
  780. SYSCON.saradc_ctrl2.max_meas_num = meas_num;
  781. // Enable max measure number limit
  782. SYSCON.saradc_ctrl2.meas_num_limit = lim_en;
  783. portEXIT_CRITICAL(&rtc_spinlock);
  784. return ESP_OK;
  785. }
  786. static esp_err_t adc_set_work_mode(adc_unit_t adc_unit)
  787. {
  788. portENTER_CRITICAL(&rtc_spinlock);
  789. if (adc_unit == ADC_UNIT_1) {
  790. // saradc mode sel : 0--single saradc; 1--double saradc; 2--alternative saradc
  791. SYSCON.saradc_ctrl.work_mode = 0;
  792. //ENABLE ADC 0: ADC1 1: ADC2, only work for single SAR mode
  793. SYSCON.saradc_ctrl.sar_sel = 0;
  794. } else if (adc_unit == ADC_UNIT_2) {
  795. // saradc mode sel : 0--single saradc; 1--double saradc; 2--alternative saradc
  796. SYSCON.saradc_ctrl.work_mode = 0;
  797. //ENABLE ADC1 0: SAR1 1: SAR2 only work for single SAR mode
  798. SYSCON.saradc_ctrl.sar_sel = 1;
  799. } else if (adc_unit == ADC_UNIT_BOTH) {
  800. // saradc mode sel : 0--single saradc; 1--double saradc; 2--alternative saradc
  801. SYSCON.saradc_ctrl.work_mode = 1;
  802. } else if (adc_unit == ADC_UNIT_ALTER) {
  803. // saradc mode sel : 0--single saradc; 1--double saradc; 2--alternative saradc
  804. SYSCON.saradc_ctrl.work_mode = 2;
  805. }
  806. portEXIT_CRITICAL(&rtc_spinlock);
  807. return ESP_OK;
  808. }
  809. static esp_err_t adc_set_atten(adc_unit_t adc_unit, adc_channel_t channel, adc_atten_t atten)
  810. {
  811. ADC_CHECK_UNIT(adc_unit);
  812. if (adc_unit & ADC_UNIT_1) {
  813. RTC_MODULE_CHECK((adc1_channel_t)channel < ADC1_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  814. }
  815. RTC_MODULE_CHECK(atten < ADC_ATTEN_MAX, "ADC Atten Err", ESP_ERR_INVALID_ARG);
  816. portENTER_CRITICAL(&rtc_spinlock);
  817. if (adc_unit & ADC_UNIT_1) {
  818. //SAR1_atten
  819. SET_PERI_REG_BITS(SENS_SAR_ATTEN1_REG, SENS_SAR1_ATTEN_VAL_MASK, atten, (channel * 2));
  820. }
  821. if (adc_unit & ADC_UNIT_2) {
  822. //SAR2_atten
  823. SET_PERI_REG_BITS(SENS_SAR_ATTEN2_REG, SENS_SAR2_ATTEN_VAL_MASK, atten, (channel * 2));
  824. }
  825. portEXIT_CRITICAL(&rtc_spinlock);
  826. return ESP_OK;
  827. }
  828. void adc_power_always_on(void)
  829. {
  830. portENTER_CRITICAL(&rtc_spinlock);
  831. #if CONFIG_IDF_TARGET_ESP32
  832. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  833. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  834. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  835. #endif
  836. portEXIT_CRITICAL(&rtc_spinlock);
  837. }
  838. void adc_power_on(void)
  839. {
  840. portENTER_CRITICAL(&rtc_spinlock);
  841. #if CONFIG_IDF_TARGET_ESP32
  842. //The power FSM controlled mode saves more power, while the ADC noise may get increased.
  843. #ifndef CONFIG_ADC_FORCE_XPD_FSM
  844. //Set the power always on to increase precision.
  845. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  846. #else
  847. //Use the FSM to turn off the power while not used to save power.
  848. if (SENS.sar_meas_wait2.force_xpd_sar & SENS_FORCE_XPD_SAR_SW_M) {
  849. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  850. } else {
  851. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_FSM;
  852. }
  853. #endif
  854. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  855. //The power FSM controlled mode saves more power, while the ADC noise may get increased.
  856. #ifndef CONFIG_ADC_FORCE_XPD_FSM
  857. //Set the power always on to increase precision.
  858. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  859. #else
  860. //Use the FSM to turn off the power while not used to save power.
  861. if (SENS.sar_power_xpd_sar.force_xpd_sar & SENS_FORCE_XPD_SAR_SW_M) {
  862. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  863. } else {
  864. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_FSM;
  865. }
  866. #endif
  867. #endif
  868. portEXIT_CRITICAL(&rtc_spinlock);
  869. }
  870. void adc_power_off(void)
  871. {
  872. portENTER_CRITICAL(&rtc_spinlock);
  873. #if CONFIG_IDF_TARGET_ESP32
  874. //Bit1 0:Fsm 1: SW mode
  875. //Bit0 0:SW mode power down 1: SW mode power on
  876. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PD;
  877. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  878. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_PD;
  879. #endif
  880. portEXIT_CRITICAL(&rtc_spinlock);
  881. }
  882. esp_err_t adc_set_clk_div(uint8_t clk_div)
  883. {
  884. portENTER_CRITICAL(&rtc_spinlock);
  885. // ADC clock divided from APB clk, 80 / 2 = 40Mhz,
  886. SYSCON.saradc_ctrl.sar_clk_div = clk_div;
  887. portEXIT_CRITICAL(&rtc_spinlock);
  888. return ESP_OK;
  889. }
  890. esp_err_t adc_set_i2s_data_source(adc_i2s_source_t src)
  891. {
  892. RTC_MODULE_CHECK(src < ADC_I2S_DATA_SRC_MAX, "ADC i2s data source error", ESP_ERR_INVALID_ARG);
  893. portENTER_CRITICAL(&rtc_spinlock);
  894. // 1: I2S input data is from SAR ADC (for DMA) 0: I2S input data is from GPIO matrix
  895. SYSCON.saradc_ctrl.data_to_i2s = src;
  896. portEXIT_CRITICAL(&rtc_spinlock);
  897. return ESP_OK;
  898. }
  899. esp_err_t adc_gpio_init(adc_unit_t adc_unit, adc_channel_t channel)
  900. {
  901. ADC_CHECK_UNIT(adc_unit);
  902. gpio_num_t gpio_num = 0;
  903. if (adc_unit & ADC_UNIT_1) {
  904. RTC_MODULE_CHECK((adc1_channel_t) channel < ADC1_CHANNEL_MAX, "ADC1 channel error", ESP_ERR_INVALID_ARG);
  905. ADC1_CHECK_FUNCTION_RET(adc1_pad_get_io_num((adc1_channel_t) channel, &gpio_num));
  906. ADC1_CHECK_FUNCTION_RET(rtc_gpio_init(gpio_num));
  907. ADC1_CHECK_FUNCTION_RET(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
  908. ADC1_CHECK_FUNCTION_RET(gpio_set_pull_mode(gpio_num, GPIO_FLOATING));
  909. }
  910. return ESP_OK;
  911. }
  912. esp_err_t adc_set_data_inv(adc_unit_t adc_unit, bool inv_en)
  913. {
  914. portENTER_CRITICAL(&rtc_spinlock);
  915. #if CONFIG_IDF_TARGET_ESP32
  916. if (adc_unit & ADC_UNIT_1) {
  917. // Enable ADC data invert
  918. SENS.sar_read_ctrl.sar1_data_inv = inv_en;
  919. }
  920. if (adc_unit & ADC_UNIT_2) {
  921. // Enable ADC data invert
  922. SENS.sar_read_ctrl2.sar2_data_inv = inv_en;
  923. }
  924. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  925. if (adc_unit & ADC_UNIT_1) {
  926. // Enable ADC data invert
  927. SENS.sar_reader1_ctrl.sar1_data_inv = inv_en;
  928. }
  929. if (adc_unit & ADC_UNIT_2) {
  930. // Enable ADC data invert
  931. SENS.sar_reader2_ctrl.sar2_data_inv = inv_en;
  932. }
  933. #endif
  934. portEXIT_CRITICAL(&rtc_spinlock);
  935. return ESP_OK;
  936. }
  937. esp_err_t adc_set_data_width(adc_unit_t adc_unit, adc_bits_width_t bits)
  938. {
  939. ADC_CHECK_UNIT(adc_unit);
  940. RTC_MODULE_CHECK(bits < ADC_WIDTH_MAX, "ADC bit width error", ESP_ERR_INVALID_ARG);
  941. portENTER_CRITICAL(&rtc_spinlock);
  942. #if CONFIG_IDF_TARGET_ESP32
  943. if (adc_unit & ADC_UNIT_1) {
  944. SENS.sar_start_force.sar1_bit_width = bits;
  945. SENS.sar_read_ctrl.sar1_sample_bit = bits;
  946. }
  947. if (adc_unit & ADC_UNIT_2) {
  948. SENS.sar_start_force.sar2_bit_width = bits;
  949. SENS.sar_read_ctrl2.sar2_sample_bit = bits;
  950. }
  951. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  952. if (adc_unit & ADC_UNIT_1) {
  953. SENS.sar_meas1_ctrl1.sar1_bit_width = bits;
  954. SENS.sar_reader1_ctrl.sar1_sample_bit = bits;
  955. }
  956. if (adc_unit & ADC_UNIT_2) {
  957. SENS.sar_meas2_ctrl1.sar2_bit_width = bits;
  958. SENS.sar_reader2_ctrl.sar2_sample_bit = bits;
  959. }
  960. #endif
  961. portEXIT_CRITICAL(&rtc_spinlock);
  962. return ESP_OK;
  963. }
  964. // this function should be called in the critical section
  965. static void adc_set_controller(adc_unit_t unit, adc_controller_t ctrl )
  966. {
  967. #if CONFIG_IDF_TARGET_ESP32
  968. if ( unit == ADC_UNIT_1 ) {
  969. switch( ctrl ) {
  970. case ADC_CTRL_RTC:
  971. SENS.sar_read_ctrl.sar1_dig_force = false; //RTC controller controls the ADC, not digital controller
  972. SENS.sar_meas_start1.meas1_start_force = true; //RTC controller controls the ADC,not ulp coprocessor
  973. SENS.sar_meas_start1.sar1_en_pad_force = true; //RTC controller controls the data port, not ulp coprocessor
  974. SENS.sar_touch_ctrl1.xpd_hall_force = true; // RTC controller controls the hall sensor power,not ulp coprocessor
  975. SENS.sar_touch_ctrl1.hall_phase_force = true; // RTC controller controls the hall sensor phase,not ulp coprocessor
  976. break;
  977. case ADC_CTRL_ULP:
  978. SENS.sar_read_ctrl.sar1_dig_force = false;
  979. SENS.sar_meas_start1.meas1_start_force = false;
  980. SENS.sar_meas_start1.sar1_en_pad_force = false;
  981. SENS.sar_touch_ctrl1.xpd_hall_force = false;
  982. SENS.sar_touch_ctrl1.hall_phase_force = false;
  983. break;
  984. case ADC_CTRL_DIG:
  985. SENS.sar_read_ctrl.sar1_dig_force = true;
  986. SENS.sar_meas_start1.meas1_start_force = true;
  987. SENS.sar_meas_start1.sar1_en_pad_force = true;
  988. SENS.sar_touch_ctrl1.xpd_hall_force = true;
  989. SENS.sar_touch_ctrl1.hall_phase_force = true;
  990. break;
  991. default:
  992. ESP_LOGE(TAG, "adc1 selects invalid controller");
  993. break;
  994. }
  995. } else if ( unit == ADC_UNIT_2) {
  996. switch( ctrl ) {
  997. case ADC_CTRL_RTC:
  998. SENS.sar_meas_start2.meas2_start_force = true; //RTC controller controls the ADC,not ulp coprocessor
  999. SENS.sar_meas_start2.sar2_en_pad_force = true; //RTC controller controls the data port, not ulp coprocessor
  1000. SENS.sar_read_ctrl2.sar2_dig_force = false; //RTC controller controls the ADC, not digital controller
  1001. SENS.sar_read_ctrl2.sar2_pwdet_force = false; //RTC controller controls the ADC, not PWDET
  1002. SYSCON.saradc_ctrl.sar2_mux = true; //RTC controller controls the ADC, not PWDET
  1003. break;
  1004. case ADC_CTRL_ULP:
  1005. SENS.sar_meas_start2.meas2_start_force = false;
  1006. SENS.sar_meas_start2.sar2_en_pad_force = false;
  1007. SENS.sar_read_ctrl2.sar2_dig_force = false;
  1008. SENS.sar_read_ctrl2.sar2_pwdet_force = false;
  1009. SYSCON.saradc_ctrl.sar2_mux = true;
  1010. break;
  1011. case ADC_CTRL_DIG:
  1012. SENS.sar_meas_start2.meas2_start_force = true;
  1013. SENS.sar_meas_start2.sar2_en_pad_force = true;
  1014. SENS.sar_read_ctrl2.sar2_dig_force = true;
  1015. SENS.sar_read_ctrl2.sar2_pwdet_force = false;
  1016. SYSCON.saradc_ctrl.sar2_mux = true;
  1017. break;
  1018. case ADC2_CTRL_PWDET:
  1019. //currently only used by Wi-Fi
  1020. SENS.sar_meas_start2.meas2_start_force = true;
  1021. SENS.sar_meas_start2.sar2_en_pad_force = true;
  1022. SENS.sar_read_ctrl2.sar2_dig_force = false;
  1023. SENS.sar_read_ctrl2.sar2_pwdet_force = true;
  1024. SYSCON.saradc_ctrl.sar2_mux = false;
  1025. break;
  1026. default:
  1027. ESP_LOGE(TAG, "adc2 selects invalid controller");
  1028. break;
  1029. }
  1030. } else {
  1031. ESP_LOGE(TAG, "invalid adc unit");
  1032. assert(0);
  1033. }
  1034. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1035. if ( unit == ADC_UNIT_1 ) {
  1036. switch( ctrl ) {
  1037. case ADC_CTRL_RTC:
  1038. SENS.sar_meas1_mux.sar1_dig_force = false; //RTC controller controls the ADC, not digital controller
  1039. SENS.sar_meas1_ctrl2.meas1_start_force = true; //RTC controller controls the ADC,not ulp coprocessor
  1040. SENS.sar_meas1_ctrl2.sar1_en_pad_force = true; //RTC controller controls the data port, not ulp coprocessor
  1041. SENS.sar_hall_ctrl.xpd_hall_force = true; // RTC controller controls the hall sensor power,not ulp coprocessor
  1042. SENS.sar_hall_ctrl.hall_phase_force = true; // RTC controller controls the hall sensor phase,not ulp coprocessor
  1043. break;
  1044. case ADC_CTRL_ULP:
  1045. SENS.sar_meas1_mux.sar1_dig_force = false;
  1046. SENS.sar_meas1_ctrl2.meas1_start_force = false;
  1047. SENS.sar_meas1_ctrl2.sar1_en_pad_force = false;
  1048. SENS.sar_hall_ctrl.xpd_hall_force = false;
  1049. SENS.sar_hall_ctrl.hall_phase_force = false;
  1050. break;
  1051. case ADC_CTRL_DIG:
  1052. SENS.sar_meas1_mux.sar1_dig_force = true;
  1053. SENS.sar_meas1_ctrl2.meas1_start_force = true;
  1054. SENS.sar_meas1_ctrl2.sar1_en_pad_force = true;
  1055. SENS.sar_hall_ctrl.xpd_hall_force = true;
  1056. SENS.sar_hall_ctrl.hall_phase_force = true;
  1057. break;
  1058. default:
  1059. ESP_LOGE(TAG, "adc1 selects invalid controller");
  1060. break;
  1061. }
  1062. } else if ( unit == ADC_UNIT_2) {
  1063. switch( ctrl ) {
  1064. case ADC_CTRL_RTC:
  1065. SENS.sar_meas2_ctrl2.meas2_start_force = true; //RTC controller controls the ADC,not ulp coprocessor
  1066. SENS.sar_meas2_ctrl2.sar2_en_pad_force = true; //RTC controller controls the data port, not ulp coprocessor
  1067. break;
  1068. case ADC_CTRL_ULP:
  1069. SENS.sar_meas2_ctrl2.meas2_start_force = false;
  1070. SENS.sar_meas2_ctrl2.sar2_en_pad_force = false;
  1071. break;
  1072. case ADC_CTRL_DIG:
  1073. SENS.sar_meas2_ctrl2.meas2_start_force = true;
  1074. SENS.sar_meas2_ctrl2.sar2_en_pad_force = true;
  1075. break;
  1076. case ADC2_CTRL_PWDET:
  1077. //currently only used by Wi-Fi
  1078. SENS.sar_meas2_ctrl2.meas2_start_force = true;
  1079. SENS.sar_meas2_ctrl2.sar2_en_pad_force = true;
  1080. break;
  1081. default:
  1082. ESP_LOGE(TAG, "adc2 selects invalid controller");
  1083. break;
  1084. }
  1085. } else {
  1086. ESP_LOGE(TAG, "invalid adc unit");
  1087. assert(0);
  1088. }
  1089. #endif
  1090. }
  1091. // this function should be called in the critical section
  1092. static int adc_convert( adc_unit_t unit, int channel)
  1093. {
  1094. uint16_t adc_value = 0;
  1095. #if CONFIG_IDF_TARGET_ESP32
  1096. if ( unit == ADC_UNIT_1 ) {
  1097. SENS.sar_meas_start1.sar1_en_pad = (1 << channel); //only one channel is selected.
  1098. while (SENS.sar_slave_addr1.meas_status != 0);
  1099. SENS.sar_meas_start1.meas1_start_sar = 0;
  1100. SENS.sar_meas_start1.meas1_start_sar = 1;
  1101. while (SENS.sar_meas_start1.meas1_done_sar == 0);
  1102. adc_value = SENS.sar_meas_start1.meas1_data_sar;
  1103. } else if ( unit == ADC_UNIT_2 ) {
  1104. SENS.sar_meas_start2.sar2_en_pad = (1 << channel); //only one channel is selected.
  1105. SENS.sar_meas_start2.meas2_start_sar = 0; //start force 0
  1106. SENS.sar_meas_start2.meas2_start_sar = 1; //start force 1
  1107. while (SENS.sar_meas_start2.meas2_done_sar == 0) {}; //read done
  1108. adc_value = SENS.sar_meas_start2.meas2_data_sar;
  1109. } else {
  1110. ESP_LOGE(TAG, "invalid adc unit");
  1111. return ESP_ERR_INVALID_ARG;
  1112. }
  1113. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1114. if ( unit == ADC_UNIT_1 ) {
  1115. SENS.sar_meas1_ctrl2.sar1_en_pad = (1 << channel); //only one channel is selected.
  1116. while (SENS.sar_slave_addr1.meas_status != 0);
  1117. SENS.sar_meas1_ctrl2.meas1_start_sar = 0;
  1118. SENS.sar_meas1_ctrl2.meas1_start_sar = 1;
  1119. while (SENS.sar_meas1_ctrl2.meas1_done_sar == 0);
  1120. adc_value = SENS.sar_meas1_ctrl2.meas1_data_sar;
  1121. } else if ( unit == ADC_UNIT_2 ) {
  1122. SENS.sar_meas2_ctrl2.sar2_en_pad = (1 << channel); //only one channel is selected.
  1123. SENS.sar_meas2_ctrl2.meas2_start_sar = 0; //start force 0
  1124. SENS.sar_meas2_ctrl2.meas2_start_sar = 1; //start force 1
  1125. while (SENS.sar_meas2_ctrl2.meas2_done_sar == 0) {}; //read done
  1126. adc_value = SENS.sar_meas2_ctrl2.meas2_data_sar;
  1127. } else {
  1128. ESP_LOGE(TAG, "invalid adc unit");
  1129. return ESP_ERR_INVALID_ARG;
  1130. }
  1131. #endif
  1132. return adc_value;
  1133. }
  1134. /*-------------------------------------------------------------------------------------
  1135. * ADC I2S
  1136. *------------------------------------------------------------------------------------*/
  1137. static esp_err_t adc_set_i2s_data_len(adc_unit_t adc_unit, int patt_len)
  1138. {
  1139. ADC_CHECK_UNIT(adc_unit);
  1140. RTC_MODULE_CHECK((patt_len < ADC_PATT_LEN_MAX) && (patt_len > 0), "ADC pattern length error", ESP_ERR_INVALID_ARG);
  1141. portENTER_CRITICAL(&rtc_spinlock);
  1142. if(adc_unit & ADC_UNIT_1) {
  1143. SYSCON.saradc_ctrl.sar1_patt_len = patt_len - 1;
  1144. }
  1145. if(adc_unit & ADC_UNIT_2) {
  1146. SYSCON.saradc_ctrl.sar2_patt_len = patt_len - 1;
  1147. }
  1148. portEXIT_CRITICAL(&rtc_spinlock);
  1149. return ESP_OK;
  1150. }
  1151. static esp_err_t adc_set_i2s_data_pattern(adc_unit_t adc_unit, int seq_num, adc_channel_t channel, adc_bits_width_t bits, adc_atten_t atten)
  1152. {
  1153. ADC_CHECK_UNIT(adc_unit);
  1154. if (adc_unit & ADC_UNIT_1) {
  1155. RTC_MODULE_CHECK((adc1_channel_t) channel < ADC1_CHANNEL_MAX, "ADC1 channel error", ESP_ERR_INVALID_ARG);
  1156. }
  1157. RTC_MODULE_CHECK(bits < ADC_WIDTH_MAX, "ADC bit width error", ESP_ERR_INVALID_ARG);
  1158. RTC_MODULE_CHECK(atten < ADC_ATTEN_MAX, "ADC Atten Err", ESP_ERR_INVALID_ARG);
  1159. portENTER_CRITICAL(&rtc_spinlock);
  1160. //Configure pattern table, each 8 bit defines one channel
  1161. //[7:4]-channel [3:2]-bit width [1:0]- attenuation
  1162. //BIT WIDTH: 3: 12BIT 2: 11BIT 1: 10BIT 0: 9BIT
  1163. //ATTEN: 3: ATTEN = 11dB 2: 6dB 1: 2.5dB 0: 0dB
  1164. uint8_t val = (channel << 4) | (bits << 2) | (atten << 0);
  1165. if (adc_unit & ADC_UNIT_1) {
  1166. SYSCON.saradc_sar1_patt_tab[seq_num / 4] &= (~(0xff << ((3 - (seq_num % 4)) * 8)));
  1167. SYSCON.saradc_sar1_patt_tab[seq_num / 4] |= (val << ((3 - (seq_num % 4)) * 8));
  1168. }
  1169. if (adc_unit & ADC_UNIT_2) {
  1170. SYSCON.saradc_sar2_patt_tab[seq_num / 4] &= (~(0xff << ((3 - (seq_num % 4)) * 8)));
  1171. SYSCON.saradc_sar2_patt_tab[seq_num / 4] |= (val << ((3 - (seq_num % 4)) * 8));
  1172. }
  1173. portEXIT_CRITICAL(&rtc_spinlock);
  1174. return ESP_OK;
  1175. }
  1176. esp_err_t adc_i2s_mode_init(adc_unit_t adc_unit, adc_channel_t channel)
  1177. {
  1178. ADC_CHECK_UNIT(adc_unit);
  1179. if (adc_unit & ADC_UNIT_1) {
  1180. RTC_MODULE_CHECK((adc1_channel_t) channel < ADC1_CHANNEL_MAX, "ADC1 channel error", ESP_ERR_INVALID_ARG);
  1181. }
  1182. uint8_t table_len = 1;
  1183. //POWER ON SAR
  1184. adc_power_always_on();
  1185. adc_gpio_init(adc_unit, channel);
  1186. adc_set_i2s_data_len(adc_unit, table_len);
  1187. adc_set_i2s_data_pattern(adc_unit, 0, channel, ADC_WIDTH_BIT_12, ADC_ATTEN_DB_11);
  1188. portENTER_CRITICAL(&rtc_spinlock);
  1189. if (adc_unit & ADC_UNIT_1) {
  1190. adc_set_controller( ADC_UNIT_1, ADC_CTRL_DIG );
  1191. }
  1192. if (adc_unit & ADC_UNIT_2) {
  1193. adc_set_controller( ADC_UNIT_2, ADC_CTRL_DIG );
  1194. }
  1195. portEXIT_CRITICAL(&rtc_spinlock);
  1196. adc_set_i2s_data_source(ADC_I2S_DATA_SRC_ADC);
  1197. adc_set_clk_div(SAR_ADC_CLK_DIV_DEFAULT);
  1198. // Set internal FSM wait time.
  1199. adc_set_fsm_time(ADC_FSM_RSTB_WAIT_DEFAULT, ADC_FSM_START_WAIT_DEFAULT, ADC_FSM_STANDBY_WAIT_DEFAULT,
  1200. ADC_FSM_TIME_KEEP);
  1201. adc_set_work_mode(adc_unit);
  1202. adc_set_data_format(ADC_ENCODE_12BIT);
  1203. adc_set_measure_limit(ADC_MAX_MEAS_NUM_DEFAULT, ADC_MEAS_NUM_LIM_DEFAULT);
  1204. //Invert The Level, Invert SAR ADC1 data
  1205. adc_set_data_inv(adc_unit, true);
  1206. return ESP_OK;
  1207. }
  1208. /*-------------------------------------------------------------------------------------
  1209. * ADC1
  1210. *------------------------------------------------------------------------------------*/
  1211. esp_err_t adc1_pad_get_io_num(adc1_channel_t channel, gpio_num_t *gpio_num)
  1212. {
  1213. RTC_MODULE_CHECK(channel < ADC1_CHANNEL_MAX, "ADC1 Channel Err", ESP_ERR_INVALID_ARG);
  1214. switch (channel) {
  1215. case ADC1_CHANNEL_0:
  1216. *gpio_num = ADC1_CHANNEL_0_GPIO_NUM;
  1217. break;
  1218. case ADC1_CHANNEL_1:
  1219. *gpio_num = ADC1_CHANNEL_1_GPIO_NUM;
  1220. break;
  1221. case ADC1_CHANNEL_2:
  1222. *gpio_num = ADC1_CHANNEL_2_GPIO_NUM;
  1223. break;
  1224. case ADC1_CHANNEL_3:
  1225. *gpio_num = ADC1_CHANNEL_3_GPIO_NUM;
  1226. break;
  1227. case ADC1_CHANNEL_4:
  1228. *gpio_num = ADC1_CHANNEL_4_GPIO_NUM;
  1229. break;
  1230. case ADC1_CHANNEL_5:
  1231. *gpio_num = ADC1_CHANNEL_5_GPIO_NUM;
  1232. break;
  1233. case ADC1_CHANNEL_6:
  1234. *gpio_num = ADC1_CHANNEL_6_GPIO_NUM;
  1235. break;
  1236. case ADC1_CHANNEL_7:
  1237. *gpio_num = ADC1_CHANNEL_7_GPIO_NUM;
  1238. break;
  1239. default:
  1240. return ESP_ERR_INVALID_ARG;
  1241. }
  1242. return ESP_OK;
  1243. }
  1244. esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten)
  1245. {
  1246. RTC_MODULE_CHECK(channel < ADC1_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  1247. RTC_MODULE_CHECK(atten < ADC_ATTEN_MAX, "ADC Atten Err", ESP_ERR_INVALID_ARG);
  1248. adc_gpio_init(ADC_UNIT_1, channel);
  1249. adc_set_atten(ADC_UNIT_1, channel, atten);
  1250. return ESP_OK;
  1251. }
  1252. esp_err_t adc1_config_width(adc_bits_width_t width_bit)
  1253. {
  1254. RTC_MODULE_CHECK(width_bit < ADC_WIDTH_MAX, "ADC bit width error", ESP_ERR_INVALID_ARG);
  1255. adc_set_data_width(ADC_UNIT_1, width_bit);
  1256. adc_set_data_inv(ADC_UNIT_1, true);
  1257. return ESP_OK;
  1258. }
  1259. static inline void adc1_fsm_disable(void)
  1260. {
  1261. #if CONFIG_IDF_TARGET_ESP32
  1262. //channel is set in the convert function
  1263. SENS.sar_meas_wait2.force_xpd_amp = SENS_FORCE_XPD_AMP_PD;
  1264. //disable FSM, it's only used by the LNA.
  1265. SENS.sar_meas_ctrl.amp_rst_fb_fsm = 0;
  1266. SENS.sar_meas_ctrl.amp_short_ref_fsm = 0;
  1267. SENS.sar_meas_ctrl.amp_short_ref_gnd_fsm = 0;
  1268. SENS.sar_meas_wait1.sar_amp_wait1 = 1;
  1269. SENS.sar_meas_wait1.sar_amp_wait2 = 1;
  1270. SENS.sar_meas_wait2.sar_amp_wait3 = 1;
  1271. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1272. //channel is set in the convert function
  1273. SENS.sar_meas1_ctrl1.force_xpd_amp = SENS_FORCE_XPD_AMP_PD;
  1274. //disable FSM, it's only used by the LNA.
  1275. SENS.sar_amp_ctrl3.amp_rst_fb_fsm = 0;
  1276. SENS.sar_amp_ctrl3.amp_short_ref_fsm = 0;
  1277. SENS.sar_amp_ctrl3.amp_short_ref_gnd_fsm = 0;
  1278. SENS.sar_amp_ctrl1.sar_amp_wait1 = 1;
  1279. SENS.sar_amp_ctrl1.sar_amp_wait2 = 1;
  1280. SENS.sar_amp_ctrl2.sar_amp_wait3 = 1;
  1281. #endif
  1282. }
  1283. esp_err_t adc1_i2s_mode_acquire(void)
  1284. {
  1285. //lazy initialization
  1286. //for i2s, block until acquire the lock
  1287. _lock_acquire( &adc1_i2s_lock );
  1288. ESP_LOGD( RTC_MODULE_TAG, "i2s mode takes adc1 lock." );
  1289. portENTER_CRITICAL(&rtc_spinlock);
  1290. #if CONFIG_IDF_TARGET_ESP32
  1291. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  1292. //switch SARADC into DIG channel
  1293. SENS.sar_read_ctrl.sar1_dig_force = 1;
  1294. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1295. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  1296. //switch SARADC into DIG channel
  1297. SENS.sar_meas1_mux.sar1_dig_force = 1;
  1298. #endif
  1299. portEXIT_CRITICAL(&rtc_spinlock);
  1300. return ESP_OK;
  1301. }
  1302. esp_err_t adc1_adc_mode_acquire(void)
  1303. {
  1304. //lazy initialization
  1305. //for adc1, block until acquire the lock
  1306. _lock_acquire( &adc1_i2s_lock );
  1307. ESP_LOGD( RTC_MODULE_TAG, "adc mode takes adc1 lock." );
  1308. portENTER_CRITICAL(&rtc_spinlock);
  1309. // for now the WiFi would use ADC2 and set xpd_sar force on.
  1310. // so we can not reset xpd_sar to fsm mode directly.
  1311. // We should handle this after the synchronization mechanism is established.
  1312. //switch SARADC into RTC channel
  1313. #if CONFIG_IDF_TARGET_ESP32
  1314. SENS.sar_read_ctrl.sar1_dig_force = 0;
  1315. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1316. SENS.sar_meas1_mux.sar1_dig_force = 0;
  1317. #endif
  1318. portEXIT_CRITICAL(&rtc_spinlock);
  1319. return ESP_OK;
  1320. }
  1321. esp_err_t adc1_lock_release(void)
  1322. {
  1323. RTC_MODULE_CHECK((uint32_t*)adc1_i2s_lock != NULL, "adc1 lock release called before acquire", ESP_ERR_INVALID_STATE );
  1324. // for now the WiFi would use ADC2 and set xpd_sar force on.
  1325. // so we can not reset xpd_sar to fsm mode directly.
  1326. // We should handle this after the synchronization mechanism is established.
  1327. _lock_release( &adc1_i2s_lock );
  1328. return ESP_OK;
  1329. }
  1330. int adc1_get_raw(adc1_channel_t channel)
  1331. {
  1332. uint16_t adc_value;
  1333. RTC_MODULE_CHECK(channel < ADC1_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  1334. adc1_adc_mode_acquire();
  1335. adc_power_on();
  1336. portENTER_CRITICAL(&rtc_spinlock);
  1337. //disable other peripherals
  1338. adc1_hall_enable(false);
  1339. adc1_fsm_disable(); //currently the LNA is not open, close it by default
  1340. //set controller
  1341. adc_set_controller( ADC_UNIT_1, ADC_CTRL_RTC );
  1342. //start conversion
  1343. adc_value = adc_convert( ADC_UNIT_1, channel );
  1344. portEXIT_CRITICAL(&rtc_spinlock);
  1345. adc1_lock_release();
  1346. return adc_value;
  1347. }
  1348. void adc1_ulp_enable(void)
  1349. {
  1350. adc_power_on();
  1351. portENTER_CRITICAL(&rtc_spinlock);
  1352. adc_set_controller( ADC_UNIT_1, ADC_CTRL_ULP );
  1353. // since most users do not need LNA and HALL with uLP, we disable them here
  1354. // open them in the uLP if needed.
  1355. adc1_fsm_disable();
  1356. adc1_hall_enable(false);
  1357. portEXIT_CRITICAL(&rtc_spinlock);
  1358. }
  1359. /*---------------------------------------------------------------
  1360. ADC2
  1361. ---------------------------------------------------------------*/
  1362. esp_err_t adc2_pad_get_io_num(adc2_channel_t channel, gpio_num_t *gpio_num)
  1363. {
  1364. RTC_MODULE_CHECK(channel < ADC2_CHANNEL_MAX, "ADC2 Channel Err", ESP_ERR_INVALID_ARG);
  1365. switch (channel) {
  1366. case ADC2_CHANNEL_0:
  1367. *gpio_num = ADC2_CHANNEL_0_GPIO_NUM;
  1368. break;
  1369. case ADC2_CHANNEL_1:
  1370. *gpio_num = ADC2_CHANNEL_1_GPIO_NUM;
  1371. break;
  1372. case ADC2_CHANNEL_2:
  1373. *gpio_num = ADC2_CHANNEL_2_GPIO_NUM;
  1374. break;
  1375. case ADC2_CHANNEL_3:
  1376. *gpio_num = ADC2_CHANNEL_3_GPIO_NUM;
  1377. break;
  1378. case ADC2_CHANNEL_4:
  1379. *gpio_num = ADC2_CHANNEL_4_GPIO_NUM;
  1380. break;
  1381. case ADC2_CHANNEL_5:
  1382. *gpio_num = ADC2_CHANNEL_5_GPIO_NUM;
  1383. break;
  1384. case ADC2_CHANNEL_6:
  1385. *gpio_num = ADC2_CHANNEL_6_GPIO_NUM;
  1386. break;
  1387. case ADC2_CHANNEL_7:
  1388. *gpio_num = ADC2_CHANNEL_7_GPIO_NUM;
  1389. break;
  1390. case ADC2_CHANNEL_8:
  1391. *gpio_num = ADC2_CHANNEL_8_GPIO_NUM;
  1392. break;
  1393. case ADC2_CHANNEL_9:
  1394. *gpio_num = ADC2_CHANNEL_9_GPIO_NUM;
  1395. break;
  1396. default:
  1397. return ESP_ERR_INVALID_ARG;
  1398. }
  1399. return ESP_OK;
  1400. }
  1401. esp_err_t adc2_wifi_acquire(void)
  1402. {
  1403. //lazy initialization
  1404. //for wifi, block until acquire the lock
  1405. _lock_acquire( &adc2_wifi_lock );
  1406. ESP_LOGD( RTC_MODULE_TAG, "Wi-Fi takes adc2 lock." );
  1407. return ESP_OK;
  1408. }
  1409. esp_err_t adc2_wifi_release(void)
  1410. {
  1411. RTC_MODULE_CHECK((uint32_t*)adc2_wifi_lock != NULL, "wifi release called before acquire", ESP_ERR_INVALID_STATE );
  1412. _lock_release( &adc2_wifi_lock );
  1413. ESP_LOGD( RTC_MODULE_TAG, "Wi-Fi returns adc2 lock." );
  1414. return ESP_OK;
  1415. }
  1416. static esp_err_t adc2_pad_init(adc2_channel_t channel)
  1417. {
  1418. gpio_num_t gpio_num = 0;
  1419. ADC2_CHECK_FUNCTION_RET(adc2_pad_get_io_num(channel, &gpio_num));
  1420. ADC2_CHECK_FUNCTION_RET(rtc_gpio_init(gpio_num));
  1421. ADC1_CHECK_FUNCTION_RET(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
  1422. ADC2_CHECK_FUNCTION_RET(gpio_set_pull_mode(gpio_num, GPIO_FLOATING));
  1423. return ESP_OK;
  1424. }
  1425. esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten)
  1426. {
  1427. RTC_MODULE_CHECK(channel < ADC2_CHANNEL_MAX, "ADC2 Channel Err", ESP_ERR_INVALID_ARG);
  1428. RTC_MODULE_CHECK(atten <= ADC_ATTEN_11db, "ADC2 Atten Err", ESP_ERR_INVALID_ARG);
  1429. adc2_pad_init(channel);
  1430. portENTER_CRITICAL( &adc2_spinlock );
  1431. //lazy initialization
  1432. //avoid collision with other tasks
  1433. if ( _lock_try_acquire( &adc2_wifi_lock ) == -1 ) {
  1434. //try the lock, return if failed (wifi using).
  1435. portEXIT_CRITICAL( &adc2_spinlock );
  1436. return ESP_ERR_TIMEOUT;
  1437. }
  1438. SENS.sar_atten2 = ( SENS.sar_atten2 & ~(3<<(channel*2)) ) | ((atten&3) << (channel*2));
  1439. _lock_release( &adc2_wifi_lock );
  1440. portEXIT_CRITICAL( &adc2_spinlock );
  1441. return ESP_OK;
  1442. }
  1443. static inline void adc2_config_width(adc_bits_width_t width_bit)
  1444. {
  1445. portENTER_CRITICAL(&rtc_spinlock);
  1446. #if CONFIG_IDF_TARGET_ESP32
  1447. //sar_start_force shared with ADC1
  1448. SENS.sar_start_force.sar2_bit_width = width_bit;
  1449. //cct set to the same value with PHY
  1450. SENS.sar_start_force.sar2_pwdet_cct = 4;
  1451. portEXIT_CRITICAL(&rtc_spinlock);
  1452. //Invert the adc value,the Output value is invert
  1453. SENS.sar_read_ctrl2.sar2_data_inv = 1;
  1454. //Set The adc sample width,invert adc value,must digital sar2_bit_width[1:0]=3
  1455. SENS.sar_read_ctrl2.sar2_sample_bit = width_bit;
  1456. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1457. //sar_start_force shared with ADC1
  1458. SENS.sar_meas2_ctrl1.sar2_bit_width = width_bit;
  1459. //cct set to the same value with PHY
  1460. SENS.sar_meas2_mux.sar2_pwdet_cct = 4;
  1461. portEXIT_CRITICAL(&rtc_spinlock);
  1462. //Invert the adc value,the Output value is invert
  1463. SENS.sar_reader2_ctrl.sar2_data_inv = 1;
  1464. //Set The adc sample width,invert adc value,must digital sar2_bit_width[1:0]=3
  1465. SENS.sar_reader2_ctrl.sar2_sample_bit = width_bit;
  1466. #endif
  1467. }
  1468. static inline void adc2_dac_disable( adc2_channel_t channel)
  1469. {
  1470. #if CONFIG_IDF_TARGET_ESP32
  1471. if ( channel == ADC2_CHANNEL_8 ) { // the same as DAC channel 1
  1472. dac_ll_power_down( DAC_CHANNEL_1 );
  1473. } else if ( channel == ADC2_CHANNEL_9 ) {
  1474. dac_ll_power_down( DAC_CHANNEL_2 );
  1475. }
  1476. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1477. if ( channel == ADC2_CHANNEL_6 ) { // the same as DAC channel 1
  1478. dac_ll_power_down( DAC_CHANNEL_1 );
  1479. } else if ( channel == ADC2_CHANNEL_7 ) {
  1480. dac_ll_power_down( DAC_CHANNEL_2 );
  1481. }
  1482. #endif
  1483. }
  1484. //registers in critical section with adc1:
  1485. //SENS_SAR_START_FORCE_REG,
  1486. esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int* raw_out)
  1487. {
  1488. uint16_t adc_value = 0;
  1489. RTC_MODULE_CHECK(channel < ADC2_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  1490. //in critical section with whole rtc module
  1491. adc_power_on();
  1492. //avoid collision with other tasks
  1493. portENTER_CRITICAL(&adc2_spinlock);
  1494. //lazy initialization
  1495. //try the lock, return if failed (wifi using).
  1496. if ( _lock_try_acquire( &adc2_wifi_lock ) == -1 ) {
  1497. portEXIT_CRITICAL( &adc2_spinlock );
  1498. return ESP_ERR_TIMEOUT;
  1499. }
  1500. //disable other peripherals
  1501. #ifdef CONFIG_ADC_DISABLE_DAC
  1502. adc2_dac_disable( channel );
  1503. #endif
  1504. // set controller
  1505. // in critical section with whole rtc module
  1506. // because the PWDET use the same registers, place it here.
  1507. adc2_config_width( width_bit );
  1508. adc_set_controller( ADC_UNIT_2, ADC_CTRL_RTC );
  1509. //start converting
  1510. adc_value = adc_convert( ADC_UNIT_2, channel );
  1511. _lock_release( &adc2_wifi_lock );
  1512. portEXIT_CRITICAL(&adc2_spinlock);
  1513. *raw_out = (int)adc_value;
  1514. return ESP_OK;
  1515. }
  1516. esp_err_t adc2_vref_to_gpio(gpio_num_t gpio)
  1517. {
  1518. #if CONFIG_IDF_TARGET_ESP32
  1519. int channel;
  1520. if(gpio == GPIO_NUM_25){
  1521. channel = 8; //Channel 8 bit
  1522. }else if (gpio == GPIO_NUM_26){
  1523. channel = 9; //Channel 9 bit
  1524. }else if (gpio == GPIO_NUM_27){
  1525. channel = 7; //Channel 7 bit
  1526. }else{
  1527. return ESP_ERR_INVALID_ARG;
  1528. }
  1529. //Configure RTC gpio
  1530. rtc_gpio_init(gpio);
  1531. rtc_gpio_set_direction(gpio, RTC_GPIO_MODE_DISABLED);
  1532. rtc_gpio_pullup_dis(gpio);
  1533. rtc_gpio_pulldown_dis(gpio);
  1534. //force fsm
  1535. adc_power_always_on(); //Select power source of ADC
  1536. RTCCNTL.bias_conf.dbg_atten = 0; //Check DBG effect outside sleep mode
  1537. //set dtest (MUX_SEL : 0 -> RTC; 1-> vdd_sar2)
  1538. RTCCNTL.test_mux.dtest_rtc = 1; //Config test mux to route v_ref to ADC2 Channels
  1539. //set ent
  1540. RTCCNTL.test_mux.ent_rtc = 1;
  1541. //set sar2_en_test
  1542. SENS.sar_start_force.sar2_en_test = 1;
  1543. //set sar2 en force
  1544. SENS.sar_meas_start2.sar2_en_pad_force = 1; //Pad bitmap controlled by SW
  1545. //set en_pad for channels 7,8,9 (bits 0x380)
  1546. SENS.sar_meas_start2.sar2_en_pad = 1<<channel;
  1547. #endif
  1548. return ESP_OK;
  1549. }
  1550. /*---------------------------------------------------------------
  1551. HALL SENSOR
  1552. ---------------------------------------------------------------*/
  1553. static inline void adc1_hall_enable(bool enable)
  1554. {
  1555. #if CONFIG_IDF_TARGET_ESP32
  1556. RTCIO.hall_sens.xpd_hall = enable;
  1557. #endif
  1558. }
  1559. static int hall_sensor_get_value(void) //hall sensor without LNA
  1560. {
  1561. int hall_value = 0;
  1562. adc_power_on();
  1563. #if CONFIG_IDF_TARGET_ESP32
  1564. int Sens_Vp0;
  1565. int Sens_Vn0;
  1566. int Sens_Vp1;
  1567. int Sens_Vn1;
  1568. portENTER_CRITICAL(&rtc_spinlock);
  1569. //disable other peripherals
  1570. adc1_fsm_disable();//currently the LNA is not open, close it by default
  1571. adc1_hall_enable(true);
  1572. // set controller
  1573. adc_set_controller( ADC_UNIT_1, ADC_CTRL_RTC );
  1574. // convert for 4 times with different phase and outputs
  1575. RTCIO.hall_sens.hall_phase = 0; // hall phase
  1576. Sens_Vp0 = adc_convert( ADC_UNIT_1, ADC1_CHANNEL_0 );
  1577. Sens_Vn0 = adc_convert( ADC_UNIT_1, ADC1_CHANNEL_3 );
  1578. RTCIO.hall_sens.hall_phase = 1;
  1579. Sens_Vp1 = adc_convert( ADC_UNIT_1, ADC1_CHANNEL_0 );
  1580. Sens_Vn1 = adc_convert( ADC_UNIT_1, ADC1_CHANNEL_3 );
  1581. portEXIT_CRITICAL(&rtc_spinlock);
  1582. hall_value = (Sens_Vp1 - Sens_Vp0) - (Sens_Vn1 - Sens_Vn0);
  1583. #endif
  1584. return hall_value;
  1585. }
  1586. int hall_sensor_read(void)
  1587. {
  1588. adc_gpio_init(ADC_UNIT_1, ADC1_CHANNEL_0);
  1589. adc_gpio_init(ADC_UNIT_1, ADC1_CHANNEL_3);
  1590. adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_0);
  1591. adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_0);
  1592. return hall_sensor_get_value();
  1593. }
  1594. /*---------------------------------------------------------------
  1595. INTERRUPT HANDLER
  1596. ---------------------------------------------------------------*/
  1597. typedef struct rtc_isr_handler_ {
  1598. uint32_t mask;
  1599. intr_handler_t handler;
  1600. void* handler_arg;
  1601. SLIST_ENTRY(rtc_isr_handler_) next;
  1602. } rtc_isr_handler_t;
  1603. static SLIST_HEAD(rtc_isr_handler_list_, rtc_isr_handler_) s_rtc_isr_handler_list =
  1604. SLIST_HEAD_INITIALIZER(s_rtc_isr_handler_list);
  1605. portMUX_TYPE s_rtc_isr_handler_list_lock = portMUX_INITIALIZER_UNLOCKED;
  1606. static intr_handle_t s_rtc_isr_handle;
  1607. static void rtc_isr(void* arg)
  1608. {
  1609. uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
  1610. rtc_isr_handler_t* it;
  1611. portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  1612. SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
  1613. if (it->mask & status) {
  1614. portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  1615. (*it->handler)(it->handler_arg);
  1616. portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  1617. }
  1618. }
  1619. portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  1620. REG_WRITE(RTC_CNTL_INT_CLR_REG, status);
  1621. }
  1622. static esp_err_t rtc_isr_ensure_installed(void)
  1623. {
  1624. esp_err_t err = ESP_OK;
  1625. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  1626. if (s_rtc_isr_handle) {
  1627. goto out;
  1628. }
  1629. REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
  1630. REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
  1631. err = esp_intr_alloc(ETS_RTC_CORE_INTR_SOURCE, 0, &rtc_isr, NULL, &s_rtc_isr_handle);
  1632. if (err != ESP_OK) {
  1633. goto out;
  1634. }
  1635. out:
  1636. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  1637. return err;
  1638. }
  1639. esp_err_t rtc_isr_register(intr_handler_t handler, void* handler_arg, uint32_t rtc_intr_mask)
  1640. {
  1641. esp_err_t err = rtc_isr_ensure_installed();
  1642. if (err != ESP_OK) {
  1643. return err;
  1644. }
  1645. rtc_isr_handler_t* item = malloc(sizeof(*item));
  1646. if (item == NULL) {
  1647. return ESP_ERR_NO_MEM;
  1648. }
  1649. item->handler = handler;
  1650. item->handler_arg = handler_arg;
  1651. item->mask = rtc_intr_mask;
  1652. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  1653. SLIST_INSERT_HEAD(&s_rtc_isr_handler_list, item, next);
  1654. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  1655. return ESP_OK;
  1656. }
  1657. esp_err_t rtc_isr_deregister(intr_handler_t handler, void* handler_arg)
  1658. {
  1659. rtc_isr_handler_t* it;
  1660. rtc_isr_handler_t* prev = NULL;
  1661. bool found = false;
  1662. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  1663. SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
  1664. if (it->handler == handler && it->handler_arg == handler_arg) {
  1665. if (it == SLIST_FIRST(&s_rtc_isr_handler_list)) {
  1666. SLIST_REMOVE_HEAD(&s_rtc_isr_handler_list, next);
  1667. } else {
  1668. SLIST_REMOVE_AFTER(prev, next);
  1669. }
  1670. found = true;
  1671. free(it);
  1672. break;
  1673. }
  1674. prev = it;
  1675. }
  1676. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  1677. return found ? ESP_OK : ESP_ERR_INVALID_STATE;
  1678. }