rtc_module.c 78 KB

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