test_esp32.c 14 KB

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