test_esp32s2.c 88 KB

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