test_esp32s2.c 85 KB

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