From ed8a14e6880b1ec6f065c6267d4ee86ae1cdb745 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Mon, 6 Jun 2011 09:33:47 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@3031 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/ARM7-AT91SAM7X-LWIP-GCC/Makefile | 2 +- demos/ARM7-AT91SAM7X-LWIP-GCC/chconf.h | 3 - demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/cc.h | 9 +++ demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/sys_arch.c | 88 +++++++++++++++------- demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/sys_arch.h | 25 +----- demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/lwip.mk | 71 ++++++++--------- demos/ARM7-AT91SAM7X-LWIP-GCC/web/web.c | 10 ++- 7 files changed, 115 insertions(+), 93 deletions(-) (limited to 'demos') diff --git a/demos/ARM7-AT91SAM7X-LWIP-GCC/Makefile b/demos/ARM7-AT91SAM7X-LWIP-GCC/Makefile index 4caf96ebf..b181251e7 100644 --- a/demos/ARM7-AT91SAM7X-LWIP-GCC/Makefile +++ b/demos/ARM7-AT91SAM7X-LWIP-GCC/Makefile @@ -150,7 +150,7 @@ CPPWARN = -Wall -Wextra # # List all default C defines here, like -D_DEBUG=1 -DDEFS = -DLWIP_PROVIDE_ERRNO +DDEFS = # List all default ASM defines here, like -D_DEBUG=1 DADEFS = diff --git a/demos/ARM7-AT91SAM7X-LWIP-GCC/chconf.h b/demos/ARM7-AT91SAM7X-LWIP-GCC/chconf.h index 496dcf2ab..202dfc511 100644 --- a/demos/ARM7-AT91SAM7X-LWIP-GCC/chconf.h +++ b/demos/ARM7-AT91SAM7X-LWIP-GCC/chconf.h @@ -435,8 +435,6 @@ #define THREAD_EXT_FIELDS \ struct { \ /* Add threads custom fields here.*/ \ - /* Space for the LWIP sys_timeouts structure.*/ \ - void *p_lwipspace[1]; \ }; #endif @@ -450,7 +448,6 @@ struct { \ #if !defined(THREAD_EXT_INIT_HOOK) || defined(__DOXYGEN__) #define THREAD_EXT_INIT_HOOK(tp) { \ /* Add threads initialization code here.*/ \ - (tp)->p_lwipspace[0] = NULL; \ } #endif diff --git a/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/cc.h b/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/cc.h index b90b447e2..ecac81e09 100644 --- a/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/cc.h +++ b/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/cc.h @@ -71,5 +71,14 @@ typedef uint32_t mem_ptr_t; } #define BYTE_ORDER LITTLE_ENDIAN +#define LWIP_PROVIDE_ERRNO + +#define PACK_STRUCT_BEGIN +#ifdef PACK_STRUCT_STRUCT +#undef PACK_STRUCT_STRUCT +#endif +#define PACK_STRUCT_STRUCT __attribute__ ((__packed__)) +#define PACK_STRUCT_END +#define PACK_STRUCT_FIELD(x) x #endif /* __CC_H__ */ diff --git a/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/sys_arch.c b/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/sys_arch.c index 082f2db4d..7e0212df7 100644 --- a/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/sys_arch.c +++ b/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/sys_arch.c @@ -52,6 +52,8 @@ * */ +// see http://lwip.wikia.com/wiki/Porting_for_an_OS for instructions + #include "ch.h" #include "lwip/opt.h" @@ -66,37 +68,39 @@ void sys_init(void) { } -sys_sem_t sys_sem_new(u8_t count) { +err_t sys_sem_new(sys_sem_t *sem, u8_t count) { - sys_sem_t sem = chHeapAlloc(NULL, sizeof(Semaphore)); - if (sem == 0) { + *sem = chHeapAlloc(NULL, sizeof(Semaphore)); + if (*sem == 0) { SYS_STATS_INC(sem.err); + return ERR_MEM; } else { - chSemInit(sem, (cnt_t)count); + chSemInit(*sem, (cnt_t)count); SYS_STATS_INC(sem.used); + return ERR_OK; } - return sem; } -void sys_sem_free(sys_sem_t sem) { +void sys_sem_free(sys_sem_t *sem) { - chHeapFree(sem); + chHeapFree(*sem); + *sem = SYS_SEM_NULL; SYS_STATS_DEC(sem.used); } -void sys_sem_signal(sys_sem_t sem) { +void sys_sem_signal(sys_sem_t *sem) { - chSemSignal(sem); + chSemSignal(*sem); } -u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout) { +u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) { systime_t time, tmo; chSysLock(); tmo = timeout > 0 ? (systime_t)timeout : TIME_INFINITE; time = chTimeNow(); - if (chSemWaitTimeoutS(sem, tmo) != RDY_OK) + if (chSemWaitTimeoutS(*sem, tmo) != RDY_OK) time = SYS_ARCH_TIMEOUT; else time = chTimeNow() - time; @@ -104,44 +108,65 @@ u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout) { return time; } -sys_mbox_t sys_mbox_new(int size) { +int sys_sem_valid(sys_sem_t *sem) { + return *sem != SYS_SEM_NULL; +} + +// typically called within lwIP after freeing a semaphore +// to make sure the pointer is not left pointing to invalid data +void sys_sem_set_invalid(sys_sem_t *sem) { + *sem = SYS_SEM_NULL; +} + +err_t sys_mbox_new(sys_mbox_t *mbox, int size) { - sys_mbox_t mbox = chHeapAlloc(NULL, sizeof(Mailbox) + sizeof(msg_t) * size); - if (mbox == 0) { + *mbox = chHeapAlloc(NULL, sizeof(Mailbox) + sizeof(msg_t) * size); + if (*mbox == 0) { SYS_STATS_INC(mbox.err); + return ERR_MEM; } else { - chMBInit(mbox, (void *)(((uint8_t *)mbox) + sizeof(Mailbox)), size); + chMBInit(*mbox, (void *)(((uint8_t *)*mbox) + sizeof(Mailbox)), size); SYS_STATS_INC(mbox.used); + return ERR_OK; } - return mbox; } -void sys_mbox_free(sys_mbox_t mbox) { +void sys_mbox_free(sys_mbox_t *mbox) { - chHeapFree(mbox); + if (chMBGetUsedCountI(*mbox) != 0) { + // If there are messages still present in the mailbox when the mailbox + // is deallocated, it is an indication of a programming error in lwIP + // and the developer should be notified. + SYS_STATS_INC(mbox.err); + chMBReset(*mbox); + } + chHeapFree(*mbox); + *mbox = SYS_MBOX_NULL; SYS_STATS_DEC(mbox.used); } -void sys_mbox_post(sys_mbox_t mbox, void *msg) { +void sys_mbox_post(sys_mbox_t *mbox, void *msg) { - chMBPost(mbox, (msg_t)msg, TIME_INFINITE); + chMBPost(*mbox, (msg_t)msg, TIME_INFINITE); } -err_t sys_mbox_trypost(sys_mbox_t mbox, void *msg) { +err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg) { - if (chMBPost(mbox, (msg_t)msg, TIME_IMMEDIATE) == RDY_TIMEOUT) + if (chMBPost(*mbox, (msg_t)msg, TIME_IMMEDIATE) == RDY_TIMEOUT) { + SYS_STATS_INC(mbox.err); return ERR_MEM; + } return ERR_OK; } -u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout) { +u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout) { systime_t time, tmo; chSysLock(); tmo = timeout > 0 ? (systime_t)timeout : TIME_INFINITE; time = chTimeNow(); - if (chMBFetchS(mbox, (msg_t *)msg, tmo) != RDY_OK) + if (chMBFetchS(*mbox, (msg_t *)msg, tmo) != RDY_OK) time = SYS_ARCH_TIMEOUT; else time = chTimeNow() - time; @@ -149,19 +174,24 @@ u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout) { return time; } -u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg) { +u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg) { - if (chMBFetch(mbox, (msg_t *)msg, TIME_IMMEDIATE) == RDY_TIMEOUT) + if (chMBFetch(*mbox, (msg_t *)msg, TIME_IMMEDIATE) == RDY_TIMEOUT) return SYS_MBOX_EMPTY; return 0; } -struct sys_timeouts *sys_arch_timeouts(void) { +int sys_mbox_valid(sys_mbox_t *mbox) { + return *mbox != SYS_MBOX_NULL; +} - return (struct sys_timeouts *)currp->p_lwipspace; +// typically called within lwIP after freeing an mbox +// to make sure the pointer is not left pointing to invalid data +void sys_mbox_set_invalid(sys_mbox_t *mbox) { + *mbox = SYS_MBOX_NULL; } -sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), +sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio) { (void)name; size_t wsz = THD_WA_SIZE(stacksize); diff --git a/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/sys_arch.h b/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/sys_arch.h index 229ff6638..5c287b243 100644 --- a/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/sys_arch.h +++ b/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/arch/sys_arch.h @@ -66,28 +66,7 @@ typedef int sys_prot_t; #define SYS_THREAD_NULL (Thread *)0 #define SYS_SEM_NULL (Semaphore *)0 -void sys_init(void); -sys_sem_t sys_sem_new(u8_t count); -void sys_sem_free(sys_sem_t sem); -void sys_sem_signal(sys_sem_t sem); -u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout); - -sys_mbox_t sys_mbox_new(int size); -void sys_mbox_free(sys_mbox_t mbox); -void sys_mbox_post(sys_mbox_t mbox, void *msg); -err_t sys_mbox_trypost(sys_mbox_t mbox, void *msg); -u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout); -u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg); - -struct sys_timeouts *sys_arch_timeouts(void); - -sys_thread_t sys_thread_new(char *name, - void (* thread)(void *arg), - void *arg, - int stacksize, - int prio); - -sys_prot_t sys_arch_protect(void); -void sys_arch_unprotect(sys_prot_t pval); +/* let sys.h use binary semaphores for mutexes */ +#define LWIP_COMPAT_MUTEX 1 #endif /* __SYS_ARCH_H__ */ diff --git a/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/lwip.mk b/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/lwip.mk index 99a046b9d..55a9bf7f4 100644 --- a/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/lwip.mk +++ b/demos/ARM7-AT91SAM7X-LWIP-GCC/lwip/lwip.mk @@ -1,44 +1,47 @@ # List of the required lwIP files. +LWIP = ${CHIBIOS}/ext/lwip-1.4.0 + LWNETIFSRC = \ - ${CHIBIOS}/ext/lwip/src/netif/etharp.c \ - ${CHIBIOS}/ext/lwip/src/netif/loopif.c + ${LWIP}/src/netif/etharp.c LWCORESRC = \ - ${CHIBIOS}/ext/lwip/src/core/dhcp.c \ - ${CHIBIOS}/ext/lwip/src/core/dns.c \ - ${CHIBIOS}/ext/lwip/src/core/init.c \ - ${CHIBIOS}/ext/lwip/src/core/mem.c \ - ${CHIBIOS}/ext/lwip/src/core/memp.c \ - ${CHIBIOS}/ext/lwip/src/core/netif.c \ - ${CHIBIOS}/ext/lwip/src/core/pbuf.c \ - ${CHIBIOS}/ext/lwip/src/core/raw.c \ - ${CHIBIOS}/ext/lwip/src/core/stats.c \ - ${CHIBIOS}/ext/lwip/src/core/sys.c \ - ${CHIBIOS}/ext/lwip/src/core/tcp.c \ - ${CHIBIOS}/ext/lwip/src/core/tcp_in.c \ - ${CHIBIOS}/ext/lwip/src/core/tcp_out.c \ - ${CHIBIOS}/ext/lwip/src/core/udp.c + ${LWIP}/src/core/dhcp.c \ + ${LWIP}/src/core/dns.c \ + ${LWIP}/src/core/init.c \ + ${LWIP}/src/core/mem.c \ + ${LWIP}/src/core/memp.c \ + ${LWIP}/src/core/netif.c \ + ${LWIP}/src/core/pbuf.c \ + ${LWIP}/src/core/raw.c \ + ${LWIP}/src/core/stats.c \ + ${LWIP}/src/core/sys.c \ + ${LWIP}/src/core/tcp.c \ + ${LWIP}/src/core/tcp_in.c \ + ${LWIP}/src/core/tcp_out.c \ + ${LWIP}/src/core/udp.c LWIPV4SRC = \ - ${CHIBIOS}/ext/lwip/src/core/ipv4/autoip.c \ - ${CHIBIOS}/ext/lwip/src/core/ipv4/icmp.c \ - ${CHIBIOS}/ext/lwip/src/core/ipv4/igmp.c \ - ${CHIBIOS}/ext/lwip/src/core/ipv4/inet.c \ - ${CHIBIOS}/ext/lwip/src/core/ipv4/inet_chksum.c \ - ${CHIBIOS}/ext/lwip/src/core/ipv4/ip.c \ - ${CHIBIOS}/ext/lwip/src/core/ipv4/ip_addr.c \ - ${CHIBIOS}/ext/lwip/src/core/ipv4/ip_frag.c + ${LWIP}/src/core/ipv4/autoip.c \ + ${LWIP}/src/core/ipv4/icmp.c \ + ${LWIP}/src/core/ipv4/igmp.c \ + ${LWIP}/src/core/ipv4/inet.c \ + ${LWIP}/src/core/ipv4/inet_chksum.c \ + ${LWIP}/src/core/ipv4/ip.c \ + ${LWIP}/src/core/ipv4/ip_addr.c \ + ${LWIP}/src/core/ipv4/ip_frag.c \ + ${LWIP}/src/core/def.c \ + ${LWIP}/src/core/timers.c LWAPISRC = \ - ${CHIBIOS}/ext/lwip/src/api/api_lib.c \ - ${CHIBIOS}/ext/lwip/src/api/api_msg.c \ - ${CHIBIOS}/ext/lwip/src/api/err.c \ - ${CHIBIOS}/ext/lwip/src/api/netbuf.c \ - ${CHIBIOS}/ext/lwip/src/api/netdb.c \ - ${CHIBIOS}/ext/lwip/src/api/netifapi.c \ - ${CHIBIOS}/ext/lwip/src/api/sockets.c \ - ${CHIBIOS}/ext/lwip/src/api/tcpip.c + ${LWIP}/src/api/api_lib.c \ + ${LWIP}/src/api/api_msg.c \ + ${LWIP}/src/api/err.c \ + ${LWIP}/src/api/netbuf.c \ + ${LWIP}/src/api/netdb.c \ + ${LWIP}/src/api/netifapi.c \ + ${LWIP}/src/api/sockets.c \ + ${LWIP}/src/api/tcpip.c LWINC = \ - ${CHIBIOS}/ext/lwip/src/include \ - ${CHIBIOS}/ext/lwip/src/include/ipv4 + ${LWIP}/src/include \ + ${LWIP}/src/include/ipv4 diff --git a/demos/ARM7-AT91SAM7X-LWIP-GCC/web/web.c b/demos/ARM7-AT91SAM7X-LWIP-GCC/web/web.c index bc8984459..3430fc5b6 100644 --- a/demos/ARM7-AT91SAM7X-LWIP-GCC/web/web.c +++ b/demos/ARM7-AT91SAM7X-LWIP-GCC/web/web.c @@ -47,12 +47,13 @@ static void http_server_serve(struct netconn *conn) { struct netbuf *inbuf; char *buf; u16_t buflen; + err_t err; /* Read the data from the port, blocking if nothing yet there. We assume the request (the part we care about) is in one netbuf */ - inbuf = netconn_recv(conn); + err = netconn_recv(conn, &inbuf); - if (netconn_err(conn) == ERR_OK) { + if (err == ERR_OK) { netbuf_data(inbuf, (void **)&buf, &buflen); /* Is this an HTTP GET command? (only check the first 5 chars, since @@ -92,6 +93,7 @@ WORKING_AREA(wa_http_server, WEB_THREAD_STACK_SIZE); */ msg_t http_server(void *p) { struct netconn *conn, *newconn; + err_t err; (void)p; @@ -109,7 +111,9 @@ msg_t http_server(void *p) { chThdSetPriority(WEB_THREAD_PRIORITY); while(1) { - newconn = netconn_accept(conn); + err = netconn_accept(conn, &newconn); + if (err != ERR_OK) + continue; http_server_serve(newconn); netconn_delete(newconn); } -- cgit v1.2.3