unity_port_esp32.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2016-2018 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 "unity.h"
  16. #include "sdkconfig.h"
  17. #include "rom/uart.h"
  18. #include "esp_clk.h"
  19. #include "soc/cpu.h"
  20. static uint32_t s_test_start, s_test_stop;
  21. void unity_putc(int c)
  22. {
  23. if (c == '\n') {
  24. uart_tx_one_char('\r');
  25. uart_tx_one_char('\n');
  26. } else if (c == '\r') {
  27. } else {
  28. uart_tx_one_char(c);
  29. }
  30. }
  31. void unity_flush()
  32. {
  33. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  34. }
  35. /* To start a unit test from a GDB session without console input,
  36. * - set a break at unity_gets ('hb unity_gets')
  37. * - resume the program ('c')
  38. * - modify this value to the desired command line ('set {char[64]} unity_input_from_gdb = "5"')
  39. * - optionally set a break point in the test being debugged
  40. * - resume the program ('c')
  41. */
  42. char unity_input_from_gdb[64];
  43. void unity_gets(char *dst, size_t len)
  44. {
  45. size_t unity_input_from_gdb_len = strlen(unity_input_from_gdb);
  46. if (unity_input_from_gdb_len > 0 && unity_input_from_gdb_len < len - 1) {
  47. memcpy(dst, unity_input_from_gdb, unity_input_from_gdb_len);
  48. dst[unity_input_from_gdb_len] = '\n';
  49. dst[unity_input_from_gdb_len + 1] = 0;
  50. memset(unity_input_from_gdb, 0, sizeof(unity_input_from_gdb));
  51. return;
  52. }
  53. /* UartRxString length argument is uint8_t */
  54. if (len >= UINT8_MAX) {
  55. len = UINT8_MAX;
  56. }
  57. /* Flush anything already in the RX buffer */
  58. uint8_t ignore;
  59. while (uart_rx_one_char(&ignore) == OK) {
  60. }
  61. /* Read input */
  62. UartRxString((uint8_t *) dst, len);
  63. }
  64. void unity_exec_time_start(void)
  65. {
  66. RSR(CCOUNT, s_test_start);
  67. }
  68. void unity_exec_time_stop(void)
  69. {
  70. RSR(CCOUNT, s_test_stop);
  71. }
  72. uint32_t unity_exec_time_get_ms(void)
  73. {
  74. return (s_test_stop - s_test_start) / (esp_clk_cpu_freq() / 1000);
  75. }