thread safety in newlib, newer lwip, optimized ISR for UART, some more stuff

This commit is contained in:
Ulrich Mohr 2023-10-06 17:29:07 +02:00
parent 1ba3d9412d
commit 1eb406074c
14 changed files with 75 additions and 157 deletions

2
.gitmodules vendored
View File

@ -6,4 +6,4 @@
url = https://egit.irs.uni-stuttgart.de/fsfw/fsfw
[submodule "contrib/lwip"]
path = contrib/lwip
url = https://github.com/lwip-tcpip/lwip
url = https://git.savannah.gnu.org/git/lwip.git

View File

@ -103,8 +103,8 @@ add_subdirectory(${MISSION_PATH})
# ##############################################################################
# Add libraries for all sources.
target_link_libraries(lwip freertos_kernel)
target_link_libraries(fsfw PUBLIC freertos_kernel)
target_link_libraries(lwip PUBLIC freertos_kernel)
target_link_libraries(fsfw PUBLIC lwip)
target_link_libraries(${TARGET_NAME} PUBLIC fsfw lwip)
target_include_directories(

View File

@ -1,4 +1,3 @@
add_subdirectory(freeRTOS)
add_subdirectory(lwip)
add_subdirectory(ps7_cortexa9_0)
add_subdirectory(objects)

View File

@ -183,42 +183,9 @@ Zynq MPU. */
#define configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET ( -0xf00 )
#define configUNIQUE_INTERRUPT_PRIORITIES 32
/* Newlib support*/
#define configUSE_NEWLIB_REENTRANT 1 // Required for thread-safety of newlib sprintf, strtok, etc...
/****** Network configuration settings - only used when the lwIP example is
built. See the page that documents this demo on the http://www.FreeRTOS.org
website for more information. ***********************************************/
/* The priority for the task that unblocked by the MAC interrupt to process
received packets. */
#define configMAC_INPUT_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
/* The priority of the task that runs the lwIP stack. */
#define configLWIP_TASK_PRIORITY ( configMAX_PRIORITIES - 2 )
/* The priority of the task that uses lwIP sockets to provide a simple command
line interface. */
#define configCLI_TASK_PRIORITY ( tskIDLE_PRIORITY )
/* MAC address configuration. */
#define configMAC_ADDR0 0x00
#define configMAC_ADDR1 0x13
#define configMAC_ADDR2 0x14
#define configMAC_ADDR3 0x15
#define configMAC_ADDR4 0x15
#define configMAC_ADDR5 0x16
/* IP address configuration. */
#define configIP_ADDR0 172
#define configIP_ADDR1 25
#define configIP_ADDR2 218
#define configIP_ADDR3 200
/* Netmask configuration. */
#define configNET_MASK0 255
#define configNET_MASK1 255
#define configNET_MASK2 255
#define configNET_MASK3 0
#endif /* FREERTOS_CONFIG_H */

View File

@ -1 +0,0 @@
target_sources(lwip PRIVATE sys_arch_protect.c)

View File

@ -10,7 +10,9 @@
#define LWIP_TIMEVAL_PRIVATE 0
#include <sys/time.h>
#define LWIP_ERRNO_INCLUDE <errno.h>
// errno is a macro. If we define LWIP_ERRNO_INCLUDE to errno.h the preprocessor will replace it,
// breaking the include. Instead we supply a helper include which in turn includes errno.h
#define LWIP_ERRNO_INCLUDE <onrre.h>
#define LWIP_RAND rand
@ -22,10 +24,4 @@
#define LWIP_PLATFORM_ASSERT(x)
#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.
#define LWIP_FREERTOS_SYS_ARCH_PROTECT_USES_MUTEX 1

View File

@ -37,7 +37,7 @@
#else /* LWIP_OPTTEST_FILE */
#define LWIP_IPV4 1
#define LWIP_IPV6 0
#define LWIP_IPV6 1
#define NO_SYS 0
#define LWIP_SOCKET (NO_SYS==0)
@ -333,8 +333,6 @@ a lot of data that needs to be copied, this should be set high. */
// 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: */

2
bsp_z7/lwip/onrre.h Normal file
View File

@ -0,0 +1,2 @@
#pragma once
#include <errno.h>

View File

@ -1,66 +0,0 @@
#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 */

@ -1 +1 @@
Subproject commit 84fde1ebbfe35b3125fc2d89b8a456cbacf148e9
Subproject commit 0a0452b2c39bdd91e252aef045c115f88f6ca773

View File

@ -1 +1 @@
target_sources(${TARGET_NAME} PRIVATE main.cpp testIp.cpp controller/PrintController.cpp)
target_sources(${TARGET_NAME} PRIVATE main.cpp testIp.cpp malloc_lock.c controller/PrintController.cpp)

View File

@ -143,6 +143,7 @@ XScuWdt xWatchDogInstance;
other modules. */
XScuGic xInterruptController;
extern SemaphoreHandle_t malloc_mutex;
/*-----------------------------------------------------------*/
void mission(void *);
@ -154,6 +155,12 @@ int main(void) {
printf("Booting Software\n");
malloc_mutex = xSemaphoreCreateRecursiveMutex();
if (malloc_mutex == NULL) {
printf("Could not obtaiin malloc mutex, bye...\n");
exit(-1);
}
int taskParameters =0;
xTaskCreate(

19
mission/malloc_lock.c Normal file
View File

@ -0,0 +1,19 @@
#include "FreeRTOS.h"
#include "semphr.h"
SemaphoreHandle_t malloc_mutex = NULL;
void __malloc_lock(struct _reent *r) {
// if the mutex is not initialized yet, no task have been started either
if (malloc_mutex != NULL) {
xSemaphoreTakeRecursive(malloc_mutex, portMAX_DELAY);
}
}
void __malloc_unlock(struct _reent *r) {
// if the mutex is not initialized yet, no task have been started either
if (malloc_mutex != NULL) {
xSemaphoreGiveRecursive(malloc_mutex);
}
}

View File

@ -1,5 +1,6 @@
#include "FreeRTOS.h"
#include "lwip/sio.h"
#include "queue.h"
#include "task.h"
#include <arpa/inet.h>
#include <cstring>
@ -12,8 +13,6 @@
#include <xscugic.h>
#include <xuartps.h>
#undef XUARTPS_IXR_RXOVR
#define XUARTPS_IXR_RXOVR 0x00000020U /**< Rx Overrun error interrupt */
#define XUARTPS_IXR_RTRIG 0x00000001U /**< RX FIFO trigger interrupt. */
@ -21,51 +20,38 @@
#define UART_INT_NR XPAR_XUARTPS_0_INTR
extern "C" {
#include <lwip/apps/tftp_server.h>
#include <lwip/apps/tftp_server.h>
static void*
tftp_open(const char* fname, const char* mode, u8_t is_write)
{
void slipif_rxbyte_input(struct netif *netif, u8_t c);
static void *tftp_open(const char *fname, const char *mode, u8_t is_write) {
LWIP_UNUSED_ARG(mode);
return (void*)13;
return (void *)13;
}
static void
tftp_close(void* handle)
{}
static void tftp_close(void *handle) {}
static int
tftp_read(void* handle, void* buf, int bytes)
{
static int tftp_read(void *handle, void *buf, int bytes) {
memset(buf, 'x', bytes);
return bytes - 60;
}
static int
tftp_write(void* handle, struct pbuf* p)
{
return 0;
}
static int tftp_write(void *handle, struct pbuf *p) { return 0; }
/* For TFTP client only */
static void
tftp_error(void* handle, int err, const char* msg, int size)
{}
static void tftp_error(void *handle, int err, const char *msg, int size) {}
static const struct tftp_context tftp = {tftp_open, tftp_close, tftp_read,
tftp_write, tftp_error};
static const struct tftp_context tftp = {
tftp_open,
tftp_close,
tftp_read,
tftp_write,
tftp_error
};
void slipif_rxbyte_input(struct netif *netif, u8_t c);
void myInitDone(void *arg) { puts("init done"); }
struct netif netif;
QueueHandle_t uartIsrQueue;
extern XScuGic xInterruptController; /* Interrupt controller instance */
/** this is based on XUartPs_InterruptHandler() in xuartps_intr.c*/
@ -83,20 +69,28 @@ void handleUARTInt(void *) {
// Onlx RX intterupts are enabled
// We do not care which interrupt actually triggered, just get all bytes
// available into the stack
u32 RecievedByte;
uint8_t RecievedByte;
BaseType_t xHigherPriorityTaskWoken;
while (XUartPs_IsReceiveData(STDIN_BASEADDRESS)) {
RecievedByte = XUartPs_ReadReg(STDIN_BASEADDRESS, XUARTPS_FIFO_OFFSET);
slipif_received_byte(&netif, (u8)RecievedByte);
xQueueSendToBackFromISR(uartIsrQueue, &RecievedByte,
&xHigherPriorityTaskWoken);
}
/* Clear the interrupt status. */
XUartPs_WriteReg(STDIN_BASEADDRESS, XUARTPS_ISR_OFFSET, IsrStatus);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
void forwardPackets(void *) {
uint8_t byte;
BaseType_t result;
while (1) {
slipif_process_rxqueue(&netif);
vTaskDelay(pdMS_TO_TICKS(10));
result = xQueueReceive(uartIsrQueue, &byte, portMAX_DELAY);
if (result == pdTRUE) {
slipif_rxbyte_input(&netif, byte);
}
}
}
@ -110,11 +104,13 @@ void sio_send(u8_t c, sio_fd_t fd) { XUartPs_SendByte(STDOUT_BASEADDRESS, c); }
void testIp() {
uartIsrQueue = xQueueCreate(512, 1); // TODO check
tcpip_init(myInitDone, nullptr);
ip_addr_t slip_addr = IPADDR4_INIT_BYTES(10, 0, 0, 32),
slip_mask = IPADDR4_INIT_BYTES(255, 255, 255, 0),
slip_gw = IPADDR4_INIT_BYTES(10, 25, 0, 1);
ip4_addr_t slip_addr = {PP_HTONL(LWIP_MAKEU32(10, 0, 0, 32))},
slip_mask = {PP_HTONL(LWIP_MAKEU32(255, 255, 255, 0))},
slip_gw = {PP_HTONL(LWIP_MAKEU32(10, 0, 0, 1))};
netifapi_netif_add(&netif, &slip_addr, &slip_mask, &slip_gw, NULL,
slipif_init, netif_input);
@ -140,7 +136,8 @@ void testIp() {
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;
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 */
@ -153,7 +150,7 @@ void testIp() {
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. */
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. */