touch_scope.c 7.5 KB

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