test(lp-core): Added unit test for lp_core_printf

This commit adds a unit test for the lp_core_printf API.
This commit is contained in:
Sudeep Mohanty
2024-04-26 16:52:31 +02:00
parent be8a0568c5
commit 3cbe88427e
3 changed files with 144 additions and 0 deletions

View File

@@ -9,6 +9,7 @@
#include "test_shared.h" #include "test_shared.h"
#include "ulp_lp_core_utils.h" #include "ulp_lp_core_utils.h"
#include "ulp_lp_core_uart.h" #include "ulp_lp_core_uart.h"
#include "ulp_lp_core_print.h"
#define LP_UART_PORT_NUM LP_UART_NUM_0 #define LP_UART_PORT_NUM LP_UART_NUM_0
#define LP_UART_BUFFER_LEN UART_BUF_SIZE #define LP_UART_BUFFER_LEN UART_BUF_SIZE
@@ -25,6 +26,14 @@ uint8_t rx_data[LP_UART_BUFFER_LEN] = {};
volatile uint8_t tx_len = 0; volatile uint8_t tx_len = 0;
volatile uint8_t rx_len = 0; volatile uint8_t rx_len = 0;
/* LP Core print test variables */
volatile char test_string[25];
volatile char test_long_string[200];
volatile int test_signed_integer;
volatile uint32_t test_unsigned_integer;
volatile int test_hex;
volatile char test_character;
int main(void) int main(void)
{ {
while (1) { while (1) {
@@ -71,6 +80,18 @@ int main(void)
} }
} }
if (test_cmd == LP_CORE_LP_UART_PRINT_TEST) {
/* Write various cases to test lp_core_printf to test various format specifiers */
lp_core_printf("%s\r\n", test_string);
lp_core_printf("%s\r\n", test_long_string);
lp_core_printf("Test printf signed integer %d\r\n", test_signed_integer);
lp_core_printf("Test printf unsigned integer %u\r\n", test_unsigned_integer);
lp_core_printf("Test printf hex %x\r\n", test_hex);
lp_core_printf("Test printf character %c\r\n", test_character);
// TODO: Floating point prints are not supported
// lp_core_printf("Test printf float %f\r\n", (float)0.99);
}
/* Synchronize with the HP core running the test */ /* Synchronize with the HP core running the test */
test_cmd = LP_CORE_NO_COMMAND; test_cmd = LP_CORE_NO_COMMAND;
test_cmd_reply = LP_CORE_COMMAND_OK; test_cmd_reply = LP_CORE_COMMAND_OK;

View File

@@ -23,6 +23,7 @@ typedef enum {
LP_CORE_LP_UART_WRITE_TEST, LP_CORE_LP_UART_WRITE_TEST,
LP_CORE_LP_UART_READ_TEST, LP_CORE_LP_UART_READ_TEST,
LP_CORE_LP_UART_MULTI_BYTE_READ_TEST, LP_CORE_LP_UART_MULTI_BYTE_READ_TEST,
LP_CORE_LP_UART_PRINT_TEST,
LP_CORE_NO_COMMAND, LP_CORE_NO_COMMAND,
} lp_core_test_commands_t; } lp_core_test_commands_t;

View File

@@ -94,6 +94,12 @@ const uint8_t start_pattern[4] = {0xDE, 0xAD, 0xBE, 0xEF};
const uint8_t end_pattern[4] = {0xFE, 0xED, 0xBE, 0xEF}; const uint8_t end_pattern[4] = {0xFE, 0xED, 0xBE, 0xEF};
uint8_t expected_rx_data[UART_BUF_SIZE]; uint8_t expected_rx_data[UART_BUF_SIZE];
#define TEST_DATA_LEN 234 // Select a random number of bytes to transmit #define TEST_DATA_LEN 234 // Select a random number of bytes to transmit
char test_string[25];
char test_long_string[200];
int test_signed_integer;
uint32_t test_unsigned_integer;
int test_hex;
char test_character;
static void setup_test_data(uint8_t *tx_data, uint8_t *rx_data) static void setup_test_data(uint8_t *tx_data, uint8_t *rx_data)
{ {
@@ -118,6 +124,16 @@ static void setup_test_data(uint8_t *tx_data, uint8_t *rx_data)
} }
} }
static void setup_test_print_data(void)
{
strcpy(test_string, "Test printf string");
strcpy(test_long_string, "Print a very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string");
test_signed_integer = -77;
test_unsigned_integer = 1234567890;
test_hex = 0xa;
test_character = 'n';
}
static void hp_uart_read(void) static void hp_uart_read(void)
{ {
/* Configure HP UART driver */ /* Configure HP UART driver */
@@ -517,6 +533,110 @@ static void test_lp_uart_read_multi_byte(void)
TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_rx_data, rx_data + data_idx, TEST_DATA_LEN - 10); TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_rx_data, rx_data + data_idx, TEST_DATA_LEN - 10);
} }
static void hp_uart_read_print(void)
{
/* Configure HP UART driver */
uart_config_t hp_uart_cfg = {
.baud_rate = lp_uart_cfg.uart_proto_cfg.baud_rate,
.data_bits = lp_uart_cfg.uart_proto_cfg.data_bits,
.parity = lp_uart_cfg.uart_proto_cfg.parity,
.stop_bits = lp_uart_cfg.uart_proto_cfg.stop_bits,
.flow_ctrl = lp_uart_cfg.uart_proto_cfg.flow_ctrl,
.source_clk = UART_SCLK_DEFAULT,
};
int intr_alloc_flags = 0;
#if CONFIG_UART_ISR_IN_IRAM
intr_alloc_flags = ESP_INTR_FLAG_IRAM;
#endif
/* Install HP UART driver */
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_1, UART_BUF_SIZE, 0, 0, NULL, intr_alloc_flags));
ESP_ERROR_CHECK(uart_param_config(UART_NUM_1, &hp_uart_cfg));
/* Cross-connect the HP UART pins and the LP UART pins for the test */
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1, lp_uart_cfg.uart_pin_cfg.rx_io_num, lp_uart_cfg.uart_pin_cfg.tx_io_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
/* Notify the LP UART that the HP UART is initialized */
unity_send_signal("HP UART init done");
/* Setup test data */
setup_test_print_data();
/* Receive data from LP UART */
uint8_t rx_data[UART_BUF_SIZE];
int recv_idx = 0;
int idle_count = 0;
while (1) {
int bytes_received = uart_read_bytes(UART_NUM_1, rx_data + recv_idx, UART_BUF_SIZE, 10 / portTICK_PERIOD_MS);
if (bytes_received < 0) {
TEST_FAIL_MESSAGE("HP UART read error");
} else if (bytes_received > 0) {
recv_idx += bytes_received;
} else if (bytes_received == 0) {
idle_count++;
vTaskDelay(10);
if (idle_count > 10) {
break;
}
}
}
rx_data[UART_BUF_SIZE - 1] = '\0';
/* Parse the rx_data to verify the printed data */
/* Search for expected_string in rx_data. Report test pass if the string is found */
char expected_string[TEST_DATA_LEN];
snprintf(expected_string, TEST_DATA_LEN, "%s", test_string);
TEST_ASSERT_NOT_NULL(strstr((const char *)rx_data, expected_string));
snprintf(expected_string, TEST_DATA_LEN, "%s", test_long_string);
TEST_ASSERT_NOT_NULL(strstr((const char *)rx_data, expected_string));
snprintf(expected_string, TEST_DATA_LEN, "Test printf signed integer %d", test_signed_integer);
TEST_ASSERT_NOT_NULL(strstr((const char *)rx_data, expected_string));
snprintf(expected_string, TEST_DATA_LEN, "Test printf unsigned integer %lu", test_unsigned_integer);
TEST_ASSERT_NOT_NULL(strstr((const char *)rx_data, expected_string));
snprintf(expected_string, TEST_DATA_LEN, "Test printf hex %x", test_hex);
TEST_ASSERT_NOT_NULL(strstr((const char *)rx_data, expected_string));
snprintf(expected_string, TEST_DATA_LEN, "Test printf character %c", test_character);
TEST_ASSERT_NOT_NULL(strstr((const char *)rx_data, expected_string));
/* Uninstall the HP UART driver */
uart_driver_delete(UART_NUM_1);
vTaskDelay(1);
}
static void test_lp_uart_print(void)
{
/* Setup LP UART with default configuration */
TEST_ASSERT(ESP_OK == lp_core_uart_init(&lp_uart_cfg));
/* Wait for the HP UART device to be initialized */
unity_wait_for_signal("HP UART init done");
/* Load and Run the LP core firmware */
ulp_lp_core_cfg_t lp_cfg = {
.wakeup_source = ULP_LP_CORE_WAKEUP_SOURCE_HP_CPU,
};
load_and_start_lp_core_firmware(&lp_cfg, lp_core_main_uart_bin_start, lp_core_main_uart_bin_end);
/* Setup test data */
setup_test_print_data();
strcpy((char *)&ulp_test_string, test_string);
strcpy((char *)&ulp_test_long_string, test_long_string);
ulp_test_signed_integer = test_signed_integer;
ulp_test_unsigned_integer = test_unsigned_integer;
ulp_test_hex = test_hex;
ulp_test_character = test_character;
/* Start the test */
ESP_LOGI(TAG, "LP Core print test start");
ulp_test_cmd = LP_CORE_LP_UART_PRINT_TEST;
}
/* Test LP UART write operation with default LP UART initialization configuration */ /* Test LP UART write operation with default LP UART initialization configuration */
TEST_CASE_MULTIPLE_DEVICES("LP-Core LP-UART write test - default config", "[lp_core][test_env=generic_multi_device][timeout=150]", test_lp_uart_write, hp_uart_read); TEST_CASE_MULTIPLE_DEVICES("LP-Core LP-UART write test - default config", "[lp_core][test_env=generic_multi_device][timeout=150]", test_lp_uart_write, hp_uart_read);
/* Test LP UART write operation with updated LP UART initialization configuration */ /* Test LP UART write operation with updated LP UART initialization configuration */
@@ -527,3 +647,5 @@ TEST_CASE_MULTIPLE_DEVICES("LP-Core LP-UART read test - default config", "[lp_co
TEST_CASE_MULTIPLE_DEVICES("LP-Core LP-UART read test - optional config", "[lp_core][test_env=generic_multi_device][timeout=150]", test_lp_uart_read_options, hp_uart_write_options); TEST_CASE_MULTIPLE_DEVICES("LP-Core LP-UART read test - optional config", "[lp_core][test_env=generic_multi_device][timeout=150]", test_lp_uart_read_options, hp_uart_write_options);
/* Test LP UART multi-byte read operation */ /* Test LP UART multi-byte read operation */
TEST_CASE_MULTIPLE_DEVICES("LP-Core LP-UART multi-byte read test", "[lp_core][test_env=generic_multi_device][timeout=150]", test_lp_uart_read_multi_byte, hp_uart_write); TEST_CASE_MULTIPLE_DEVICES("LP-Core LP-UART multi-byte read test", "[lp_core][test_env=generic_multi_device][timeout=150]", test_lp_uart_read_multi_byte, hp_uart_write);
/* Test LP Core print */
TEST_CASE_MULTIPLE_DEVICES("LP-Core LP-UART print test", "[lp_core][test_env=generic_multi_device][timeout=150]", test_lp_uart_print, hp_uart_read_print);