test_esp32s2.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  1. // Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Tests for the touch sensor device driver for ESP32-S2 only
  16. */
  17. #include "sdkconfig.h"
  18. #if CONFIG_IDF_TARGET_ESP32S2
  19. #include <string.h>
  20. #include "esp_system.h"
  21. #include "driver/touch_pad.h"
  22. #include "unity.h"
  23. #include "esp_system.h"
  24. #include "freertos/FreeRTOS.h"
  25. #include "freertos/task.h"
  26. #include "freertos/semphr.h"
  27. #include "freertos/queue.h"
  28. #include "esp_log.h"
  29. #include "test_utils.h"
  30. #include "soc/rtc_cntl_reg.h"
  31. #include "soc/rtc_cntl_struct.h"
  32. #include "soc/sens_reg.h"
  33. #include "soc/sens_struct.h"
  34. #include "soc/rtc_cntl_reg.h"
  35. #include "soc/rtc_cntl_struct.h"
  36. #include "soc/rtc_io_reg.h"
  37. #include "soc/rtc_io_struct.h"
  38. #include "soc/apb_ctrl_reg.h"
  39. #include "driver/rtc_io.h"
  40. #include "esp_rom_sys.h"
  41. static const char *TAG = "test_touch";
  42. #define PLATFORM_SELECT (1) //0: pxp; 1: chip
  43. #if (PLATFORM_SELECT == 0) //PXP platform
  44. #define SET_BREAK_POINT(flag) REG_WRITE(APB_CTRL_DATE_REG, flag)
  45. //PXP clk is slower.
  46. #define SYS_DELAY_TIME_MOM (1/40)
  47. #define RTC_SLOW_CLK_FLAG 1 // Slow clock is 32KHz.
  48. void test_pxp_deinit_io(void)
  49. {
  50. for (int i = 0; i < 22; i++) {
  51. rtc_gpio_init(i);
  52. }
  53. }
  54. #else
  55. //PXP clk is slower.
  56. #define SET_BREAK_POINT(flag)
  57. #define SYS_DELAY_TIME_MOM (1)
  58. #define RTC_SLOW_CLK_FLAG 0 // Slow clock is 32KHz.
  59. void test_pxp_deinit_io(void)
  60. {
  61. ;
  62. }
  63. #endif
  64. #define TOUCH_READ_INVALID_VAL (SOC_TOUCH_PAD_THRESHOLD_MAX)
  65. #define TOUCH_READ_ERROR (100)
  66. #define TOUCH_INTR_THRESHOLD (0.1)
  67. #define TOUCH_EXCEED_TIME_MS (1000)
  68. #define TOUCH_REG_BASE_TEST() ({ \
  69. TEST_ASSERT_EQUAL_UINT32(REG_GET_FIELD(RTC_CNTL_DATE_REG, RTC_CNTL_CNTL_DATE), RTCCNTL.date.date); \
  70. TEST_ASSERT_EQUAL_UINT32(REG_GET_FIELD(SENS_SARDATE_REG, SENS_SAR_DATE), SENS.sardate.sar_date); \
  71. TEST_ASSERT_EQUAL_UINT32(REG_GET_FIELD(RTC_IO_DATE_REG, RTC_IO_IO_DATE), RTCIO.date.date); \
  72. })
  73. #define TEST_TOUCH_COUNT_NUM (5)
  74. #define TEST_TOUCH_CHANNEL (14)
  75. static touch_pad_t touch_list[TEST_TOUCH_CHANNEL] = {
  76. // TOUCH_PAD_NUM0, is GPIO0, for download.
  77. TOUCH_PAD_NUM1,
  78. TOUCH_PAD_NUM2,
  79. TOUCH_PAD_NUM3,
  80. TOUCH_PAD_NUM4,
  81. TOUCH_PAD_NUM5,
  82. TOUCH_PAD_NUM6,
  83. TOUCH_PAD_NUM7,
  84. TOUCH_PAD_NUM8,
  85. TOUCH_PAD_NUM9,
  86. TOUCH_PAD_NUM10,
  87. TOUCH_PAD_NUM11,
  88. TOUCH_PAD_NUM12,
  89. TOUCH_PAD_NUM13,
  90. TOUCH_PAD_NUM14
  91. };
  92. #define TOUCH_WATERPROOF_RING_PAD TOUCH_PAD_NUM1
  93. static touch_pad_t proximity_pad[3] = {
  94. TOUCH_PAD_NUM2,
  95. TOUCH_PAD_NUM3,
  96. TOUCH_PAD_NUM4,
  97. };
  98. static QueueHandle_t que_touch = NULL;
  99. typedef struct touch_msg {
  100. touch_pad_intr_mask_t intr_mask;
  101. uint32_t pad_num;
  102. uint32_t pad_status;
  103. uint32_t pad_val;
  104. uint32_t slp_proxi_cnt;
  105. uint32_t slp_proxi_base;
  106. } touch_event_t;
  107. static uint32_t s_touch_timeout_mask = 0;
  108. static void printf_touch_hw_read(const char *str)
  109. {
  110. uint32_t touch_value;
  111. printf("[%s] ", str);
  112. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  113. touch_pad_read_raw_data(touch_list[i], &touch_value);
  114. printf("[%d]%d ", touch_list[i], touch_value);
  115. }
  116. printf("\r\n");
  117. }
  118. static void printf_touch_benchmark_read(const char *str)
  119. {
  120. uint32_t touch_value;
  121. printf("[%s] ", str);
  122. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  123. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  124. printf("[%d]%d ", touch_list[i], touch_value);
  125. }
  126. printf("\r\n");
  127. }
  128. static void printf_touch_smooth_read(const char *str)
  129. {
  130. uint32_t touch_value;
  131. printf("[%s] ", str);
  132. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  133. touch_pad_filter_read_smooth(touch_list[i], &touch_value);
  134. printf("[%d]%d ", touch_list[i], touch_value);
  135. }
  136. printf("\r\n");
  137. }
  138. static void test_timeout_trigger_fake(touch_pad_t pad_num)
  139. {
  140. touch_pad_set_cnt_mode(pad_num, TOUCH_PAD_SLOPE_0, TOUCH_PAD_TIE_OPT_DEFAULT);
  141. }
  142. static void test_timeout_normal(touch_pad_t pad_num)
  143. {
  144. touch_pad_set_cnt_mode(pad_num, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_DEFAULT);
  145. }
  146. /*
  147. * Change the slope to get larger value from touch sensor.
  148. */
  149. static void test_press_fake(touch_pad_t pad_num)
  150. {
  151. touch_pad_set_cnt_mode(pad_num, TOUCH_PAD_SLOPE_3, TOUCH_PAD_TIE_OPT_DEFAULT);
  152. }
  153. /*
  154. * Change the slope to get larger value from touch sensor.
  155. */
  156. static void test_release_fake(touch_pad_t pad_num)
  157. {
  158. touch_pad_set_cnt_mode(pad_num, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_DEFAULT);
  159. }
  160. static void test_touch_push_all(void)
  161. {
  162. ESP_LOGI(TAG, "touch push");
  163. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  164. test_press_fake(touch_list[i]);
  165. }
  166. }
  167. static void test_touch_release_all(void)
  168. {
  169. ESP_LOGI(TAG, "touch release");
  170. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  171. test_release_fake(touch_list[i]);
  172. }
  173. }
  174. static void test_touch_benchmark(void)
  175. {
  176. uint32_t touch_val[TEST_TOUCH_CHANNEL] = {0};
  177. uint32_t touch_temp[TEST_TOUCH_CHANNEL] = {0};
  178. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  179. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_val[i]) );
  180. }
  181. for (int i = 0; i < 10; i++) {
  182. vTaskDelay(20 / portTICK_PERIOD_MS);
  183. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  184. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_temp[i]) );
  185. TEST_ASSERT_EQUAL(touch_temp[i], touch_val[i]);
  186. }
  187. }
  188. }
  189. /*
  190. * Test the stable and change of touch sensor reading in SW mode.
  191. */
  192. esp_err_t test_touch_sw_read(void)
  193. {
  194. uint32_t touch_value[TEST_TOUCH_CHANNEL] = {0};
  195. uint32_t touch_temp[TEST_TOUCH_CHANNEL] = {0};
  196. uint32_t touch_push[TEST_TOUCH_CHANNEL] = {0};
  197. int test_cnt = TEST_TOUCH_COUNT_NUM;
  198. ESP_LOGI(TAG, " >> %s << \n", __func__);
  199. TEST_ESP_OK( touch_pad_init() );
  200. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  201. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  202. }
  203. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW) );
  204. TEST_ESP_OK( touch_pad_fsm_start() );
  205. while (test_cnt--) {
  206. test_touch_release_all();
  207. /* Read the touch sensor raw data in SW mode. */
  208. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  209. TEST_ESP_OK( touch_pad_sw_start() );
  210. while (!touch_pad_meas_is_done()) ;
  211. TEST_ESP_OK( touch_pad_read_raw_data(touch_list[i], &touch_value[i]) );
  212. printf("T%d:[%4d] ", touch_list[i], touch_value[i]);
  213. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_value[i]);
  214. }
  215. printf("\n");
  216. /* Check the stable of reading. */
  217. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  218. if (touch_temp[i]) {
  219. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp[i], touch_value[i]);
  220. }
  221. touch_temp[i] = touch_value[i];
  222. }
  223. test_touch_push_all();
  224. /* Read the touch sensor raw data in SW mode. */
  225. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  226. TEST_ESP_OK( touch_pad_sw_start() );
  227. while (!touch_pad_meas_is_done()) ;
  228. TEST_ESP_OK( touch_pad_read_raw_data(touch_list[i], &touch_push[i]) );
  229. printf("T%d:[%4d] ", touch_list[i], touch_push[i]);
  230. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_push[i]);
  231. }
  232. printf("\n");
  233. /* Check the change of reading. */
  234. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  235. TEST_ASSERT_GREATER_THAN(touch_value[i], touch_push[i]);
  236. }
  237. }
  238. TEST_ESP_OK( touch_pad_deinit() );
  239. return ESP_OK;
  240. }
  241. /*
  242. * Test the stable and change of touch sensor reading in timer mode.
  243. * TEST POINT:
  244. * 1. Timer mode for FSM.
  245. * 2. Touch channel slope setting.
  246. * 3. Touch reading stable.
  247. */
  248. esp_err_t test_touch_timer_read(void)
  249. {
  250. uint32_t touch_value[TEST_TOUCH_CHANNEL] = {0};
  251. uint32_t touch_temp[TEST_TOUCH_CHANNEL] = {0};
  252. uint32_t touch_push[TEST_TOUCH_CHANNEL] = {0};
  253. int test_cnt = TEST_TOUCH_COUNT_NUM;
  254. ESP_LOGI(TAG, " >> %s << \n", __func__);
  255. TEST_ESP_OK( touch_pad_init() );
  256. /* Set different slope for channels to test slope function. */
  257. printf("Set slope for channel: ");
  258. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  259. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  260. TEST_ESP_OK( touch_pad_set_cnt_mode(touch_list[i], i % 7 ? i % 7 : 1, TOUCH_PAD_TIE_OPT_DEFAULT) );
  261. printf("[ch%d-%d] ", touch_list[i], i % 7 ? i % 7 : 1);
  262. }
  263. printf("\n");
  264. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  265. TEST_ESP_OK( touch_pad_fsm_start() );
  266. /* Wait touch sensor stable */
  267. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  268. while (test_cnt--) {
  269. test_touch_release_all();
  270. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  271. // Start task to read values sensed by pads
  272. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  273. TEST_ESP_OK( touch_pad_read_raw_data(touch_list[i], &touch_value[i]) );
  274. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_value[i]);
  275. printf("T%d:[%4d] ", touch_list[i], touch_value[i]);
  276. }
  277. printf("\n");
  278. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  279. if (touch_temp[i]) {
  280. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp[i], touch_value[i]);
  281. }
  282. touch_temp[i] = touch_value[i];
  283. }
  284. test_touch_push_all();
  285. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  286. /* Read the touch sensor raw data in FSM mode. */
  287. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  288. TEST_ESP_OK( touch_pad_read_raw_data(touch_list[i], &touch_push[i]) );
  289. printf("T%d:[%4d] ", touch_list[i], touch_push[i]);
  290. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_push[i]);
  291. }
  292. printf("\n");
  293. /* Check the change of reading. */
  294. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  295. TEST_ASSERT_GREATER_THAN(touch_value[i], touch_push[i]);
  296. }
  297. }
  298. TEST_ESP_OK( touch_pad_deinit() );
  299. return ESP_OK;
  300. }
  301. /*
  302. * Test the filter mode.
  303. * TEST POINT:
  304. * 1. Timer mode for FSM.
  305. * 2. Touch reading stable.
  306. * 3. Touch reading init value.
  307. * 4. Touch reading filtered value equal to raw data.
  308. */
  309. esp_err_t test_touch_filtered_read(void)
  310. {
  311. uint32_t touch_value[TEST_TOUCH_CHANNEL] = {0};
  312. uint32_t touch_temp[TEST_TOUCH_CHANNEL] = {0};
  313. ESP_LOGI(TAG, " >> %s << \n", __func__);
  314. TEST_ESP_OK( touch_pad_init() );
  315. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  316. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  317. }
  318. touch_filter_config_t filter_info = {
  319. .mode = TOUCH_PAD_FILTER_IIR_32, // Test jitter and filter 1/4.
  320. .debounce_cnt = 1, // 1 time count.
  321. .noise_thr = 0, // 50%
  322. .jitter_step = 4, // use for jitter mode.
  323. .smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,
  324. };
  325. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  326. TEST_ESP_OK( touch_pad_filter_enable() );
  327. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  328. TEST_ESP_OK( touch_pad_fsm_start() );
  329. /* Wait touch pad init done. */
  330. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  331. /* Test the stable for init value of touch reading.
  332. * Ideal: benchmark == raw data == smooth data.
  333. */
  334. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  335. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value[i]) );
  336. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_value[i]);
  337. TEST_ESP_OK( touch_pad_read_raw_data(touch_list[i], &touch_temp[i]) );
  338. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_temp[i]);
  339. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp[i], touch_value[i]);
  340. TEST_ESP_OK( touch_pad_filter_read_smooth(touch_list[i], &touch_temp[i]) );
  341. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_temp[i]);
  342. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp[i], touch_value[i]);
  343. }
  344. printf("touch filter init value:\n");
  345. printf_touch_hw_read("raw ");
  346. printf_touch_benchmark_read("base ");
  347. printf_touch_smooth_read("smooth");
  348. printf("\n");
  349. int test_cnt = TEST_TOUCH_COUNT_NUM;
  350. while (test_cnt--) {
  351. /* Touch reading filtered value equal to raw data. */
  352. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  353. TEST_ESP_OK( touch_pad_read_raw_data(touch_list[i], &touch_value[i]) );
  354. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_temp[i]) );
  355. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp[i], touch_value[i]);
  356. TEST_ESP_OK( touch_pad_filter_read_smooth(touch_list[i], &touch_temp[i]) );
  357. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp[i], touch_value[i]);
  358. }
  359. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  360. if (touch_temp[i]) {
  361. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp[i], touch_value[i]);
  362. }
  363. touch_temp[i] = touch_value[i];
  364. }
  365. vTaskDelay(20 / portTICK_PERIOD_MS);
  366. }
  367. TEST_ESP_OK( touch_pad_deinit() );
  368. return ESP_OK;
  369. }
  370. TEST_CASE("Touch Sensor reading test (SW, Timer, filter)", "[touch]")
  371. {
  372. TOUCH_REG_BASE_TEST();
  373. TEST_ESP_OK( test_touch_sw_read() );
  374. TEST_ESP_OK( test_touch_timer_read() );
  375. TEST_ESP_OK( test_touch_filtered_read() );
  376. }
  377. /*
  378. * Test the base patameter mode.
  379. * TEST POINT:
  380. * 1. measure time and sleep time setting.
  381. * 2. Charge / incharge voltage threshold setting.
  382. * 3. Touch slope setting.
  383. * 4. Touch reading filtered value equal to raw data.
  384. */
  385. int test_touch_base_parameter(touch_pad_t pad_num, int meas_time, int slp_time,
  386. int vol_h, int vol_l, int vol_a, int slope, bool is_conn_gnd)
  387. {
  388. uint32_t touch_value = 0;
  389. uint32_t touch_temp = 0, touch_filter;
  390. uint64_t val_sum = 0;
  391. int test_cnt = TEST_TOUCH_COUNT_NUM;
  392. ESP_LOGI(TAG, " >> %s << \n", __func__);
  393. TEST_ESP_OK( touch_pad_init() );
  394. /* Note: init all channel, but test one channel. */
  395. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  396. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  397. }
  398. TEST_ESP_OK( touch_pad_set_cnt_mode(pad_num, slope, TOUCH_PAD_TIE_OPT_DEFAULT) );
  399. TEST_ESP_OK( touch_pad_set_meas_time(slp_time, meas_time) );
  400. TEST_ESP_OK( touch_pad_set_voltage(vol_h, vol_l, vol_a) );
  401. TEST_ESP_OK( touch_pad_set_idle_channel_connect(is_conn_gnd) );
  402. ESP_LOGI(TAG, "meas_time[%d]_slp_time[%d]_vol_h[%d]_vol_l[%d]_vol_a[%d]_slope[%d]_is_conn_gnd[%d]",
  403. meas_time, slp_time, vol_h, vol_l, vol_a, slope, is_conn_gnd);
  404. touch_filter_config_t filter_info = {
  405. .mode = TOUCH_PAD_FILTER_IIR_32, // Test jitter and filter 1/4.
  406. .debounce_cnt = 1, // 1 time count.
  407. .noise_thr = 0, // 50%
  408. .jitter_step = 4, // use for jitter mode.
  409. .smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,
  410. };
  411. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  412. TEST_ESP_OK( touch_pad_filter_enable() );
  413. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  414. TEST_ESP_OK( touch_pad_fsm_start() );
  415. /* Some parameters will delay the init time. so wait longger time */
  416. vTaskDelay(100 / portTICK_PERIOD_MS);
  417. while (test_cnt--) {
  418. /* Correctness of reading. Ideal: benchmark == raw data == smooth data. */
  419. TEST_ESP_OK( touch_pad_read_raw_data(pad_num, &touch_value) );
  420. TEST_ESP_OK( touch_pad_read_benchmark(pad_num, &touch_filter) );
  421. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_filter, touch_value);
  422. TEST_ESP_OK( touch_pad_filter_read_smooth(pad_num, &touch_filter) );
  423. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_filter, touch_value);
  424. /* Stable of reading */
  425. TEST_ESP_OK( touch_pad_read_raw_data(pad_num, &touch_value) );
  426. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_value);
  427. if (touch_temp) {
  428. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp, touch_value);
  429. }
  430. touch_temp = touch_value;
  431. printf("T%d:[%4d] ", pad_num, touch_value);
  432. val_sum += touch_value; // For check.
  433. vTaskDelay(20 / portTICK_PERIOD_MS);
  434. }
  435. printf("\n");
  436. TEST_ESP_OK( touch_pad_deinit() );
  437. return (uint32_t)(val_sum / TEST_TOUCH_COUNT_NUM);
  438. }
  439. TEST_CASE("Touch Sensor base parameters test (meas_time, voltage, slope, inv_conn)", "[touch]")
  440. {
  441. int touch_val[5] = {0};
  442. ESP_LOGI(TAG, "Charge / incharge voltage level test");
  443. touch_val[0] = test_touch_base_parameter(touch_list[2], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  444. TOUCH_HVOLT_2V4, TOUCH_LVOLT_0V8, TOUCH_HVOLT_ATTEN_1V5,
  445. TOUCH_PAD_SLOPE_DEFAULT, true);
  446. touch_val[1] = test_touch_base_parameter(touch_list[2], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  447. TOUCH_HVOLT_2V5, TOUCH_LVOLT_0V6, TOUCH_HVOLT_ATTEN_1V,
  448. TOUCH_PAD_SLOPE_DEFAULT, true);
  449. touch_val[2] = test_touch_base_parameter(touch_list[2], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  450. TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_0V,
  451. TOUCH_PAD_SLOPE_DEFAULT, true);
  452. TEST_ASSERT_GREATER_THAN(touch_val[0], touch_val[1]);
  453. TEST_ASSERT_GREATER_THAN(touch_val[1], touch_val[2]);
  454. ESP_LOGI(TAG, "Measure time / sleep time test");
  455. touch_val[0] = test_touch_base_parameter(touch_list[1], 0xff, 0x1ff,
  456. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, TOUCH_PAD_SLOPE_DEFAULT, true);
  457. touch_val[1] = test_touch_base_parameter(touch_list[1], 0xfff, 0xff,
  458. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, TOUCH_PAD_SLOPE_DEFAULT, true);
  459. touch_val[2] = test_touch_base_parameter(touch_list[1], 0x1fff, 0xf,
  460. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, TOUCH_PAD_SLOPE_DEFAULT, true);
  461. TEST_ASSERT_GREATER_THAN(touch_val[0], touch_val[1]);
  462. TEST_ASSERT_GREATER_THAN(touch_val[1], touch_val[2]);
  463. ESP_LOGI(TAG, "Charge / incharge slope level test");
  464. touch_val[0] = test_touch_base_parameter(touch_list[0], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  465. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, 7, true);
  466. touch_val[1] = test_touch_base_parameter(touch_list[0], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  467. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, 5, true);
  468. touch_val[2] = test_touch_base_parameter(touch_list[0], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  469. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, 3, true);
  470. TEST_ASSERT_GREATER_THAN(touch_val[0], touch_val[1]);
  471. TEST_ASSERT_GREATER_THAN(touch_val[1], touch_val[2]);
  472. /* The GND option causes larger parasitic capacitance and larger reading */
  473. ESP_LOGI(TAG, "Inactive connect test");
  474. touch_val[0] = test_touch_base_parameter(touch_list[3], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  475. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, TOUCH_PAD_SLOPE_DEFAULT,
  476. false);
  477. touch_val[1] = test_touch_base_parameter(touch_list[3], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  478. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, TOUCH_PAD_SLOPE_DEFAULT,
  479. true);
  480. TEST_ASSERT_GREATER_THAN(touch_val[0], touch_val[1]);
  481. }
  482. /*
  483. * Check active interrupt of touch channels.
  484. */
  485. static esp_err_t test_touch_check_ch_touched(uint32_t test_ch_num, uint32_t exceed_time_ms)
  486. {
  487. touch_event_t evt = {0};
  488. esp_err_t ret = ESP_FAIL;
  489. printf("Active: ");
  490. while (1) {
  491. if (pdTRUE == xQueueReceive(que_touch, &evt, exceed_time_ms / portTICK_PERIOD_MS)) {
  492. if (evt.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
  493. printf("0x%x, ", evt.pad_status);
  494. if (test_ch_num == __builtin_popcount(evt.pad_status)) {
  495. ret = ESP_OK;
  496. break;
  497. }
  498. } else if (evt.intr_mask & (TOUCH_PAD_INTR_MASK_DONE | TOUCH_PAD_INTR_MASK_SCAN_DONE)) {
  499. continue;
  500. } else { // If the interrupt type error, test error.
  501. ESP_LOGI(TAG, "Touch[%d] intr error, status %d, evt_msk0x%x", evt.pad_num, evt.pad_status, evt.intr_mask);
  502. break;
  503. }
  504. } else {
  505. ESP_LOGI(TAG, "Touch intr exceed time");
  506. break;
  507. }
  508. }
  509. printf("\n");
  510. return ret;
  511. }
  512. /*
  513. * Check inactive interrupt of touch channels.
  514. */
  515. static esp_err_t test_touch_check_ch_released(uint32_t test_ch_num, uint32_t exceed_time_ms)
  516. {
  517. touch_event_t evt = {0};
  518. esp_err_t ret = ESP_FAIL;
  519. printf("Inactive: ");
  520. while (1) {
  521. if (pdTRUE == xQueueReceive(que_touch, &evt, exceed_time_ms / portTICK_PERIOD_MS)) {
  522. if (evt.intr_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
  523. printf("0x%x, ", evt.pad_status);
  524. if ((TEST_TOUCH_CHANNEL - test_ch_num) == __builtin_popcount(evt.pad_status)) {
  525. ret = ESP_OK;
  526. break;
  527. }
  528. } else if (evt.intr_mask & (TOUCH_PAD_INTR_MASK_DONE | TOUCH_PAD_INTR_MASK_SCAN_DONE)) {
  529. continue;
  530. } else { // If the interrupt type error, test error.
  531. ESP_LOGI(TAG, "Touch[%d] intr error, status %d, evt_msk0x%x", evt.pad_num, evt.pad_status, evt.intr_mask);
  532. break;
  533. }
  534. } else {
  535. ESP_LOGI(TAG, "Touch intr exceed time");
  536. break;
  537. }
  538. }
  539. printf("\n");
  540. return ret;
  541. }
  542. static esp_err_t test_touch_check_ch_touched_with_proximity(uint32_t test_ch_num, uint32_t exceed_time_ms)
  543. {
  544. uint32_t count = 0;
  545. uint16_t ch_mask = 0;
  546. touch_event_t evt = {0};
  547. esp_err_t ret = ESP_FAIL;
  548. TEST_ESP_OK( touch_pad_proximity_get_count(TOUCH_PAD_MAX, &count) );
  549. printf("Active: ");
  550. while (1) {
  551. if (pdTRUE == xQueueReceive(que_touch, &evt, exceed_time_ms / portTICK_PERIOD_MS)) {
  552. if (evt.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
  553. printf("0x%x, ", evt.pad_status);
  554. if (test_ch_num == __builtin_popcount(evt.pad_status)) {
  555. ret = ESP_OK;
  556. break;
  557. }
  558. } else if (evt.intr_mask & (TOUCH_PAD_INTR_MASK_SCAN_DONE)) {
  559. touch_pad_get_channel_mask(&ch_mask);
  560. for (int i = TOUCH_PAD_MAX - 1; i >= 0; i--) {
  561. if (BIT(i) & ch_mask) {
  562. if (evt.pad_num == i) {
  563. if (count == evt.slp_proxi_cnt) {
  564. esp_rom_printf("priximity base(%d) cnt(%d)\n", evt.slp_proxi_base, evt.slp_proxi_cnt);
  565. }
  566. }
  567. }
  568. }
  569. continue;
  570. } else { // If the interrupt type error, test error.
  571. ESP_LOGI(TAG, "Touch[%d] intr error, status %d, evt_msk0x%x", evt.pad_num, evt.pad_status, evt.intr_mask);
  572. continue;;
  573. }
  574. } else {
  575. ESP_LOGI(TAG, "Touch intr exceed time");
  576. break;
  577. }
  578. }
  579. printf("\n");
  580. return (esp_err_t)ret;
  581. }
  582. static esp_err_t test_touch_check_ch_released_with_proximity(uint32_t test_ch_num, uint32_t exceed_time_ms)
  583. {
  584. uint32_t count = 0;
  585. uint16_t ch_mask = 0;
  586. touch_event_t evt = {0};
  587. esp_err_t ret = ESP_FAIL;
  588. TEST_ESP_OK( touch_pad_proximity_get_count(TOUCH_PAD_MAX, &count) );
  589. printf("Inactive: ");
  590. while (1) {
  591. if (pdTRUE == xQueueReceive(que_touch, &evt, exceed_time_ms / portTICK_PERIOD_MS)) {
  592. if (evt.intr_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
  593. printf("0x%x, ", evt.pad_status);
  594. if ((TEST_TOUCH_CHANNEL - test_ch_num) == __builtin_popcount(evt.pad_status)) {
  595. ret = ESP_OK;
  596. break;
  597. }
  598. } else if (evt.intr_mask & (TOUCH_PAD_INTR_MASK_SCAN_DONE)) {
  599. touch_pad_get_channel_mask(&ch_mask);
  600. for (int i = TOUCH_PAD_MAX - 1; i >= 0; i--) {
  601. if (BIT(i) & ch_mask) {
  602. if (evt.pad_num == i) {
  603. if (count == evt.slp_proxi_cnt) {
  604. esp_rom_printf("priximity base(%d) cnt(%d)\n", evt.slp_proxi_base, evt.slp_proxi_cnt);
  605. }
  606. }
  607. }
  608. }
  609. continue;
  610. } else { // If the interrupt type error, test error.
  611. ESP_LOGI(TAG, "Touch[%d] intr error, status %d, evt_msk0x%x", evt.pad_num, evt.pad_status, evt.intr_mask);
  612. continue;;
  613. }
  614. } else {
  615. ESP_LOGI(TAG, "Touch intr exceed time");
  616. break;
  617. }
  618. }
  619. printf("\n");
  620. return (esp_err_t)ret;
  621. }
  622. /*
  623. * Check scan done interrupt of touch channels.
  624. */
  625. static esp_err_t test_touch_check_ch_intr_scan_done(void)
  626. {
  627. touch_event_t evt = {0};
  628. uint16_t ch_mask = 0;
  629. esp_err_t ret = ESP_FAIL;
  630. /* Check the scan done interrupt. */
  631. while (1) {
  632. if (pdTRUE == xQueueReceive(que_touch, &evt, 1000 / portTICK_PERIOD_MS)) {
  633. /* Scan done interrupt have bug that be trigger by last two channel. */
  634. if (evt.intr_mask & TOUCH_PAD_INTR_MASK_SCAN_DONE) {
  635. touch_pad_get_channel_mask(&ch_mask);
  636. for (int i = TOUCH_PAD_MAX - 1; i >= 0; i--) {
  637. if (BIT(i) & ch_mask) {
  638. if (evt.pad_num == i) {
  639. ESP_LOGI(TAG, "touch _SCAN_DONE INTR be triggered");
  640. ret = ESP_OK;
  641. }
  642. goto NEXT_TEST;
  643. }
  644. }
  645. } else if (evt.intr_mask & (TOUCH_PAD_INTR_MASK_DONE | TOUCH_PAD_INTR_MASK_SCAN_DONE)) {
  646. continue;
  647. } else { // If the interrupt type error, test error.
  648. ESP_LOGI(TAG, "Touch[%d] intr error, status %d, evt_msk0x%x", evt.pad_num, evt.pad_status, evt.intr_mask);
  649. break;
  650. }
  651. } else {
  652. ESP_LOGI(TAG, "Touch intr exceed time");
  653. break;
  654. }
  655. }
  656. NEXT_TEST:
  657. printf("\n");
  658. return (esp_err_t)ret;
  659. }
  660. /*
  661. * Check timeout interrupt of touch channels.
  662. */
  663. static esp_err_t test_touch_check_ch_intr_timeout(touch_pad_t pad_num)
  664. {
  665. esp_err_t ret = ESP_FAIL;
  666. touch_event_t evt = {0};
  667. while (1) {
  668. if (pdTRUE == xQueueReceive(que_touch, &evt, 1000 / portTICK_PERIOD_MS)) {
  669. /* Scan done interrupt have bug that be trigger by last two channel. */
  670. if (evt.intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
  671. if (pad_num == evt.pad_num) {
  672. ESP_LOGI(TAG, "touch TIMEOUT be triggered");
  673. s_touch_timeout_mask = 0;
  674. ret = ESP_OK;
  675. touch_pad_timeout_resume();
  676. break;
  677. } else {
  678. esp_rom_printf("-timeout %x T[%d] status %d, evt_msk %x -\n",
  679. s_touch_timeout_mask, evt.pad_num, evt.pad_status, evt.intr_mask);
  680. touch_pad_timeout_resume();
  681. }
  682. } else {
  683. continue;
  684. }
  685. } else {
  686. ESP_LOGI(TAG, "Touch intr exceed time");
  687. break;
  688. }
  689. }
  690. printf("\n");
  691. return (esp_err_t)ret;
  692. }
  693. static void test_touch_intr_cb(void *arg)
  694. {
  695. uint32_t cnt, touch_value;
  696. int task_awoken = pdFALSE;
  697. touch_event_t evt;
  698. evt.intr_mask = touch_pad_read_intr_status_mask();
  699. evt.pad_status = touch_pad_get_status();
  700. evt.pad_num = touch_pad_get_current_meas_channel();
  701. if (!evt.intr_mask) {
  702. esp_rom_printf(".");
  703. return;
  704. }
  705. if (evt.intr_mask & TOUCH_PAD_INTR_MASK_SCAN_DONE) {
  706. touch_pad_read_benchmark(evt.pad_num, &evt.pad_val);
  707. touch_pad_sleep_channel_t slp_config;
  708. touch_pad_sleep_channel_get_info(&slp_config);
  709. touch_pad_sleep_channel_read_benchmark(slp_config.touch_num, &touch_value);
  710. touch_pad_sleep_channel_read_proximity_cnt(slp_config.touch_num, &cnt);
  711. evt.slp_proxi_cnt = cnt;
  712. evt.slp_proxi_base = touch_value;
  713. }
  714. if (evt.intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
  715. s_touch_timeout_mask |= (BIT(evt.pad_num));
  716. esp_rom_printf("-%dtout-", SENS.sar_touch_status0.touch_scan_curr);
  717. }
  718. xQueueSendFromISR(que_touch, &evt, &task_awoken);
  719. if (task_awoken == pdTRUE) {
  720. portYIELD_FROM_ISR();
  721. }
  722. }
  723. /*
  724. * Test the touch active/inactive interrupt.
  725. * TEST POINT:
  726. * 1. Touch interrupt.
  727. * 2. Raw data noise.
  728. * 3. smooth data and benchmark data.
  729. */
  730. esp_err_t test_touch_interrupt(void)
  731. {
  732. uint32_t touch_value, smooth;
  733. int test_cnt = TEST_TOUCH_COUNT_NUM;
  734. ESP_LOGI(TAG, " >> %s << \n", __func__);
  735. if (que_touch == NULL) {
  736. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  737. /* Should register once. */
  738. touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL);
  739. } else {
  740. xQueueReset(que_touch);
  741. }
  742. TEST_ESP_OK( touch_pad_init() );
  743. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  744. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  745. }
  746. touch_filter_config_t filter_info = {
  747. .mode = TOUCH_PAD_FILTER_IIR_16, // Test jitter and filter 1/4.
  748. .debounce_cnt = 1, // 1 time count.
  749. .noise_thr = 0, // 50%
  750. .jitter_step = 4, // use for jitter mode.
  751. .smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,
  752. };
  753. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  754. TEST_ESP_OK( touch_pad_filter_enable() );
  755. /* Register touch interrupt ISR, enable intr type. */
  756. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  757. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  758. TEST_ESP_OK( touch_pad_fsm_start() );
  759. // Initialize and start a software filter to detect slight change of capacitance.
  760. vTaskDelay(50 / portTICK_PERIOD_MS);
  761. /* Set threshold of touch sensor */
  762. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  763. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  764. TEST_ESP_OK( touch_pad_filter_read_smooth(touch_list[i], &smooth) );
  765. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  766. ESP_LOGI(TAG, "test init: touch pad [%d] base %d, smooth %d, thresh %d",
  767. touch_list[i], touch_value, smooth, (uint32_t)(touch_value * TOUCH_INTR_THRESHOLD));
  768. }
  769. while (test_cnt--) {
  770. test_touch_push_all();
  771. TEST_ESP_OK( test_touch_check_ch_touched(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  772. printf_touch_hw_read("push");
  773. test_touch_benchmark();
  774. test_touch_release_all();
  775. TEST_ESP_OK( test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  776. printf_touch_hw_read("release");
  777. }
  778. TEST_ESP_OK( touch_pad_deinit() );
  779. return ESP_OK;
  780. }
  781. /*
  782. * Test the touch active/inactive, scan_done interrupt.
  783. * TEST POINT:
  784. * 1. Touch interrupt.
  785. * 2. Raw data noise.
  786. * 3. smooth data and benchmark data.
  787. */
  788. esp_err_t test_touch_scan_done_interrupt(void)
  789. {
  790. ESP_LOGI(TAG, " >> %s << \n", __func__);
  791. uint32_t touch_value, smooth;
  792. int test_cnt = TEST_TOUCH_COUNT_NUM;
  793. if (que_touch == NULL) {
  794. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  795. /* Should register once. */
  796. TEST_ESP_OK( touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL) );
  797. } else {
  798. xQueueReset(que_touch);
  799. }
  800. TEST_ESP_OK( touch_pad_init() );
  801. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  802. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  803. }
  804. touch_filter_config_t filter_info = {
  805. .mode = TOUCH_PAD_FILTER_IIR_16, // Test jitter and filter 1/4.
  806. .debounce_cnt = 1, // 1 time count.
  807. .noise_thr = 0, // 50%
  808. .jitter_step = 4, // use for jitter mode.
  809. .smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,
  810. };
  811. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  812. TEST_ESP_OK( touch_pad_filter_enable() );
  813. /* Register touch interrupt ISR, enable intr type. */
  814. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_SCAN_DONE | TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  815. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  816. TEST_ESP_OK( touch_pad_fsm_start() );
  817. /* Check the scan done interrupt */
  818. TEST_ESP_OK( test_touch_check_ch_intr_scan_done() );
  819. vTaskDelay(50 / portTICK_PERIOD_MS);
  820. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  821. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  822. TEST_ESP_OK( touch_pad_filter_read_smooth(touch_list[i], &smooth) );
  823. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  824. ESP_LOGI(TAG, "test init: touch pad [%d] base %d, smooth %d, thresh %d", \
  825. touch_list[i], touch_value, smooth, (uint32_t)(touch_value * TOUCH_INTR_THRESHOLD));
  826. }
  827. while (test_cnt--) {
  828. test_touch_push_all();
  829. TEST_ESP_OK( test_touch_check_ch_touched(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  830. printf_touch_hw_read("push");
  831. test_touch_benchmark();
  832. test_touch_release_all();
  833. TEST_ESP_OK( test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  834. printf_touch_hw_read("release");
  835. }
  836. TEST_ESP_OK( touch_pad_deinit() );
  837. return ESP_OK;
  838. }
  839. /*
  840. * Test the touch active/inactive, timeout interrupt.
  841. * TEST POINT:
  842. * 1. Touch interrupt.
  843. * 2. Raw data noise.
  844. * 3. smooth data and benchmark data.
  845. */
  846. esp_err_t test_touch_timeout_interrupt(void)
  847. {
  848. ESP_LOGI(TAG, " >> %s << \n", __func__);
  849. uint32_t touch_value, smooth;
  850. if (que_touch == NULL) {
  851. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  852. /* Should register once. */
  853. TEST_ESP_OK( touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL) );
  854. } else {
  855. xQueueReset(que_touch);
  856. }
  857. TEST_ESP_OK( touch_pad_init() );
  858. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  859. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  860. }
  861. touch_filter_config_t filter_info = {
  862. .mode = TOUCH_PAD_FILTER_IIR_16, // Test jitter and filter 1/4.
  863. .debounce_cnt = 1, // 1 time count.
  864. .noise_thr = 0, // 50%
  865. .jitter_step = 4, // use for jitter mode.
  866. .smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,
  867. };
  868. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  869. TEST_ESP_OK( touch_pad_filter_enable() );
  870. /* Register touch interrupt ISR, enable intr type. */
  871. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_TIMEOUT | TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  872. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  873. TEST_ESP_OK( touch_pad_fsm_start() );
  874. // Initialize and start a software filter to detect slight change of capacitance.
  875. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  876. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  877. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  878. TEST_ESP_OK( touch_pad_filter_read_smooth(touch_list[i], &smooth) );
  879. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  880. ESP_LOGI(TAG, "test init: touch pad [%d] base %d, smooth %d, thresh %d",
  881. touch_list[i], touch_value, smooth, (uint32_t)(touch_value * TOUCH_INTR_THRESHOLD));
  882. }
  883. /* Set timeout parameter */
  884. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[0], &touch_value) );
  885. TEST_ESP_OK( touch_pad_timeout_set(true , touch_value * 10) );
  886. // Only fake push one touch pad.
  887. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  888. test_timeout_trigger_fake(touch_list[0]);
  889. TEST_ESP_OK( test_touch_check_ch_intr_timeout(touch_list[0]) );
  890. test_timeout_normal(touch_list[0]);
  891. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  892. printf_touch_hw_read("raw ");
  893. printf_touch_benchmark_read("base ");
  894. printf_touch_smooth_read("smooth");
  895. int test_cnt = TEST_TOUCH_COUNT_NUM;
  896. while (test_cnt--) {
  897. test_touch_push_all();
  898. TEST_ESP_OK( test_touch_check_ch_touched(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  899. printf_touch_hw_read("push");
  900. test_touch_benchmark();
  901. test_touch_release_all();
  902. TEST_ESP_OK( test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  903. printf_touch_hw_read("release");
  904. }
  905. TEST_ESP_OK( touch_pad_deinit() );
  906. return ESP_OK;
  907. }
  908. TEST_CASE("Touch Sensor interrupt test (active, inactive, scan_done, timeout)", "[touch]")
  909. {
  910. TEST_ESP_OK( test_touch_interrupt() );
  911. TEST_ESP_OK( test_touch_scan_done_interrupt() );
  912. TEST_ESP_OK( test_touch_timeout_interrupt() );
  913. }
  914. static void test_touch_measure_step(uint32_t step)
  915. {
  916. /* Fake the process of debounce. */
  917. // printf("measure cnt %d: [ ", step);
  918. for (int i = 0; i < step; i++) {
  919. for (int j = 0; j < TEST_TOUCH_CHANNEL; j++) {
  920. TEST_ESP_OK( touch_pad_sw_start() );
  921. while (!touch_pad_meas_is_done()) ;
  922. }
  923. // printf(".");
  924. }
  925. // printf(" ]\n");
  926. }
  927. /*
  928. * Test the touch active/inactive, scan_done interrupt.
  929. * TEST POINT:
  930. * 1. Touch interrupt.
  931. * 2. Raw data noise.
  932. * 3. smooth data and benchmark data.
  933. */
  934. esp_err_t test_touch_filter_parameter_debounce(int deb_cnt)
  935. {
  936. uint32_t touch_value;
  937. int test_cnt = 2;
  938. ESP_LOGI(TAG, " >> %s << \n", __func__);
  939. if (que_touch == NULL) {
  940. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  941. /* Should register once. */
  942. TEST_ESP_OK( touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL) );
  943. } else {
  944. xQueueReset(que_touch);
  945. }
  946. TEST_ESP_OK( touch_pad_init() );
  947. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  948. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  949. }
  950. touch_filter_config_t filter_info = {
  951. .mode = TOUCH_PAD_FILTER_IIR_128, // Test jitter and filter 1/4.
  952. .debounce_cnt = ((deb_cnt < 0) ? 1 : deb_cnt) , // 1 time count.
  953. .noise_thr = 0, // 50%
  954. .jitter_step = 4, // use for jitter mode.
  955. .smh_lvl = TOUCH_PAD_SMOOTH_OFF,
  956. };
  957. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  958. TEST_ESP_OK( touch_pad_filter_enable() );
  959. /* Register touch interrupt ISR, enable intr type. */
  960. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  961. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW) );
  962. TEST_ESP_OK( touch_pad_fsm_start() );
  963. /* Run to wait the data become stable. */
  964. test_touch_measure_step(20); // 2 scan loop
  965. /* Set the threshold. */
  966. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  967. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  968. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  969. ESP_LOGI(TAG, "test init: touch pad [%d] base %d, thresh %d", \
  970. touch_list[i], touch_value, (uint32_t)(touch_value * TOUCH_INTR_THRESHOLD));
  971. }
  972. while (test_cnt--) {
  973. test_touch_push_all();
  974. /* Fake the process of push debounce. */
  975. test_touch_measure_step(deb_cnt); // measure n times. touch state not changed.
  976. TEST_ESP_ERR( ESP_FAIL, test_touch_check_ch_touched(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  977. test_touch_measure_step(1); // measure n+1 times. touch state changed.
  978. TEST_ESP_OK( test_touch_check_ch_touched(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  979. printf_touch_hw_read("push");
  980. test_touch_release_all();
  981. /* Fake the process of release debounce. */
  982. test_touch_measure_step(deb_cnt); // measure n times. touch state not changed.
  983. TEST_ESP_ERR( ESP_FAIL, test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  984. test_touch_measure_step(1); // measure n+1 times. touch state changed.
  985. TEST_ESP_OK( test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  986. printf_touch_hw_read("release");
  987. }
  988. TEST_ESP_OK( touch_pad_deinit() );
  989. return ESP_OK;
  990. }
  991. esp_err_t test_touch_filter_parameter_reset(int reset_cnt)
  992. {
  993. uint32_t touch_value, base_value;
  994. ESP_LOGI(TAG, " >> %s << \n", __func__);
  995. if (que_touch == NULL) {
  996. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  997. /* Should register once. */
  998. TEST_ESP_OK( touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL) );
  999. } else {
  1000. xQueueReset(que_touch);
  1001. }
  1002. TEST_ESP_OK( touch_pad_init() );
  1003. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1004. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  1005. }
  1006. reset_cnt = ((reset_cnt < 0) ? 10 : reset_cnt);
  1007. touch_filter_config_t filter_info = {
  1008. .mode = TOUCH_PAD_FILTER_IIR_16, // Test jitter and filter 1/4.
  1009. .debounce_cnt = 1, // 1 time count.
  1010. .noise_thr = 0, // 50%
  1011. .jitter_step = 4, // use for jitter mode.
  1012. .smh_lvl = TOUCH_PAD_SMOOTH_OFF,
  1013. };
  1014. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  1015. TEST_ESP_OK( touch_pad_filter_enable() );
  1016. /* Register touch interrupt ISR, enable intr type. */
  1017. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  1018. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW) );
  1019. TEST_ESP_OK( touch_pad_fsm_start() );
  1020. /* Run to wait the data become stable. */
  1021. test_touch_measure_step(20); // 2 scan loop
  1022. /* Set the threshold. */
  1023. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1024. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1025. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  1026. ESP_LOGI(TAG, "test init: touch pad [%d] base %d, thresh %d", \
  1027. touch_list[i], touch_value, (uint32_t)(touch_value * TOUCH_INTR_THRESHOLD));
  1028. }
  1029. /* 1. Fake init status is touched. */
  1030. test_touch_push_all();
  1031. TEST_ESP_OK( touch_pad_reset_benchmark(TOUCH_PAD_MAX) );
  1032. /* Run to wait the data become stable. */
  1033. test_touch_measure_step(20); // 2 scan loop
  1034. printf_touch_hw_read("[raw ] reset:");
  1035. printf_touch_benchmark_read("[base] reset:");
  1036. /* 2. Fake the touch status is released. */
  1037. test_touch_release_all();
  1038. /* 3. Fake measure `reset_cnt + 1` times to reset the benchmark. */
  1039. test_touch_measure_step(reset_cnt);
  1040. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1041. TEST_ESP_OK( touch_pad_read_raw_data(touch_list[i], &touch_value) );
  1042. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &base_value) );
  1043. if ((base_value - touch_value) < (base_value * TOUCH_INTR_THRESHOLD)) {
  1044. ESP_LOGE(TAG, "reset cnt err");
  1045. TEST_FAIL();
  1046. }
  1047. }
  1048. printf_touch_hw_read("[raw ] cnt:");
  1049. printf_touch_benchmark_read("[base] cnt:");
  1050. test_touch_measure_step(1);
  1051. /* ESP32S2 reset benchmark to raw data */
  1052. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1053. TEST_ESP_OK( touch_pad_read_raw_data(touch_list[i], &touch_value) );
  1054. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &base_value) );
  1055. TEST_ASSERT_EQUAL_UINT32(base_value, touch_value);
  1056. }
  1057. printf_touch_hw_read("[raw ] cnt+1:");
  1058. printf_touch_benchmark_read("[base] cnt+1:");
  1059. int test_cnt = 2;
  1060. while (test_cnt--) {
  1061. test_touch_push_all();
  1062. /* Fake the process of push debounce. */
  1063. test_touch_measure_step(filter_info.debounce_cnt + 1);
  1064. TEST_ESP_OK( test_touch_check_ch_touched(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1065. printf_touch_hw_read("push");
  1066. test_touch_release_all();
  1067. /* Fake the process of release debounce. */
  1068. test_touch_measure_step(filter_info.debounce_cnt + 1);
  1069. TEST_ESP_OK( test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1070. printf_touch_hw_read("release");
  1071. }
  1072. TEST_ESP_OK( touch_pad_deinit() );
  1073. return ESP_OK;
  1074. }
  1075. esp_err_t test_touch_filter_parameter_jitter(int jitter_step)
  1076. {
  1077. uint32_t touch_value, base_value = 0;
  1078. ESP_LOGI(TAG, " >> %s << \n", __func__);
  1079. if (que_touch == NULL) {
  1080. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  1081. /* Should register once. */
  1082. TEST_ESP_OK( touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL) );
  1083. } else {
  1084. xQueueReset(que_touch);
  1085. }
  1086. TEST_ESP_OK( touch_pad_init() );
  1087. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1088. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  1089. }
  1090. jitter_step = ((jitter_step < 0) ? 4 : jitter_step);
  1091. touch_filter_config_t filter_info = {
  1092. .mode = TOUCH_PAD_FILTER_JITTER, // Test jitter and filter 1/4.
  1093. .debounce_cnt = 1, // 1 time count.
  1094. .noise_thr = 0, // 50%
  1095. .jitter_step = jitter_step, // use for jitter mode.
  1096. };
  1097. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  1098. TEST_ESP_OK( touch_pad_filter_enable() );
  1099. /* Register touch interrupt ISR, enable intr type. */
  1100. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  1101. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW) );
  1102. TEST_ESP_OK( touch_pad_fsm_start() );
  1103. /* Run to wait the data become stable. */
  1104. test_touch_measure_step(20); // 2 scan loop
  1105. /* Check the jitter step. */
  1106. printf_touch_benchmark_read("[smooth] t1:");
  1107. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1108. test_touch_measure_step(1);
  1109. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1110. test_press_fake(touch_list[i]);
  1111. test_touch_measure_step(1);
  1112. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &base_value) );
  1113. TEST_ASSERT_EQUAL_UINT32(jitter_step, (base_value - touch_value));
  1114. }
  1115. printf_touch_benchmark_read("[smooth] t2:");
  1116. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1117. test_touch_measure_step(1);
  1118. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1119. test_release_fake(touch_list[i]);
  1120. test_touch_measure_step(1);
  1121. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &base_value) );
  1122. TEST_ASSERT_EQUAL_UINT32(jitter_step, (touch_value - base_value));
  1123. }
  1124. printf_touch_benchmark_read("[smooth] t3:");
  1125. /* Set the threshold. */
  1126. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1127. //read benchmark value
  1128. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1129. //set interrupt threshold.
  1130. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  1131. ESP_LOGI(TAG, "test init: touch pad [%d] base %d, thresh %d", \
  1132. touch_list[i], touch_value, (uint32_t)(touch_value * TOUCH_INTR_THRESHOLD));
  1133. }
  1134. int test_cnt = 2;
  1135. while (test_cnt--) {
  1136. test_touch_push_all();
  1137. /* Fake the process of push debounce. */
  1138. test_touch_measure_step(filter_info.debounce_cnt + 1);
  1139. TEST_ESP_OK( test_touch_check_ch_touched(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1140. printf_touch_smooth_read("push");
  1141. test_touch_release_all();
  1142. /* Fake the process of release debounce. */
  1143. test_touch_measure_step(filter_info.debounce_cnt + 1);
  1144. TEST_ESP_OK( test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1145. printf_touch_smooth_read("release");
  1146. }
  1147. TEST_ESP_OK( touch_pad_deinit() );
  1148. return ESP_OK;
  1149. }
  1150. TEST_CASE("Touch Sensor filter paramter test (debounce, reset, jitter)", "[touch]")
  1151. {
  1152. ESP_LOGI(TAG, "*********** touch filter debounce test ********************");
  1153. TEST_ESP_OK( test_touch_filter_parameter_debounce(0) );
  1154. TEST_ESP_OK( test_touch_filter_parameter_debounce(3) );
  1155. TEST_ESP_OK( test_touch_filter_parameter_debounce(7) );
  1156. ESP_LOGI(TAG, "*********** touch filter benchmark reset ********************");
  1157. TEST_ESP_OK( test_touch_filter_parameter_reset(0xF) );
  1158. ESP_LOGI(TAG, "*********** touch filter jitter test ********************");
  1159. TEST_ESP_OK( test_touch_filter_parameter_jitter(1) );
  1160. TEST_ESP_OK( test_touch_filter_parameter_jitter(5) );
  1161. TEST_ESP_OK( test_touch_filter_parameter_jitter(15) );
  1162. }
  1163. esp_err_t test_touch_denoise(uint32_t out_val[], uint32_t *denoise_val, touch_pad_denoise_grade_t grade, touch_pad_denoise_cap_t cap)
  1164. {
  1165. uint32_t touch_value;
  1166. ESP_LOGI(TAG, " >> %s << \n", __func__);
  1167. ESP_LOGI(TAG, "Denoise level (%d), cap level (%d) \n", grade, cap);
  1168. if (que_touch == NULL) {
  1169. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  1170. /* Should register once. */
  1171. TEST_ESP_OK( touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL) );
  1172. } else {
  1173. xQueueReset(que_touch);
  1174. }
  1175. TEST_ESP_OK( touch_pad_init() );
  1176. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1177. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  1178. }
  1179. /* Denoise setting at TouchSensor 0. */
  1180. touch_pad_denoise_t denoise = {
  1181. /* The bits to be cancelled are determined according to the noise level. */
  1182. .grade = (grade < 0) ? TOUCH_PAD_DENOISE_BIT4 : grade,
  1183. .cap_level = (cap < 0) ? TOUCH_PAD_DENOISE_CAP_L4 : cap,
  1184. };
  1185. TEST_ESP_OK( touch_pad_denoise_set_config(&denoise) );
  1186. TEST_ESP_OK( touch_pad_denoise_enable() );
  1187. ESP_LOGI(TAG, "Denoise function init");
  1188. touch_filter_config_t filter_info = {
  1189. .mode = TOUCH_PAD_FILTER_IIR_16, // Test jitter and filter 1/4.
  1190. .debounce_cnt = 1, // 1 time count.
  1191. .noise_thr = 0, // 50%
  1192. .jitter_step = 4, // use for jitter mode.
  1193. };
  1194. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  1195. TEST_ESP_OK( touch_pad_filter_enable() );
  1196. /* Register touch interrupt ISR, enable intr type. */
  1197. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  1198. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW) );
  1199. TEST_ESP_OK( touch_pad_fsm_start() );
  1200. /* Run to wait the data become stable. */
  1201. test_touch_measure_step(20); // 2 scan loop
  1202. /* Set the threshold. */
  1203. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1204. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1205. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  1206. if (out_val) {
  1207. /* Output value for check. */
  1208. out_val[i] = touch_value;
  1209. }
  1210. }
  1211. printf_touch_benchmark_read("Denoise");
  1212. if (denoise_val) {
  1213. touch_pad_denoise_read_data(denoise_val);
  1214. }
  1215. int test_cnt = 1;
  1216. while (test_cnt--) {
  1217. test_touch_push_all();
  1218. /* Fake the process of push debounce. */
  1219. test_touch_measure_step(filter_info.debounce_cnt + 1);
  1220. TEST_ESP_OK( test_touch_check_ch_touched(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1221. test_touch_release_all();
  1222. /* Fake the process of release debounce. */
  1223. test_touch_measure_step(filter_info.debounce_cnt + 1);
  1224. TEST_ESP_OK( test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1225. }
  1226. TEST_ESP_OK( touch_pad_deinit() );
  1227. return ESP_OK;
  1228. }
  1229. TEST_CASE("Touch Sensor denoise test (cap, level)", "[touch]")
  1230. {
  1231. uint32_t val_1[TEST_TOUCH_CHANNEL];
  1232. uint32_t val_2[TEST_TOUCH_CHANNEL];
  1233. uint32_t val_3[TEST_TOUCH_CHANNEL];
  1234. uint32_t denoise_val[TOUCH_PAD_DENOISE_CAP_MAX];
  1235. ESP_LOGI(TAG, "*********** touch filter denoise level test ********************");
  1236. TEST_ESP_OK( test_touch_denoise(val_1, &denoise_val[0], TOUCH_PAD_DENOISE_BIT4, TOUCH_PAD_DENOISE_CAP_L0) );
  1237. TEST_ESP_OK( test_touch_denoise(val_2, NULL, TOUCH_PAD_DENOISE_BIT8, TOUCH_PAD_DENOISE_CAP_L0) );
  1238. TEST_ESP_OK( test_touch_denoise(val_3, NULL, TOUCH_PAD_DENOISE_BIT12, TOUCH_PAD_DENOISE_CAP_L0) );
  1239. /*`TOUCH_PAD_DENOISE_BIT4` has a small denoise value, which may be smaller than the noise amplitude of the touch reading, so no verification for it.*/
  1240. if ((((denoise_val[0] >> 4) & 0xF) != 0) && (((denoise_val[0] >> 8) & 0xF) != 0)) {
  1241. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1242. TEST_ASSERT_GREATER_THAN(val_3[i], val_2[i]);
  1243. }
  1244. } else {
  1245. /* If the value of denoise is approximately 0,
  1246. The difference between touch reading is very small. Should skip value test. */
  1247. ESP_LOGI(TAG, "denoise value is %d", denoise_val[0]);
  1248. }
  1249. ESP_LOGI(TAG, "*********** touch filter denoise cap level test ********************");
  1250. TEST_ESP_OK( test_touch_denoise(NULL, &denoise_val[0], TOUCH_PAD_DENOISE_BIT8, TOUCH_PAD_DENOISE_CAP_L0) );
  1251. TEST_ESP_OK( test_touch_denoise(NULL, &denoise_val[1], TOUCH_PAD_DENOISE_BIT8, TOUCH_PAD_DENOISE_CAP_L1) );
  1252. TEST_ESP_OK( test_touch_denoise(NULL, &denoise_val[2], TOUCH_PAD_DENOISE_BIT8, TOUCH_PAD_DENOISE_CAP_L2) );
  1253. TEST_ESP_OK( test_touch_denoise(NULL, &denoise_val[3], TOUCH_PAD_DENOISE_BIT8, TOUCH_PAD_DENOISE_CAP_L3) );
  1254. TEST_ESP_OK( test_touch_denoise(NULL, &denoise_val[4], TOUCH_PAD_DENOISE_BIT8, TOUCH_PAD_DENOISE_CAP_L4) );
  1255. TEST_ESP_OK( test_touch_denoise(NULL, &denoise_val[5], TOUCH_PAD_DENOISE_BIT8, TOUCH_PAD_DENOISE_CAP_L5) );
  1256. TEST_ESP_OK( test_touch_denoise(NULL, &denoise_val[6], TOUCH_PAD_DENOISE_BIT8, TOUCH_PAD_DENOISE_CAP_L6) );
  1257. TEST_ESP_OK( test_touch_denoise(NULL, &denoise_val[7], TOUCH_PAD_DENOISE_BIT8, TOUCH_PAD_DENOISE_CAP_L7) );
  1258. printf("denoise read: ");
  1259. for (int i = 0; i < TOUCH_PAD_DENOISE_CAP_MAX - 1; i++) {
  1260. TEST_ASSERT_GREATER_OR_EQUAL(denoise_val[i], denoise_val[i + 1]);
  1261. printf("%d ", denoise_val[i]);
  1262. }
  1263. printf("\n");
  1264. }
  1265. esp_err_t test_touch_waterproof(void)
  1266. {
  1267. uint32_t touch_value;
  1268. int test_cnt = TEST_TOUCH_COUNT_NUM;
  1269. ESP_LOGI(TAG, " >> %s << \n", __func__);
  1270. if (que_touch == NULL) {
  1271. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  1272. /* Should register once. */
  1273. touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL);
  1274. } else {
  1275. xQueueReset(que_touch);
  1276. }
  1277. TEST_ESP_OK( touch_pad_init() );
  1278. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1279. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  1280. }
  1281. /* Denoise setting at TouchSensor 0. */
  1282. touch_pad_denoise_t denoise = {
  1283. /* The bits to be cancelled are determined according to the noise level. */
  1284. .grade = TOUCH_PAD_DENOISE_BIT4,
  1285. .cap_level = TOUCH_PAD_DENOISE_CAP_L4,
  1286. };
  1287. TEST_ESP_OK( touch_pad_denoise_set_config(&denoise) );
  1288. TEST_ESP_OK( touch_pad_denoise_enable() );
  1289. ESP_LOGI(TAG, "Denoise function init");
  1290. touch_filter_config_t filter_info = {
  1291. .mode = TOUCH_PAD_FILTER_IIR_16, // Test jitter and filter 1/4.
  1292. .debounce_cnt = 1, // 1 time count.
  1293. .noise_thr = 0, // 50%
  1294. .jitter_step = 4, // use for jitter mode.
  1295. };
  1296. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  1297. TEST_ESP_OK( touch_pad_filter_enable() );
  1298. /* Register touch interrupt ISR, enable intr type. */
  1299. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  1300. /* Waterproof function */
  1301. touch_pad_waterproof_t waterproof = {
  1302. .guard_ring_pad = TOUCH_WATERPROOF_RING_PAD, // If no ring pad, set 0;
  1303. /* It depends on the number of the parasitic capacitance of the shield pad. */
  1304. .shield_driver = TOUCH_PAD_SHIELD_DRV_L0, //40pf
  1305. };
  1306. TEST_ESP_OK( touch_pad_waterproof_set_config(&waterproof) );
  1307. TEST_ESP_OK( touch_pad_waterproof_enable() );
  1308. ESP_LOGI(TAG, "touch pad waterproof init");
  1309. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  1310. TEST_ESP_OK( touch_pad_fsm_start() );
  1311. vTaskDelay(50 / portTICK_PERIOD_MS);
  1312. /* Set the threshold. */
  1313. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1314. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1315. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  1316. }
  1317. while (test_cnt--) {
  1318. test_touch_push_all();
  1319. vTaskDelay(20 / portTICK_PERIOD_MS);
  1320. TEST_ESP_OK( test_touch_check_ch_touched(TEST_TOUCH_CHANNEL - 1, TOUCH_EXCEED_TIME_MS) ); // take off shield pad
  1321. printf_touch_hw_read("push");
  1322. test_touch_release_all();
  1323. vTaskDelay(20 / portTICK_PERIOD_MS);
  1324. TEST_ESP_OK( test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1325. printf_touch_hw_read("release");
  1326. }
  1327. TEST_ESP_OK( touch_pad_deinit() );
  1328. return ESP_OK;
  1329. }
  1330. TEST_CASE("Touch Sensor waterproof guard test", "[touch]")
  1331. {
  1332. ESP_LOGI(TAG, "*********** touch filter waterproof guard test ********************");
  1333. TEST_ESP_OK( test_touch_waterproof() );
  1334. }
  1335. esp_err_t test_touch_proximity(int meas_num)
  1336. {
  1337. ESP_LOGI(TAG, " >> %s << \n", __func__);
  1338. uint32_t touch_value;
  1339. if (que_touch == NULL) {
  1340. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  1341. /* Should register once. */
  1342. TEST_ESP_OK( touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL) );
  1343. } else {
  1344. xQueueReset(que_touch);
  1345. }
  1346. TEST_ESP_OK( touch_pad_init() );
  1347. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1348. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  1349. }
  1350. /* Denoise setting at TouchSensor 0. */
  1351. touch_pad_denoise_t denoise = {
  1352. /* The bits to be cancelled are determined according to the noise level. */
  1353. .grade = TOUCH_PAD_DENOISE_BIT4,
  1354. .cap_level = TOUCH_PAD_DENOISE_CAP_L4,
  1355. };
  1356. TEST_ESP_OK( touch_pad_denoise_set_config(&denoise) );
  1357. TEST_ESP_OK( touch_pad_denoise_enable() );
  1358. ESP_LOGI(TAG, "Denoise function init");
  1359. touch_filter_config_t filter_info = {
  1360. .mode = TOUCH_PAD_FILTER_IIR_16, // Test jitter and filter 1/4.
  1361. .debounce_cnt = 1, // 1 time count.
  1362. .noise_thr = 0, // 50%
  1363. .jitter_step = 4, // use for jitter mode.
  1364. };
  1365. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  1366. TEST_ESP_OK( touch_pad_filter_enable() );
  1367. /* Register touch interrupt ISR, enable intr type. */
  1368. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  1369. /* Waterproof function */
  1370. touch_pad_waterproof_t waterproof = {
  1371. .guard_ring_pad = TOUCH_WATERPROOF_RING_PAD,// If no ring pad, set 0;
  1372. /* It depends on the number of the parasitic capacitance of the shield pad. */
  1373. .shield_driver = TOUCH_PAD_SHIELD_DRV_L0, //40pf
  1374. };
  1375. TEST_ESP_OK( touch_pad_waterproof_set_config(&waterproof) );
  1376. TEST_ESP_OK( touch_pad_waterproof_enable() );
  1377. ESP_LOGI(TAG, "touch pad waterproof init");
  1378. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  1379. TEST_ESP_OK( touch_pad_fsm_start() );
  1380. vTaskDelay(50 / portTICK_PERIOD_MS);
  1381. /* Set the threshold. */
  1382. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1383. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1384. if (touch_list[i] == proximity_pad[0] ||
  1385. touch_list[i] == proximity_pad[1] ||
  1386. touch_list[i] == proximity_pad[2]) {
  1387. /* The threshold of proximity pad is the sum of touch reading `meas_num` times */
  1388. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i],
  1389. meas_num * touch_value * (1 + TOUCH_INTR_THRESHOLD)) );
  1390. ESP_LOGI(TAG, "proximity pad [%d] base %d, thresh %d", touch_list[i], touch_value,
  1391. (uint32_t)(meas_num * touch_value * (1 + TOUCH_INTR_THRESHOLD)));
  1392. } else {
  1393. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  1394. ESP_LOGI(TAG, "touch pad [%d] base %d, thresh %d", \
  1395. touch_list[i], touch_value, (uint32_t)(touch_value * TOUCH_INTR_THRESHOLD));
  1396. }
  1397. }
  1398. /* Should stop the measure, then change the config. */
  1399. while (!touch_pad_meas_is_done());
  1400. TEST_ESP_OK( touch_pad_fsm_stop() );
  1401. /* Proximity function */
  1402. TEST_ESP_OK( touch_pad_proximity_enable(proximity_pad[0], true) );
  1403. TEST_ESP_OK( touch_pad_proximity_enable(proximity_pad[1], true) );
  1404. TEST_ESP_OK( touch_pad_proximity_enable(proximity_pad[2], true) );
  1405. TEST_ESP_OK( touch_pad_proximity_set_count(TOUCH_PAD_MAX, meas_num < 0 ? 16 : meas_num) );
  1406. ESP_LOGI(TAG, "touch pad proximity init");
  1407. TEST_ESP_OK( touch_pad_fsm_start() );
  1408. vTaskDelay(20 / portTICK_PERIOD_MS);
  1409. int test_cnt = TEST_TOUCH_COUNT_NUM;
  1410. while (test_cnt--) {
  1411. test_touch_push_all();
  1412. vTaskDelay(20 / portTICK_PERIOD_MS);
  1413. TEST_ESP_OK( test_touch_check_ch_touched(TEST_TOUCH_CHANNEL - 1, TOUCH_EXCEED_TIME_MS) ); // take off shield pad
  1414. printf_touch_hw_read("push");
  1415. test_touch_release_all();
  1416. vTaskDelay(20 / portTICK_PERIOD_MS);
  1417. TEST_ESP_OK( test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1418. printf_touch_hw_read("release");
  1419. }
  1420. TEST_ESP_OK( touch_pad_deinit() );
  1421. return ESP_OK;
  1422. }
  1423. TEST_CASE("Touch Sensor proximity test", "[touch]")
  1424. {
  1425. ESP_LOGI(TAG, "*********** touch proximity test ********************");
  1426. TEST_ESP_OK( test_touch_proximity(5) );
  1427. TEST_ESP_OK( test_touch_proximity(1) );
  1428. }
  1429. esp_err_t test_touch_sleep_reading_stable(touch_pad_t sleep_pad)
  1430. {
  1431. uint32_t touch_temp = 0;
  1432. uint32_t touch_value, smooth, ret_val;
  1433. int test_cnt = TEST_TOUCH_COUNT_NUM;
  1434. ESP_LOGI(TAG, " >> %s << \n", __func__);
  1435. if (que_touch == NULL) {
  1436. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  1437. /* Should register once. */
  1438. TEST_ESP_OK( touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL) );
  1439. } else {
  1440. xQueueReset(que_touch);
  1441. }
  1442. TEST_ESP_OK( touch_pad_init() );
  1443. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1444. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  1445. }
  1446. // /* Denoise setting at TouchSensor 0. */
  1447. touch_pad_denoise_t denoise = {
  1448. /* The bits to be cancelled are determined according to the noise level. */
  1449. .grade = TOUCH_PAD_DENOISE_BIT4,
  1450. .cap_level = TOUCH_PAD_DENOISE_CAP_L4,
  1451. };
  1452. TEST_ESP_OK( touch_pad_denoise_set_config(&denoise) );
  1453. TEST_ESP_OK( touch_pad_denoise_enable() );
  1454. touch_filter_config_t filter_info = {
  1455. .mode = TOUCH_PAD_FILTER_IIR_16, // Test jitter and filter 1/4.
  1456. .debounce_cnt = 1, // 1 time count.
  1457. .noise_thr = 0, // 50%
  1458. .jitter_step = 4, // use for jitter mode.
  1459. .smh_lvl = TOUCH_PAD_SMOOTH_OFF,
  1460. };
  1461. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  1462. TEST_ESP_OK( touch_pad_filter_enable() );
  1463. TEST_ESP_OK( touch_pad_sleep_channel_enable(sleep_pad, true) );
  1464. TEST_ESP_OK( touch_pad_sleep_channel_enable_proximity(sleep_pad, false) );
  1465. /* Register touch interrupt ISR, enable intr type. */
  1466. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  1467. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  1468. TEST_ESP_OK( touch_pad_fsm_start() );
  1469. // Initialize and start a software filter to detect slight change of capacitance.
  1470. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  1471. /* Set threshold of touch sensor */
  1472. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1473. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1474. TEST_ESP_OK( touch_pad_filter_read_smooth(touch_list[i], &smooth) );
  1475. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  1476. ESP_LOGI(TAG, "test init: touch pad [%d] base %d, smooth %d, thresh %d",
  1477. touch_list[i], touch_value, smooth, (uint32_t)(touch_value * TOUCH_INTR_THRESHOLD));
  1478. }
  1479. /* Sleep channel setting */
  1480. TEST_ESP_OK( touch_pad_sleep_channel_read_benchmark(sleep_pad, &touch_value) );
  1481. TEST_ESP_OK( touch_pad_sleep_set_threshold(sleep_pad, touch_value * TOUCH_INTR_THRESHOLD) );
  1482. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  1483. while (test_cnt--) {
  1484. /* Touch reading filtered value equal to raw data. */
  1485. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1486. TEST_ESP_OK( touch_pad_sleep_channel_read_data(sleep_pad, &touch_value) );
  1487. TEST_ESP_OK( touch_pad_sleep_channel_read_benchmark(sleep_pad, &touch_temp) );
  1488. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp, touch_value);
  1489. TEST_ESP_OK( touch_pad_sleep_channel_read_smooth(sleep_pad, &touch_temp) );
  1490. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp, touch_value);
  1491. }
  1492. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1493. if (touch_temp) {
  1494. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp, touch_value);
  1495. }
  1496. touch_temp = touch_value;
  1497. }
  1498. vTaskDelay(20 / portTICK_PERIOD_MS);
  1499. }
  1500. TEST_ESP_OK( touch_pad_sleep_channel_read_benchmark(sleep_pad, &ret_val) );
  1501. TEST_ESP_OK( touch_pad_deinit() );
  1502. return ret_val;
  1503. }
  1504. TEST_CASE("Touch Sensor sleep pad reading stable test", "[touch]")
  1505. {
  1506. ESP_LOGI(TAG, "*********** touch sleep pad low power (wakeup) test ********************");
  1507. test_touch_sleep_reading_stable(touch_list[0]);
  1508. }
  1509. /*
  1510. * Test the touch sleep pad interrupt in normal mode.
  1511. * TEST POINT:
  1512. * 1. Touch sleep pad interrupt.
  1513. * 2. sleep pad reading.
  1514. * 3. sleep pad enable proximity.
  1515. */
  1516. uint32_t test_touch_sleep_pad_proximity(touch_pad_t sleep_pad, bool is_proximity, uint32_t meas_num)
  1517. {
  1518. uint32_t touch_value, smooth, ret_val;
  1519. uint32_t measure_out;
  1520. uint32_t proximity_cnt;
  1521. uint32_t touch_thres;
  1522. int test_cnt = TEST_TOUCH_COUNT_NUM;
  1523. ESP_LOGI(TAG, " >> %s << \n", __func__);
  1524. if (que_touch == NULL) {
  1525. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  1526. /* Should register once. */
  1527. TEST_ESP_OK( touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL) );
  1528. } else {
  1529. xQueueReset(que_touch);
  1530. }
  1531. TEST_ESP_OK( touch_pad_init() );
  1532. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1533. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  1534. }
  1535. /* Denoise setting at TouchSensor 0. */
  1536. touch_pad_denoise_t denoise = {
  1537. /* The bits to be cancelled are determined according to the noise level. */
  1538. .grade = TOUCH_PAD_DENOISE_BIT4,
  1539. .cap_level = TOUCH_PAD_DENOISE_CAP_L4,
  1540. };
  1541. TEST_ESP_OK( touch_pad_denoise_set_config(&denoise) );
  1542. TEST_ESP_OK( touch_pad_denoise_enable() );
  1543. touch_filter_config_t filter_info = {
  1544. .mode = TOUCH_PAD_FILTER_IIR_16, // Test jitter and filter 1/4.
  1545. .debounce_cnt = 1, // 1 time count.
  1546. .noise_thr = 0, // 50%
  1547. .jitter_step = 4, // use for jitter mode.
  1548. .smh_lvl = TOUCH_PAD_SMOOTH_OFF,
  1549. };
  1550. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  1551. TEST_ESP_OK( touch_pad_filter_enable() );
  1552. /* Sleep channel setting */
  1553. TEST_ESP_OK( touch_pad_sleep_channel_enable(sleep_pad, true) );
  1554. TEST_ESP_OK( touch_pad_sleep_channel_enable_proximity(sleep_pad, is_proximity) );
  1555. /* Register touch interrupt ISR, enable intr type. */
  1556. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_SCAN_DONE | TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  1557. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  1558. TEST_ESP_OK( touch_pad_fsm_start() );
  1559. // Initialize and start a software filter to detect slight change of capacitance.
  1560. vTaskDelay(100 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  1561. if (is_proximity) {
  1562. /* Set the threshold. */
  1563. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1564. if (touch_list[i] == sleep_pad) {
  1565. touch_pad_sleep_channel_read_smooth(sleep_pad, &touch_value);
  1566. touch_pad_sleep_set_threshold(sleep_pad, meas_num * touch_value * (1 + TOUCH_INTR_THRESHOLD));
  1567. ESP_LOGI(TAG, "Sleep pad [%d] base %d, thresh %d", touch_list[i], touch_value,
  1568. (uint32_t)(meas_num * touch_value * (1 + TOUCH_INTR_THRESHOLD)));
  1569. } else if (touch_list[i] == sleep_pad) {
  1570. touch_pad_sleep_channel_read_smooth(sleep_pad, &touch_value);
  1571. /* The threshold of proximity pad is the sum of touch reading `meas_num` times */
  1572. touch_pad_sleep_set_threshold(sleep_pad, meas_num * touch_value * (1 + TOUCH_INTR_THRESHOLD));
  1573. ESP_LOGI(TAG, "proximity pad [%d] base %d, thresh %d", touch_list[i], touch_value,
  1574. (uint32_t)(meas_num * touch_value * (1 + TOUCH_INTR_THRESHOLD)));
  1575. } else {
  1576. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1577. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  1578. ESP_LOGI(TAG, "touch pad [%d] base %d, thresh %d", \
  1579. touch_list[i], touch_value, (uint32_t)(touch_value * TOUCH_INTR_THRESHOLD));
  1580. }
  1581. }
  1582. /* Should stop the measure, then change the config. */
  1583. while (!touch_pad_meas_is_done());
  1584. TEST_ESP_OK( touch_pad_fsm_stop() );
  1585. /* Proximity function */
  1586. TEST_ESP_OK( touch_pad_proximity_enable(proximity_pad[0], false) );
  1587. TEST_ESP_OK( touch_pad_proximity_enable(proximity_pad[1], false) );
  1588. TEST_ESP_OK( touch_pad_proximity_enable(proximity_pad[2], false) );
  1589. TEST_ESP_OK( touch_pad_proximity_enable(sleep_pad, true) );
  1590. TEST_ESP_OK( touch_pad_proximity_set_count(TOUCH_PAD_MAX, meas_num) );
  1591. ESP_LOGI(TAG, "touch pad proximity init");
  1592. TEST_ESP_OK( touch_pad_fsm_start() );
  1593. vTaskDelay(100 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  1594. } else {
  1595. /* Set threshold of touch sensor */
  1596. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1597. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1598. TEST_ESP_OK( touch_pad_filter_read_smooth(touch_list[i], &smooth) );
  1599. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  1600. ESP_LOGI(TAG, "test init: touch pad [%d] base %d, smooth %d, thresh %d",
  1601. touch_list[i], touch_value, smooth, (uint32_t)(touch_value * TOUCH_INTR_THRESHOLD));
  1602. }
  1603. /* Sleep channel setting */
  1604. TEST_ESP_OK( touch_pad_sleep_channel_read_smooth(sleep_pad, &touch_value) );
  1605. TEST_ESP_OK( touch_pad_sleep_set_threshold(sleep_pad, touch_value * TOUCH_INTR_THRESHOLD) );
  1606. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  1607. }
  1608. TEST_ESP_OK( touch_pad_sleep_channel_read_smooth(sleep_pad, &ret_val) );
  1609. while (test_cnt--) {
  1610. test_touch_push_all();
  1611. TEST_ESP_OK( test_touch_check_ch_touched_with_proximity(TEST_TOUCH_CHANNEL, 5000) );
  1612. printf_touch_hw_read("push");
  1613. if (is_proximity) {
  1614. TEST_ESP_OK( touch_pad_sleep_channel_read_smooth(sleep_pad, &smooth) );
  1615. TEST_ESP_OK( touch_pad_sleep_channel_read_benchmark(sleep_pad, &touch_value) );
  1616. TEST_ESP_OK( touch_pad_proximity_get_data(sleep_pad, &measure_out) );
  1617. TEST_ESP_OK( touch_pad_sleep_channel_read_proximity_cnt(sleep_pad, &proximity_cnt) );
  1618. TEST_ESP_OK( touch_pad_sleep_get_threshold(sleep_pad, &touch_thres) );
  1619. printf("touch slp smooth %d, base %d, proxi %d cnt %d thres%d status 0x%x\n",
  1620. smooth, touch_value, measure_out, proximity_cnt,
  1621. touch_thres, touch_pad_get_status());
  1622. }
  1623. test_touch_release_all();
  1624. TEST_ESP_OK( test_touch_check_ch_released_with_proximity(TEST_TOUCH_CHANNEL, 5000) );
  1625. printf_touch_hw_read("release");
  1626. if (is_proximity) {
  1627. TEST_ESP_OK( touch_pad_sleep_channel_read_smooth(sleep_pad, &smooth) );
  1628. TEST_ESP_OK( touch_pad_sleep_channel_read_benchmark(sleep_pad, &touch_value) );
  1629. TEST_ESP_OK( touch_pad_proximity_get_data(sleep_pad, &measure_out) );
  1630. TEST_ESP_OK( touch_pad_sleep_channel_read_proximity_cnt(sleep_pad, &proximity_cnt) );
  1631. printf("touch slp smooth %d, base %d, proxi %d cnt %d status 0x%x\n",
  1632. smooth, touch_value, measure_out, proximity_cnt, touch_pad_get_status());
  1633. }
  1634. }
  1635. TEST_ESP_OK( touch_pad_deinit() );
  1636. return ret_val;
  1637. }
  1638. TEST_CASE("Touch Sensor sleep pad and proximity interrupt test", "[touch]")
  1639. {
  1640. ESP_LOGI(TAG, "*********** touch sleep pad interrupt test ********************");
  1641. test_touch_sleep_pad_proximity(touch_list[0], false, 0);
  1642. test_touch_sleep_pad_proximity(touch_list[0], false, 0);
  1643. test_touch_sleep_pad_proximity(touch_list[0], false, 0);
  1644. ESP_LOGI(TAG, "*********** touch sleep pad interrupt (proximity) test ********************");
  1645. test_touch_sleep_pad_proximity(touch_list[0], true, 1);
  1646. test_touch_sleep_pad_proximity(touch_list[0], true, 3);
  1647. test_touch_sleep_pad_proximity(touch_list[0], true, 5);
  1648. }
  1649. /*
  1650. * Test the touch sleep pad interrupt in normal mode.
  1651. * TEST POINT:
  1652. * 1. Touch sleep pad interrupt.
  1653. * 2. sleep pad reading.
  1654. * 3. denoise, waterproof
  1655. */
  1656. esp_err_t test_touch_sleep_pad_interrupt_wakeup_deep_sleep(touch_pad_t sleep_pad)
  1657. {
  1658. uint32_t touch_value, smooth, raw;
  1659. ESP_LOGI(TAG, " >> %s << \n", __func__);
  1660. if (que_touch == NULL) {
  1661. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  1662. /* Should register once. */
  1663. TEST_ESP_OK( touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL) );
  1664. } else {
  1665. xQueueReset(que_touch);
  1666. }
  1667. TEST_ESP_OK( touch_pad_init() );
  1668. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1669. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  1670. }
  1671. // /* Denoise setting at TouchSensor 0. */
  1672. touch_pad_denoise_t denoise = {
  1673. /* The bits to be cancelled are determined according to the noise level. */
  1674. .grade = TOUCH_PAD_DENOISE_BIT4,
  1675. .cap_level = TOUCH_PAD_DENOISE_CAP_L4,
  1676. };
  1677. TEST_ESP_OK( touch_pad_denoise_set_config(&denoise) );
  1678. TEST_ESP_OK( touch_pad_denoise_disable() );
  1679. touch_filter_config_t filter_info = {
  1680. .mode = TOUCH_PAD_FILTER_IIR_16, // Test jitter and filter 1/4.
  1681. .debounce_cnt = 1, // 1 time count.
  1682. .noise_thr = 0, // 50%
  1683. .jitter_step = 4, // use for jitter mode.
  1684. .smh_lvl = TOUCH_PAD_SMOOTH_OFF,
  1685. };
  1686. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  1687. TEST_ESP_OK( touch_pad_filter_enable() );
  1688. /* Sleep channel setting */
  1689. TEST_ESP_OK( touch_pad_sleep_channel_enable(sleep_pad, true) );
  1690. TEST_ESP_OK( touch_pad_sleep_channel_enable_proximity(sleep_pad, false) );
  1691. /* Register touch interrupt ISR, enable intr type. */
  1692. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  1693. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  1694. TEST_ESP_OK( touch_pad_fsm_start() );
  1695. // Initialize and start a software filter to detect slight change of capacitance.
  1696. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  1697. /* Set threshold of touch sensor */
  1698. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1699. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1700. TEST_ESP_OK( touch_pad_filter_read_smooth(touch_list[i], &smooth) );
  1701. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_INTR_THRESHOLD) );
  1702. ESP_LOGI(TAG, "test init: touch pad [%d] base %d, smooth %d, thresh %d",
  1703. touch_list[i], touch_value, smooth, (uint32_t)(touch_value * TOUCH_INTR_THRESHOLD));
  1704. }
  1705. /* Sleep channel setting */
  1706. TEST_ESP_OK( touch_pad_sleep_channel_read_benchmark(sleep_pad, &touch_value) );
  1707. TEST_ESP_OK( touch_pad_sleep_set_threshold(sleep_pad, touch_value * TOUCH_INTR_THRESHOLD) );
  1708. vTaskDelay(50 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  1709. test_touch_push_all();
  1710. TEST_ESP_OK( test_touch_check_ch_touched(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1711. printf_touch_hw_read("push");
  1712. TEST_ESP_OK( touch_pad_sleep_channel_read_smooth(sleep_pad, &smooth) );
  1713. TEST_ESP_OK( touch_pad_sleep_channel_read_data(sleep_pad, &raw) );
  1714. TEST_ESP_OK( touch_pad_sleep_channel_read_benchmark(sleep_pad, &touch_value) );
  1715. printf("touch slp raw %d, smooth %d, base %d, status 0x%x\n", raw, smooth, touch_value, touch_pad_get_status());
  1716. test_touch_release_all();
  1717. TEST_ESP_OK( test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1718. printf_touch_hw_read("release");
  1719. TEST_ESP_OK( touch_pad_sleep_channel_read_smooth(sleep_pad, &smooth) );
  1720. TEST_ESP_OK( touch_pad_sleep_channel_read_data(sleep_pad, &raw) );
  1721. TEST_ESP_OK( touch_pad_sleep_channel_read_benchmark(sleep_pad, &touch_value) );
  1722. printf("touch slp raw %d, smooth %d, base %d, status 0x%x\n", raw, smooth, touch_value, touch_pad_get_status());
  1723. return ESP_OK;
  1724. }
  1725. #include <sys/time.h>
  1726. #include "esp_sleep.h"
  1727. static RTC_DATA_ATTR struct timeval sleep_enter_time;
  1728. static void test_deep_sleep_init(void)
  1729. {
  1730. struct timeval now;
  1731. gettimeofday(&now, NULL);
  1732. int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
  1733. printf("RTC_CNTL_SLP_WAKEUP_CAUSE_REG %x\n", REG_READ(RTC_CNTL_SLP_WAKEUP_CAUSE_REG));
  1734. switch (esp_sleep_get_wakeup_cause()) {
  1735. case ESP_SLEEP_WAKEUP_EXT1: {
  1736. uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
  1737. if (wakeup_pin_mask != 0) {
  1738. int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
  1739. printf("Wake up from GPIO %d\n", pin);
  1740. } else {
  1741. printf("Wake up from GPIO\n");
  1742. }
  1743. break;
  1744. }
  1745. case ESP_SLEEP_WAKEUP_TIMER: {
  1746. printf("Wake up from timer. Time spent in deep sleep: %dms\n", sleep_time_ms);
  1747. break;
  1748. }
  1749. case ESP_SLEEP_WAKEUP_TOUCHPAD: {
  1750. printf("Wake up from touch on pad %d\n", esp_sleep_get_touchpad_wakeup_status());
  1751. break;
  1752. }
  1753. case ESP_SLEEP_WAKEUP_UNDEFINED:
  1754. default: {
  1755. printf("Not a deep sleep reset\n");
  1756. ESP_LOGI(TAG, "*********** touch sleep pad wakeup test ********************");
  1757. /* Sleep pad should be init once. */
  1758. test_touch_sleep_pad_interrupt_wakeup_deep_sleep(touch_list[0]);
  1759. }
  1760. }
  1761. vTaskDelay(100 * SYS_DELAY_TIME_MOM / portTICK_PERIOD_MS);
  1762. printf("Enabling touch pad wakeup\n");
  1763. esp_sleep_enable_touchpad_wakeup();
  1764. printf("Entering deep sleep\n");
  1765. gettimeofday(&sleep_enter_time, NULL);
  1766. }
  1767. TEST_CASE("Touch Sensor sleep pad wakeup deep sleep test", "[touch][ignore]")
  1768. {
  1769. test_deep_sleep_init();
  1770. /* Change the work duty of touch sensor to reduce current. */
  1771. touch_pad_set_meas_time(100, TOUCH_PAD_MEASURE_CYCLE_DEFAULT);
  1772. /* Close PD current in deep sleep. */
  1773. RTCCNTL.bias_conf.pd_cur_deep_slp = 1;
  1774. RTCCNTL.bias_conf.pd_cur_monitor = 1;
  1775. RTCCNTL.bias_conf.bias_sleep_deep_slp = 1;
  1776. RTCCNTL.bias_conf.bias_sleep_monitor = 1;
  1777. esp_deep_sleep_start();
  1778. }
  1779. #include "touch_scope.h"
  1780. /*
  1781. * 0: 10 channels raw/smooth/benchmark data debug.
  1782. * 1: 5 channges smooth + benchmark data debug.
  1783. * 2: 1 channels filter data.
  1784. */
  1785. #define SCOPE_DEBUG_TYPE 2
  1786. #define TOUCH_THRESHOLD 0.5
  1787. #define TOUCH_SHELD_PAD (1)
  1788. #define SCOPE_DEBUG_CHANNEL_MAX (10)
  1789. #define SCOPE_DEBUG_ENABLE (0)
  1790. #define SCOPE_UART_BUADRATE (256000)
  1791. #define SCOPE_DEBUG_FREQ_MS (50)
  1792. void test_touch_slope_debug(int pad_num)
  1793. {
  1794. touch_event_t evt;
  1795. uint32_t touch_value, smooth;
  1796. ESP_LOGI(TAG, " >> %s << \n", __func__);
  1797. if (que_touch == NULL) {
  1798. que_touch = xQueueCreate(TEST_TOUCH_CHANNEL, sizeof(touch_event_t));
  1799. /* Should register once. */
  1800. touch_pad_isr_register(test_touch_intr_cb, NULL, TOUCH_PAD_INTR_MASK_ALL);
  1801. } else {
  1802. xQueueReset(que_touch);
  1803. }
  1804. TEST_ESP_OK( touch_pad_init() );
  1805. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1806. TEST_ESP_OK( touch_pad_config(touch_list[i]) );
  1807. }
  1808. touch_filter_config_t filter_info = {
  1809. .mode = TOUCH_PAD_FILTER_IIR_32, // Test jitter and filter 1/4.
  1810. .debounce_cnt = 1, // 1 time count.
  1811. .noise_thr = 3, // 50%
  1812. .jitter_step = 4, // use for jitter mode.
  1813. .smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,
  1814. };
  1815. TEST_ESP_OK( touch_pad_filter_set_config(&filter_info) );
  1816. TEST_ESP_OK( touch_pad_filter_enable() );
  1817. /* Register touch interrupt ISR, enable intr type. */
  1818. TEST_ESP_OK( touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE) );
  1819. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  1820. TEST_ESP_OK( touch_pad_fsm_start() );
  1821. /* Waterproof function */
  1822. touch_pad_waterproof_t waterproof = {
  1823. .guard_ring_pad = 0, // If no ring pad, set 0;
  1824. /* It depends on the number of the parasitic capacitance of the shield pad. */
  1825. .shield_driver = TOUCH_PAD_SHIELD_DRV_L2, //40pf
  1826. };
  1827. TEST_ESP_OK( touch_pad_waterproof_set_config(&waterproof) );
  1828. TEST_ESP_OK( touch_pad_waterproof_enable() );
  1829. ESP_LOGI(TAG, "touch pad waterproof init");
  1830. // Initialize and start a software filter to detect slight change of capacitance.
  1831. vTaskDelay(50 / portTICK_PERIOD_MS);
  1832. /* Set threshold of touch sensor */
  1833. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1834. TEST_ESP_OK( touch_pad_read_benchmark(touch_list[i], &touch_value) );
  1835. TEST_ESP_OK( touch_pad_filter_read_smooth(touch_list[i], &smooth) );
  1836. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * TOUCH_THRESHOLD) );
  1837. ESP_LOGI(TAG, "test init: touch pad [%d] base %d, smooth %d, thresh %d", \
  1838. touch_list[i], touch_value, smooth, (uint32_t)(touch_value * TOUCH_THRESHOLD));
  1839. }
  1840. float scope_temp[SCOPE_DEBUG_CHANNEL_MAX] = {0}; // max scope channel is 10.
  1841. uint32_t scope_data[SCOPE_DEBUG_CHANNEL_MAX] = {0}; // max scope channel is 10.
  1842. test_tp_scope_debug_init(0, -1, -1, SCOPE_UART_BUADRATE);
  1843. #if SCOPE_DEBUG_TYPE == 0
  1844. while (1) {
  1845. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  1846. touch_pad_read_raw_data(touch_list[i], &scope_data[i]);
  1847. // touch_pad_filter_read_smooth(touch_list[i], &scope_data[i]);
  1848. // touch_pad_read_benchmark(touch_list[i], &scope_data[i]);
  1849. scope_temp[i] = scope_data[i];
  1850. }
  1851. test_tp_print_to_scope(scope_temp, TEST_TOUCH_CHANNEL);
  1852. vTaskDelay(SCOPE_DEBUG_FREQ_MS / portTICK_RATE_MS);
  1853. }
  1854. #elif SCOPE_DEBUG_TYPE == 1
  1855. while (1) {
  1856. int cnt = 0;
  1857. for (int i = 0; i < 5; i++) {
  1858. touch_pad_read_raw_data(touch_list[i], &scope_data[i]);
  1859. scope_temp[i] = scope_data[i];
  1860. }
  1861. for (int i = 0; i < 5; i++) {
  1862. touch_pad_filter_read_smooth(touch_list[i], &scope_data[i]);
  1863. scope_temp[i + SCOPE_DEBUG_CHANNEL_MAX / 2] = scope_data[i];
  1864. }
  1865. test_tp_print_to_scope(scope_temp, SCOPE_DEBUG_CHANNEL_MAX);
  1866. vTaskDelay(SCOPE_DEBUG_FREQ_MS / portTICK_RATE_MS);
  1867. }
  1868. #elif SCOPE_DEBUG_TYPE == 2
  1869. uint32_t status;
  1870. touch_pad_read_benchmark(pad_num, &status);
  1871. while (1) {
  1872. xQueueReceive(que_touch, &evt, SCOPE_DEBUG_FREQ_MS / portTICK_RATE_MS);
  1873. //read filtered value
  1874. touch_pad_read_raw_data(pad_num, &scope_data[0]);
  1875. touch_pad_read_benchmark(pad_num, &scope_data[1]);
  1876. touch_pad_get_thresh(pad_num, &scope_data[2]);
  1877. touch_pad_filter_read_smooth(pad_num, &scope_data[8]);
  1878. // raw data
  1879. scope_temp[0] = scope_data[0];
  1880. // benchmark
  1881. scope_temp[1] = scope_data[1];
  1882. // smooth data
  1883. scope_temp[8] = scope_data[8];
  1884. // noise thr
  1885. scope_temp[2] = scope_temp[1] - scope_data[2] * 0.5;
  1886. // noise thr
  1887. scope_temp[3] = scope_temp[1] + scope_data[2] * 0.5;
  1888. // touch thr
  1889. scope_temp[4] = scope_temp[1] + scope_data[2];
  1890. scope_temp[5] = scope_temp[4] - scope_data[2] * 0.0625;
  1891. scope_temp[6] = scope_temp[4] + scope_data[2] * 0.0625;
  1892. // touch status
  1893. if (touch_pad_get_status() & BIT(pad_num)) {
  1894. scope_temp[7] = status + 100;
  1895. } else {
  1896. scope_temp[7] = status - 100; //0:release; 1:push;
  1897. }
  1898. test_tp_print_to_scope(scope_temp, 9);
  1899. }
  1900. #elif SCOPE_DEBUG_TYPE == 3
  1901. while (1) {
  1902. test_touch_push_all();
  1903. TEST_ESP_OK( test_touch_check_ch_touched(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1904. printf_touch_hw_read("push");
  1905. test_touch_benchmark();
  1906. test_touch_release_all();
  1907. TEST_ESP_OK( test_touch_check_ch_released(TEST_TOUCH_CHANNEL, TOUCH_EXCEED_TIME_MS) );
  1908. printf_touch_hw_read("release");
  1909. }
  1910. #endif
  1911. TEST_ESP_OK( touch_pad_deinit() );
  1912. }
  1913. #endif // CONFIG_IDF_TARGET_ESP32S2