rtc_module.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  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 "adc1_i2s_private.h"
  36. #include "sdkconfig.h"
  37. #if CONFIG_IDF_TARGET_ESP32
  38. #include "esp32/rom/ets_sys.h"
  39. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  40. #include "esp32s2beta/rom/ets_sys.h"
  41. #endif
  42. #ifndef NDEBUG
  43. // Enable built-in checks in queue.h in debug builds
  44. #define INVARIANTS
  45. #endif
  46. #include "sys/queue.h"
  47. #define ADC_FSM_RSTB_WAIT_DEFAULT (8)
  48. #define ADC_FSM_START_WAIT_DEFAULT (5)
  49. #define ADC_FSM_STANDBY_WAIT_DEFAULT (100)
  50. #define ADC_FSM_TIME_KEEP (-1)
  51. #define ADC_MAX_MEAS_NUM_DEFAULT (255)
  52. #define ADC_MEAS_NUM_LIM_DEFAULT (1)
  53. #define SAR_ADC_CLK_DIV_DEFUALT (2)
  54. #define ADC_PATT_LEN_MAX (16)
  55. #define TOUCH_PAD_FILTER_FACTOR_DEFAULT (4) // IIR filter coefficient.
  56. #define TOUCH_PAD_SHIFT_DEFAULT (4) // Increase computing accuracy.
  57. #define TOUCH_PAD_SHIFT_ROUND_DEFAULT (8) // ROUND = 2^(n-1); rounding off for fractional.
  58. #define DAC_ERR_STR_CHANNEL_ERROR "DAC channel error"
  59. static const char *RTC_MODULE_TAG = "RTC_MODULE";
  60. #define RTC_MODULE_CHECK(a, str, ret_val) if (!(a)) { \
  61. ESP_LOGE(RTC_MODULE_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
  62. return (ret_val); \
  63. }
  64. #define RTC_RES_CHECK(res, ret_val) if ( (a) != ESP_OK) { \
  65. ESP_LOGE(RTC_MODULE_TAG,"%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__); \
  66. return (ret_val); \
  67. }
  68. #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)
  69. #define ADC1_CHECK_FUNCTION_RET(fun_ret) if(fun_ret!=ESP_OK){\
  70. ESP_LOGE(RTC_MODULE_TAG,"%s:%d\n",__FUNCTION__,__LINE__);\
  71. return ESP_FAIL;\
  72. }
  73. #define ADC2_CHECK_FUNCTION_RET(fun_ret) do { if(fun_ret!=ESP_OK){\
  74. ESP_LOGE(RTC_MODULE_TAG,"%s:%d\n",__FUNCTION__,__LINE__);\
  75. return ESP_FAIL;\
  76. } }while (0)
  77. portMUX_TYPE rtc_spinlock = portMUX_INITIALIZER_UNLOCKED;
  78. #if CONFIG_IDF_TARGET_ESP32
  79. static SemaphoreHandle_t rtc_touch_mux = NULL;
  80. #endif
  81. /*
  82. In ADC2, there're two locks used for different cases:
  83. 1. lock shared with app and WIFI:
  84. when wifi using the ADC2, we assume it will never stop,
  85. so app checks the lock and returns immediately if failed.
  86. 2. lock shared between tasks:
  87. when several tasks sharing the ADC2, we want to guarantee
  88. all the requests will be handled.
  89. Since conversions are short (about 31us), app returns the lock very soon,
  90. we use a spinlock to stand there waiting to do conversions one by one.
  91. adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock.
  92. */
  93. //prevent ADC2 being used by wifi and other tasks at the same time.
  94. static _lock_t adc2_wifi_lock;
  95. //prevent ADC2 being used by tasks (regardless of WIFI)
  96. portMUX_TYPE adc2_spinlock = portMUX_INITIALIZER_UNLOCKED;
  97. //prevent ADC1 being used by I2S dma and other tasks at the same time.
  98. static _lock_t adc1_i2s_lock;
  99. #if CONFIG_IDF_TARGET_ESP32
  100. typedef struct {
  101. TimerHandle_t timer;
  102. uint16_t filtered_val[TOUCH_PAD_MAX];
  103. uint16_t raw_val[TOUCH_PAD_MAX];
  104. uint32_t filter_period;
  105. uint32_t period;
  106. bool enable;
  107. } touch_pad_filter_t;
  108. static touch_pad_filter_t *s_touch_pad_filter = NULL;
  109. // check if touch pad be inited.
  110. static uint16_t s_touch_pad_init_bit = 0x0000;
  111. static filter_cb_t s_filter_cb = NULL;
  112. #endif
  113. typedef enum {
  114. ADC_CTRL_RTC = 0,
  115. ADC_CTRL_ULP = 1,
  116. ADC_CTRL_DIG = 2,
  117. ADC2_CTRL_PWDET = 3,
  118. } adc_controller_t ;
  119. static const char TAG[] = "adc";
  120. static inline void dac_output_set_enable(dac_channel_t channel, bool enable);
  121. static inline void adc1_hall_enable(bool enable);
  122. /*---------------------------------------------------------------
  123. RTC IO
  124. ---------------------------------------------------------------*/
  125. esp_err_t rtc_gpio_init(gpio_num_t gpio_num)
  126. {
  127. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  128. portENTER_CRITICAL(&rtc_spinlock);
  129. #if CONFIG_IDF_TARGET_ESP32
  130. // 0: GPIO connected to digital GPIO module. 1: GPIO connected to analog RTC module.
  131. SET_PERI_REG_MASK(rtc_gpio_desc[gpio_num].reg, (rtc_gpio_desc[gpio_num].mux));
  132. //0:RTC FUNCIOTN 1,2,3:Reserved
  133. SET_PERI_REG_BITS(rtc_gpio_desc[gpio_num].reg, RTC_IO_TOUCH_PAD1_FUN_SEL_V, 0x0, rtc_gpio_desc[gpio_num].func);
  134. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  135. rtc_gpio_reg[gpio_num]->mux_sel = 0x1;
  136. rtc_gpio_reg[gpio_num]->fun_sel = 0x0;
  137. #endif
  138. portEXIT_CRITICAL(&rtc_spinlock);
  139. return ESP_OK;
  140. }
  141. esp_err_t rtc_gpio_deinit(gpio_num_t gpio_num)
  142. {
  143. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  144. portENTER_CRITICAL(&rtc_spinlock);
  145. //Select Gpio as Digital Gpio
  146. #if CONFIG_IDF_TARGET_ESP32
  147. CLEAR_PERI_REG_MASK(rtc_gpio_desc[gpio_num].reg, (rtc_gpio_desc[gpio_num].mux));
  148. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  149. rtc_gpio_reg[gpio_num]->mux_sel = 0x0;
  150. #endif
  151. portEXIT_CRITICAL(&rtc_spinlock);
  152. return ESP_OK;
  153. }
  154. static esp_err_t rtc_gpio_output_enable(gpio_num_t gpio_num)
  155. {
  156. #if CONFIG_IDF_TARGET_ESP32
  157. int rtc_gpio_num = rtc_gpio_desc[gpio_num].rtc_num;
  158. RTC_MODULE_CHECK(rtc_gpio_num != -1, "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  159. SET_PERI_REG_MASK(RTC_GPIO_ENABLE_W1TS_REG, (1 << (rtc_gpio_num + RTC_GPIO_ENABLE_W1TS_S)));
  160. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  161. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  162. SET_PERI_REG_MASK(RTC_GPIO_ENABLE_W1TS_REG, (1 << ( gpio_num + RTC_GPIO_ENABLE_W1TS_S)));
  163. #endif
  164. return ESP_OK;
  165. }
  166. static esp_err_t rtc_gpio_output_disable(gpio_num_t gpio_num)
  167. {
  168. #if CONFIG_IDF_TARGET_ESP32
  169. int rtc_gpio_num = rtc_gpio_desc[gpio_num].rtc_num;
  170. RTC_MODULE_CHECK(rtc_gpio_num != -1, "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  171. SET_PERI_REG_MASK(RTC_GPIO_ENABLE_W1TC_REG, (1 << ( rtc_gpio_num + RTC_GPIO_ENABLE_W1TC_S)));
  172. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  173. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  174. SET_PERI_REG_MASK(RTC_GPIO_ENABLE_W1TC_REG, (1 << ( gpio_num + RTC_GPIO_ENABLE_W1TC_S)));
  175. #endif
  176. return ESP_OK;
  177. }
  178. static esp_err_t rtc_gpio_input_enable(gpio_num_t gpio_num)
  179. {
  180. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  181. portENTER_CRITICAL(&rtc_spinlock);
  182. #if CONFIG_IDF_TARGET_ESP32
  183. SET_PERI_REG_MASK(rtc_gpio_desc[gpio_num].reg, rtc_gpio_desc[gpio_num].ie);
  184. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  185. rtc_gpio_reg[gpio_num]->fun_ie = 1;
  186. #endif
  187. portEXIT_CRITICAL(&rtc_spinlock);
  188. return ESP_OK;
  189. }
  190. static esp_err_t rtc_gpio_input_disable(gpio_num_t gpio_num)
  191. {
  192. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  193. portENTER_CRITICAL(&rtc_spinlock);
  194. #if CONFIG_IDF_TARGET_ESP32
  195. CLEAR_PERI_REG_MASK(rtc_gpio_desc[gpio_num].reg, rtc_gpio_desc[gpio_num].ie);
  196. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  197. rtc_gpio_reg[gpio_num]->fun_ie = 0;
  198. #endif
  199. portEXIT_CRITICAL(&rtc_spinlock);
  200. return ESP_OK;
  201. }
  202. #if CONFIG_IDF_TARGET_ESP32S2BETA
  203. esp_err_t rtc_gpio_sleep_output_enable(gpio_num_t gpio_num, bool output)
  204. {
  205. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  206. rtc_gpio_reg[gpio_num]->slp_sel = 1;
  207. rtc_gpio_reg[gpio_num]->slp_oe = output;
  208. return ESP_OK;
  209. }
  210. esp_err_t rtc_gpio_sleep_input_enable(gpio_num_t gpio_num, bool input)
  211. {
  212. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  213. rtc_gpio_reg[gpio_num]->slp_sel = 1;
  214. rtc_gpio_reg[gpio_num]->slp_ie = input;
  215. return ESP_OK;
  216. }
  217. esp_err_t rtc_gpio_sleep_mode_disable(gpio_num_t gpio_num)
  218. {
  219. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  220. rtc_gpio_reg[gpio_num]->slp_sel = 0;
  221. return ESP_OK;
  222. }
  223. #endif
  224. esp_err_t rtc_gpio_set_level(gpio_num_t gpio_num, uint32_t level)
  225. {
  226. #if CONFIG_IDF_TARGET_ESP32
  227. int rtc_gpio_num = rtc_gpio_num = rtc_gpio_desc[gpio_num].rtc_num;;
  228. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  229. if (level) {
  230. WRITE_PERI_REG(RTC_GPIO_OUT_W1TS_REG, (1 << (rtc_gpio_num + RTC_GPIO_OUT_DATA_W1TS_S)));
  231. } else {
  232. WRITE_PERI_REG(RTC_GPIO_OUT_W1TC_REG, (1 << (rtc_gpio_num + RTC_GPIO_OUT_DATA_W1TC_S)));
  233. }
  234. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  235. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  236. if (level) {
  237. WRITE_PERI_REG(RTC_GPIO_OUT_W1TS_REG, (1 << (gpio_num + RTC_GPIO_OUT_DATA_W1TS_S)));
  238. } else {
  239. WRITE_PERI_REG(RTC_GPIO_OUT_W1TC_REG, (1 << (gpio_num + RTC_GPIO_OUT_DATA_W1TC_S)));
  240. }
  241. #endif
  242. return ESP_OK;
  243. }
  244. uint32_t rtc_gpio_get_level(gpio_num_t gpio_num)
  245. {
  246. uint32_t level = 0;
  247. #if CONFIG_IDF_TARGET_ESP32
  248. int rtc_gpio_num = rtc_gpio_desc[gpio_num].rtc_num;
  249. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  250. portENTER_CRITICAL(&rtc_spinlock);
  251. level = READ_PERI_REG(RTC_GPIO_IN_REG);
  252. portEXIT_CRITICAL(&rtc_spinlock);
  253. return ((level >> (RTC_GPIO_IN_NEXT_S + rtc_gpio_num)) & 0x01);
  254. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  255. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  256. portENTER_CRITICAL(&rtc_spinlock);
  257. level = RTCIO.in_val.in;
  258. portEXIT_CRITICAL(&rtc_spinlock);
  259. return ((level >> gpio_num) & 0x1);
  260. #endif
  261. }
  262. esp_err_t rtc_gpio_set_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t strength)
  263. {
  264. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  265. RTC_MODULE_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Output pad only", ESP_ERR_INVALID_ARG);
  266. RTC_MODULE_CHECK(strength < GPIO_DRIVE_CAP_MAX, "GPIO drive capability error", ESP_ERR_INVALID_ARG);
  267. portENTER_CRITICAL(&rtc_spinlock);
  268. #if CONFIG_IDF_TARGET_ESP32
  269. SET_PERI_REG_BITS(rtc_gpio_desc[gpio_num].reg, rtc_gpio_desc[gpio_num].drv_v, strength, rtc_gpio_desc[gpio_num].drv_s);
  270. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  271. rtc_gpio_reg[gpio_num]->drv = strength;
  272. #endif
  273. portEXIT_CRITICAL(&rtc_spinlock);
  274. return ESP_OK;
  275. }
  276. esp_err_t rtc_gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t* strength)
  277. {
  278. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  279. RTC_MODULE_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Output pad only", ESP_ERR_INVALID_ARG);
  280. RTC_MODULE_CHECK(strength != NULL, "GPIO drive pointer error", ESP_ERR_INVALID_ARG);
  281. #if CONFIG_IDF_TARGET_ESP32
  282. *strength = GET_PERI_REG_BITS2(rtc_gpio_desc[gpio_num].reg, rtc_gpio_desc[gpio_num].drv_v, rtc_gpio_desc[gpio_num].drv_s);
  283. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  284. *strength = rtc_gpio_reg[gpio_num]->drv;
  285. #endif
  286. return ESP_OK;
  287. }
  288. esp_err_t rtc_gpio_set_direction(gpio_num_t gpio_num, rtc_gpio_mode_t mode)
  289. {
  290. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  291. switch (mode) {
  292. case RTC_GPIO_MODE_INPUT_ONLY:
  293. rtc_gpio_output_disable(gpio_num);
  294. rtc_gpio_input_enable(gpio_num);
  295. break;
  296. case RTC_GPIO_MODE_OUTPUT_ONLY:
  297. rtc_gpio_output_enable(gpio_num);
  298. rtc_gpio_input_disable(gpio_num);
  299. break;
  300. case RTC_GPIO_MODE_INPUT_OUTPUT:
  301. rtc_gpio_output_enable(gpio_num);
  302. rtc_gpio_input_enable(gpio_num);
  303. break;
  304. case RTC_GPIO_MODE_DISABLED:
  305. rtc_gpio_output_disable(gpio_num);
  306. rtc_gpio_input_disable(gpio_num);
  307. break;
  308. }
  309. return ESP_OK;
  310. }
  311. esp_err_t rtc_gpio_pullup_en(gpio_num_t gpio_num)
  312. {
  313. #if CONFIG_IDF_TARGET_ESP32
  314. //this is a digital pad
  315. if (rtc_gpio_desc[gpio_num].pullup == 0) {
  316. return ESP_ERR_INVALID_ARG;
  317. }
  318. #endif
  319. //this is a rtc pad
  320. portENTER_CRITICAL(&rtc_spinlock);
  321. #if CONFIG_IDF_TARGET_ESP32
  322. SET_PERI_REG_MASK(rtc_gpio_desc[gpio_num].reg, rtc_gpio_desc[gpio_num].pullup);
  323. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  324. rtc_gpio_reg[gpio_num]->rue = 0x1;
  325. #endif
  326. portEXIT_CRITICAL(&rtc_spinlock);
  327. return ESP_OK;
  328. }
  329. #if CONFIG_IDF_TARGET_ESP32S2BETA
  330. esp_err_t rtc_gpio_set_output_mode(gpio_num_t gpio_num, rtc_io_out_mode_t mode)
  331. {
  332. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  333. portENTER_CRITICAL(&rtc_spinlock);
  334. RTCIO.pin[gpio_num].pad_driver = mode;
  335. portEXIT_CRITICAL(&rtc_spinlock);
  336. return ESP_OK;
  337. }
  338. esp_err_t rtc_gpio_get_output_mode(gpio_num_t gpio_num, rtc_io_out_mode_t *mode)
  339. {
  340. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  341. *mode = RTCIO.pin[gpio_num].pad_driver;
  342. return ESP_OK;
  343. }
  344. #endif
  345. esp_err_t rtc_gpio_pulldown_en(gpio_num_t gpio_num)
  346. {
  347. #if CONFIG_IDF_TARGET_ESP32
  348. //this is a digital pad
  349. if (rtc_gpio_desc[gpio_num].pulldown == 0) {
  350. return ESP_ERR_INVALID_ARG;
  351. }
  352. //this is a rtc pad
  353. portENTER_CRITICAL(&rtc_spinlock);
  354. SET_PERI_REG_MASK(rtc_gpio_desc[gpio_num].reg, rtc_gpio_desc[gpio_num].pulldown);
  355. portEXIT_CRITICAL(&rtc_spinlock);
  356. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  357. portENTER_CRITICAL(&rtc_spinlock);
  358. rtc_gpio_reg[gpio_num]->rde = 0x1;
  359. portEXIT_CRITICAL(&rtc_spinlock);
  360. #endif
  361. return ESP_OK;
  362. }
  363. esp_err_t rtc_gpio_pullup_dis(gpio_num_t gpio_num)
  364. {
  365. #if CONFIG_IDF_TARGET_ESP32
  366. //this is a digital pad
  367. if ( rtc_gpio_desc[gpio_num].pullup == 0 ) {
  368. return ESP_ERR_INVALID_ARG;
  369. }
  370. //this is a rtc pad
  371. portENTER_CRITICAL(&rtc_spinlock);
  372. CLEAR_PERI_REG_MASK(rtc_gpio_desc[gpio_num].reg, rtc_gpio_desc[gpio_num].pullup);
  373. portEXIT_CRITICAL(&rtc_spinlock);
  374. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  375. portENTER_CRITICAL(&rtc_spinlock);
  376. rtc_gpio_reg[gpio_num]->rue = 0x0;
  377. portEXIT_CRITICAL(&rtc_spinlock);
  378. #endif
  379. return ESP_OK;
  380. }
  381. esp_err_t rtc_gpio_pulldown_dis(gpio_num_t gpio_num)
  382. {
  383. #if CONFIG_IDF_TARGET_ESP32
  384. //this is a digital pad
  385. if (rtc_gpio_desc[gpio_num].pulldown == 0) {
  386. return ESP_ERR_INVALID_ARG;
  387. }
  388. //this is a rtc pad
  389. portENTER_CRITICAL(&rtc_spinlock);
  390. CLEAR_PERI_REG_MASK(rtc_gpio_desc[gpio_num].reg, rtc_gpio_desc[gpio_num].pulldown);
  391. portEXIT_CRITICAL(&rtc_spinlock);
  392. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  393. portENTER_CRITICAL(&rtc_spinlock);
  394. rtc_gpio_reg[gpio_num]->rde = 0x0;
  395. portEXIT_CRITICAL(&rtc_spinlock);
  396. #endif
  397. return ESP_OK;
  398. }
  399. esp_err_t rtc_gpio_hold_en(gpio_num_t gpio_num)
  400. {
  401. #if CONFIG_IDF_TARGET_ESP32
  402. // check if an RTC IO
  403. if (rtc_gpio_desc[gpio_num].pullup == 0) {
  404. return ESP_ERR_INVALID_ARG;
  405. }
  406. portENTER_CRITICAL(&rtc_spinlock);
  407. SET_PERI_REG_MASK(rtc_gpio_desc[gpio_num].reg, rtc_gpio_desc[gpio_num].hold);
  408. portEXIT_CRITICAL(&rtc_spinlock);
  409. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  410. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  411. portENTER_CRITICAL(&rtc_spinlock);
  412. RTCCNTL.pad_hold.val |= BIT(gpio_num);
  413. portEXIT_CRITICAL(&rtc_spinlock);
  414. #endif
  415. return ESP_OK;
  416. }
  417. esp_err_t rtc_gpio_hold_dis(gpio_num_t gpio_num)
  418. {
  419. #if CONFIG_IDF_TARGET_ESP32
  420. // check if an RTC IO
  421. if (rtc_gpio_desc[gpio_num].pullup == 0) {
  422. return ESP_ERR_INVALID_ARG;
  423. }
  424. portENTER_CRITICAL(&rtc_spinlock);
  425. CLEAR_PERI_REG_MASK(rtc_gpio_desc[gpio_num].reg, rtc_gpio_desc[gpio_num].hold);
  426. portEXIT_CRITICAL(&rtc_spinlock);
  427. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  428. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  429. portENTER_CRITICAL(&rtc_spinlock);
  430. RTCCNTL.pad_hold.val &= ~(BIT(gpio_num));
  431. portEXIT_CRITICAL(&rtc_spinlock);
  432. #endif
  433. return ESP_OK;
  434. }
  435. esp_err_t rtc_gpio_isolate(gpio_num_t gpio_num)
  436. {
  437. #if CONFIG_IDF_TARGET_ESP32
  438. if (rtc_gpio_desc[gpio_num].reg == 0) {
  439. return ESP_ERR_INVALID_ARG;
  440. }
  441. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  442. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  443. #endif
  444. rtc_gpio_pullup_dis(gpio_num);
  445. rtc_gpio_pulldown_dis(gpio_num);
  446. rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED);
  447. rtc_gpio_hold_en(gpio_num);
  448. return ESP_OK;
  449. }
  450. void rtc_gpio_force_hold_dis_all(void)
  451. {
  452. #if CONFIG_IDF_TARGET_ESP32
  453. for (int gpio = 0; gpio < GPIO_PIN_COUNT; ++gpio) {
  454. const rtc_gpio_desc_t* desc = &rtc_gpio_desc[gpio];
  455. if (desc->hold_force != 0) {
  456. REG_CLR_BIT(RTC_CNTL_HOLD_FORCE_REG, desc->hold_force);
  457. }
  458. }
  459. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  460. portENTER_CRITICAL(&rtc_spinlock);
  461. RTCCNTL.rtc_pwc.rtc_pad_force_hold = 0;
  462. portEXIT_CRITICAL(&rtc_spinlock);
  463. #endif
  464. }
  465. esp_err_t rtc_gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
  466. {
  467. #if CONFIG_IDF_TARGET_ESP32
  468. int rtc_num = rtc_gpio_desc[gpio_num].rtc_num;
  469. if (rtc_num < 0) {
  470. return ESP_ERR_INVALID_ARG;
  471. }
  472. if (( intr_type != GPIO_INTR_LOW_LEVEL ) && ( intr_type != GPIO_INTR_HIGH_LEVEL )) {
  473. return ESP_ERR_INVALID_ARG;
  474. }
  475. uint32_t reg = RTC_GPIO_PIN0_REG + rtc_num * sizeof(uint32_t);
  476. /* each pin has its own register, spinlock not needed */
  477. REG_SET_BIT(reg, RTC_GPIO_PIN0_WAKEUP_ENABLE);
  478. REG_SET_FIELD(reg, RTC_GPIO_PIN0_INT_TYPE, intr_type);
  479. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  480. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  481. if (( intr_type != GPIO_INTR_LOW_LEVEL ) && ( intr_type != GPIO_INTR_HIGH_LEVEL )) {
  482. return ESP_ERR_INVALID_ARG;
  483. }
  484. /* each pin has its own register, spinlock not needed */
  485. RTCIO.pin[gpio_num].wakeup_enable = 1;
  486. RTCIO.pin[gpio_num].int_type = intr_type;
  487. #endif
  488. return ESP_OK;
  489. }
  490. esp_err_t rtc_gpio_wakeup_disable(gpio_num_t gpio_num)
  491. {
  492. #if CONFIG_IDF_TARGET_ESP32
  493. int rtc_num = rtc_gpio_desc[gpio_num].rtc_num;
  494. if (rtc_num < 0) {
  495. return ESP_ERR_INVALID_ARG;
  496. }
  497. uint32_t reg = RTC_GPIO_PIN0_REG + rtc_num * sizeof(uint32_t);
  498. /* each pin has its own register, spinlock not needed */
  499. REG_CLR_BIT(reg, RTC_GPIO_PIN0_WAKEUP_ENABLE);
  500. REG_SET_FIELD(reg, RTC_GPIO_PIN0_INT_TYPE, 0);
  501. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  502. RTC_MODULE_CHECK(rtc_gpio_is_valid_gpio(gpio_num), "RTC_GPIO number error", ESP_ERR_INVALID_ARG);
  503. /* each pin has its own register, spinlock not needed */
  504. RTCIO.pin[gpio_num].wakeup_enable = 0;
  505. RTCIO.pin[gpio_num].int_type = 0;
  506. #endif
  507. return ESP_OK;
  508. }
  509. #if CONFIG_IDF_TARGET_ESP32S2BETA
  510. esp_err_t rtc_gpio_force_hold_all()
  511. {
  512. portENTER_CRITICAL(&rtc_spinlock);
  513. RTCCNTL.rtc_pwc.rtc_pad_force_hold = 1;
  514. portEXIT_CRITICAL(&rtc_spinlock);
  515. return ESP_OK;
  516. }
  517. #endif
  518. #if CONFIG_IDF_TARGET_ESP32
  519. /*---------------------------------------------------------------
  520. Touch Pad
  521. ---------------------------------------------------------------*/
  522. //Some register bits of touch sensor 8 and 9 are mismatched, we need to swap the bits.
  523. #define BITSWAP(data, n, m) (((data >> n) & 0x1) == ((data >> m) & 0x1) ? (data) : ((data) ^ ((0x1 <<n) | (0x1 << m))))
  524. #define TOUCH_BITS_SWAP(v) BITSWAP(v, TOUCH_PAD_NUM8, TOUCH_PAD_NUM9)
  525. static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, touch_fsm_mode_t mode);
  526. //Some registers of touch sensor 8 and 9 are mismatched, we need to swap register index
  527. inline static touch_pad_t touch_pad_num_wrap(touch_pad_t touch_num)
  528. {
  529. if (touch_num == TOUCH_PAD_NUM8) {
  530. return TOUCH_PAD_NUM9;
  531. } else if (touch_num == TOUCH_PAD_NUM9) {
  532. return TOUCH_PAD_NUM8;
  533. }
  534. return touch_num;
  535. }
  536. esp_err_t touch_pad_isr_handler_register(void (*fn)(void *), void *arg, int no_use, intr_handle_t *handle_no_use)
  537. {
  538. RTC_MODULE_CHECK(fn, "Touch_Pad ISR null", ESP_ERR_INVALID_ARG);
  539. #if CONFIG_IDF_TARGET_ESP32
  540. return rtc_isr_register(fn, arg, RTC_CNTL_TOUCH_INT_ST_M);
  541. #else
  542. return ESP_FAIL;
  543. #endif
  544. }
  545. esp_err_t touch_pad_isr_register(intr_handler_t fn, void* arg)
  546. {
  547. RTC_MODULE_CHECK(fn, "Touch_Pad ISR null", ESP_ERR_INVALID_ARG);
  548. #if CONFIG_IDF_TARGET_ESP32
  549. return rtc_isr_register(fn, arg, RTC_CNTL_TOUCH_INT_ST_M);
  550. #else
  551. return ESP_FAIL;
  552. #endif
  553. }
  554. esp_err_t touch_pad_isr_deregister(intr_handler_t fn, void *arg)
  555. {
  556. return rtc_isr_deregister(fn, arg);
  557. }
  558. static esp_err_t touch_pad_get_io_num(touch_pad_t touch_num, gpio_num_t *gpio_num)
  559. {
  560. switch (touch_num) {
  561. case TOUCH_PAD_NUM0:
  562. *gpio_num = TOUCH_PAD_NUM0_GPIO_NUM;
  563. break;
  564. case TOUCH_PAD_NUM1:
  565. *gpio_num = TOUCH_PAD_NUM1_GPIO_NUM;
  566. break;
  567. case TOUCH_PAD_NUM2:
  568. *gpio_num = TOUCH_PAD_NUM2_GPIO_NUM;
  569. break;
  570. case TOUCH_PAD_NUM3:
  571. *gpio_num = TOUCH_PAD_NUM3_GPIO_NUM;
  572. break;
  573. case TOUCH_PAD_NUM4:
  574. *gpio_num = TOUCH_PAD_NUM4_GPIO_NUM;
  575. break;
  576. case TOUCH_PAD_NUM5:
  577. *gpio_num = TOUCH_PAD_NUM5_GPIO_NUM;
  578. break;
  579. case TOUCH_PAD_NUM6:
  580. *gpio_num = TOUCH_PAD_NUM6_GPIO_NUM;
  581. break;
  582. case TOUCH_PAD_NUM7:
  583. *gpio_num = TOUCH_PAD_NUM7_GPIO_NUM;
  584. break;
  585. case TOUCH_PAD_NUM8:
  586. *gpio_num = TOUCH_PAD_NUM8_GPIO_NUM;
  587. break;
  588. case TOUCH_PAD_NUM9:
  589. *gpio_num = TOUCH_PAD_NUM9_GPIO_NUM;
  590. break;
  591. default:
  592. return ESP_ERR_INVALID_ARG;
  593. }
  594. return ESP_OK;
  595. }
  596. static uint32_t _touch_filter_iir(uint32_t in_now, uint32_t out_last, uint32_t k)
  597. {
  598. if (k == 0) {
  599. return in_now;
  600. } else {
  601. uint32_t out_now = (in_now + (k - 1) * out_last) / k;
  602. return out_now;
  603. }
  604. }
  605. esp_err_t touch_pad_set_filter_read_cb(filter_cb_t read_cb)
  606. {
  607. s_filter_cb = read_cb;
  608. return ESP_OK;
  609. }
  610. static void touch_pad_filter_cb(void *arg)
  611. {
  612. static uint32_t s_filtered_temp[TOUCH_PAD_MAX] = {0};
  613. if (s_touch_pad_filter == NULL || rtc_touch_mux == NULL) {
  614. return;
  615. }
  616. uint16_t val = 0;
  617. touch_fsm_mode_t mode;
  618. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  619. touch_pad_get_fsm_mode(&mode);
  620. for (int i = 0; i < TOUCH_PAD_MAX; i++) {
  621. if ((s_touch_pad_init_bit >> i) & 0x1) {
  622. _touch_pad_read(i, &val, mode);
  623. s_touch_pad_filter->raw_val[i] = val;
  624. s_filtered_temp[i] = s_filtered_temp[i] == 0 ? ((uint32_t)val << TOUCH_PAD_SHIFT_DEFAULT) : s_filtered_temp[i];
  625. s_filtered_temp[i] = _touch_filter_iir((val << TOUCH_PAD_SHIFT_DEFAULT),
  626. s_filtered_temp[i], TOUCH_PAD_FILTER_FACTOR_DEFAULT);
  627. s_touch_pad_filter->filtered_val[i] = (s_filtered_temp[i] + TOUCH_PAD_SHIFT_ROUND_DEFAULT) >> TOUCH_PAD_SHIFT_DEFAULT;
  628. }
  629. }
  630. xTimerReset(s_touch_pad_filter->timer, portMAX_DELAY);
  631. xSemaphoreGive(rtc_touch_mux);
  632. if(s_filter_cb != NULL) {
  633. //return the raw data and filtered data.
  634. s_filter_cb(s_touch_pad_filter->raw_val, s_touch_pad_filter->filtered_val);
  635. }
  636. }
  637. esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_cycle)
  638. {
  639. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  640. portENTER_CRITICAL(&rtc_spinlock);
  641. //touch sensor sleep cycle Time = sleep_cycle / RTC_SLOW_CLK( can be 150k or 32k depending on the options)
  642. SENS.sar_touch_ctrl2.touch_sleep_cycles = sleep_cycle;
  643. //touch sensor measure time= meas_cycle / 8Mhz
  644. SENS.sar_touch_ctrl1.touch_meas_delay = meas_cycle;
  645. //the waiting cycles (in 8MHz) between TOUCH_START and TOUCH_XPD
  646. SENS.sar_touch_ctrl1.touch_xpd_wait = TOUCH_PAD_MEASURE_WAIT_DEFAULT;
  647. portEXIT_CRITICAL(&rtc_spinlock);
  648. xSemaphoreGive(rtc_touch_mux);
  649. return ESP_OK;
  650. }
  651. esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_cycle)
  652. {
  653. portENTER_CRITICAL(&rtc_spinlock);
  654. if (sleep_cycle) {
  655. *sleep_cycle = SENS.sar_touch_ctrl2.touch_sleep_cycles;
  656. }
  657. if (meas_cycle) {
  658. *meas_cycle = SENS.sar_touch_ctrl1.touch_meas_delay;
  659. }
  660. portEXIT_CRITICAL(&rtc_spinlock);
  661. return ESP_OK;
  662. }
  663. esp_err_t touch_pad_set_voltage(touch_high_volt_t refh, touch_low_volt_t refl, touch_volt_atten_t atten)
  664. {
  665. RTC_MODULE_CHECK(((refh < TOUCH_HVOLT_MAX) && (refh >= (int )TOUCH_HVOLT_KEEP)), "touch refh error",
  666. ESP_ERR_INVALID_ARG);
  667. RTC_MODULE_CHECK(((refl < TOUCH_LVOLT_MAX) && (refh >= (int )TOUCH_LVOLT_KEEP)), "touch refl error",
  668. ESP_ERR_INVALID_ARG);
  669. RTC_MODULE_CHECK(((atten < TOUCH_HVOLT_ATTEN_MAX) && (refh >= (int )TOUCH_HVOLT_ATTEN_KEEP)), "touch atten error",
  670. ESP_ERR_INVALID_ARG);
  671. #if CONFIG_IDF_TARGET_ESP32
  672. portENTER_CRITICAL(&rtc_spinlock);
  673. if (refh > TOUCH_HVOLT_KEEP) {
  674. RTCIO.touch_cfg.drefh = refh;
  675. }
  676. if (refl > TOUCH_LVOLT_KEEP) {
  677. RTCIO.touch_cfg.drefl = refl;
  678. }
  679. if (atten > TOUCH_HVOLT_ATTEN_KEEP) {
  680. RTCIO.touch_cfg.drange = atten;
  681. }
  682. portEXIT_CRITICAL(&rtc_spinlock);
  683. #endif
  684. return ESP_OK;
  685. }
  686. esp_err_t touch_pad_get_voltage(touch_high_volt_t *refh, touch_low_volt_t *refl, touch_volt_atten_t *atten)
  687. {
  688. #if CONFIG_IDF_TARGET_ESP32
  689. portENTER_CRITICAL(&rtc_spinlock);
  690. if (refh) {
  691. *refh = RTCIO.touch_cfg.drefh;
  692. }
  693. if (refl) {
  694. *refl = RTCIO.touch_cfg.drefl;
  695. }
  696. if (atten) {
  697. *atten = RTCIO.touch_cfg.drange;
  698. }
  699. portEXIT_CRITICAL(&rtc_spinlock);
  700. #endif
  701. return ESP_OK;
  702. }
  703. esp_err_t touch_pad_set_cnt_mode(touch_pad_t touch_num, touch_cnt_slope_t slope, touch_tie_opt_t opt)
  704. {
  705. RTC_MODULE_CHECK((slope < TOUCH_PAD_SLOPE_MAX), "touch slope error", ESP_ERR_INVALID_ARG);
  706. RTC_MODULE_CHECK((opt < TOUCH_PAD_TIE_OPT_MAX), "touch opt error", ESP_ERR_INVALID_ARG);
  707. touch_pad_t touch_pad_wrap = touch_pad_num_wrap(touch_num);
  708. portENTER_CRITICAL(&rtc_spinlock);
  709. RTCIO.touch_pad[touch_pad_wrap].tie_opt = opt;
  710. RTCIO.touch_pad[touch_num].dac = slope;
  711. portEXIT_CRITICAL(&rtc_spinlock);
  712. return ESP_OK;
  713. }
  714. esp_err_t touch_pad_get_cnt_mode(touch_pad_t touch_num, touch_cnt_slope_t *slope, touch_tie_opt_t *opt)
  715. {
  716. RTC_MODULE_CHECK((touch_num < TOUCH_PAD_MAX), "touch IO error", ESP_ERR_INVALID_ARG);
  717. touch_pad_t touch_pad_wrap = touch_pad_num_wrap(touch_num);
  718. portENTER_CRITICAL(&rtc_spinlock);
  719. if(opt) {
  720. *opt = RTCIO.touch_pad[touch_pad_wrap].tie_opt;
  721. }
  722. if(slope) {
  723. *slope = RTCIO.touch_pad[touch_num].dac;
  724. }
  725. portEXIT_CRITICAL(&rtc_spinlock);
  726. return ESP_OK;
  727. }
  728. esp_err_t touch_pad_io_init(touch_pad_t touch_num)
  729. {
  730. RTC_MODULE_CHECK((touch_num < TOUCH_PAD_MAX), "touch IO error", ESP_ERR_INVALID_ARG);
  731. gpio_num_t gpio_num = GPIO_NUM_0;
  732. touch_pad_get_io_num(touch_num, &gpio_num);
  733. rtc_gpio_init(gpio_num);
  734. rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED);
  735. rtc_gpio_pulldown_dis(gpio_num);
  736. rtc_gpio_pullup_dis(gpio_num);
  737. return ESP_OK;
  738. }
  739. esp_err_t touch_pad_set_fsm_mode(touch_fsm_mode_t mode)
  740. {
  741. RTC_MODULE_CHECK((mode < TOUCH_FSM_MODE_MAX), "touch fsm mode error", ESP_ERR_INVALID_ARG);
  742. portENTER_CRITICAL(&rtc_spinlock);
  743. SENS.sar_touch_ctrl2.touch_start_en = 0;
  744. SENS.sar_touch_ctrl2.touch_start_force = mode;
  745. RTCCNTL.state0.touch_slp_timer_en = (mode == TOUCH_FSM_MODE_TIMER ? 1 : 0);
  746. portEXIT_CRITICAL(&rtc_spinlock);
  747. return ESP_OK;
  748. }
  749. esp_err_t touch_pad_get_fsm_mode(touch_fsm_mode_t *mode)
  750. {
  751. if (mode) {
  752. *mode = SENS.sar_touch_ctrl2.touch_start_force;
  753. }
  754. return ESP_OK;
  755. }
  756. esp_err_t touch_pad_sw_start(void)
  757. {
  758. portENTER_CRITICAL(&rtc_spinlock);
  759. SENS.sar_touch_ctrl2.touch_start_en = 0;
  760. SENS.sar_touch_ctrl2.touch_start_en = 1;
  761. portEXIT_CRITICAL(&rtc_spinlock);
  762. return ESP_OK;
  763. }
  764. esp_err_t touch_pad_set_thresh(touch_pad_t touch_num, uint16_t threshold)
  765. {
  766. RTC_MODULE_CHECK((touch_num < TOUCH_PAD_MAX), "touch IO error", ESP_ERR_INVALID_ARG);
  767. touch_pad_t tp_wrap = touch_pad_num_wrap(touch_num);
  768. portENTER_CRITICAL(&rtc_spinlock);
  769. if (tp_wrap & 0x1) {
  770. SENS.touch_thresh[tp_wrap / 2].l_thresh = threshold;
  771. } else {
  772. SENS.touch_thresh[tp_wrap / 2].h_thresh = threshold;
  773. }
  774. portEXIT_CRITICAL(&rtc_spinlock);
  775. return ESP_OK;
  776. }
  777. esp_err_t touch_pad_get_thresh(touch_pad_t touch_num, uint16_t *threshold)
  778. {
  779. RTC_MODULE_CHECK((touch_num < TOUCH_PAD_MAX), "touch IO error", ESP_ERR_INVALID_ARG);
  780. touch_pad_t tp_wrap = touch_pad_num_wrap(touch_num);
  781. if (threshold) {
  782. *threshold = (tp_wrap & 0x1 )? \
  783. SENS.touch_thresh[tp_wrap / 2].l_thresh : \
  784. SENS.touch_thresh[tp_wrap / 2].h_thresh;
  785. }
  786. return ESP_OK;
  787. }
  788. esp_err_t touch_pad_set_trigger_mode(touch_trigger_mode_t mode)
  789. {
  790. RTC_MODULE_CHECK((mode < TOUCH_TRIGGER_MAX), "touch trigger mode error", ESP_ERR_INVALID_ARG);
  791. portENTER_CRITICAL(&rtc_spinlock);
  792. SENS.sar_touch_ctrl1.touch_out_sel = mode;
  793. portEXIT_CRITICAL(&rtc_spinlock);
  794. return ESP_OK;
  795. }
  796. esp_err_t touch_pad_get_trigger_mode(touch_trigger_mode_t *mode)
  797. {
  798. if (mode) {
  799. *mode = SENS.sar_touch_ctrl1.touch_out_sel;
  800. }
  801. return ESP_OK;
  802. }
  803. esp_err_t touch_pad_set_trigger_source(touch_trigger_src_t src)
  804. {
  805. RTC_MODULE_CHECK((src < TOUCH_TRIGGER_SOURCE_MAX), "touch trigger source error", ESP_ERR_INVALID_ARG);
  806. portENTER_CRITICAL(&rtc_spinlock);
  807. SENS.sar_touch_ctrl1.touch_out_1en = src;
  808. portEXIT_CRITICAL(&rtc_spinlock);
  809. return ESP_OK;
  810. }
  811. esp_err_t touch_pad_get_trigger_source(touch_trigger_src_t *src)
  812. {
  813. if (src) {
  814. *src = SENS.sar_touch_ctrl1.touch_out_1en;
  815. }
  816. return ESP_OK;
  817. }
  818. esp_err_t touch_pad_set_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask)
  819. {
  820. RTC_MODULE_CHECK((set1_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch set1 bitmask error", ESP_ERR_INVALID_ARG);
  821. RTC_MODULE_CHECK((set2_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch set2 bitmask error", ESP_ERR_INVALID_ARG);
  822. RTC_MODULE_CHECK((en_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch work_en bitmask error", ESP_ERR_INVALID_ARG);
  823. portENTER_CRITICAL(&rtc_spinlock);
  824. SENS.sar_touch_enable.touch_pad_outen1 |= TOUCH_BITS_SWAP(set1_mask);
  825. SENS.sar_touch_enable.touch_pad_outen2 |= TOUCH_BITS_SWAP(set2_mask);
  826. SENS.sar_touch_enable.touch_pad_worken |= TOUCH_BITS_SWAP(en_mask);
  827. portEXIT_CRITICAL(&rtc_spinlock);
  828. return ESP_OK;
  829. }
  830. esp_err_t touch_pad_get_group_mask(uint16_t *set1_mask, uint16_t *set2_mask, uint16_t *en_mask)
  831. {
  832. portENTER_CRITICAL(&rtc_spinlock);
  833. if (set1_mask) {
  834. *set1_mask = TOUCH_BITS_SWAP(SENS.sar_touch_enable.touch_pad_outen1);
  835. }
  836. if (set2_mask) {
  837. *set2_mask = TOUCH_BITS_SWAP(SENS.sar_touch_enable.touch_pad_outen2);
  838. }
  839. if (en_mask) {
  840. *en_mask = TOUCH_BITS_SWAP(SENS.sar_touch_enable.touch_pad_worken);
  841. }
  842. portEXIT_CRITICAL(&rtc_spinlock);
  843. return ESP_OK;
  844. }
  845. esp_err_t touch_pad_clear_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask)
  846. {
  847. RTC_MODULE_CHECK((set1_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch set1 bitmask error", ESP_ERR_INVALID_ARG);
  848. RTC_MODULE_CHECK((set2_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch set2 bitmask error", ESP_ERR_INVALID_ARG);
  849. RTC_MODULE_CHECK((en_mask <= TOUCH_PAD_BIT_MASK_MAX), "touch work_en bitmask error", ESP_ERR_INVALID_ARG);
  850. portENTER_CRITICAL(&rtc_spinlock);
  851. SENS.sar_touch_enable.touch_pad_outen1 &= TOUCH_BITS_SWAP(~set1_mask);
  852. SENS.sar_touch_enable.touch_pad_outen2 &= TOUCH_BITS_SWAP(~set2_mask);
  853. SENS.sar_touch_enable.touch_pad_worken &= TOUCH_BITS_SWAP(~en_mask);
  854. portEXIT_CRITICAL(&rtc_spinlock);
  855. return ESP_OK;
  856. }
  857. uint32_t IRAM_ATTR touch_pad_get_status(void)
  858. {
  859. uint32_t status = SENS.sar_touch_ctrl2.touch_meas_en;
  860. return TOUCH_BITS_SWAP(status);
  861. }
  862. esp_err_t IRAM_ATTR touch_pad_clear_status(void)
  863. {
  864. SENS.sar_touch_ctrl2.touch_meas_en_clr = 1;
  865. return ESP_OK;
  866. }
  867. esp_err_t touch_pad_intr_enable(void)
  868. {
  869. portENTER_CRITICAL(&rtc_spinlock);
  870. RTCCNTL.int_ena.rtc_touch = 1;
  871. portEXIT_CRITICAL(&rtc_spinlock);
  872. return ESP_OK;
  873. }
  874. esp_err_t touch_pad_intr_disable(void)
  875. {
  876. portENTER_CRITICAL(&rtc_spinlock);
  877. RTCCNTL.int_ena.rtc_touch = 0;
  878. portEXIT_CRITICAL(&rtc_spinlock);
  879. return ESP_OK;
  880. }
  881. esp_err_t touch_pad_config(touch_pad_t touch_num, uint16_t threshold)
  882. {
  883. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  884. RTC_MODULE_CHECK(touch_num < TOUCH_PAD_MAX, "Touch_Pad Num Err", ESP_ERR_INVALID_ARG);
  885. touch_fsm_mode_t mode;
  886. touch_pad_set_thresh(touch_num, threshold);
  887. touch_pad_io_init(touch_num);
  888. touch_pad_set_cnt_mode(touch_num, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_LOW);
  889. touch_pad_get_fsm_mode(&mode);
  890. if (TOUCH_FSM_MODE_SW == mode) {
  891. touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  892. s_touch_pad_init_bit |= (1 << touch_num);
  893. } else if (TOUCH_FSM_MODE_TIMER == mode){
  894. uint16_t sleep_time = 0;
  895. uint16_t meas_cycle = 0;
  896. uint32_t wait_time_ms = 0;
  897. uint32_t wait_tick = 0;
  898. uint32_t rtc_clk = rtc_clk_slow_freq_get_hz();
  899. touch_pad_set_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  900. touch_pad_get_meas_time(&sleep_time, &meas_cycle);
  901. //If the FSM mode is 'TOUCH_FSM_MODE_TIMER', The data will be ready after one measurement cycle
  902. //after this function is executed, otherwise, the "touch_value" by "touch_pad_read" is 0.
  903. wait_time_ms = sleep_time/(rtc_clk/1000) + meas_cycle/(RTC_FAST_CLK_FREQ_APPROX/1000);
  904. wait_tick = wait_time_ms/portTICK_RATE_MS;
  905. vTaskDelay(wait_tick ? wait_tick : 1);
  906. s_touch_pad_init_bit |= (1 << touch_num);
  907. } else {
  908. return ESP_FAIL;
  909. }
  910. return ESP_OK;
  911. }
  912. esp_err_t touch_pad_init(void)
  913. {
  914. if (rtc_touch_mux == NULL) {
  915. rtc_touch_mux = xSemaphoreCreateMutex();
  916. }
  917. if (rtc_touch_mux == NULL) {
  918. return ESP_FAIL;
  919. }
  920. touch_pad_intr_disable();
  921. touch_pad_clear_group_mask(TOUCH_PAD_BIT_MASK_MAX, TOUCH_PAD_BIT_MASK_MAX, TOUCH_PAD_BIT_MASK_MAX);
  922. touch_pad_set_trigger_mode(TOUCH_TRIGGER_MODE_DEFAULT);
  923. touch_pad_set_trigger_source(TOUCH_TRIGGER_SOURCE_DEFAULT);
  924. touch_pad_clear_status();
  925. touch_pad_set_meas_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT, TOUCH_PAD_MEASURE_CYCLE_DEFAULT);
  926. touch_pad_set_fsm_mode(TOUCH_FSM_MODE_DEFAULT);
  927. return ESP_OK;
  928. }
  929. esp_err_t touch_pad_deinit(void)
  930. {
  931. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  932. if (s_touch_pad_filter != NULL) {
  933. touch_pad_filter_stop();
  934. touch_pad_filter_delete();
  935. }
  936. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  937. s_touch_pad_init_bit = 0x0000;
  938. touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW);
  939. touch_pad_clear_status();
  940. touch_pad_intr_disable();
  941. xSemaphoreGive(rtc_touch_mux);
  942. vSemaphoreDelete(rtc_touch_mux);
  943. rtc_touch_mux = NULL;
  944. return ESP_OK;
  945. }
  946. static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, touch_fsm_mode_t mode)
  947. {
  948. esp_err_t res = ESP_OK;
  949. touch_pad_t tp_wrap = touch_pad_num_wrap(touch_num);
  950. if (TOUCH_FSM_MODE_SW == mode) {
  951. touch_pad_set_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  952. touch_pad_sw_start();
  953. while (SENS.sar_touch_ctrl2.touch_meas_done == 0) {};
  954. *touch_value = (tp_wrap & 0x1) ? \
  955. SENS.touch_meas[tp_wrap / 2].l_val: \
  956. SENS.touch_meas[tp_wrap / 2].h_val;
  957. touch_pad_clear_group_mask((1 << touch_num), (1 << touch_num), (1 << touch_num));
  958. } else if (TOUCH_FSM_MODE_TIMER == mode) {
  959. while (SENS.sar_touch_ctrl2.touch_meas_done == 0) {};
  960. *touch_value = (tp_wrap & 0x1) ? \
  961. SENS.touch_meas[tp_wrap / 2].l_val: \
  962. SENS.touch_meas[tp_wrap / 2].h_val;
  963. } else {
  964. res = ESP_FAIL;
  965. }
  966. if (*touch_value == 0) {
  967. res = ESP_ERR_INVALID_STATE;
  968. }
  969. return res;
  970. }
  971. esp_err_t touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value)
  972. {
  973. RTC_MODULE_CHECK(touch_num < TOUCH_PAD_MAX, "Touch_Pad Num Err", ESP_ERR_INVALID_ARG);
  974. RTC_MODULE_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
  975. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  976. esp_err_t res = ESP_OK;
  977. touch_fsm_mode_t mode;
  978. touch_pad_get_fsm_mode(&mode);
  979. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  980. res = _touch_pad_read(touch_num, touch_value, mode);
  981. xSemaphoreGive(rtc_touch_mux);
  982. return res;
  983. }
  984. IRAM_ATTR esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint16_t *touch_value)
  985. {
  986. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  987. RTC_MODULE_CHECK(touch_num < TOUCH_PAD_MAX, "Touch_Pad Num Err", ESP_ERR_INVALID_ARG);
  988. RTC_MODULE_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
  989. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_FAIL);
  990. *touch_value = s_touch_pad_filter->raw_val[touch_num];
  991. if (*touch_value == 0) {
  992. return ESP_ERR_INVALID_STATE;
  993. }
  994. return ESP_OK;
  995. }
  996. IRAM_ATTR esp_err_t touch_pad_read_filtered(touch_pad_t touch_num, uint16_t *touch_value)
  997. {
  998. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_FAIL);
  999. RTC_MODULE_CHECK(touch_num < TOUCH_PAD_MAX, "Touch_Pad Num Err", ESP_ERR_INVALID_ARG);
  1000. RTC_MODULE_CHECK(touch_value != NULL, "touch_value", ESP_ERR_INVALID_ARG);
  1001. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_FAIL);
  1002. *touch_value = (s_touch_pad_filter->filtered_val[touch_num]);
  1003. if (*touch_value == 0) {
  1004. return ESP_ERR_INVALID_STATE;
  1005. }
  1006. return ESP_OK;
  1007. }
  1008. esp_err_t touch_pad_set_filter_period(uint32_t new_period_ms)
  1009. {
  1010. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  1011. RTC_MODULE_CHECK(new_period_ms > 0, "Touch pad filter period error", ESP_ERR_INVALID_ARG);
  1012. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  1013. esp_err_t ret = ESP_OK;
  1014. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  1015. if (s_touch_pad_filter != NULL) {
  1016. xTimerChangePeriod(s_touch_pad_filter->timer, new_period_ms / portTICK_PERIOD_MS, portMAX_DELAY);
  1017. s_touch_pad_filter->period = new_period_ms;
  1018. } else {
  1019. ESP_LOGE(RTC_MODULE_TAG, "Touch pad filter deleted");
  1020. ret = ESP_ERR_INVALID_STATE;
  1021. }
  1022. xSemaphoreGive(rtc_touch_mux);
  1023. return ret;
  1024. }
  1025. esp_err_t touch_pad_get_filter_period(uint32_t* p_period_ms)
  1026. {
  1027. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  1028. RTC_MODULE_CHECK(p_period_ms != NULL, "Touch pad period pointer error", ESP_ERR_INVALID_ARG);
  1029. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  1030. esp_err_t ret = ESP_OK;
  1031. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  1032. if (s_touch_pad_filter != NULL) {
  1033. *p_period_ms = s_touch_pad_filter->period;
  1034. } else {
  1035. ESP_LOGE(RTC_MODULE_TAG, "Touch pad filter deleted");
  1036. ret = ESP_ERR_INVALID_STATE;
  1037. }
  1038. xSemaphoreGive(rtc_touch_mux);
  1039. return ret;
  1040. }
  1041. esp_err_t touch_pad_filter_start(uint32_t filter_period_ms)
  1042. {
  1043. RTC_MODULE_CHECK(filter_period_ms >= portTICK_PERIOD_MS, "Touch pad filter period error", ESP_ERR_INVALID_ARG);
  1044. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  1045. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  1046. if (s_touch_pad_filter == NULL) {
  1047. s_touch_pad_filter = (touch_pad_filter_t *) calloc(1, sizeof(touch_pad_filter_t));
  1048. if (s_touch_pad_filter == NULL) {
  1049. goto err_no_mem;
  1050. }
  1051. }
  1052. if (s_touch_pad_filter->timer == NULL) {
  1053. s_touch_pad_filter->timer = xTimerCreate("filter_tmr", filter_period_ms / portTICK_PERIOD_MS, pdFALSE,
  1054. NULL, touch_pad_filter_cb);
  1055. if (s_touch_pad_filter->timer == NULL) {
  1056. free(s_touch_pad_filter);
  1057. s_touch_pad_filter = NULL;
  1058. goto err_no_mem;
  1059. }
  1060. s_touch_pad_filter->period = filter_period_ms;
  1061. }
  1062. xSemaphoreGive(rtc_touch_mux);
  1063. touch_pad_filter_cb(NULL);
  1064. return ESP_OK;
  1065. err_no_mem:
  1066. xSemaphoreGive(rtc_touch_mux);
  1067. return ESP_ERR_NO_MEM;
  1068. }
  1069. esp_err_t touch_pad_filter_stop(void)
  1070. {
  1071. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  1072. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  1073. esp_err_t ret = ESP_OK;
  1074. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  1075. if (s_touch_pad_filter != NULL) {
  1076. xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY);
  1077. } else {
  1078. ESP_LOGE(RTC_MODULE_TAG, "Touch pad filter deleted");
  1079. ret = ESP_ERR_INVALID_STATE;
  1080. }
  1081. xSemaphoreGive(rtc_touch_mux);
  1082. return ret;
  1083. }
  1084. esp_err_t touch_pad_filter_delete(void)
  1085. {
  1086. RTC_MODULE_CHECK(s_touch_pad_filter != NULL, "Touch pad filter not initialized", ESP_ERR_INVALID_STATE);
  1087. RTC_MODULE_CHECK(rtc_touch_mux != NULL, "Touch pad not initialized", ESP_ERR_INVALID_STATE);
  1088. xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
  1089. if (s_touch_pad_filter != NULL) {
  1090. if (s_touch_pad_filter->timer != NULL) {
  1091. xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY);
  1092. xTimerDelete(s_touch_pad_filter->timer, portMAX_DELAY);
  1093. s_touch_pad_filter->timer = NULL;
  1094. }
  1095. free(s_touch_pad_filter);
  1096. s_touch_pad_filter = NULL;
  1097. }
  1098. xSemaphoreGive(rtc_touch_mux);
  1099. return ESP_OK;
  1100. }
  1101. esp_err_t touch_pad_get_wakeup_status(touch_pad_t *pad_num)
  1102. {
  1103. uint32_t touch_mask = SENS.sar_touch_ctrl2.touch_meas_en;
  1104. if(touch_mask == 0) {
  1105. return ESP_FAIL;
  1106. }
  1107. *pad_num = touch_pad_num_wrap((touch_pad_t)(__builtin_ffs(touch_mask) - 1));
  1108. return ESP_OK;
  1109. }
  1110. #endif
  1111. /*---------------------------------------------------------------
  1112. ADC Common
  1113. ---------------------------------------------------------------*/
  1114. #if CONFIG_IDF_TARGET_ESP32S2BETA
  1115. #define SENS_FORCE_XPD_AMP_FSM 0 // Use FSM to control power down
  1116. #define SENS_FORCE_XPD_AMP_PD 2 // Force power down
  1117. #define SENS_FORCE_XPD_AMP_PU 3 // Force power up
  1118. #define SENS_SAR1_ATTEN_VAL_MASK 0x3
  1119. #define SENS_SAR2_ATTEN_VAL_MASK 0x3
  1120. #define SENS_FORCE_XPD_SAR_SW_M (BIT(1))
  1121. #define SENS_FORCE_XPD_SAR_FSM 0 // Use FSM to control power down
  1122. #define SENS_FORCE_XPD_SAR_PD 2 // Force power down
  1123. #define SENS_FORCE_XPD_SAR_PU 3 // Force power up
  1124. #endif
  1125. static esp_err_t adc_set_fsm_time(int rst_wait, int start_wait, int standby_wait, int sample_cycle)
  1126. {
  1127. portENTER_CRITICAL(&rtc_spinlock);
  1128. #if CONFIG_IDF_TARGET_ESP32
  1129. // Internal FSM reset wait time
  1130. if (rst_wait >= 0) {
  1131. SYSCON.saradc_fsm.rstb_wait = rst_wait;
  1132. }
  1133. // Internal FSM start wait time
  1134. if (start_wait >= 0) {
  1135. SYSCON.saradc_fsm.start_wait = start_wait;
  1136. }
  1137. // Internal FSM standby wait time
  1138. if (standby_wait >= 0) {
  1139. SYSCON.saradc_fsm.standby_wait = standby_wait;
  1140. }
  1141. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1142. // Internal FSM reset wait time
  1143. if (rst_wait >= 0) {
  1144. SYSCON.saradc_fsm_wait.rstb_wait = rst_wait;
  1145. }
  1146. // Internal FSM start wait time
  1147. if (start_wait >= 0) {
  1148. SYSCON.saradc_fsm_wait.xpd_wait = start_wait;
  1149. }
  1150. // Internal FSM standby wait time
  1151. if (standby_wait >= 0) {
  1152. SYSCON.saradc_fsm_wait.standby_wait = standby_wait;
  1153. }
  1154. #endif
  1155. // Internal FSM standby sample cycle
  1156. if (sample_cycle >= 0) {
  1157. SYSCON.saradc_fsm.sample_cycle = sample_cycle;
  1158. }
  1159. portEXIT_CRITICAL(&rtc_spinlock);
  1160. return ESP_OK;
  1161. }
  1162. static esp_err_t adc_set_data_format(adc_i2s_encode_t mode)
  1163. {
  1164. portENTER_CRITICAL(&rtc_spinlock);
  1165. //data format:
  1166. //0: ADC_ENCODE_12BIT [15:12]-channel [11:0]-12 bits ADC data
  1167. //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.
  1168. SYSCON.saradc_ctrl.data_sar_sel = mode;
  1169. portEXIT_CRITICAL(&rtc_spinlock);
  1170. return ESP_OK;
  1171. }
  1172. static esp_err_t adc_set_measure_limit(uint8_t meas_num, bool lim_en)
  1173. {
  1174. portENTER_CRITICAL(&rtc_spinlock);
  1175. // Set max measure number
  1176. SYSCON.saradc_ctrl2.max_meas_num = meas_num;
  1177. // Enable max measure number limit
  1178. SYSCON.saradc_ctrl2.meas_num_limit = lim_en;
  1179. portEXIT_CRITICAL(&rtc_spinlock);
  1180. return ESP_OK;
  1181. }
  1182. static esp_err_t adc_set_work_mode(adc_unit_t adc_unit)
  1183. {
  1184. portENTER_CRITICAL(&rtc_spinlock);
  1185. if (adc_unit == ADC_UNIT_1) {
  1186. // saradc mode sel : 0--single saradc; 1--double saradc; 2--alternative saradc
  1187. SYSCON.saradc_ctrl.work_mode = 0;
  1188. //ENABLE ADC 0: ADC1 1: ADC2, only work for single SAR mode
  1189. SYSCON.saradc_ctrl.sar_sel = 0;
  1190. } else if (adc_unit == ADC_UNIT_2) {
  1191. // saradc mode sel : 0--single saradc; 1--double saradc; 2--alternative saradc
  1192. SYSCON.saradc_ctrl.work_mode = 0;
  1193. //ENABLE ADC1 0: SAR1 1: SAR2 only work for single SAR mode
  1194. SYSCON.saradc_ctrl.sar_sel = 1;
  1195. } else if (adc_unit == ADC_UNIT_BOTH) {
  1196. // saradc mode sel : 0--single saradc; 1--double saradc; 2--alternative saradc
  1197. SYSCON.saradc_ctrl.work_mode = 1;
  1198. } else if (adc_unit == ADC_UNIT_ALTER) {
  1199. // saradc mode sel : 0--single saradc; 1--double saradc; 2--alternative saradc
  1200. SYSCON.saradc_ctrl.work_mode = 2;
  1201. }
  1202. portEXIT_CRITICAL(&rtc_spinlock);
  1203. return ESP_OK;
  1204. }
  1205. static esp_err_t adc_set_atten(adc_unit_t adc_unit, adc_channel_t channel, adc_atten_t atten)
  1206. {
  1207. ADC_CHECK_UNIT(adc_unit);
  1208. if (adc_unit & ADC_UNIT_1) {
  1209. RTC_MODULE_CHECK((adc1_channel_t)channel < ADC1_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  1210. }
  1211. RTC_MODULE_CHECK(atten < ADC_ATTEN_MAX, "ADC Atten Err", ESP_ERR_INVALID_ARG);
  1212. portENTER_CRITICAL(&rtc_spinlock);
  1213. if (adc_unit & ADC_UNIT_1) {
  1214. //SAR1_atten
  1215. SET_PERI_REG_BITS(SENS_SAR_ATTEN1_REG, SENS_SAR1_ATTEN_VAL_MASK, atten, (channel * 2));
  1216. }
  1217. if (adc_unit & ADC_UNIT_2) {
  1218. //SAR2_atten
  1219. SET_PERI_REG_BITS(SENS_SAR_ATTEN2_REG, SENS_SAR2_ATTEN_VAL_MASK, atten, (channel * 2));
  1220. }
  1221. portEXIT_CRITICAL(&rtc_spinlock);
  1222. return ESP_OK;
  1223. }
  1224. void adc_power_always_on(void)
  1225. {
  1226. portENTER_CRITICAL(&rtc_spinlock);
  1227. #if CONFIG_IDF_TARGET_ESP32
  1228. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  1229. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1230. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  1231. #endif
  1232. portEXIT_CRITICAL(&rtc_spinlock);
  1233. }
  1234. void adc_power_on(void)
  1235. {
  1236. portENTER_CRITICAL(&rtc_spinlock);
  1237. #if CONFIG_IDF_TARGET_ESP32
  1238. //The power FSM controlled mode saves more power, while the ADC noise may get increased.
  1239. #ifndef CONFIG_ADC_FORCE_XPD_FSM
  1240. //Set the power always on to increase precision.
  1241. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  1242. #else
  1243. //Use the FSM to turn off the power while not used to save power.
  1244. if (SENS.sar_meas_wait2.force_xpd_sar & SENS_FORCE_XPD_SAR_SW_M) {
  1245. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  1246. } else {
  1247. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_FSM;
  1248. }
  1249. #endif
  1250. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1251. //The power FSM controlled mode saves more power, while the ADC noise may get increased.
  1252. #ifndef CONFIG_ADC_FORCE_XPD_FSM
  1253. //Set the power always on to increase precision.
  1254. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  1255. #else
  1256. //Use the FSM to turn off the power while not used to save power.
  1257. if (SENS.sar_power_xpd_sar.force_xpd_sar & SENS_FORCE_XPD_SAR_SW_M) {
  1258. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  1259. } else {
  1260. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_FSM;
  1261. }
  1262. #endif
  1263. #endif
  1264. portEXIT_CRITICAL(&rtc_spinlock);
  1265. }
  1266. void adc_power_off(void)
  1267. {
  1268. portENTER_CRITICAL(&rtc_spinlock);
  1269. #if CONFIG_IDF_TARGET_ESP32
  1270. //Bit1 0:Fsm 1: SW mode
  1271. //Bit0 0:SW mode power down 1: SW mode power on
  1272. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PD;
  1273. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1274. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_PD;
  1275. #endif
  1276. portEXIT_CRITICAL(&rtc_spinlock);
  1277. }
  1278. esp_err_t adc_set_clk_div(uint8_t clk_div)
  1279. {
  1280. portENTER_CRITICAL(&rtc_spinlock);
  1281. // ADC clock devided from APB clk, 80 / 2 = 40Mhz,
  1282. SYSCON.saradc_ctrl.sar_clk_div = clk_div;
  1283. portEXIT_CRITICAL(&rtc_spinlock);
  1284. return ESP_OK;
  1285. }
  1286. esp_err_t adc_set_i2s_data_source(adc_i2s_source_t src)
  1287. {
  1288. RTC_MODULE_CHECK(src < ADC_I2S_DATA_SRC_MAX, "ADC i2s data source error", ESP_ERR_INVALID_ARG);
  1289. portENTER_CRITICAL(&rtc_spinlock);
  1290. // 1: I2S input data is from SAR ADC (for DMA) 0: I2S input data is from GPIO matrix
  1291. SYSCON.saradc_ctrl.data_to_i2s = src;
  1292. portEXIT_CRITICAL(&rtc_spinlock);
  1293. return ESP_OK;
  1294. }
  1295. esp_err_t adc_gpio_init(adc_unit_t adc_unit, adc_channel_t channel)
  1296. {
  1297. ADC_CHECK_UNIT(adc_unit);
  1298. gpio_num_t gpio_num = 0;
  1299. if (adc_unit & ADC_UNIT_1) {
  1300. RTC_MODULE_CHECK((adc1_channel_t) channel < ADC1_CHANNEL_MAX, "ADC1 channel error", ESP_ERR_INVALID_ARG);
  1301. ADC1_CHECK_FUNCTION_RET(adc1_pad_get_io_num((adc1_channel_t) channel, &gpio_num));
  1302. ADC1_CHECK_FUNCTION_RET(rtc_gpio_init(gpio_num));
  1303. ADC1_CHECK_FUNCTION_RET(rtc_gpio_output_disable(gpio_num));
  1304. ADC1_CHECK_FUNCTION_RET(rtc_gpio_input_disable(gpio_num));
  1305. ADC1_CHECK_FUNCTION_RET(gpio_set_pull_mode(gpio_num, GPIO_FLOATING));
  1306. }
  1307. return ESP_OK;
  1308. }
  1309. esp_err_t adc_set_data_inv(adc_unit_t adc_unit, bool inv_en)
  1310. {
  1311. portENTER_CRITICAL(&rtc_spinlock);
  1312. #if CONFIG_IDF_TARGET_ESP32
  1313. if (adc_unit & ADC_UNIT_1) {
  1314. // Enable ADC data invert
  1315. SENS.sar_read_ctrl.sar1_data_inv = inv_en;
  1316. }
  1317. if (adc_unit & ADC_UNIT_2) {
  1318. // Enable ADC data invert
  1319. SENS.sar_read_ctrl2.sar2_data_inv = inv_en;
  1320. }
  1321. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1322. if (adc_unit & ADC_UNIT_1) {
  1323. // Enable ADC data invert
  1324. SENS.sar_reader1_ctrl.sar1_data_inv = inv_en;
  1325. }
  1326. if (adc_unit & ADC_UNIT_2) {
  1327. // Enable ADC data invert
  1328. SENS.sar_reader2_ctrl.sar2_data_inv = inv_en;
  1329. }
  1330. #endif
  1331. portEXIT_CRITICAL(&rtc_spinlock);
  1332. return ESP_OK;
  1333. }
  1334. esp_err_t adc_set_data_width(adc_unit_t adc_unit, adc_bits_width_t bits)
  1335. {
  1336. ADC_CHECK_UNIT(adc_unit);
  1337. RTC_MODULE_CHECK(bits < ADC_WIDTH_MAX, "ADC bit width error", ESP_ERR_INVALID_ARG);
  1338. portENTER_CRITICAL(&rtc_spinlock);
  1339. #if CONFIG_IDF_TARGET_ESP32
  1340. if (adc_unit & ADC_UNIT_1) {
  1341. SENS.sar_start_force.sar1_bit_width = bits;
  1342. SENS.sar_read_ctrl.sar1_sample_bit = bits;
  1343. }
  1344. if (adc_unit & ADC_UNIT_2) {
  1345. SENS.sar_start_force.sar2_bit_width = bits;
  1346. SENS.sar_read_ctrl2.sar2_sample_bit = bits;
  1347. }
  1348. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1349. if (adc_unit & ADC_UNIT_1) {
  1350. SENS.sar_meas1_ctrl1.sar1_bit_width = bits;
  1351. SENS.sar_reader1_ctrl.sar1_sample_bit = bits;
  1352. }
  1353. if (adc_unit & ADC_UNIT_2) {
  1354. SENS.sar_meas2_ctrl1.sar2_bit_width = bits;
  1355. SENS.sar_reader2_ctrl.sar2_sample_bit = bits;
  1356. }
  1357. #endif
  1358. portEXIT_CRITICAL(&rtc_spinlock);
  1359. return ESP_OK;
  1360. }
  1361. // this function should be called in the critical section
  1362. static void adc_set_controller(adc_unit_t unit, adc_controller_t ctrl )
  1363. {
  1364. #if CONFIG_IDF_TARGET_ESP32
  1365. if ( unit == ADC_UNIT_1 ) {
  1366. switch( ctrl ) {
  1367. case ADC_CTRL_RTC:
  1368. SENS.sar_read_ctrl.sar1_dig_force = false; //RTC controller controls the ADC, not digital controller
  1369. SENS.sar_meas_start1.meas1_start_force = true; //RTC controller controls the ADC,not ulp coprocessor
  1370. SENS.sar_meas_start1.sar1_en_pad_force = true; //RTC controller controls the data port, not ulp coprocessor
  1371. SENS.sar_touch_ctrl1.xpd_hall_force = true; // RTC controller controls the hall sensor power,not ulp coprocessor
  1372. SENS.sar_touch_ctrl1.hall_phase_force = true; // RTC controller controls the hall sensor phase,not ulp coprocessor
  1373. break;
  1374. case ADC_CTRL_ULP:
  1375. SENS.sar_read_ctrl.sar1_dig_force = false;
  1376. SENS.sar_meas_start1.meas1_start_force = false;
  1377. SENS.sar_meas_start1.sar1_en_pad_force = false;
  1378. SENS.sar_touch_ctrl1.xpd_hall_force = false;
  1379. SENS.sar_touch_ctrl1.hall_phase_force = false;
  1380. break;
  1381. case ADC_CTRL_DIG:
  1382. SENS.sar_read_ctrl.sar1_dig_force = true;
  1383. SENS.sar_meas_start1.meas1_start_force = true;
  1384. SENS.sar_meas_start1.sar1_en_pad_force = true;
  1385. SENS.sar_touch_ctrl1.xpd_hall_force = true;
  1386. SENS.sar_touch_ctrl1.hall_phase_force = true;
  1387. break;
  1388. default:
  1389. ESP_LOGE(TAG, "adc1 selects invalid controller");
  1390. break;
  1391. }
  1392. } else if ( unit == ADC_UNIT_2) {
  1393. switch( ctrl ) {
  1394. case ADC_CTRL_RTC:
  1395. SENS.sar_meas_start2.meas2_start_force = true; //RTC controller controls the ADC,not ulp coprocessor
  1396. SENS.sar_meas_start2.sar2_en_pad_force = true; //RTC controller controls the data port, not ulp coprocessor
  1397. SENS.sar_read_ctrl2.sar2_dig_force = false; //RTC controller controls the ADC, not digital controller
  1398. SENS.sar_read_ctrl2.sar2_pwdet_force = false; //RTC controller controls the ADC, not PWDET
  1399. SYSCON.saradc_ctrl.sar2_mux = true; //RTC controller controls the ADC, not PWDET
  1400. break;
  1401. case ADC_CTRL_ULP:
  1402. SENS.sar_meas_start2.meas2_start_force = false;
  1403. SENS.sar_meas_start2.sar2_en_pad_force = false;
  1404. SENS.sar_read_ctrl2.sar2_dig_force = false;
  1405. SENS.sar_read_ctrl2.sar2_pwdet_force = false;
  1406. SYSCON.saradc_ctrl.sar2_mux = true;
  1407. break;
  1408. case ADC_CTRL_DIG:
  1409. SENS.sar_meas_start2.meas2_start_force = true;
  1410. SENS.sar_meas_start2.sar2_en_pad_force = true;
  1411. SENS.sar_read_ctrl2.sar2_dig_force = true;
  1412. SENS.sar_read_ctrl2.sar2_pwdet_force = false;
  1413. SYSCON.saradc_ctrl.sar2_mux = true;
  1414. break;
  1415. case ADC2_CTRL_PWDET:
  1416. //currently only used by Wi-Fi
  1417. SENS.sar_meas_start2.meas2_start_force = true;
  1418. SENS.sar_meas_start2.sar2_en_pad_force = true;
  1419. SENS.sar_read_ctrl2.sar2_dig_force = false;
  1420. SENS.sar_read_ctrl2.sar2_pwdet_force = true;
  1421. SYSCON.saradc_ctrl.sar2_mux = false;
  1422. break;
  1423. default:
  1424. ESP_LOGE(TAG, "adc2 selects invalid controller");
  1425. break;
  1426. }
  1427. } else {
  1428. ESP_LOGE(TAG, "invalid adc unit");
  1429. assert(0);
  1430. }
  1431. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1432. if ( unit == ADC_UNIT_1 ) {
  1433. switch( ctrl ) {
  1434. case ADC_CTRL_RTC:
  1435. SENS.sar_meas1_mux.sar1_dig_force = false; //RTC controller controls the ADC, not digital controller
  1436. SENS.sar_meas1_ctrl2.meas1_start_force = true; //RTC controller controls the ADC,not ulp coprocessor
  1437. SENS.sar_meas1_ctrl2.sar1_en_pad_force = true; //RTC controller controls the data port, not ulp coprocessor
  1438. SENS.sar_hall_ctrl.xpd_hall_force = true; // RTC controller controls the hall sensor power,not ulp coprocessor
  1439. SENS.sar_hall_ctrl.hall_phase_force = true; // RTC controller controls the hall sensor phase,not ulp coprocessor
  1440. break;
  1441. case ADC_CTRL_ULP:
  1442. SENS.sar_meas1_mux.sar1_dig_force = false;
  1443. SENS.sar_meas1_ctrl2.meas1_start_force = false;
  1444. SENS.sar_meas1_ctrl2.sar1_en_pad_force = false;
  1445. SENS.sar_hall_ctrl.xpd_hall_force = false;
  1446. SENS.sar_hall_ctrl.hall_phase_force = false;
  1447. break;
  1448. case ADC_CTRL_DIG:
  1449. SENS.sar_meas1_mux.sar1_dig_force = true;
  1450. SENS.sar_meas1_ctrl2.meas1_start_force = true;
  1451. SENS.sar_meas1_ctrl2.sar1_en_pad_force = true;
  1452. SENS.sar_hall_ctrl.xpd_hall_force = true;
  1453. SENS.sar_hall_ctrl.hall_phase_force = true;
  1454. break;
  1455. default:
  1456. ESP_LOGE(TAG, "adc1 selects invalid controller");
  1457. break;
  1458. }
  1459. } else if ( unit == ADC_UNIT_2) {
  1460. switch( ctrl ) {
  1461. case ADC_CTRL_RTC:
  1462. SENS.sar_meas2_ctrl2.meas2_start_force = true; //RTC controller controls the ADC,not ulp coprocessor
  1463. SENS.sar_meas2_ctrl2.sar2_en_pad_force = true; //RTC controller controls the data port, not ulp coprocessor
  1464. break;
  1465. case ADC_CTRL_ULP:
  1466. SENS.sar_meas2_ctrl2.meas2_start_force = false;
  1467. SENS.sar_meas2_ctrl2.sar2_en_pad_force = false;
  1468. break;
  1469. case ADC_CTRL_DIG:
  1470. SENS.sar_meas2_ctrl2.meas2_start_force = true;
  1471. SENS.sar_meas2_ctrl2.sar2_en_pad_force = true;
  1472. break;
  1473. case ADC2_CTRL_PWDET:
  1474. //currently only used by Wi-Fi
  1475. SENS.sar_meas2_ctrl2.meas2_start_force = true;
  1476. SENS.sar_meas2_ctrl2.sar2_en_pad_force = true;
  1477. break;
  1478. default:
  1479. ESP_LOGE(TAG, "adc2 selects invalid controller");
  1480. break;
  1481. }
  1482. } else {
  1483. ESP_LOGE(TAG, "invalid adc unit");
  1484. assert(0);
  1485. }
  1486. #endif
  1487. }
  1488. // this function should be called in the critical section
  1489. static int adc_convert( adc_unit_t unit, int channel)
  1490. {
  1491. uint16_t adc_value = 0;
  1492. #if CONFIG_IDF_TARGET_ESP32
  1493. if ( unit == ADC_UNIT_1 ) {
  1494. SENS.sar_meas_start1.sar1_en_pad = (1 << channel); //only one channel is selected.
  1495. while (SENS.sar_slave_addr1.meas_status != 0);
  1496. SENS.sar_meas_start1.meas1_start_sar = 0;
  1497. SENS.sar_meas_start1.meas1_start_sar = 1;
  1498. while (SENS.sar_meas_start1.meas1_done_sar == 0);
  1499. adc_value = SENS.sar_meas_start1.meas1_data_sar;
  1500. } else if ( unit == ADC_UNIT_2 ) {
  1501. SENS.sar_meas_start2.sar2_en_pad = (1 << channel); //only one channel is selected.
  1502. SENS.sar_meas_start2.meas2_start_sar = 0; //start force 0
  1503. SENS.sar_meas_start2.meas2_start_sar = 1; //start force 1
  1504. while (SENS.sar_meas_start2.meas2_done_sar == 0) {}; //read done
  1505. adc_value = SENS.sar_meas_start2.meas2_data_sar;
  1506. } else {
  1507. ESP_LOGE(TAG, "invalid adc unit");
  1508. return ESP_ERR_INVALID_ARG;
  1509. }
  1510. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1511. if ( unit == ADC_UNIT_1 ) {
  1512. SENS.sar_meas1_ctrl2.sar1_en_pad = (1 << channel); //only one channel is selected.
  1513. while (SENS.sar_slave_addr1.meas_status != 0);
  1514. SENS.sar_meas1_ctrl2.meas1_start_sar = 0;
  1515. SENS.sar_meas1_ctrl2.meas1_start_sar = 1;
  1516. while (SENS.sar_meas1_ctrl2.meas1_done_sar == 0);
  1517. adc_value = SENS.sar_meas1_ctrl2.meas1_data_sar;
  1518. } else if ( unit == ADC_UNIT_2 ) {
  1519. SENS.sar_meas2_ctrl2.sar2_en_pad = (1 << channel); //only one channel is selected.
  1520. SENS.sar_meas2_ctrl2.meas2_start_sar = 0; //start force 0
  1521. SENS.sar_meas2_ctrl2.meas2_start_sar = 1; //start force 1
  1522. while (SENS.sar_meas2_ctrl2.meas2_done_sar == 0) {}; //read done
  1523. adc_value = SENS.sar_meas2_ctrl2.meas2_data_sar;
  1524. } else {
  1525. ESP_LOGE(TAG, "invalid adc unit");
  1526. return ESP_ERR_INVALID_ARG;
  1527. }
  1528. #endif
  1529. return adc_value;
  1530. }
  1531. /*-------------------------------------------------------------------------------------
  1532. * ADC I2S
  1533. *------------------------------------------------------------------------------------*/
  1534. static esp_err_t adc_set_i2s_data_len(adc_unit_t adc_unit, int patt_len)
  1535. {
  1536. ADC_CHECK_UNIT(adc_unit);
  1537. RTC_MODULE_CHECK((patt_len < ADC_PATT_LEN_MAX) && (patt_len > 0), "ADC pattern length error", ESP_ERR_INVALID_ARG);
  1538. portENTER_CRITICAL(&rtc_spinlock);
  1539. if(adc_unit & ADC_UNIT_1) {
  1540. SYSCON.saradc_ctrl.sar1_patt_len = patt_len - 1;
  1541. }
  1542. if(adc_unit & ADC_UNIT_2) {
  1543. SYSCON.saradc_ctrl.sar2_patt_len = patt_len - 1;
  1544. }
  1545. portEXIT_CRITICAL(&rtc_spinlock);
  1546. return ESP_OK;
  1547. }
  1548. 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)
  1549. {
  1550. ADC_CHECK_UNIT(adc_unit);
  1551. if (adc_unit & ADC_UNIT_1) {
  1552. RTC_MODULE_CHECK((adc1_channel_t) channel < ADC1_CHANNEL_MAX, "ADC1 channel error", ESP_ERR_INVALID_ARG);
  1553. }
  1554. RTC_MODULE_CHECK(bits < ADC_WIDTH_MAX, "ADC bit width error", ESP_ERR_INVALID_ARG);
  1555. RTC_MODULE_CHECK(atten < ADC_ATTEN_MAX, "ADC Atten Err", ESP_ERR_INVALID_ARG);
  1556. portENTER_CRITICAL(&rtc_spinlock);
  1557. //Configure pattern table, each 8 bit defines one channel
  1558. //[7:4]-channel [3:2]-bit width [1:0]- attenuation
  1559. //BIT WIDTH: 3: 12BIT 2: 11BIT 1: 10BIT 0: 9BIT
  1560. //ATTEN: 3: ATTEN = 11dB 2: 6dB 1: 2.5dB 0: 0dB
  1561. uint8_t val = (channel << 4) | (bits << 2) | (atten << 0);
  1562. if (adc_unit & ADC_UNIT_1) {
  1563. SYSCON.saradc_sar1_patt_tab[seq_num / 4] &= (~(0xff << ((3 - (seq_num % 4)) * 8)));
  1564. SYSCON.saradc_sar1_patt_tab[seq_num / 4] |= (val << ((3 - (seq_num % 4)) * 8));
  1565. }
  1566. if (adc_unit & ADC_UNIT_2) {
  1567. SYSCON.saradc_sar2_patt_tab[seq_num / 4] &= (~(0xff << ((3 - (seq_num % 4)) * 8)));
  1568. SYSCON.saradc_sar2_patt_tab[seq_num / 4] |= (val << ((3 - (seq_num % 4)) * 8));
  1569. }
  1570. portEXIT_CRITICAL(&rtc_spinlock);
  1571. return ESP_OK;
  1572. }
  1573. esp_err_t adc_i2s_mode_init(adc_unit_t adc_unit, adc_channel_t channel)
  1574. {
  1575. ADC_CHECK_UNIT(adc_unit);
  1576. if (adc_unit & ADC_UNIT_1) {
  1577. RTC_MODULE_CHECK((adc1_channel_t) channel < ADC1_CHANNEL_MAX, "ADC1 channel error", ESP_ERR_INVALID_ARG);
  1578. }
  1579. uint8_t table_len = 1;
  1580. //POWER ON SAR
  1581. adc_power_always_on();
  1582. adc_gpio_init(adc_unit, channel);
  1583. adc_set_i2s_data_len(adc_unit, table_len);
  1584. adc_set_i2s_data_pattern(adc_unit, 0, channel, ADC_WIDTH_BIT_12, ADC_ATTEN_DB_11);
  1585. portENTER_CRITICAL(&rtc_spinlock);
  1586. if (adc_unit & ADC_UNIT_1) {
  1587. adc_set_controller( ADC_UNIT_1, ADC_CTRL_DIG );
  1588. }
  1589. if (adc_unit & ADC_UNIT_2) {
  1590. adc_set_controller( ADC_UNIT_2, ADC_CTRL_DIG );
  1591. }
  1592. portEXIT_CRITICAL(&rtc_spinlock);
  1593. adc_set_i2s_data_source(ADC_I2S_DATA_SRC_ADC);
  1594. adc_set_clk_div(SAR_ADC_CLK_DIV_DEFUALT);
  1595. // Set internal FSM wait time.
  1596. adc_set_fsm_time(ADC_FSM_RSTB_WAIT_DEFAULT, ADC_FSM_START_WAIT_DEFAULT, ADC_FSM_STANDBY_WAIT_DEFAULT,
  1597. ADC_FSM_TIME_KEEP);
  1598. adc_set_work_mode(adc_unit);
  1599. adc_set_data_format(ADC_ENCODE_12BIT);
  1600. adc_set_measure_limit(ADC_MAX_MEAS_NUM_DEFAULT, ADC_MEAS_NUM_LIM_DEFAULT);
  1601. //Invert The Level, Invert SAR ADC1 data
  1602. adc_set_data_inv(adc_unit, true);
  1603. return ESP_OK;
  1604. }
  1605. /*-------------------------------------------------------------------------------------
  1606. * ADC1
  1607. *------------------------------------------------------------------------------------*/
  1608. esp_err_t adc1_pad_get_io_num(adc1_channel_t channel, gpio_num_t *gpio_num)
  1609. {
  1610. RTC_MODULE_CHECK(channel < ADC1_CHANNEL_MAX, "ADC1 Channel Err", ESP_ERR_INVALID_ARG);
  1611. switch (channel) {
  1612. case ADC1_CHANNEL_0:
  1613. *gpio_num = ADC1_CHANNEL_0_GPIO_NUM;
  1614. break;
  1615. case ADC1_CHANNEL_1:
  1616. *gpio_num = ADC1_CHANNEL_1_GPIO_NUM;
  1617. break;
  1618. case ADC1_CHANNEL_2:
  1619. *gpio_num = ADC1_CHANNEL_2_GPIO_NUM;
  1620. break;
  1621. case ADC1_CHANNEL_3:
  1622. *gpio_num = ADC1_CHANNEL_3_GPIO_NUM;
  1623. break;
  1624. case ADC1_CHANNEL_4:
  1625. *gpio_num = ADC1_CHANNEL_4_GPIO_NUM;
  1626. break;
  1627. case ADC1_CHANNEL_5:
  1628. *gpio_num = ADC1_CHANNEL_5_GPIO_NUM;
  1629. break;
  1630. case ADC1_CHANNEL_6:
  1631. *gpio_num = ADC1_CHANNEL_6_GPIO_NUM;
  1632. break;
  1633. case ADC1_CHANNEL_7:
  1634. *gpio_num = ADC1_CHANNEL_7_GPIO_NUM;
  1635. break;
  1636. default:
  1637. return ESP_ERR_INVALID_ARG;
  1638. }
  1639. return ESP_OK;
  1640. }
  1641. esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten)
  1642. {
  1643. RTC_MODULE_CHECK(channel < ADC1_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  1644. RTC_MODULE_CHECK(atten < ADC_ATTEN_MAX, "ADC Atten Err", ESP_ERR_INVALID_ARG);
  1645. adc_gpio_init(ADC_UNIT_1, channel);
  1646. adc_set_atten(ADC_UNIT_1, channel, atten);
  1647. return ESP_OK;
  1648. }
  1649. esp_err_t adc1_config_width(adc_bits_width_t width_bit)
  1650. {
  1651. RTC_MODULE_CHECK(width_bit < ADC_WIDTH_MAX, "ADC bit width error", ESP_ERR_INVALID_ARG);
  1652. adc_set_data_width(ADC_UNIT_1, width_bit);
  1653. adc_set_data_inv(ADC_UNIT_1, true);
  1654. return ESP_OK;
  1655. }
  1656. static inline void adc1_fsm_disable(void)
  1657. {
  1658. #if CONFIG_IDF_TARGET_ESP32
  1659. //channel is set in the convert function
  1660. SENS.sar_meas_wait2.force_xpd_amp = SENS_FORCE_XPD_AMP_PD;
  1661. //disable FSM, it's only used by the LNA.
  1662. SENS.sar_meas_ctrl.amp_rst_fb_fsm = 0;
  1663. SENS.sar_meas_ctrl.amp_short_ref_fsm = 0;
  1664. SENS.sar_meas_ctrl.amp_short_ref_gnd_fsm = 0;
  1665. SENS.sar_meas_wait1.sar_amp_wait1 = 1;
  1666. SENS.sar_meas_wait1.sar_amp_wait2 = 1;
  1667. SENS.sar_meas_wait2.sar_amp_wait3 = 1;
  1668. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1669. //channel is set in the convert function
  1670. SENS.sar_meas1_ctrl1.force_xpd_amp = SENS_FORCE_XPD_AMP_PD;
  1671. //disable FSM, it's only used by the LNA.
  1672. SENS.sar_amp_ctrl3.amp_rst_fb_fsm = 0;
  1673. SENS.sar_amp_ctrl3.amp_short_ref_fsm = 0;
  1674. SENS.sar_amp_ctrl3.amp_short_ref_gnd_fsm = 0;
  1675. SENS.sar_amp_ctrl1.sar_amp_wait1 = 1;
  1676. SENS.sar_amp_ctrl1.sar_amp_wait2 = 1;
  1677. SENS.sar_amp_ctrl2.sar_amp_wait3 = 1;
  1678. #endif
  1679. }
  1680. esp_err_t adc1_i2s_mode_acquire(void)
  1681. {
  1682. //lazy initialization
  1683. //for i2s, block until acquire the lock
  1684. _lock_acquire( &adc1_i2s_lock );
  1685. ESP_LOGD( RTC_MODULE_TAG, "i2s mode takes adc1 lock." );
  1686. portENTER_CRITICAL(&rtc_spinlock);
  1687. #if CONFIG_IDF_TARGET_ESP32
  1688. SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  1689. //switch SARADC into DIG channel
  1690. SENS.sar_read_ctrl.sar1_dig_force = 1;
  1691. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1692. SENS.sar_power_xpd_sar.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
  1693. //switch SARADC into DIG channel
  1694. SENS.sar_meas1_mux.sar1_dig_force = 1;
  1695. #endif
  1696. portEXIT_CRITICAL(&rtc_spinlock);
  1697. return ESP_OK;
  1698. }
  1699. esp_err_t adc1_adc_mode_acquire(void)
  1700. {
  1701. //lazy initialization
  1702. //for adc1, block until acquire the lock
  1703. _lock_acquire( &adc1_i2s_lock );
  1704. ESP_LOGD( RTC_MODULE_TAG, "adc mode takes adc1 lock." );
  1705. portENTER_CRITICAL(&rtc_spinlock);
  1706. // for now the WiFi would use ADC2 and set xpd_sar force on.
  1707. // so we can not reset xpd_sar to fsm mode directly.
  1708. // We should handle this after the synchronization mechanism is established.
  1709. //switch SARADC into RTC channel
  1710. #if CONFIG_IDF_TARGET_ESP32
  1711. SENS.sar_read_ctrl.sar1_dig_force = 0;
  1712. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1713. SENS.sar_meas1_mux.sar1_dig_force = 0;
  1714. #endif
  1715. portEXIT_CRITICAL(&rtc_spinlock);
  1716. return ESP_OK;
  1717. }
  1718. esp_err_t adc1_lock_release(void)
  1719. {
  1720. RTC_MODULE_CHECK((uint32_t*)adc1_i2s_lock != NULL, "adc1 lock release called before acquire", ESP_ERR_INVALID_STATE );
  1721. // for now the WiFi would use ADC2 and set xpd_sar force on.
  1722. // so we can not reset xpd_sar to fsm mode directly.
  1723. // We should handle this after the synchronization mechanism is established.
  1724. _lock_release( &adc1_i2s_lock );
  1725. return ESP_OK;
  1726. }
  1727. int adc1_get_raw(adc1_channel_t channel)
  1728. {
  1729. uint16_t adc_value;
  1730. RTC_MODULE_CHECK(channel < ADC1_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  1731. adc1_adc_mode_acquire();
  1732. adc_power_on();
  1733. portENTER_CRITICAL(&rtc_spinlock);
  1734. //disable other peripherals
  1735. adc1_hall_enable(false);
  1736. adc1_fsm_disable(); //currently the LNA is not open, close it by default
  1737. //set controller
  1738. adc_set_controller( ADC_UNIT_1, ADC_CTRL_RTC );
  1739. //start conversion
  1740. adc_value = adc_convert( ADC_UNIT_1, channel );
  1741. portEXIT_CRITICAL(&rtc_spinlock);
  1742. adc1_lock_release();
  1743. return adc_value;
  1744. }
  1745. void adc1_ulp_enable(void)
  1746. {
  1747. adc_power_on();
  1748. portENTER_CRITICAL(&rtc_spinlock);
  1749. adc_set_controller( ADC_UNIT_1, ADC_CTRL_ULP );
  1750. // since most users do not need LNA and HALL with uLP, we disable them here
  1751. // open them in the uLP if needed.
  1752. adc1_fsm_disable();
  1753. adc1_hall_enable(false);
  1754. portEXIT_CRITICAL(&rtc_spinlock);
  1755. }
  1756. /*---------------------------------------------------------------
  1757. ADC2
  1758. ---------------------------------------------------------------*/
  1759. esp_err_t adc2_pad_get_io_num(adc2_channel_t channel, gpio_num_t *gpio_num)
  1760. {
  1761. RTC_MODULE_CHECK(channel < ADC2_CHANNEL_MAX, "ADC2 Channel Err", ESP_ERR_INVALID_ARG);
  1762. switch (channel) {
  1763. case ADC2_CHANNEL_0:
  1764. *gpio_num = ADC2_CHANNEL_0_GPIO_NUM;
  1765. break;
  1766. case ADC2_CHANNEL_1:
  1767. *gpio_num = ADC2_CHANNEL_1_GPIO_NUM;
  1768. break;
  1769. case ADC2_CHANNEL_2:
  1770. *gpio_num = ADC2_CHANNEL_2_GPIO_NUM;
  1771. break;
  1772. case ADC2_CHANNEL_3:
  1773. *gpio_num = ADC2_CHANNEL_3_GPIO_NUM;
  1774. break;
  1775. case ADC2_CHANNEL_4:
  1776. *gpio_num = ADC2_CHANNEL_4_GPIO_NUM;
  1777. break;
  1778. case ADC2_CHANNEL_5:
  1779. *gpio_num = ADC2_CHANNEL_5_GPIO_NUM;
  1780. break;
  1781. case ADC2_CHANNEL_6:
  1782. *gpio_num = ADC2_CHANNEL_6_GPIO_NUM;
  1783. break;
  1784. case ADC2_CHANNEL_7:
  1785. *gpio_num = ADC2_CHANNEL_7_GPIO_NUM;
  1786. break;
  1787. case ADC2_CHANNEL_8:
  1788. *gpio_num = ADC2_CHANNEL_8_GPIO_NUM;
  1789. break;
  1790. case ADC2_CHANNEL_9:
  1791. *gpio_num = ADC2_CHANNEL_9_GPIO_NUM;
  1792. break;
  1793. default:
  1794. return ESP_ERR_INVALID_ARG;
  1795. }
  1796. return ESP_OK;
  1797. }
  1798. esp_err_t adc2_wifi_acquire(void)
  1799. {
  1800. //lazy initialization
  1801. //for wifi, block until acquire the lock
  1802. _lock_acquire( &adc2_wifi_lock );
  1803. ESP_LOGD( RTC_MODULE_TAG, "Wi-Fi takes adc2 lock." );
  1804. return ESP_OK;
  1805. }
  1806. esp_err_t adc2_wifi_release(void)
  1807. {
  1808. RTC_MODULE_CHECK((uint32_t*)adc2_wifi_lock != NULL, "wifi release called before acquire", ESP_ERR_INVALID_STATE );
  1809. _lock_release( &adc2_wifi_lock );
  1810. ESP_LOGD( RTC_MODULE_TAG, "Wi-Fi returns adc2 lock." );
  1811. return ESP_OK;
  1812. }
  1813. static esp_err_t adc2_pad_init(adc2_channel_t channel)
  1814. {
  1815. gpio_num_t gpio_num = 0;
  1816. ADC2_CHECK_FUNCTION_RET(adc2_pad_get_io_num(channel, &gpio_num));
  1817. ADC2_CHECK_FUNCTION_RET(rtc_gpio_init(gpio_num));
  1818. ADC2_CHECK_FUNCTION_RET(rtc_gpio_output_disable(gpio_num));
  1819. ADC2_CHECK_FUNCTION_RET(rtc_gpio_input_disable(gpio_num));
  1820. ADC2_CHECK_FUNCTION_RET(gpio_set_pull_mode(gpio_num, GPIO_FLOATING));
  1821. return ESP_OK;
  1822. }
  1823. esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten)
  1824. {
  1825. RTC_MODULE_CHECK(channel < ADC2_CHANNEL_MAX, "ADC2 Channel Err", ESP_ERR_INVALID_ARG);
  1826. RTC_MODULE_CHECK(atten <= ADC_ATTEN_11db, "ADC2 Atten Err", ESP_ERR_INVALID_ARG);
  1827. adc2_pad_init(channel);
  1828. portENTER_CRITICAL( &adc2_spinlock );
  1829. //lazy initialization
  1830. //avoid collision with other tasks
  1831. if ( _lock_try_acquire( &adc2_wifi_lock ) == -1 ) {
  1832. //try the lock, return if failed (wifi using).
  1833. portEXIT_CRITICAL( &adc2_spinlock );
  1834. return ESP_ERR_TIMEOUT;
  1835. }
  1836. SENS.sar_atten2 = ( SENS.sar_atten2 & ~(3<<(channel*2)) ) | ((atten&3) << (channel*2));
  1837. _lock_release( &adc2_wifi_lock );
  1838. portEXIT_CRITICAL( &adc2_spinlock );
  1839. return ESP_OK;
  1840. }
  1841. static inline void adc2_config_width(adc_bits_width_t width_bit)
  1842. {
  1843. portENTER_CRITICAL(&rtc_spinlock);
  1844. #if CONFIG_IDF_TARGET_ESP32
  1845. //sar_start_force shared with ADC1
  1846. SENS.sar_start_force.sar2_bit_width = width_bit;
  1847. //cct set to the same value with PHY
  1848. SENS.sar_start_force.sar2_pwdet_cct = 4;
  1849. portEXIT_CRITICAL(&rtc_spinlock);
  1850. //Invert the adc value,the Output value is invert
  1851. SENS.sar_read_ctrl2.sar2_data_inv = 1;
  1852. //Set The adc sample width,invert adc value,must digital sar2_bit_width[1:0]=3
  1853. SENS.sar_read_ctrl2.sar2_sample_bit = width_bit;
  1854. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1855. //sar_start_force shared with ADC1
  1856. SENS.sar_meas2_ctrl1.sar2_bit_width = width_bit;
  1857. //cct set to the same value with PHY
  1858. SENS.sar_meas2_mux.sar2_pwdet_cct = 4;
  1859. portEXIT_CRITICAL(&rtc_spinlock);
  1860. //Invert the adc value,the Output value is invert
  1861. SENS.sar_reader2_ctrl.sar2_data_inv = 1;
  1862. //Set The adc sample width,invert adc value,must digital sar2_bit_width[1:0]=3
  1863. SENS.sar_reader2_ctrl.sar2_sample_bit = width_bit;
  1864. #endif
  1865. }
  1866. static inline void adc2_dac_disable( adc2_channel_t channel)
  1867. {
  1868. #if CONFIG_IDF_TARGET_ESP32
  1869. if ( channel == ADC2_CHANNEL_8 ) { // the same as DAC channel 1
  1870. dac_output_set_enable( DAC_CHANNEL_1, false );
  1871. } else if ( channel == ADC2_CHANNEL_9 ) {
  1872. dac_output_set_enable( DAC_CHANNEL_2, false );
  1873. }
  1874. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  1875. if ( channel == ADC2_CHANNEL_6 ) { // the same as DAC channel 1
  1876. dac_output_set_enable( DAC_CHANNEL_1, false );
  1877. } else if ( channel == ADC2_CHANNEL_7 ) {
  1878. dac_output_set_enable( DAC_CHANNEL_2, false );
  1879. }
  1880. #endif
  1881. }
  1882. //registers in critical section with adc1:
  1883. //SENS_SAR_START_FORCE_REG,
  1884. esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int* raw_out)
  1885. {
  1886. uint16_t adc_value = 0;
  1887. RTC_MODULE_CHECK(channel < ADC2_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  1888. //in critical section with whole rtc module
  1889. adc_power_on();
  1890. //avoid collision with other tasks
  1891. portENTER_CRITICAL(&adc2_spinlock);
  1892. //lazy initialization
  1893. //try the lock, return if failed (wifi using).
  1894. if ( _lock_try_acquire( &adc2_wifi_lock ) == -1 ) {
  1895. portEXIT_CRITICAL( &adc2_spinlock );
  1896. return ESP_ERR_TIMEOUT;
  1897. }
  1898. //disable other peripherals
  1899. #ifdef CONFIG_ADC_DISABLE_DAC
  1900. adc2_dac_disable( channel );
  1901. #endif
  1902. // set controller
  1903. // in critical section with whole rtc module
  1904. // because the PWDET use the same registers, place it here.
  1905. adc2_config_width( width_bit );
  1906. adc_set_controller( ADC_UNIT_2, ADC_CTRL_RTC );
  1907. //start converting
  1908. adc_value = adc_convert( ADC_UNIT_2, channel );
  1909. _lock_release( &adc2_wifi_lock );
  1910. portEXIT_CRITICAL(&adc2_spinlock);
  1911. *raw_out = (int)adc_value;
  1912. return ESP_OK;
  1913. }
  1914. esp_err_t adc2_vref_to_gpio(gpio_num_t gpio)
  1915. {
  1916. #if CONFIG_IDF_TARGET_ESP32
  1917. int channel;
  1918. if(gpio == GPIO_NUM_25){
  1919. channel = 8; //Channel 8 bit
  1920. }else if (gpio == GPIO_NUM_26){
  1921. channel = 9; //Channel 9 bit
  1922. }else if (gpio == GPIO_NUM_27){
  1923. channel = 7; //Channel 7 bit
  1924. }else{
  1925. return ESP_ERR_INVALID_ARG;
  1926. }
  1927. //Configure RTC gpio
  1928. rtc_gpio_init(gpio);
  1929. rtc_gpio_output_disable(gpio);
  1930. rtc_gpio_input_disable(gpio);
  1931. rtc_gpio_pullup_dis(gpio);
  1932. rtc_gpio_pulldown_dis(gpio);
  1933. //force fsm
  1934. adc_power_always_on(); //Select power source of ADC
  1935. RTCCNTL.bias_conf.dbg_atten = 0; //Check DBG effect outside sleep mode
  1936. //set dtest (MUX_SEL : 0 -> RTC; 1-> vdd_sar2)
  1937. RTCCNTL.test_mux.dtest_rtc = 1; //Config test mux to route v_ref to ADC2 Channels
  1938. //set ent
  1939. RTCCNTL.test_mux.ent_rtc = 1;
  1940. //set sar2_en_test
  1941. SENS.sar_start_force.sar2_en_test = 1;
  1942. //set sar2 en force
  1943. SENS.sar_meas_start2.sar2_en_pad_force = 1; //Pad bitmap controlled by SW
  1944. //set en_pad for channels 7,8,9 (bits 0x380)
  1945. SENS.sar_meas_start2.sar2_en_pad = 1<<channel;
  1946. #endif
  1947. return ESP_OK;
  1948. }
  1949. /*---------------------------------------------------------------
  1950. DAC
  1951. ---------------------------------------------------------------*/
  1952. esp_err_t dac_pad_get_io_num(dac_channel_t channel, gpio_num_t *gpio_num)
  1953. {
  1954. RTC_MODULE_CHECK((channel >= DAC_CHANNEL_1) && (channel < DAC_CHANNEL_MAX), DAC_ERR_STR_CHANNEL_ERROR, ESP_ERR_INVALID_ARG);
  1955. RTC_MODULE_CHECK(gpio_num, "Param null", ESP_ERR_INVALID_ARG);
  1956. switch (channel) {
  1957. case DAC_CHANNEL_1:
  1958. *gpio_num = DAC_CHANNEL_1_GPIO_NUM;
  1959. break;
  1960. case DAC_CHANNEL_2:
  1961. *gpio_num = DAC_CHANNEL_2_GPIO_NUM;
  1962. break;
  1963. default:
  1964. return ESP_ERR_INVALID_ARG;
  1965. }
  1966. return ESP_OK;
  1967. }
  1968. static esp_err_t dac_rtc_pad_init(dac_channel_t channel)
  1969. {
  1970. RTC_MODULE_CHECK((channel >= DAC_CHANNEL_1) && (channel < DAC_CHANNEL_MAX), DAC_ERR_STR_CHANNEL_ERROR, ESP_ERR_INVALID_ARG);
  1971. gpio_num_t gpio_num = 0;
  1972. dac_pad_get_io_num(channel, &gpio_num);
  1973. rtc_gpio_init(gpio_num);
  1974. rtc_gpio_output_disable(gpio_num);
  1975. rtc_gpio_input_disable(gpio_num);
  1976. rtc_gpio_pullup_dis(gpio_num);
  1977. rtc_gpio_pulldown_dis(gpio_num);
  1978. return ESP_OK;
  1979. }
  1980. static inline void dac_output_set_enable(dac_channel_t channel, bool enable)
  1981. {
  1982. RTCIO.pad_dac[channel-DAC_CHANNEL_1].dac_xpd_force = enable;
  1983. RTCIO.pad_dac[channel-DAC_CHANNEL_1].xpd_dac = enable;
  1984. }
  1985. esp_err_t dac_output_enable(dac_channel_t channel)
  1986. {
  1987. RTC_MODULE_CHECK((channel >= DAC_CHANNEL_1) && (channel < DAC_CHANNEL_MAX), DAC_ERR_STR_CHANNEL_ERROR, ESP_ERR_INVALID_ARG);
  1988. dac_rtc_pad_init(channel);
  1989. portENTER_CRITICAL(&rtc_spinlock);
  1990. dac_output_set_enable(channel, true);
  1991. portEXIT_CRITICAL(&rtc_spinlock);
  1992. return ESP_OK;
  1993. }
  1994. esp_err_t dac_output_disable(dac_channel_t channel)
  1995. {
  1996. RTC_MODULE_CHECK((channel >= DAC_CHANNEL_1) && (channel < DAC_CHANNEL_MAX), DAC_ERR_STR_CHANNEL_ERROR, ESP_ERR_INVALID_ARG);
  1997. portENTER_CRITICAL(&rtc_spinlock);
  1998. dac_output_set_enable(channel, false);
  1999. portEXIT_CRITICAL(&rtc_spinlock);
  2000. return ESP_OK;
  2001. }
  2002. esp_err_t dac_output_voltage(dac_channel_t channel, uint8_t dac_value)
  2003. {
  2004. RTC_MODULE_CHECK((channel >= DAC_CHANNEL_1) && (channel < DAC_CHANNEL_MAX), DAC_ERR_STR_CHANNEL_ERROR, ESP_ERR_INVALID_ARG);
  2005. portENTER_CRITICAL(&rtc_spinlock);
  2006. //Disable Tone
  2007. CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_SW_TONE_EN);
  2008. //Disable Channel Tone
  2009. if (channel == DAC_CHANNEL_1) {
  2010. CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN1_M);
  2011. } else if (channel == DAC_CHANNEL_2) {
  2012. CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN2_M);
  2013. }
  2014. //Set the Dac value
  2015. if (channel == DAC_CHANNEL_1) {
  2016. SET_PERI_REG_BITS(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_DAC, dac_value, RTC_IO_PDAC1_DAC_S); //dac_output
  2017. } else if (channel == DAC_CHANNEL_2) {
  2018. SET_PERI_REG_BITS(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_DAC, dac_value, RTC_IO_PDAC2_DAC_S); //dac_output
  2019. }
  2020. portEXIT_CRITICAL(&rtc_spinlock);
  2021. return ESP_OK;
  2022. }
  2023. esp_err_t dac_i2s_enable(void)
  2024. {
  2025. portENTER_CRITICAL(&rtc_spinlock);
  2026. SET_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_DAC_DIG_FORCE_M | SENS_DAC_CLK_INV_M);
  2027. portEXIT_CRITICAL(&rtc_spinlock);
  2028. return ESP_OK;
  2029. }
  2030. esp_err_t dac_i2s_disable(void)
  2031. {
  2032. portENTER_CRITICAL(&rtc_spinlock);
  2033. CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_DAC_DIG_FORCE_M | SENS_DAC_CLK_INV_M);
  2034. portEXIT_CRITICAL(&rtc_spinlock);
  2035. return ESP_OK;
  2036. }
  2037. /*---------------------------------------------------------------
  2038. HALL SENSOR
  2039. ---------------------------------------------------------------*/
  2040. static inline void adc1_hall_enable(bool enable)
  2041. {
  2042. #if CONFIG_IDF_TARGET_ESP32
  2043. RTCIO.hall_sens.xpd_hall = enable;
  2044. #endif
  2045. }
  2046. static int hall_sensor_get_value(void) //hall sensor without LNA
  2047. {
  2048. int hall_value = 0;
  2049. adc_power_on();
  2050. #if CONFIG_IDF_TARGET_ESP32
  2051. int Sens_Vp0;
  2052. int Sens_Vn0;
  2053. int Sens_Vp1;
  2054. int Sens_Vn1;
  2055. portENTER_CRITICAL(&rtc_spinlock);
  2056. //disable other peripherals
  2057. adc1_fsm_disable();//currently the LNA is not open, close it by default
  2058. adc1_hall_enable(true);
  2059. // set controller
  2060. adc_set_controller( ADC_UNIT_1, ADC_CTRL_RTC );
  2061. // convert for 4 times with different phase and outputs
  2062. RTCIO.hall_sens.hall_phase = 0; // hall phase
  2063. Sens_Vp0 = adc_convert( ADC_UNIT_1, ADC1_CHANNEL_0 );
  2064. Sens_Vn0 = adc_convert( ADC_UNIT_1, ADC1_CHANNEL_3 );
  2065. RTCIO.hall_sens.hall_phase = 1;
  2066. Sens_Vp1 = adc_convert( ADC_UNIT_1, ADC1_CHANNEL_0 );
  2067. Sens_Vn1 = adc_convert( ADC_UNIT_1, ADC1_CHANNEL_3 );
  2068. portEXIT_CRITICAL(&rtc_spinlock);
  2069. hall_value = (Sens_Vp1 - Sens_Vp0) - (Sens_Vn1 - Sens_Vn0);
  2070. #endif
  2071. return hall_value;
  2072. }
  2073. int hall_sensor_read(void)
  2074. {
  2075. adc_gpio_init(ADC_UNIT_1, ADC1_CHANNEL_0);
  2076. adc_gpio_init(ADC_UNIT_1, ADC1_CHANNEL_3);
  2077. adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_0);
  2078. adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_0);
  2079. return hall_sensor_get_value();
  2080. }
  2081. /*---------------------------------------------------------------
  2082. INTERRUPT HANDLER
  2083. ---------------------------------------------------------------*/
  2084. typedef struct rtc_isr_handler_ {
  2085. uint32_t mask;
  2086. intr_handler_t handler;
  2087. void* handler_arg;
  2088. SLIST_ENTRY(rtc_isr_handler_) next;
  2089. } rtc_isr_handler_t;
  2090. static SLIST_HEAD(rtc_isr_handler_list_, rtc_isr_handler_) s_rtc_isr_handler_list =
  2091. SLIST_HEAD_INITIALIZER(s_rtc_isr_handler_list);
  2092. portMUX_TYPE s_rtc_isr_handler_list_lock = portMUX_INITIALIZER_UNLOCKED;
  2093. static intr_handle_t s_rtc_isr_handle;
  2094. static void rtc_isr(void* arg)
  2095. {
  2096. uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
  2097. rtc_isr_handler_t* it;
  2098. portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  2099. SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
  2100. if (it->mask & status) {
  2101. portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  2102. (*it->handler)(it->handler_arg);
  2103. portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  2104. }
  2105. }
  2106. portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  2107. REG_WRITE(RTC_CNTL_INT_CLR_REG, status);
  2108. }
  2109. static esp_err_t rtc_isr_ensure_installed(void)
  2110. {
  2111. esp_err_t err = ESP_OK;
  2112. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  2113. if (s_rtc_isr_handle) {
  2114. goto out;
  2115. }
  2116. REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
  2117. REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
  2118. err = esp_intr_alloc(ETS_RTC_CORE_INTR_SOURCE, 0, &rtc_isr, NULL, &s_rtc_isr_handle);
  2119. if (err != ESP_OK) {
  2120. goto out;
  2121. }
  2122. out:
  2123. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  2124. return err;
  2125. }
  2126. esp_err_t rtc_isr_register(intr_handler_t handler, void* handler_arg, uint32_t rtc_intr_mask)
  2127. {
  2128. esp_err_t err = rtc_isr_ensure_installed();
  2129. if (err != ESP_OK) {
  2130. return err;
  2131. }
  2132. rtc_isr_handler_t* item = malloc(sizeof(*item));
  2133. if (item == NULL) {
  2134. return ESP_ERR_NO_MEM;
  2135. }
  2136. item->handler = handler;
  2137. item->handler_arg = handler_arg;
  2138. item->mask = rtc_intr_mask;
  2139. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  2140. SLIST_INSERT_HEAD(&s_rtc_isr_handler_list, item, next);
  2141. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  2142. return ESP_OK;
  2143. }
  2144. esp_err_t rtc_isr_deregister(intr_handler_t handler, void* handler_arg)
  2145. {
  2146. rtc_isr_handler_t* it;
  2147. rtc_isr_handler_t* prev = NULL;
  2148. bool found = false;
  2149. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  2150. SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
  2151. if (it->handler == handler && it->handler_arg == handler_arg) {
  2152. if (it == SLIST_FIRST(&s_rtc_isr_handler_list)) {
  2153. SLIST_REMOVE_HEAD(&s_rtc_isr_handler_list, next);
  2154. } else {
  2155. SLIST_REMOVE_AFTER(prev, next);
  2156. }
  2157. found = true;
  2158. free(it);
  2159. break;
  2160. }
  2161. prev = it;
  2162. }
  2163. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  2164. return found ? ESP_OK : ESP_ERR_INVALID_STATE;
  2165. }