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 "soc/cpu.h"
18 #include "hal/cpu_hal.h"
19 #include "esp_rom_uart.h"
20 #if CONFIG_IDF_TARGET_ESP32
21 #include "esp32/clk.h"
22 #elif CONFIG_IDF_TARGET_ESP32S2
23 #include "esp32s2/clk.h"
24 #elif CONFIG_IDF_TARGET_ESP32S3
25 #include "esp32s3/clk.h"
26 #elif CONFIG_IDF_TARGET_ESP32C3
27 #include "esp32c3/clk.h"
28 #elif CONFIG_IDF_TARGET_ESP32H2
29 #include "esp32h2/clk.h"
30 #endif
31 
32 static uint32_t s_test_start, s_test_stop;
33 
unity_putc(int c)34 void unity_putc(int c)
35 {
36     if (c == '\n') {
37         esp_rom_uart_tx_one_char('\r');
38         esp_rom_uart_tx_one_char('\n');
39     } else if (c == '\r') {
40     } else {
41         esp_rom_uart_tx_one_char(c);
42     }
43 }
44 
unity_flush(void)45 void unity_flush(void)
46 {
47     esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
48 }
49 
50 /* To start a unit test from a GDB session without console input,
51  * - set a break at unity_gets ('hb unity_gets')
52  * - resume the program ('c')
53  * - modify this value to the desired command line ('set {char[64]} unity_input_from_gdb = "5"')
54  * - optionally set a break point in the test being debugged
55  * - resume the program ('c')
56  */
57 char unity_input_from_gdb[64];
58 
unity_gets(char * dst,size_t len)59 void unity_gets(char *dst, size_t len)
60 {
61     size_t unity_input_from_gdb_len = strlen(unity_input_from_gdb);
62     if (unity_input_from_gdb_len > 0 && unity_input_from_gdb_len < len - 1) {
63         memcpy(dst, unity_input_from_gdb, unity_input_from_gdb_len);
64         dst[unity_input_from_gdb_len] = '\n';
65         dst[unity_input_from_gdb_len + 1] = 0;
66         memset(unity_input_from_gdb, 0, sizeof(unity_input_from_gdb));
67         return;
68     }
69     /* esp_rom_uart_rx_string length argument is uint8_t */
70     if (len >= UINT8_MAX) {
71         len = UINT8_MAX;
72     }
73     /* Flush anything already in the RX buffer */
74     uint8_t ignore;
75     while (esp_rom_uart_rx_one_char(&ignore) == 0) {
76     }
77     /* Read input */
78     esp_rom_uart_rx_string((uint8_t *) dst, len);
79 }
80 
unity_exec_time_start(void)81 void unity_exec_time_start(void)
82 {
83     s_test_start = cpu_hal_get_cycle_count();
84 }
85 
unity_exec_time_stop(void)86 void unity_exec_time_stop(void)
87 {
88     s_test_stop = cpu_hal_get_cycle_count();
89 }
90 
unity_exec_time_get_ms(void)91 uint32_t unity_exec_time_get_ms(void)
92 {
93     return (s_test_stop - s_test_start) / (esp_clk_cpu_freq() / 1000);
94 }
95