test_esp32.c 14 KB

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