| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | // Copyright 2016-2018 Espressif Systems (Shanghai) PTE LTD
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include "unity.h"
 | 
					
						
							|  |  |  | #include "sdkconfig.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | #include "soc/cpu.h"
 | 
					
						
							| 
									
										
										
										
											2020-11-06 15:00:07 +11:00
										 |  |  | #include "hal/cpu_hal.h"
 | 
					
						
							| 
									
										
										
										
											2020-07-13 21:33:23 +08:00
										 |  |  | #include "esp_rom_uart.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | #if CONFIG_IDF_TARGET_ESP32
 | 
					
						
							| 
									
										
										
										
											2019-03-18 15:46:15 +08:00
										 |  |  | #include "esp32/clk.h"
 | 
					
						
							| 
									
										
										
										
											2020-01-17 11:47:08 +08:00
										 |  |  | #elif CONFIG_IDF_TARGET_ESP32S2
 | 
					
						
							|  |  |  | #include "esp32s2/clk.h"
 | 
					
						
							| 
									
										
										
										
											2020-07-29 13:13:51 +08:00
										 |  |  | #elif CONFIG_IDF_TARGET_ESP32S3
 | 
					
						
							|  |  |  | #include "esp32s3/clk.h"
 | 
					
						
							| 
									
										
										
										
											2020-11-26 19:56:13 +11:00
										 |  |  | #elif CONFIG_IDF_TARGET_ESP32C3
 | 
					
						
							|  |  |  | #include "esp32c3/clk.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-13 21:33:23 +08:00
										 |  |  |     /* esp_rom_uart_rx_string length argument is uint8_t */ | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  |     if (len >= UINT8_MAX) { | 
					
						
							|  |  |  |         len = UINT8_MAX; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /* Flush anything already in the RX buffer */ | 
					
						
							|  |  |  |     uint8_t ignore; | 
					
						
							| 
									
										
										
										
											2020-07-13 21:33:23 +08:00
										 |  |  |     while (esp_rom_uart_rx_one_char(&ignore) == 0) { | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     /* Read input */ | 
					
						
							| 
									
										
										
										
											2020-07-13 21:33:23 +08:00
										 |  |  |     esp_rom_uart_rx_string((uint8_t *) dst, len); | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void unity_exec_time_start(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-06 15:00:07 +11:00
										 |  |  |     s_test_start = cpu_hal_get_cycle_count(); | 
					
						
							| 
									
										
										
										
											2018-10-25 12:52:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void unity_exec_time_stop(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-06 15:00:07 +11:00
										 |  |  |     s_test_stop = cpu_hal_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); | 
					
						
							|  |  |  | } |