rtc_module.c 76 KB

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