rtc_module.c 72 KB

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