From 8bca815cd29cdbee6d6b480e56f5787b0fc45b5f Mon Sep 17 00:00:00 2001 From: Ulrich Mohr Date: Thu, 28 Sep 2023 18:03:02 +0200 Subject: [PATCH] slip working with int driven rx --- bsp_z7/CMakeLists.txt | 1 + bsp_z7/lwip/CMakeLists.txt | 1 + bsp_z7/lwip/arch/cc.h | 10 +- bsp_z7/lwip/lwipopts.h | 13 ++- bsp_z7/lwip/sys_arch_protect.c | 66 +++++++++++++ .../libsrc/standalone/src/outbyte.c | 2 +- mission/main.cpp | 2 +- mission/testIp.cpp | 92 ++++++++++++++----- 8 files changed, 160 insertions(+), 27 deletions(-) create mode 100644 bsp_z7/lwip/CMakeLists.txt create mode 100644 bsp_z7/lwip/sys_arch_protect.c diff --git a/bsp_z7/CMakeLists.txt b/bsp_z7/CMakeLists.txt index 68d6496..11dc482 100644 --- a/bsp_z7/CMakeLists.txt +++ b/bsp_z7/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(freeRTOS) +add_subdirectory(lwip) add_subdirectory(ps7_cortexa9_0) add_subdirectory(objects) \ No newline at end of file diff --git a/bsp_z7/lwip/CMakeLists.txt b/bsp_z7/lwip/CMakeLists.txt new file mode 100644 index 0000000..b9ad746 --- /dev/null +++ b/bsp_z7/lwip/CMakeLists.txt @@ -0,0 +1 @@ +target_sources(lwip PRIVATE sys_arch_protect.c) \ No newline at end of file diff --git a/bsp_z7/lwip/arch/cc.h b/bsp_z7/lwip/arch/cc.h index ed502c5..0fb5a7e 100644 --- a/bsp_z7/lwip/arch/cc.h +++ b/bsp_z7/lwip/arch/cc.h @@ -20,4 +20,12 @@ #define PACK_STRUCT_END #define LWIP_PLATFORM_ASSERT(x) -#define LWIP_PLATFORM_DIAG(x) do { printf x; } while(0) \ No newline at end of file +#define LWIP_PLATFORM_DIAG(x) do { printf x; } while(0) + + +//overwrite protection function, we need ISR aware one, which the default FreeRTOS port does not support +#define SYS_ARCH_DECL_PROTECT(lev) uint32_t lev //caution, type is actually sys_prot_t, which is not available here. +#define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect_ca0() +#define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect_ca0(lev) +uint32_t sys_arch_protect_ca0(void); //caution, type is actually sys_prot_t, which is not available here. +void sys_arch_unprotect_ca0(uint32_t pval); //caution, type is actually sys_prot_t, which is not available here. \ No newline at end of file diff --git a/bsp_z7/lwip/lwipopts.h b/bsp_z7/lwip/lwipopts.h index f2006a4..8146618 100644 --- a/bsp_z7/lwip/lwipopts.h +++ b/bsp_z7/lwip/lwipopts.h @@ -328,9 +328,17 @@ a lot of data that needs to be copied, this should be set high. */ #endif /* LWIP_OPTTEST_FILE */ +// FreeRTOS defines +#define LWIP_FREERTOS_CHECK_CORE_LOCKING 1 + +// Disable slip task +#define SLIP_USE_RX_THREAD 0 +#define SLIP_RX_FROM_ISR 1 +#define SLIP_RX_QUEUE 1 + /* The following defines must be done even in OPTTEST mode: */ -#define LWIP_FREERTOS_CHECK_CORE_LOCKING 1 + void sys_check_core_locking(void); @@ -339,7 +347,6 @@ void sys_check_core_locking(void); void sys_mark_tcpip_thread(void); #define LWIP_MARK_TCPIP_THREAD() sys_mark_tcpip_thread() -// Disable slip task -#define SLIP_USE_RX_THREAD 0 + #endif /* LWIP_LWIPOPTS_H */ diff --git a/bsp_z7/lwip/sys_arch_protect.c b/bsp_z7/lwip/sys_arch_protect.c new file mode 100644 index 0000000..53cfc0c --- /dev/null +++ b/bsp_z7/lwip/sys_arch_protect.c @@ -0,0 +1,66 @@ +#include "lwip/sys.h" + +#include "FreeRTOS.h" +#include "task.h" + +extern volatile uint32_t ulPortInterruptNesting; + +#if SYS_LIGHTWEIGHT_PROT + +sys_prot_t +sys_arch_protect_ca0(void) +{ +#if LWIP_FREERTOS_SYS_ARCH_PROTECT_USES_MUTEX + BaseType_t ret; + LWIP_ASSERT("sys_arch_protect_mutex != NULL", sys_arch_protect_mutex != NULL); + + ret = xSemaphoreTakeRecursive(sys_arch_protect_mutex, portMAX_DELAY); + LWIP_ASSERT("sys_arch_protect failed to take the mutex", ret == pdTRUE); +#else /* LWIP_FREERTOS_SYS_ARCH_PROTECT_USES_MUTEX */ + if ( ulPortInterruptNesting == 0) { + taskENTER_CRITICAL(); + } else { + taskENTER_CRITICAL_FROM_ISR(); + } +#endif /* LWIP_FREERTOS_SYS_ARCH_PROTECT_USES_MUTEX */ +#if LWIP_FREERTOS_SYS_ARCH_PROTECT_SANITY_CHECK + { + /* every nested call to sys_arch_protect() returns an increased number */ + sys_prot_t ret = sys_arch_protect_nesting; + sys_arch_protect_nesting++; + LWIP_ASSERT("sys_arch_protect overflow", sys_arch_protect_nesting > ret); + return ret; + } +#else + return 1; +#endif +} + +void +sys_arch_unprotect_ca0(sys_prot_t pval) +{ +#if LWIP_FREERTOS_SYS_ARCH_PROTECT_USES_MUTEX + BaseType_t ret; +#endif +#if LWIP_FREERTOS_SYS_ARCH_PROTECT_SANITY_CHECK + LWIP_ASSERT("unexpected sys_arch_protect_nesting", sys_arch_protect_nesting > 0); + sys_arch_protect_nesting--; + LWIP_ASSERT("unexpected sys_arch_protect_nesting", sys_arch_protect_nesting == pval); +#endif + +#if LWIP_FREERTOS_SYS_ARCH_PROTECT_USES_MUTEX + LWIP_ASSERT("sys_arch_protect_mutex != NULL", sys_arch_protect_mutex != NULL); + + ret = xSemaphoreGiveRecursive(sys_arch_protect_mutex); + LWIP_ASSERT("sys_arch_unprotect failed to give the mutex", ret == pdTRUE); +#else /* LWIP_FREERTOS_SYS_ARCH_PROTECT_USES_MUTEX */ + if ( ulPortInterruptNesting == 0) { + taskEXIT_CRITICAL(); + } else { + taskEXIT_CRITICAL_FROM_ISR(0); + } +#endif /* LWIP_FREERTOS_SYS_ARCH_PROTECT_USES_MUTEX */ + LWIP_UNUSED_ARG(pval); +} + +#endif /* SYS_LIGHTWEIGHT_PROT */ \ No newline at end of file diff --git a/bsp_z7/ps7_cortexa9_0/libsrc/standalone/src/outbyte.c b/bsp_z7/ps7_cortexa9_0/libsrc/standalone/src/outbyte.c index c7a3b12..db60e6d 100644 --- a/bsp_z7/ps7_cortexa9_0/libsrc/standalone/src/outbyte.c +++ b/bsp_z7/ps7_cortexa9_0/libsrc/standalone/src/outbyte.c @@ -15,5 +15,5 @@ void outbyte(char c); #endif void outbyte(char c) { - //XUartPs_SendByte(STDOUT_BASEADDRESS, c); + XUartPs_SendByte(STDOUT_BASEADDRESS, c); } diff --git a/mission/main.cpp b/mission/main.cpp index 9889f5f..2b63a29 100644 --- a/mission/main.cpp +++ b/mission/main.cpp @@ -212,7 +212,7 @@ void mission(void *){ currPrio = 2; PeriodicTaskIF* controllerTask = taskFactory->createPeriodicTask( - "controller", currPrio, PeriodicTaskIF::MINIMUM_STACK_SIZE, 1, nullptr); + "controller", currPrio, 1024, 1, nullptr); controllerTask->addComponent(123); printf("Created Tasks\n"); diff --git a/mission/testIp.cpp b/mission/testIp.cpp index 6543e7d..1726787 100644 --- a/mission/testIp.cpp +++ b/mission/testIp.cpp @@ -9,8 +9,13 @@ #include #include +#include #include +#undef XUARTPS_IXR_RXOVR +#define XUARTPS_IXR_RXOVR 0x00000020U /**< Rx Overrun error interrupt */ +#define XUARTPS_IXR_RTRIG 0x00000001U /**< RX FIFO trigger interrupt. */ + extern "C" { void slipif_rxbyte_input(struct netif *netif, u8_t c); @@ -18,22 +23,41 @@ void myInitDone(void *arg) { puts("init done"); } struct netif netif; -void pollUart(void *) { +extern XScuGic xInterruptController; /* Interrupt controller instance */ + +/** this is based on XUartPs_InterruptHandler() in xuartps_intr.c*/ +void handleUARTInt(void *) { + XUartPs_SendByte(STDOUT_BASEADDRESS, 'I'); + + u32 IsrStatus; + + /* + * Read the interrupt ID register to determine which + * interrupt is active + */ + IsrStatus = XUartPs_ReadReg(STDIN_BASEADDRESS, XUARTPS_IMR_OFFSET); + + IsrStatus &= XUartPs_ReadReg(STDIN_BASEADDRESS, XUARTPS_ISR_OFFSET); + + // Onlx RX intterupts are enabled + // We do not care which interrupt actually triggered, just get all bytes + // available into the stack + u32 RecievedByte; + while (XUartPs_IsReceiveData(STDIN_BASEADDRESS)) { + RecievedByte = XUartPs_ReadReg(STDIN_BASEADDRESS, XUARTPS_FIFO_OFFSET); + slipif_received_byte(&netif, (u8)RecievedByte); + } + + /* Clear the interrupt status. */ + XUartPs_WriteReg(STDIN_BASEADDRESS, XUARTPS_ISR_OFFSET, IsrStatus); +} + +void forwardPackets(void *) { while (1) { - if (XUartPs_IsReceiveData(STDIN_BASEADDRESS)) { - u32 RecievedByte; - /* Wait until there is data */ - while (XUartPs_IsReceiveData(STDIN_BASEADDRESS)) { - RecievedByte = XUartPs_ReadReg(STDIN_BASEADDRESS, XUARTPS_FIFO_OFFSET); - slipif_rxbyte_input(&netif, (u8)RecievedByte); - } - } - vTaskDelay(pdMS_TO_TICKS(5)); + slipif_process_rxqueue(&netif); + vTaskDelay(pdMS_TO_TICKS(10)); } } -} - - uint32_t sio_data; @@ -41,6 +65,8 @@ sio_fd_t sio_open(u8_t devnum) { return &sio_data; } void sio_send(u8_t c, sio_fd_t fd) { XUartPs_SendByte(STDOUT_BASEADDRESS, c); } +} // extern "C" + void testIp() { tcpip_init(myInitDone, nullptr); @@ -59,14 +85,38 @@ void testIp() { vTaskDelay(pdMS_TO_TICKS(2000)); - xTaskCreate( - pollUart, /* The function that implements the task. */ - "uart", /* The text name assigned to the task - for debug only as it is not used by the - kernel. */ - 2048, /* The size of the stack to allocate to the task. */ - nullptr, /* The parameter passed to the task - not used in this simple case. */ - 1, /* The priority assigned to the task. */ - nullptr); + /* Install the UART Interrupt handler. */ + BaseType_t xStatus = + XScuGic_Connect(&xInterruptController, XPAR_XUARTPS_1_INTR, + (Xil_ExceptionHandler)handleUARTInt, nullptr); + configASSERT(xStatus == XST_SUCCESS); + (void)xStatus; /* Remove compiler warning if configASSERT() is not defined. */ + + // Set trigger level to 62 of 64 bytes, giving interrupt some time to react + XUartPs_WriteReg(STDIN_BASEADDRESS, XUARTPS_RXWM_OFFSET, 62); + + // Setting the rx timeout to n*4 -1 bits + XUartPs_WriteReg(STDIN_BASEADDRESS, XUARTPS_RXTOUT_OFFSET, 50); + + // enable UART Interrupts + u32 mask = XUARTPS_IXR_RTRIG | XUARTPS_IXR_RXOVR | XUARTPS_IXR_RXFULL | XUARTPS_IXR_TOUT; + /* Write the mask to the IER Register */ + XUartPs_WriteReg(STDIN_BASEADDRESS, XUARTPS_IER_OFFSET, mask); + /* Write the inverse of the Mask to the IDR register */ + XUartPs_WriteReg(STDIN_BASEADDRESS, XUARTPS_IDR_OFFSET, (~mask)); + + /* Enable the interrupt for the UART1 in the interrupt controller. */ + XScuGic_Enable(&xInterruptController, XPAR_XUARTPS_1_INTR); + + // Start task to forwad packets from ISR to IP Task + xTaskCreate(forwardPackets, /* The function that implements the task. */ + "slip forward", /* The text name assigned to the task - for debug + only as it is not used by the kernel. */ + 1024, /* The size of the stack to allocate to the task. */ + nullptr, /* The parameter passed to the task - not used in this + simple case. */ + 4, /* The priority assigned to the task. */ + nullptr); puts("socket");