git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@3031 35acf78f-673a-0410-8e92-d51de3d6d3f4

This commit is contained in:
gdisirio 2011-06-06 09:33:47 +00:00
parent 079f323649
commit ed8a14e688
9 changed files with 124 additions and 102 deletions

View File

@ -150,7 +150,7 @@ CPPWARN = -Wall -Wextra
# #
# List all default C defines here, like -D_DEBUG=1 # 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 # List all default ASM defines here, like -D_DEBUG=1
DADEFS = DADEFS =

View File

@ -435,8 +435,6 @@
#define THREAD_EXT_FIELDS \ #define THREAD_EXT_FIELDS \
struct { \ struct { \
/* Add threads custom fields here.*/ \ /* Add threads custom fields here.*/ \
/* Space for the LWIP sys_timeouts structure.*/ \
void *p_lwipspace[1]; \
}; };
#endif #endif
@ -450,7 +448,6 @@ struct { \
#if !defined(THREAD_EXT_INIT_HOOK) || defined(__DOXYGEN__) #if !defined(THREAD_EXT_INIT_HOOK) || defined(__DOXYGEN__)
#define THREAD_EXT_INIT_HOOK(tp) { \ #define THREAD_EXT_INIT_HOOK(tp) { \
/* Add threads initialization code here.*/ \ /* Add threads initialization code here.*/ \
(tp)->p_lwipspace[0] = NULL; \
} }
#endif #endif

View File

@ -71,5 +71,14 @@ typedef uint32_t mem_ptr_t;
} }
#define BYTE_ORDER LITTLE_ENDIAN #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__ */ #endif /* __CC_H__ */

View File

@ -52,6 +52,8 @@
* *
*/ */
// see http://lwip.wikia.com/wiki/Porting_for_an_OS for instructions
#include "ch.h" #include "ch.h"
#include "lwip/opt.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)); *sem = chHeapAlloc(NULL, sizeof(Semaphore));
if (sem == 0) { if (*sem == 0) {
SYS_STATS_INC(sem.err); SYS_STATS_INC(sem.err);
return ERR_MEM;
} }
else { else {
chSemInit(sem, (cnt_t)count); chSemInit(*sem, (cnt_t)count);
SYS_STATS_INC(sem.used); 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); 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; systime_t time, tmo;
chSysLock(); chSysLock();
tmo = timeout > 0 ? (systime_t)timeout : TIME_INFINITE; tmo = timeout > 0 ? (systime_t)timeout : TIME_INFINITE;
time = chTimeNow(); time = chTimeNow();
if (chSemWaitTimeoutS(sem, tmo) != RDY_OK) if (chSemWaitTimeoutS(*sem, tmo) != RDY_OK)
time = SYS_ARCH_TIMEOUT; time = SYS_ARCH_TIMEOUT;
else else
time = chTimeNow() - time; time = chTimeNow() - time;
@ -104,44 +108,65 @@ u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout) {
return time; return time;
} }
sys_mbox_t sys_mbox_new(int size) { int sys_sem_valid(sys_sem_t *sem) {
return *sem != SYS_SEM_NULL;
sys_mbox_t mbox = chHeapAlloc(NULL, sizeof(Mailbox) + sizeof(msg_t) * size);
if (mbox == 0) {
SYS_STATS_INC(mbox.err);
}
else {
chMBInit(mbox, (void *)(((uint8_t *)mbox) + sizeof(Mailbox)), size);
SYS_STATS_INC(mbox.used);
}
return mbox;
} }
void sys_mbox_free(sys_mbox_t mbox) { // 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;
}
chHeapFree(mbox); err_t sys_mbox_new(sys_mbox_t *mbox, int size) {
*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);
SYS_STATS_INC(mbox.used);
return ERR_OK;
}
}
void sys_mbox_free(sys_mbox_t *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); 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_MEM;
}
return ERR_OK; 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; systime_t time, tmo;
chSysLock(); chSysLock();
tmo = timeout > 0 ? (systime_t)timeout : TIME_INFINITE; tmo = timeout > 0 ? (systime_t)timeout : TIME_INFINITE;
time = chTimeNow(); time = chTimeNow();
if (chMBFetchS(mbox, (msg_t *)msg, tmo) != RDY_OK) if (chMBFetchS(*mbox, (msg_t *)msg, tmo) != RDY_OK)
time = SYS_ARCH_TIMEOUT; time = SYS_ARCH_TIMEOUT;
else else
time = chTimeNow() - time; time = chTimeNow() - time;
@ -149,19 +174,24 @@ u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout) {
return time; 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 SYS_MBOX_EMPTY;
return 0; 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;
} }
sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), // 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(const char *name, lwip_thread_fn thread,
void *arg, int stacksize, int prio) { void *arg, int stacksize, int prio) {
(void)name; (void)name;
size_t wsz = THD_WA_SIZE(stacksize); size_t wsz = THD_WA_SIZE(stacksize);

View File

@ -66,28 +66,7 @@ typedef int sys_prot_t;
#define SYS_THREAD_NULL (Thread *)0 #define SYS_THREAD_NULL (Thread *)0
#define SYS_SEM_NULL (Semaphore *)0 #define SYS_SEM_NULL (Semaphore *)0
void sys_init(void); /* let sys.h use binary semaphores for mutexes */
sys_sem_t sys_sem_new(u8_t count); #define LWIP_COMPAT_MUTEX 1
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);
#endif /* __SYS_ARCH_H__ */ #endif /* __SYS_ARCH_H__ */

View File

@ -1,44 +1,47 @@
# List of the required lwIP files. # List of the required lwIP files.
LWIP = ${CHIBIOS}/ext/lwip-1.4.0
LWNETIFSRC = \ LWNETIFSRC = \
${CHIBIOS}/ext/lwip/src/netif/etharp.c \ ${LWIP}/src/netif/etharp.c
${CHIBIOS}/ext/lwip/src/netif/loopif.c
LWCORESRC = \ LWCORESRC = \
${CHIBIOS}/ext/lwip/src/core/dhcp.c \ ${LWIP}/src/core/dhcp.c \
${CHIBIOS}/ext/lwip/src/core/dns.c \ ${LWIP}/src/core/dns.c \
${CHIBIOS}/ext/lwip/src/core/init.c \ ${LWIP}/src/core/init.c \
${CHIBIOS}/ext/lwip/src/core/mem.c \ ${LWIP}/src/core/mem.c \
${CHIBIOS}/ext/lwip/src/core/memp.c \ ${LWIP}/src/core/memp.c \
${CHIBIOS}/ext/lwip/src/core/netif.c \ ${LWIP}/src/core/netif.c \
${CHIBIOS}/ext/lwip/src/core/pbuf.c \ ${LWIP}/src/core/pbuf.c \
${CHIBIOS}/ext/lwip/src/core/raw.c \ ${LWIP}/src/core/raw.c \
${CHIBIOS}/ext/lwip/src/core/stats.c \ ${LWIP}/src/core/stats.c \
${CHIBIOS}/ext/lwip/src/core/sys.c \ ${LWIP}/src/core/sys.c \
${CHIBIOS}/ext/lwip/src/core/tcp.c \ ${LWIP}/src/core/tcp.c \
${CHIBIOS}/ext/lwip/src/core/tcp_in.c \ ${LWIP}/src/core/tcp_in.c \
${CHIBIOS}/ext/lwip/src/core/tcp_out.c \ ${LWIP}/src/core/tcp_out.c \
${CHIBIOS}/ext/lwip/src/core/udp.c ${LWIP}/src/core/udp.c
LWIPV4SRC = \ LWIPV4SRC = \
${CHIBIOS}/ext/lwip/src/core/ipv4/autoip.c \ ${LWIP}/src/core/ipv4/autoip.c \
${CHIBIOS}/ext/lwip/src/core/ipv4/icmp.c \ ${LWIP}/src/core/ipv4/icmp.c \
${CHIBIOS}/ext/lwip/src/core/ipv4/igmp.c \ ${LWIP}/src/core/ipv4/igmp.c \
${CHIBIOS}/ext/lwip/src/core/ipv4/inet.c \ ${LWIP}/src/core/ipv4/inet.c \
${CHIBIOS}/ext/lwip/src/core/ipv4/inet_chksum.c \ ${LWIP}/src/core/ipv4/inet_chksum.c \
${CHIBIOS}/ext/lwip/src/core/ipv4/ip.c \ ${LWIP}/src/core/ipv4/ip.c \
${CHIBIOS}/ext/lwip/src/core/ipv4/ip_addr.c \ ${LWIP}/src/core/ipv4/ip_addr.c \
${CHIBIOS}/ext/lwip/src/core/ipv4/ip_frag.c ${LWIP}/src/core/ipv4/ip_frag.c \
${LWIP}/src/core/def.c \
${LWIP}/src/core/timers.c
LWAPISRC = \ LWAPISRC = \
${CHIBIOS}/ext/lwip/src/api/api_lib.c \ ${LWIP}/src/api/api_lib.c \
${CHIBIOS}/ext/lwip/src/api/api_msg.c \ ${LWIP}/src/api/api_msg.c \
${CHIBIOS}/ext/lwip/src/api/err.c \ ${LWIP}/src/api/err.c \
${CHIBIOS}/ext/lwip/src/api/netbuf.c \ ${LWIP}/src/api/netbuf.c \
${CHIBIOS}/ext/lwip/src/api/netdb.c \ ${LWIP}/src/api/netdb.c \
${CHIBIOS}/ext/lwip/src/api/netifapi.c \ ${LWIP}/src/api/netifapi.c \
${CHIBIOS}/ext/lwip/src/api/sockets.c \ ${LWIP}/src/api/sockets.c \
${CHIBIOS}/ext/lwip/src/api/tcpip.c ${LWIP}/src/api/tcpip.c
LWINC = \ LWINC = \
${CHIBIOS}/ext/lwip/src/include \ ${LWIP}/src/include \
${CHIBIOS}/ext/lwip/src/include/ipv4 ${LWIP}/src/include/ipv4

View File

@ -47,12 +47,13 @@ static void http_server_serve(struct netconn *conn) {
struct netbuf *inbuf; struct netbuf *inbuf;
char *buf; char *buf;
u16_t buflen; u16_t buflen;
err_t err;
/* Read the data from the port, blocking if nothing yet there. /* Read the data from the port, blocking if nothing yet there.
We assume the request (the part we care about) is in one netbuf */ 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); netbuf_data(inbuf, (void **)&buf, &buflen);
/* Is this an HTTP GET command? (only check the first 5 chars, since /* 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) { msg_t http_server(void *p) {
struct netconn *conn, *newconn; struct netconn *conn, *newconn;
err_t err;
(void)p; (void)p;
@ -109,7 +111,9 @@ msg_t http_server(void *p) {
chThdSetPriority(WEB_THREAD_PRIORITY); chThdSetPriority(WEB_THREAD_PRIORITY);
while(1) { while(1) {
newconn = netconn_accept(conn); err = netconn_accept(conn, &newconn);
if (err != ERR_OK)
continue;
http_server_serve(newconn); http_server_serve(newconn);
netconn_delete(newconn); netconn_delete(newconn);
} }

BIN
ext/lwip-1.4.0.zip Normal file

Binary file not shown.

View File

@ -10,7 +10,7 @@ instructions contained in the various distributions.
The currently included items are: The currently included items are:
1. uip-1.0, a minimal TCP/IP implementation: http://www.sics.se/~adam/uip/ 1. uip-1.0, a minimal TCP/IP implementation: http://www.sics.se/~adam/uip/
2. lwip-1.3.1, lightweight TCP/IP stack: http://savannah.nongnu.org/projects/lwip/ 2. lwip-1.4.0, lightweight TCP/IP stack: http://savannah.nongnu.org/projects/lwip/
3. STM32 firmware library 3.3.0 (partial, library only) the full download is 3. STM32 firmware library 3.3.0 (partial, library only) the full download is
available from http://www.st.com available from http://www.st.com
4. FatFS 0.7e (patched), the original version is available from 4. FatFS 0.7e (patched), the original version is available from
@ -21,7 +21,7 @@ and without any modification, in order to use the libraries unpack them
under ./ext as: under ./ext as:
./ext/uip-1.0 ./ext/uip-1.0
./ext/lwip ./ext/lwip-1.4.0
./ext/stm32lib ./ext/stm32lib
./ext/fatfs ./ext/fatfs