| 
									
										
										
										
											2021-11-06 17:31:05 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: Apache-2.0 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-02 13:39:19 +00:00
										 |  |  | #include <stdbool.h>
 | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | #include "unity.h"
 | 
					
						
							|  |  |  | #include "sdkconfig.h"
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:24:42 +08:00
										 |  |  | #include "esp_cpu.h"
 | 
					
						
							| 
									
										
										
										
											2020-07-13 21:33:23 +08:00
										 |  |  | #include "esp_rom_uart.h"
 | 
					
						
							| 
									
										
										
										
											2021-11-06 17:31:05 +08:00
										 |  |  | #include "esp_private/esp_clk.h"
 | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static uint32_t s_test_start, s_test_stop; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void unity_putc(int c) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (c == '\n') { | 
					
						
							| 
									
										
										
										
											2020-07-13 21:33:23 +08:00
										 |  |  |         esp_rom_uart_tx_one_char('\r'); | 
					
						
							|  |  |  |         esp_rom_uart_tx_one_char('\n'); | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  |     } else if (c == '\r') { | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-07-13 21:33:23 +08:00
										 |  |  |         esp_rom_uart_tx_one_char(c); | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | void unity_flush(void) | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-13 21:33:23 +08:00
										 |  |  |     esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM); | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-02 13:39:19 +00:00
										 |  |  | #define iscontrol(c) ((c) <= '\x1f' || (c) == '\x7f')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void esp_unity_readline(char* dst, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Read line from console with support for echoing and backspaces */ | 
					
						
							|  |  |  |     size_t write_index = 0; | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							|  |  |  |         char c = 0; | 
					
						
							|  |  |  |         bool got_char = esp_rom_uart_rx_one_char((uint8_t*)&c) == 0; | 
					
						
							|  |  |  |         if (!got_char) { | 
					
						
							|  |  |  |           continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (c == '\r' || c == '\n') { | 
					
						
							| 
									
										
										
										
											2022-02-03 10:09:32 +00:00
										 |  |  |             /* Add null terminator and return on newline */ | 
					
						
							| 
									
										
										
										
											2022-02-02 13:39:19 +00:00
										 |  |  |             unity_putc('\n'); | 
					
						
							|  |  |  |             dst[write_index] = '\0'; | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } else if (c == '\b') { | 
					
						
							|  |  |  |             if (write_index > 0) { | 
					
						
							| 
									
										
										
										
											2022-02-03 10:09:32 +00:00
										 |  |  |                 /* Delete previously entered character */ | 
					
						
							| 
									
										
										
										
											2022-02-02 13:39:19 +00:00
										 |  |  |                 write_index--; | 
					
						
							|  |  |  |                 esp_rom_uart_tx_one_char('\b'); | 
					
						
							|  |  |  |                 esp_rom_uart_tx_one_char(' '); | 
					
						
							|  |  |  |                 esp_rom_uart_tx_one_char('\b'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-02-03 10:09:32 +00:00
										 |  |  |         } else if (len > 0 && write_index < len - 1 && !iscontrol(c)) { | 
					
						
							|  |  |  |             /* Write a max of len - 1 characters to allow for null terminator */ | 
					
						
							| 
									
										
										
										
											2022-02-02 13:39:19 +00:00
										 |  |  |             unity_putc(c); | 
					
						
							|  |  |  |             dst[write_index++] = c; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 19:28:45 +08:00
										 |  |  | /* To start a unit test from a GDB session without console input,
 | 
					
						
							|  |  |  |  * - set a break at unity_gets ('hb unity_gets') | 
					
						
							|  |  |  |  * - resume the program ('c') | 
					
						
							|  |  |  |  * - modify this value to the desired command line ('set {char[64]} unity_input_from_gdb = "5"') | 
					
						
							|  |  |  |  * - optionally set a break point in the test being debugged | 
					
						
							|  |  |  |  * - resume the program ('c') | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | char unity_input_from_gdb[64]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | void unity_gets(char *dst, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-06 19:28:45 +08:00
										 |  |  |     size_t unity_input_from_gdb_len = strlen(unity_input_from_gdb); | 
					
						
							|  |  |  |     if (unity_input_from_gdb_len > 0 && unity_input_from_gdb_len < len - 1) { | 
					
						
							|  |  |  |         memcpy(dst, unity_input_from_gdb, unity_input_from_gdb_len); | 
					
						
							|  |  |  |         dst[unity_input_from_gdb_len] = '\n'; | 
					
						
							|  |  |  |         dst[unity_input_from_gdb_len + 1] = 0; | 
					
						
							|  |  |  |         memset(unity_input_from_gdb, 0, sizeof(unity_input_from_gdb)); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  |     /* Flush anything already in the RX buffer */ | 
					
						
							|  |  |  |     uint8_t ignore; | 
					
						
							| 
									
										
										
										
											2022-02-03 10:09:32 +00:00
										 |  |  |     while (esp_rom_uart_rx_one_char(&ignore) == 0) { } | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  |     /* Read input */ | 
					
						
							| 
									
										
										
										
											2022-02-02 13:39:19 +00:00
										 |  |  |     esp_unity_readline(dst, len); | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void unity_exec_time_start(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-21 19:24:42 +08:00
										 |  |  |     s_test_start = esp_cpu_get_cycle_count(); | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void unity_exec_time_stop(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-21 19:24:42 +08:00
										 |  |  |     s_test_stop = esp_cpu_get_cycle_count(); | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint32_t unity_exec_time_get_ms(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return (s_test_stop - s_test_start) / (esp_clk_cpu_freq() / 1000); | 
					
						
							|  |  |  | } |