unity_port_esp32.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "unity.h"
  8. #include "sdkconfig.h"
  9. #include "hal/cpu_hal.h"
  10. #include "esp_rom_uart.h"
  11. #include "esp_private/esp_clk.h"
  12. static uint32_t s_test_start, s_test_stop;
  13. void unity_putc(int c)
  14. {
  15. if (c == '\n') {
  16. esp_rom_uart_tx_one_char('\r');
  17. esp_rom_uart_tx_one_char('\n');
  18. } else if (c == '\r') {
  19. } else {
  20. esp_rom_uart_tx_one_char(c);
  21. }
  22. }
  23. void unity_flush(void)
  24. {
  25. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  26. }
  27. /* To start a unit test from a GDB session without console input,
  28. * - set a break at unity_gets ('hb unity_gets')
  29. * - resume the program ('c')
  30. * - modify this value to the desired command line ('set {char[64]} unity_input_from_gdb = "5"')
  31. * - optionally set a break point in the test being debugged
  32. * - resume the program ('c')
  33. */
  34. char unity_input_from_gdb[64];
  35. void unity_gets(char *dst, size_t len)
  36. {
  37. size_t unity_input_from_gdb_len = strlen(unity_input_from_gdb);
  38. if (unity_input_from_gdb_len > 0 && unity_input_from_gdb_len < len - 1) {
  39. memcpy(dst, unity_input_from_gdb, unity_input_from_gdb_len);
  40. dst[unity_input_from_gdb_len] = '\n';
  41. dst[unity_input_from_gdb_len + 1] = 0;
  42. memset(unity_input_from_gdb, 0, sizeof(unity_input_from_gdb));
  43. return;
  44. }
  45. /* esp_rom_uart_rx_string length argument is uint8_t */
  46. if (len >= UINT8_MAX) {
  47. len = UINT8_MAX;
  48. }
  49. /* Flush anything already in the RX buffer */
  50. uint8_t ignore;
  51. while (esp_rom_uart_rx_one_char(&ignore) == 0) {
  52. }
  53. /* Read input */
  54. esp_rom_uart_rx_string((uint8_t *) dst, len);
  55. }
  56. void unity_exec_time_start(void)
  57. {
  58. s_test_start = cpu_hal_get_cycle_count();
  59. }
  60. void unity_exec_time_stop(void)
  61. {
  62. s_test_stop = cpu_hal_get_cycle_count();
  63. }
  64. uint32_t unity_exec_time_get_ms(void)
  65. {
  66. return (s_test_stop - s_test_start) / (esp_clk_cpu_freq() / 1000);
  67. }