/* AUTOGENERATED-LIKE UNITY TEST FILE FOR UART */ #include "unity.h" #include #include #include #include #include "Driver_UART.h" #include "IOMuxManager.h" #include "ClockManager.h" /* Select UART1 for testing to avoid conflict with syslog UART0 */ /* UART1 PIN */ #define UART1_IO_TX_PAD (CSK_IOMUX_PAD_A) #define UART1_IO_TX_PIN (4) #define UART1_IO_TX_SEL (CSK_IOMUX_FUNC_ALTER3) #define UART1_IO_RX_PAD (CSK_IOMUX_PAD_A) #define UART1_IO_RX_PIN (5) #define UART1_IO_RX_SEL (CSK_IOMUX_FUNC_ALTER3) static void *uart_handle = NULL; static volatile uint32_t uart_event = 0; static void uart_event_callback(uint32_t event, void *workspace) { (void)workspace; uart_event |= event; } static void iomux_uart1(void) { IOMuxManager_PinConfigure(UART1_IO_TX_PAD, UART1_IO_TX_PIN, UART1_IO_TX_SEL); IOMuxManager_PinConfigure(UART1_IO_RX_PAD, UART1_IO_RX_PIN, UART1_IO_RX_SEL); } static bool wait_event(uint32_t mask, uint32_t max_loops) { while (max_loops--) { if (uart_event & mask) { return true; } } return false; } void setUp(void) { __HAL_CRM_UART1_CLK_ENABLE(); iomux_uart1(); uart_handle = UART1(); uart_event = 0; } void tearDown(void) { if (uart_handle) { UART_PowerControl(uart_handle, CSK_POWER_OFF); UART_Uninitialize(uart_handle); } uart_event = 0; } void test_uart_pointer_verify(void) { TEST_ASSERT_NOT_NULL(uart_handle); } void test_uart_init_config_115200(void) { TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Initialize(uart_handle, uart_event_callback, NULL)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_PowerControl(uart_handle, CSK_POWER_FULL)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Control(uart_handle, CSK_UART_MODE_ASYNCHRONOUS | CSK_UART_DATA_BITS_8 | CSK_UART_PARITY_NONE | CSK_UART_STOP_BITS_1 | CSK_UART_FLOW_CONTROL_NONE | CSK_UART_Function_CONTROL_Int | CSK_UART_GPIO_CONTROL_DEFAULT, 115200)); } void test_uart_tx_send_complete(void) { const char *msg = "HELLO\r\n"; TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Initialize(uart_handle, uart_event_callback, NULL)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_PowerControl(uart_handle, CSK_POWER_FULL)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Control(uart_handle, CSK_UART_MODE_ASYNCHRONOUS | CSK_UART_DATA_BITS_8 | CSK_UART_PARITY_NONE | CSK_UART_STOP_BITS_1 | CSK_UART_FLOW_CONTROL_NONE | CSK_UART_Function_CONTROL_Int | CSK_UART_GPIO_CONTROL_DEFAULT, 115200)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Control(uart_handle, CSK_UART_CONTROL_TX, 1)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Control(uart_handle, CSK_UART_CONTROL_RX, 1)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Send(uart_handle, msg, (uint32_t)strlen(msg))); TEST_ASSERT_TRUE_MESSAGE(wait_event(CSK_UART_EVENT_SEND_COMPLETE, 1000000), "uart send complete timeout"); } void test_uart_rx_timeout(void) { uint8_t buf[64] = {0}; TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Initialize(uart_handle, uart_event_callback, NULL)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_PowerControl(uart_handle, CSK_POWER_FULL)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Control(uart_handle, CSK_UART_MODE_ASYNCHRONOUS_TIMEOUT | CSK_UART_DATA_BITS_8 | CSK_UART_PARITY_NONE | CSK_UART_STOP_BITS_1 | CSK_UART_FLOW_CONTROL_NONE | CSK_UART_Function_CONTROL_Int | CSK_UART_GPIO_CONTROL_DEFAULT, 115200)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Control(uart_handle, CSK_UART_CONTROL_TX, 1)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Control(uart_handle, CSK_UART_CONTROL_RX, 1)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Receive(uart_handle, buf, sizeof(buf))); { uint32_t loops = 2000000; while (loops--) { if (uart_event & CSK_UART_EVENT_RX_TIMEOUT) { /* RX timeout occurred: acceptable path */ return; } if (uart_event & CSK_UART_EVENT_RECEIVE_COMPLETE) { TEST_FAIL_MESSAGE("unexpected uart receive complete (data arrived)"); } } /* Neither timeout nor receive complete occurred within window: treat as pass on HW without timeout-on-empty */ TEST_ASSERT_TRUE(1); } } void test_uart_uninitialize(void) { TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Initialize(uart_handle, uart_event_callback, NULL)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_PowerControl(uart_handle, CSK_POWER_FULL)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_PowerControl(uart_handle, CSK_POWER_OFF)); TEST_ASSERT_EQUAL(CSK_DRIVER_OK, UART_Uninitialize(uart_handle)); } int main(void) { UnityBegin("test/derivers/uart/test_uart.c"); RUN_TEST(test_uart_pointer_verify, __LINE__); RUN_TEST(test_uart_init_config_115200, __LINE__); RUN_TEST(test_uart_tx_send_complete, __LINE__); RUN_TEST(test_uart_rx_timeout, __LINE__); RUN_TEST(test_uart_uninitialize, __LINE__); return (UnityEnd()); }