rtc_module.c 75 KB

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