unity_port_esp32.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "soc/cpu.h"
  10. #include "hal/cpu_hal.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. /* To start a unit test from a GDB session without console input,
  29. * - set a break at unity_gets ('hb unity_gets')
  30. * - resume the program ('c')
  31. * - modify this value to the desired command line ('set {char[64]} unity_input_from_gdb = "5"')
  32. * - optionally set a break point in the test being debugged
  33. * - resume the program ('c')
  34. */
  35. char unity_input_from_gdb[64];
  36. void unity_gets(char *dst, size_t len)
  37. {
  38. size_t unity_input_from_gdb_len = strlen(unity_input_from_gdb);
  39. if (unity_input_from_gdb_len > 0 && unity_input_from_gdb_len < len - 1) {
  40. memcpy(dst, unity_input_from_gdb, unity_input_from_gdb_len);
  41. dst[unity_input_from_gdb_len] = '\n';
  42. dst[unity_input_from_gdb_len + 1] = 0;
  43. memset(unity_input_from_gdb, 0, sizeof(unity_input_from_gdb));
  44. return;
  45. }
  46. /* esp_rom_uart_rx_string length argument is uint8_t */
  47. if (len >= UINT8_MAX) {
  48. len = UINT8_MAX;
  49. }
  50. /* Flush anything already in the RX buffer */
  51. uint8_t ignore;
  52. while (esp_rom_uart_rx_one_char(&ignore) == 0) {
  53. }
  54. /* Read input */
  55. esp_rom_uart_rx_string((uint8_t *) dst, len);
  56. }
  57. void unity_exec_time_start(void)
  58. {
  59. s_test_start = cpu_hal_get_cycle_count();
  60. }
  61. void unity_exec_time_stop(void)
  62. {
  63. s_test_stop = cpu_hal_get_cycle_count();
  64. }
  65. uint32_t unity_exec_time_get_ms(void)
  66. {
  67. return (s_test_stop - s_test_start) / (esp_clk_cpu_freq() / 1000);
  68. }