touch_scope.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "esp_err.h"
  8. #include "driver/uart.h"
  9. #include "esp_rom_uart.h"
  10. #define ROM_UART_DRIVER_ENABLE 0
  11. #if ROM_UART_DRIVER_ENABLE
  12. static uint8_t scope_uart_num = 0;
  13. static int8_t uart_used = 0;
  14. #else
  15. static uint8_t scope_uart_num = 255;
  16. static int8_t uart_used = -1;
  17. #endif
  18. static int scope_tx_io_num = 0;
  19. static int scope_rx_io_num = 0;
  20. static int scope_debug_baud_rate = 256000;
  21. static unsigned char datascope_output_buffer[42] = {0}; // Data buff.
  22. /**
  23. * @brief Translate a float data to four unsigned char data for uart TX.
  24. * @param target target float data address.
  25. * @param buf save translated data.
  26. * @param offset the start position in buf.
  27. * @return
  28. * - void
  29. */
  30. static void float_to_byte(float *target, unsigned char *buf, unsigned char offset)
  31. {
  32. memcpy(buf + offset, (uint8_t*)target, 4);
  33. }
  34. /**
  35. * @brief Add data to channel buff.
  36. * @param Data target data.
  37. * @param Channel target channel (1 - 10).
  38. * @return
  39. * - void
  40. */
  41. static void datascope_get_channel_data(float data, unsigned char channel)
  42. {
  43. if ( (channel > 10) || (channel == 0) ) {
  44. return;
  45. } else {
  46. switch (channel) {
  47. case 1: float_to_byte(&data,datascope_output_buffer, channel*4-3); break;
  48. case 2: float_to_byte(&data,datascope_output_buffer, channel*4-3); break;
  49. case 3: float_to_byte(&data,datascope_output_buffer, channel*4-3); break;
  50. case 4: float_to_byte(&data,datascope_output_buffer, channel*4-3); break;
  51. case 5: float_to_byte(&data,datascope_output_buffer, channel*4-3); break;
  52. case 6: float_to_byte(&data,datascope_output_buffer, channel*4-3); break;
  53. case 7: float_to_byte(&data,datascope_output_buffer, channel*4-3); break;
  54. case 8: float_to_byte(&data,datascope_output_buffer, channel*4-3); break;
  55. case 9: float_to_byte(&data,datascope_output_buffer, channel*4-3); break;
  56. case 10: float_to_byte(&data,datascope_output_buffer, channel*4-3); break;
  57. }
  58. }
  59. }
  60. /**
  61. * @brief Transform float data to DataScopeV1.0 data format.
  62. * @param channel_num the number of channel that wait to be translated.
  63. * @return
  64. * - The number of the UART TX data.
  65. */
  66. static unsigned char datascope_data_generate(unsigned char channel_num)
  67. {
  68. if ( (channel_num > 10) || (channel_num == 0) ) {
  69. return 0;
  70. } else {
  71. datascope_output_buffer[0] = '$'; //frame header
  72. switch(channel_num) {
  73. case 1: datascope_output_buffer[channel_num*4+1] = channel_num*4+1; return channel_num*4+2; break;
  74. case 2: datascope_output_buffer[channel_num*4+1] = channel_num*4+1; return channel_num*4+2; break;
  75. case 3: datascope_output_buffer[channel_num*4+1] = channel_num*4+1; return channel_num*4+2; break;
  76. case 4: datascope_output_buffer[channel_num*4+1] = channel_num*4+1; return channel_num*4+2; break;
  77. case 5: datascope_output_buffer[channel_num*4+1] = channel_num*4+1; return channel_num*4+2; break;
  78. case 6: datascope_output_buffer[channel_num*4+1] = channel_num*4+1; return channel_num*4+2; break;
  79. case 7: datascope_output_buffer[channel_num*4+1] = channel_num*4+1; return channel_num*4+2; break;
  80. case 8: datascope_output_buffer[channel_num*4+1] = channel_num*4+1; return channel_num*4+2; break;
  81. case 9: datascope_output_buffer[channel_num*4+1] = channel_num*4+1; return channel_num*4+2; break;
  82. case 10: datascope_output_buffer[channel_num*4+1] = channel_num*4+1; return channel_num*4+2; break;
  83. }
  84. }
  85. return 0;
  86. }
  87. /**
  88. * @brief Send touch sensor data to HMI in PC via UART.
  89. * @param uart_num Choose uart port (0, 1).
  90. * @param data The addr of the touch sensor data.
  91. * @param channel_num The number of channel that wait to be translated.
  92. * @return
  93. * - ESP_FAIL Error
  94. * - The number of the UART TX data.
  95. */
  96. int test_tp_print_to_scope(float *data, unsigned char channel_num)
  97. {
  98. uint8_t uart_num = scope_uart_num;
  99. if (uart_num >= UART_NUM_MAX) {
  100. return ESP_FAIL;
  101. }
  102. if ( (channel_num > 10) || (channel_num == 0) || (NULL == data) ) {
  103. return ESP_FAIL;
  104. }
  105. for(uint8_t i = 0 ; i < channel_num; i++) {
  106. datascope_get_channel_data(data[i] , i+1); // write data x into channel 1~10.
  107. }
  108. unsigned char out_len = datascope_data_generate(channel_num); // Generate n number data.
  109. unsigned char *out_data = datascope_output_buffer;
  110. // Init uart.
  111. if(uart_num != uart_used) {
  112. return 0;
  113. } else {
  114. #if ROM_UART_DRIVER_ENABLE
  115. esp_rom_uart_tx_wait_idle(uart_num); // Default print uart mumber is 0.
  116. for(int i=0; i<out_len; i++) {
  117. esp_rom_uart_tx_one_char(out_data[i]);
  118. }
  119. return out_len;
  120. #else
  121. uart_wait_tx_done(uart_num, portMAX_DELAY);
  122. return uart_write_bytes(uart_num, (const char *)out_data, out_len);
  123. #endif
  124. }
  125. }
  126. /**
  127. * @brief Enable scope debug function. Print the touch sensor raw data to "DataScope" tool via UART.
  128. * "DataScope" tool is touch sensor tune tool. User can monitor the data of each touch channel,
  129. * evaluate the touch system's touch performance (sensitivity, SNR, stability, channel coupling)
  130. * and determine the threshold for each channel.
  131. *
  132. * @attention 1. Choose a UART port that will only be used for scope debug.
  133. * @attention 2. Use this feature only during the testing phase.
  134. * @attention 3. "DataScope" tool can be downloaded from Espressif's official website.
  135. *
  136. * @param uart_num The uart port to send touch sensor raw data.
  137. * @param tx_io_num set UART TXD IO.
  138. * @param rx_io_num set UART RXD IO.
  139. * @param baud_rate set debug port baud rate.
  140. *
  141. * @return
  142. * - ESP_OK: succeed
  143. * - ESP_FAIL: the param uart_num is error
  144. */
  145. esp_err_t test_tp_scope_debug_init(uint8_t uart_num, int tx_io_num, int rx_io_num, int baud_rate)
  146. {
  147. #if ROM_UART_DRIVER_ENABLE
  148. esp_rom_uart_tx_wait_idle(0); // Default print uart mumber is 0.
  149. if(uart_num != 0) {
  150. esp_rom_uart_set_as_console(uart_num);
  151. }
  152. #else
  153. if(uart_used == uart_num) {
  154. return ESP_FAIL;
  155. }
  156. if (uart_num >= UART_NUM_MAX) {
  157. return ESP_FAIL;
  158. }
  159. scope_uart_num = uart_num;
  160. scope_tx_io_num = tx_io_num;
  161. scope_rx_io_num = rx_io_num;
  162. scope_debug_baud_rate = baud_rate;
  163. uart_config_t uart_config = {
  164. .baud_rate = scope_debug_baud_rate,
  165. .data_bits = UART_DATA_8_BITS,
  166. .parity = UART_PARITY_DISABLE,
  167. .stop_bits = UART_STOP_BITS_1,
  168. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  169. };
  170. uart_param_config(uart_num, &uart_config);
  171. // Set UART pins using UART0 default pins i.e. no changes
  172. uart_set_pin(uart_num, scope_tx_io_num, scope_rx_io_num,
  173. UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
  174. uart_driver_install(uart_num, 1024, 2048, 0, NULL, 0);
  175. uart_used = uart_num;
  176. #endif
  177. return ESP_OK;
  178. }