test_esp32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 "esp_system.h"
  18. #include "driver/touch_pad.h"
  19. #include "unity.h"
  20. #include "esp_system.h"
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/task.h"
  23. #include "esp_log.h"
  24. #include "nvs_flash.h"
  25. #include "test_utils.h"
  26. #include "soc/rtc_cntl_reg.h"
  27. #include "soc/rtc_cntl_struct.h"
  28. #include "soc/sens_reg.h"
  29. #include "soc/sens_struct.h"
  30. #include "soc/rtc_cntl_reg.h"
  31. #include "soc/rtc_cntl_struct.h"
  32. #include "soc/rtc_io_reg.h"
  33. #include "soc/rtc_io_struct.h"
  34. #include "esp_rom_sys.h"
  35. #if !DISABLED_FOR_TARGETS(ESP8266, ESP32S2, ESP32S3) // This testcase for ESP32
  36. static const char *TAG = "test_touch";
  37. #define TOUCH_READ_INVALID_VAL (0)
  38. #define TOUCHPAD_FILTER_TOUCH_PERIOD (10)
  39. #define TOUCH_REG_BASE_TEST() ({ \
  40. TEST_ASSERT_EQUAL_UINT32(RTC_CNTL_BROWN_OUT_REG, &RTCCNTL.brown_out.val); \
  41. TEST_ASSERT_EQUAL_UINT32(REG_GET_FIELD(SENS_SARDATE_REG, SENS_SAR_DATE), SENS.sardate.sar_date); \
  42. TEST_ASSERT_EQUAL_UINT32(REG_GET_FIELD(RTC_IO_DATE_REG, RTC_IO_IO_DATE), RTCIO.date.date); \
  43. })
  44. #define TOUCH_READ_ERROR (100)
  45. #define TEST_TOUCH_COUNT_NUM (10)
  46. #define TEST_TOUCH_CHANNEL (3)
  47. static touch_pad_t touch_list[TEST_TOUCH_CHANNEL] = {
  48. // TOUCH_PAD_NUM0,
  49. // TOUCH_PAD_NUM1 is GPIO0, for download.
  50. // TOUCH_PAD_NUM2,
  51. // TOUCH_PAD_NUM3,
  52. // TOUCH_PAD_NUM4,
  53. // TOUCH_PAD_NUM5,
  54. // TOUCH_PAD_NUM6,
  55. TOUCH_PAD_NUM7,
  56. TOUCH_PAD_NUM8,
  57. TOUCH_PAD_NUM9,
  58. };
  59. static void printf_touch_hw_read(const char *str)
  60. {
  61. uint16_t touch_value;
  62. printf("[%s] ", str);
  63. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  64. while (!touch_pad_meas_is_done()) ;
  65. touch_pad_read_raw_data(touch_list[i], &touch_value);
  66. printf("[%d]%d ", touch_list[i], touch_value);
  67. }
  68. printf("\r\n");
  69. }
  70. /*
  71. * Change the slope to get larger value from touch sensor.
  72. */
  73. static void test_press_fake(touch_pad_t pad_num)
  74. {
  75. touch_pad_set_cnt_mode(pad_num, TOUCH_PAD_SLOPE_2, TOUCH_PAD_TIE_OPT_DEFAULT);
  76. }
  77. /*
  78. * Change the slope to get smaller value from touch sensor.
  79. */
  80. static void test_release_fake(touch_pad_t pad_num)
  81. {
  82. touch_pad_set_cnt_mode(pad_num, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_DEFAULT);
  83. }
  84. static esp_err_t test_touch_sw_read_test_runner(void)
  85. {
  86. ESP_LOGI(TAG, "%s", __func__);
  87. uint16_t touch_value;
  88. TEST_ESP_OK( touch_pad_init() );
  89. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW) );
  90. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  91. TEST_ESP_OK( touch_pad_config(touch_list[i], TOUCH_READ_INVALID_VAL) );
  92. }
  93. // Start task to read values sensed by pads
  94. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  95. printf("test T%d\n", touch_list[i]);
  96. touch_pad_read(touch_list[i], &touch_value);
  97. printf("T%d:[%4d] ", touch_list[i], touch_value);
  98. }
  99. printf("\n");
  100. TEST_ESP_OK( touch_pad_deinit() );
  101. return ESP_OK;
  102. }
  103. static esp_err_t test_touch_sw_read(void)
  104. {
  105. ESP_LOGI(TAG, "%s", __func__);
  106. uint16_t touch_value;
  107. TEST_ESP_OK( touch_pad_init() );
  108. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW) );
  109. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  110. TEST_ESP_OK( touch_pad_config(touch_list[i], TOUCH_READ_INVALID_VAL) );
  111. }
  112. // Start task to read values sensed by pads
  113. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  114. printf("test T%d\n", touch_list[i]);
  115. TEST_ESP_OK( touch_pad_read(touch_list[i], &touch_value) );
  116. printf("T%d:[%4d] ", touch_list[i], touch_value);
  117. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_value);
  118. }
  119. printf("\n");
  120. TEST_ESP_OK( touch_pad_deinit() );
  121. return ESP_OK;
  122. }
  123. static esp_err_t test_touch_timer_read(void)
  124. {
  125. ESP_LOGI(TAG, "%s", __func__);
  126. uint16_t touch_value[TEST_TOUCH_CHANNEL], touch_temp[TEST_TOUCH_CHANNEL];
  127. int t_cnt = TEST_TOUCH_COUNT_NUM;
  128. TEST_ESP_OK( touch_pad_init() );
  129. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  130. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  131. TEST_ESP_OK( touch_pad_config(touch_list[i], TOUCH_READ_INVALID_VAL) );
  132. }
  133. // Start task to read values sensed by pads
  134. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  135. TEST_ESP_OK( touch_pad_read(touch_list[i], &touch_value[i]) );
  136. printf("T%d:[%4d] ", touch_list[i], touch_value[i]);
  137. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_value[i]);
  138. }
  139. while (t_cnt--) {
  140. // Start task to read values sensed by pads
  141. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  142. TEST_ESP_OK( touch_pad_read(touch_list[i], &touch_temp[i]) );
  143. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_temp[i]);
  144. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp[i], touch_value[i]);
  145. }
  146. vTaskDelay(50 / portTICK_PERIOD_MS);
  147. }
  148. TEST_ESP_OK( touch_pad_deinit() );
  149. return ESP_OK;
  150. }
  151. static esp_err_t test_touch_filtered_read(void)
  152. {
  153. ESP_LOGI(TAG, "%s", __func__);
  154. uint16_t touch_value, touch_temp;
  155. int t_cnt = TEST_TOUCH_COUNT_NUM;
  156. TEST_ESP_OK( touch_pad_init() );
  157. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  158. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  159. TEST_ESP_OK( touch_pad_config(touch_list[i], TOUCH_READ_INVALID_VAL) );
  160. }
  161. // Initialize and start a software filter to detect slight change of capacitance.
  162. touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
  163. vTaskDelay(10 / portTICK_PERIOD_MS);
  164. while (t_cnt--) {
  165. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  166. TEST_ESP_OK( touch_pad_read(touch_list[i], &touch_value) );
  167. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_value);
  168. TEST_ESP_OK( touch_pad_read_raw_data(touch_list[i], &touch_value) );
  169. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_value);
  170. TEST_ESP_OK( touch_pad_read_filtered(touch_list[i], &touch_temp) );
  171. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_temp);
  172. TEST_ASSERT_UINT32_WITHIN(TOUCH_READ_ERROR, touch_temp, touch_value);
  173. printf("T%d:[%4d] ", touch_list[i], touch_value);
  174. }
  175. vTaskDelay(50 / portTICK_PERIOD_MS);
  176. }
  177. printf("\n");
  178. TEST_ESP_OK( touch_pad_deinit() );
  179. return ESP_OK;
  180. }
  181. // test the basic configuration function with right parameters and error parameters
  182. TEST_CASE("Touch Sensor all channel read test", "[touch]")
  183. {
  184. TOUCH_REG_BASE_TEST();
  185. test_touch_sw_read_test_runner();
  186. TEST_ESP_OK( test_touch_sw_read() );
  187. TEST_ESP_OK( test_touch_timer_read() );
  188. TEST_ESP_OK( test_touch_filtered_read() );
  189. }
  190. static int test_touch_parameter(touch_pad_t pad_num, int meas_time, int slp_time, int vol_h, int vol_l, int vol_a, int slope)
  191. {
  192. ESP_LOGI(TAG, "%s", __func__);
  193. uint16_t touch_value;
  194. TEST_ESP_OK( touch_pad_init() );
  195. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  196. TEST_ESP_OK( touch_pad_config(pad_num, TOUCH_READ_INVALID_VAL) );
  197. touch_pad_set_meas_time(slp_time, meas_time);
  198. touch_pad_set_voltage(vol_h, vol_l, vol_a);
  199. touch_pad_set_cnt_mode(pad_num, slope, TOUCH_PAD_TIE_OPT_DEFAULT);
  200. // Initialize and start a software filter to detect slight change of capacitance.
  201. touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
  202. vTaskDelay(500 / portTICK_PERIOD_MS);
  203. // Start task to read values sensed by pads
  204. TEST_ESP_OK( touch_pad_read(pad_num, &touch_value) );
  205. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_value);
  206. printf("T%d:[%4d] ", pad_num, touch_value);
  207. TEST_ESP_OK( touch_pad_read_raw_data(pad_num, &touch_value) );
  208. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_value);
  209. printf("T%d:[%4d] ", pad_num, touch_value);
  210. TEST_ESP_OK( touch_pad_read_filtered(pad_num, &touch_value) );
  211. TEST_ASSERT_NOT_EQUAL(TOUCH_READ_INVALID_VAL, touch_value);
  212. printf("T%d:[%4d] \n", pad_num, touch_value);
  213. TEST_ESP_OK( touch_pad_deinit() );
  214. return touch_value;
  215. }
  216. TEST_CASE("Touch Sensor parameters test", "[touch]")
  217. {
  218. int touch_val[5] = {0};
  219. ESP_LOGI(TAG, "Charge / incharge voltage level test");
  220. touch_val[0] = test_touch_parameter(touch_list[2], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  221. TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_0V,
  222. TOUCH_PAD_SLOPE_DEFAULT);
  223. touch_val[1] = test_touch_parameter(touch_list[2], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  224. TOUCH_HVOLT_2V5, TOUCH_LVOLT_0V6, TOUCH_HVOLT_ATTEN_1V,
  225. TOUCH_PAD_SLOPE_DEFAULT);
  226. touch_val[2] = test_touch_parameter(touch_list[0], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  227. TOUCH_HVOLT_2V4, TOUCH_LVOLT_0V8, TOUCH_HVOLT_ATTEN_1V5,
  228. TOUCH_PAD_SLOPE_DEFAULT);
  229. TEST_ASSERT_GREATER_OR_EQUAL(touch_val[0], touch_val[1]);
  230. TEST_ASSERT_GREATER_OR_EQUAL(touch_val[1], touch_val[2]);
  231. ESP_LOGI(TAG, "Measure time / sleep time test");
  232. touch_val[0] = test_touch_parameter(touch_list[0], 0xff, 0xa,
  233. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, TOUCH_PAD_SLOPE_DEFAULT);
  234. touch_val[1] = test_touch_parameter(touch_list[0], 0x1ff, 0xf,
  235. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, TOUCH_PAD_SLOPE_DEFAULT);
  236. touch_val[2] = test_touch_parameter(touch_list[0], 0x2fff, 0x1f,
  237. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, TOUCH_PAD_SLOPE_DEFAULT);
  238. TEST_ASSERT_GREATER_OR_EQUAL(touch_val[0], touch_val[1]);
  239. TEST_ASSERT_GREATER_OR_EQUAL(touch_val[1], touch_val[2]);
  240. ESP_LOGI(TAG, "Charge / incharge slope level test");
  241. touch_val[0] = test_touch_parameter(touch_list[1], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  242. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, 1);
  243. touch_val[1] = test_touch_parameter(touch_list[1], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  244. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, 3);
  245. touch_val[2] = test_touch_parameter(touch_list[1], TOUCH_PAD_MEASURE_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT,
  246. TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD, 7);
  247. TEST_ASSERT_GREATER_OR_EQUAL(touch_val[0], touch_val[1]);
  248. TEST_ASSERT_GREATER_OR_EQUAL(touch_val[1], touch_val[2]);
  249. }
  250. static bool s_pad_activated[TOUCH_PAD_MAX];
  251. static void test_touch_intr_cb(void *arg)
  252. {
  253. uint32_t pad_intr = touch_pad_get_status();
  254. esp_rom_printf("T%x ", pad_intr);
  255. //clear interrupt
  256. touch_pad_clear_status();
  257. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  258. if ((pad_intr >> touch_list[i]) & 0x1) {
  259. s_pad_activated[touch_list[i]] = true;
  260. }
  261. }
  262. }
  263. static esp_err_t test_touch_interrupt(void)
  264. {
  265. ESP_LOGI(TAG, "%s", __func__);
  266. uint16_t touch_value;
  267. TEST_ESP_OK( touch_pad_init() );
  268. TEST_ESP_OK( touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER) );
  269. touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
  270. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  271. TEST_ESP_OK( touch_pad_config(touch_list[i], TOUCH_READ_INVALID_VAL) );
  272. }
  273. // Initialize and start a software filter to detect slight change of capacitance.
  274. touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
  275. vTaskDelay(10 / portTICK_PERIOD_MS);
  276. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  277. //read filtered value
  278. TEST_ESP_OK( touch_pad_read_filtered(touch_list[i], &touch_value) );
  279. ESP_LOGI(TAG, "test init: touch pad [%d] val is %d", touch_list[i], touch_value);
  280. //set interrupt threshold.
  281. TEST_ESP_OK( touch_pad_set_thresh(touch_list[i], touch_value * 2 / 3) );
  282. }
  283. // Register touch interrupt ISR
  284. TEST_ESP_OK( touch_pad_isr_register(test_touch_intr_cb, NULL) );
  285. TEST_ESP_OK( touch_pad_clear_status() );
  286. TEST_ESP_OK( touch_pad_intr_enable() );
  287. int test_cnt = TEST_TOUCH_COUNT_NUM;
  288. while (test_cnt--) {
  289. ESP_LOGI(TAG, "touch push");
  290. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  291. test_press_fake(touch_list[i]);
  292. }
  293. vTaskDelay(100 / portTICK_PERIOD_MS);
  294. printf_touch_hw_read("push");
  295. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  296. if (s_pad_activated[touch_list[i]] == false) {
  297. ESP_LOGE(TAG, "touch%d not active", touch_list[i]);
  298. TEST_FAIL();
  299. }
  300. }
  301. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  302. s_pad_activated[touch_list[i]] = 0;
  303. }
  304. ESP_LOGI(TAG, "touch release");
  305. for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
  306. test_release_fake(touch_list[i]);
  307. }
  308. printf_touch_hw_read("release");
  309. }
  310. TEST_ESP_OK( touch_pad_deinit() );
  311. return ESP_OK;
  312. }
  313. TEST_CASE("Touch Sensor interrupt test", "[touch]")
  314. {
  315. TEST_ESP_OK( test_touch_interrupt() );
  316. }
  317. #endif // !DISABLED_FOR_TARGETS(ESP8266, ESP32)