unity_port_esp32.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdbool.h>
  8. #include "unity.h"
  9. #include "sdkconfig.h"
  10. #include "esp_cpu.h"
  11. #include "esp_rom_uart.h"
  12. #include "esp_private/esp_clk.h"
  13. static uint32_t s_test_start, s_test_stop;
  14. void unity_putc(int c)
  15. {
  16. if (c == '\n') {
  17. esp_rom_uart_tx_one_char('\r');
  18. esp_rom_uart_tx_one_char('\n');
  19. } else if (c == '\r') {
  20. } else {
  21. esp_rom_uart_tx_one_char(c);
  22. }
  23. }
  24. void unity_flush(void)
  25. {
  26. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  27. }
  28. #define iscontrol(c) ((c) <= '\x1f' || (c) == '\x7f')
  29. static void esp_unity_readline(char* dst, size_t len)
  30. {
  31. /* Read line from console with support for echoing and backspaces */
  32. size_t write_index = 0;
  33. for (;;) {
  34. char c = 0;
  35. bool got_char = esp_rom_uart_rx_one_char((uint8_t*)&c) == 0;
  36. if (!got_char) {
  37. continue;
  38. }
  39. if (c == '\r' || c == '\n') {
  40. /* Add null terminator and return on newline */
  41. unity_putc('\n');
  42. dst[write_index] = '\0';
  43. return;
  44. } else if (c == '\b') {
  45. if (write_index > 0) {
  46. /* Delete previously entered character */
  47. write_index--;
  48. esp_rom_uart_tx_one_char('\b');
  49. esp_rom_uart_tx_one_char(' ');
  50. esp_rom_uart_tx_one_char('\b');
  51. }
  52. } else if (len > 0 && write_index < len - 1 && !iscontrol(c)) {
  53. /* Write a max of len - 1 characters to allow for null terminator */
  54. unity_putc(c);
  55. dst[write_index++] = c;
  56. }
  57. }
  58. }
  59. /* To start a unit test from a GDB session without console input,
  60. * - set a break at unity_gets ('hb unity_gets')
  61. * - resume the program ('c')
  62. * - modify this value to the desired command line ('set {char[64]} unity_input_from_gdb = "5"')
  63. * - optionally set a break point in the test being debugged
  64. * - resume the program ('c')
  65. */
  66. char unity_input_from_gdb[64];
  67. void unity_gets(char *dst, size_t len)
  68. {
  69. size_t unity_input_from_gdb_len = strlen(unity_input_from_gdb);
  70. if (unity_input_from_gdb_len > 0 && unity_input_from_gdb_len < len - 1) {
  71. memcpy(dst, unity_input_from_gdb, unity_input_from_gdb_len);
  72. dst[unity_input_from_gdb_len] = '\n';
  73. dst[unity_input_from_gdb_len + 1] = 0;
  74. memset(unity_input_from_gdb, 0, sizeof(unity_input_from_gdb));
  75. return;
  76. }
  77. /* Flush anything already in the RX buffer */
  78. uint8_t ignore;
  79. while (esp_rom_uart_rx_one_char(&ignore) == 0) { }
  80. /* Read input */
  81. esp_unity_readline(dst, len);
  82. }
  83. void unity_exec_time_start(void)
  84. {
  85. s_test_start = esp_cpu_get_cycle_count();
  86. }
  87. void unity_exec_time_stop(void)
  88. {
  89. s_test_stop = esp_cpu_get_cycle_count();
  90. }
  91. uint32_t unity_exec_time_get_ms(void)
  92. {
  93. return (s_test_stop - s_test_start) / (esp_clk_cpu_freq() / 1000);
  94. }