From 5894ef3bf7a80c1c05cf9e4ee18084f846c6c8e2 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sat, 9 Jun 2012 16:43:14 +0000 Subject: [PATCH] Fixed bug 3533887. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/stable_2.4.x@4262 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/various/lwip_bindings/arch/cc.h | 91 +++++++ os/various/lwip_bindings/arch/perf.h | 68 +++++ os/various/lwip_bindings/arch/sys_arch.c | 222 ++++++++++++++++ os/various/lwip_bindings/arch/sys_arch.h | 79 ++++++ os/various/lwip_bindings/lwip.mk | 54 ++++ os/various/lwip_bindings/lwipthread.c | 320 +++++++++++++++++++++++ os/various/lwip_bindings/lwipthread.h | 135 ++++++++++ os/various/lwip_bindings/readme.txt | 6 + readme.txt | 2 + 9 files changed, 977 insertions(+) create mode 100644 os/various/lwip_bindings/arch/cc.h create mode 100644 os/various/lwip_bindings/arch/perf.h create mode 100644 os/various/lwip_bindings/arch/sys_arch.c create mode 100644 os/various/lwip_bindings/arch/sys_arch.h create mode 100644 os/various/lwip_bindings/lwip.mk create mode 100644 os/various/lwip_bindings/lwipthread.c create mode 100644 os/various/lwip_bindings/lwipthread.h create mode 100644 os/various/lwip_bindings/readme.txt diff --git a/os/various/lwip_bindings/arch/cc.h b/os/various/lwip_bindings/arch/cc.h new file mode 100644 index 000000000..3a9356a0f --- /dev/null +++ b/os/various/lwip_bindings/arch/cc.h @@ -0,0 +1,91 @@ +/* + ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, + 2011,2012 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS/RT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + --- + + A special exception to the GPL can be applied should you wish to distribute + a combined work that includes ChibiOS/RT, without being obliged to provide + the source code for any proprietary components. See the file exception.txt + for full details of how and when the exception can be applied. +*/ +/* + * **** This file incorporates work covered by the following copyright and **** + * **** permission notice: **** + * + * Copyright (c) 2001-2004 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels + * + */ + +#ifndef __CC_H__ +#define __CC_H__ + +#include + +typedef uint8_t u8_t; +typedef int8_t s8_t; +typedef uint16_t u16_t; +typedef int16_t s16_t; +typedef uint32_t u32_t; +typedef int32_t s32_t; +typedef uint32_t mem_ptr_t; + +#define LWIP_PLATFORM_DIAG(x) +#define LWIP_PLATFORM_ASSERT(x) { \ + chSysHalt(); \ +} + +#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/os/various/lwip_bindings/arch/perf.h b/os/various/lwip_bindings/arch/perf.h new file mode 100644 index 000000000..244dffcab --- /dev/null +++ b/os/various/lwip_bindings/arch/perf.h @@ -0,0 +1,68 @@ +/* + ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, + 2011,2012 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS/RT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + --- + + A special exception to the GPL can be applied should you wish to distribute + a combined work that includes ChibiOS/RT, without being obliged to provide + the source code for any proprietary components. See the file exception.txt + for full details of how and when the exception can be applied. +*/ +/* + * **** This file incorporates work covered by the following copyright and **** + * **** permission notice: **** + * + * Copyright (c) 2001-2004 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels + * + */ + +#ifndef __PERF_H__ +#define __PERF_H__ + +#define PERF_START +#define PERF_STOP(x) + +#endif /* __PERF_H__ */ diff --git a/os/various/lwip_bindings/arch/sys_arch.c b/os/various/lwip_bindings/arch/sys_arch.c new file mode 100644 index 000000000..e9a844ac8 --- /dev/null +++ b/os/various/lwip_bindings/arch/sys_arch.c @@ -0,0 +1,222 @@ +/* + ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, + 2011,2012 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS/RT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + --- + + A special exception to the GPL can be applied should you wish to distribute + a combined work that includes ChibiOS/RT, without being obliged to provide + the source code for any proprietary components. See the file exception.txt + for full details of how and when the exception can be applied. +*/ +/* + * **** This file incorporates work covered by the following copyright and **** + * **** permission notice: **** + * + * Copyright (c) 2001-2004 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels + * + */ + +// see http://lwip.wikia.com/wiki/Porting_for_an_OS for instructions + +#include "ch.h" + +#include "lwip/opt.h" +#include "lwip/mem.h" +#include "lwip/sys.h" +#include "lwip/stats.h" + +#include "arch/cc.h" +#include "arch/sys_arch.h" + +void sys_init(void) { + +} + +err_t sys_sem_new(sys_sem_t *sem, u8_t count) { + + *sem = chHeapAlloc(NULL, sizeof(Semaphore)); + if (*sem == 0) { + SYS_STATS_INC(sem.err); + return ERR_MEM; + } + else { + chSemInit(*sem, (cnt_t)count); + SYS_STATS_INC(sem.used); + return ERR_OK; + } +} + +void sys_sem_free(sys_sem_t *sem) { + + chHeapFree(*sem); + *sem = SYS_SEM_NULL; + SYS_STATS_DEC(sem.used); +} + +void sys_sem_signal(sys_sem_t *sem) { + + chSemSignal(*sem); +} + +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) + time = SYS_ARCH_TIMEOUT; + else + time = chTimeNow() - time; + chSysUnlock(); + return time; +} + +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) { + + *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); +} + +void sys_mbox_post(sys_mbox_t *mbox, void *msg) { + + chMBPost(*mbox, (msg_t)msg, TIME_INFINITE); +} + +err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg) { + + 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) { + systime_t time, tmo; + + chSysLock(); + tmo = timeout > 0 ? (systime_t)timeout : TIME_INFINITE; + time = chTimeNow(); + if (chMBFetchS(*mbox, (msg_t *)msg, tmo) != RDY_OK) + time = SYS_ARCH_TIMEOUT; + else + time = chTimeNow() - time; + chSysUnlock(); + return time; +} + +u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg) { + + if (chMBFetch(*mbox, (msg_t *)msg, TIME_IMMEDIATE) == RDY_TIMEOUT) + return SYS_MBOX_EMPTY; + return 0; +} + +int sys_mbox_valid(sys_mbox_t *mbox) { + return *mbox != SYS_MBOX_NULL; +} + +// 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) { + + chRegSetThreadName(name); + size_t wsz = THD_WA_SIZE(stacksize); + void *wsp = chCoreAlloc(wsz); + if (wsp == NULL) + return NULL; + return (sys_thread_t)chThdCreateStatic(wsp, wsz, prio, (tfunc_t)thread, arg); +} + +sys_prot_t sys_arch_protect(void) { + + chSysLock(); + return 0; +} + +void sys_arch_unprotect(sys_prot_t pval) { + + (void)pval; + chSysUnlock(); +} diff --git a/os/various/lwip_bindings/arch/sys_arch.h b/os/various/lwip_bindings/arch/sys_arch.h new file mode 100644 index 000000000..43deb2757 --- /dev/null +++ b/os/various/lwip_bindings/arch/sys_arch.h @@ -0,0 +1,79 @@ +/* + ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, + 2011,2012 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS/RT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + --- + + A special exception to the GPL can be applied should you wish to distribute + a combined work that includes ChibiOS/RT, without being obliged to provide + the source code for any proprietary components. See the file exception.txt + for full details of how and when the exception can be applied. +*/ +/* + * **** This file incorporates work covered by the following copyright and **** + * **** permission notice: **** + * + * Copyright (c) 2001-2004 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels + * + */ + +#include + +#ifndef __SYS_ARCH_H__ +#define __SYS_ARCH_H__ + +typedef Semaphore * sys_sem_t; +typedef Mailbox * sys_mbox_t; +typedef Thread * sys_thread_t; +typedef int sys_prot_t; + +#define SYS_MBOX_NULL (Mailbox *)0 +#define SYS_THREAD_NULL (Thread *)0 +#define SYS_SEM_NULL (Semaphore *)0 + +/* let sys.h use binary semaphores for mutexes */ +#define LWIP_COMPAT_MUTEX 1 + +#endif /* __SYS_ARCH_H__ */ diff --git a/os/various/lwip_bindings/lwip.mk b/os/various/lwip_bindings/lwip.mk new file mode 100644 index 000000000..1f56b698d --- /dev/null +++ b/os/various/lwip_bindings/lwip.mk @@ -0,0 +1,54 @@ +# List of the required lwIP files. +LWIP = ${CHIBIOS}/ext/lwip-1.4.0 + +LWBINDSRC = \ + $(CHIBIOS)/os/various/lwip_bindings/lwipthread.c \ + $(CHIBIOS)/os/various/lwip_bindings/arch/sys_arch.c + +LWNETIFSRC = \ + ${LWIP}/src/netif/etharp.c + +LWCORESRC = \ + ${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 = \ + ${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 = \ + ${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 + +LWSRC = $(LWBINDSRC) $(LWNETIFSRC) $(LWCORESRC) $(LWIPV4SRC) $(LWAPISRC) + +LWINC = \ + $(CHIBIOS)/os/various/lwip_bindings \ + ${LWIP}/src/include \ + ${LWIP}/src/include/ipv4 diff --git a/os/various/lwip_bindings/lwipthread.c b/os/various/lwip_bindings/lwipthread.c new file mode 100644 index 000000000..87e8cd895 --- /dev/null +++ b/os/various/lwip_bindings/lwipthread.c @@ -0,0 +1,320 @@ +/* + ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, + 2011,2012 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS/RT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + --- + + A special exception to the GPL can be applied should you wish to distribute + a combined work that includes ChibiOS/RT, without being obliged to provide + the source code for any proprietary components. See the file exception.txt + for full details of how and when the exception can be applied. +*/ +/* + * **** This file incorporates work covered by the following copyright and **** + * **** permission notice: **** + * + * Copyright (c) 2001-2004 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels + * + */ + +/** + * @file lwipthread.c + * @brief LWIP wrapper thread code. + * @addtogroup LWIP_THREAD + * @{ + */ + +#include "ch.h" +#include "hal.h" +#include "evtimer.h" + +#include "lwip/opt.h" + +#include "lwip/def.h" +#include "lwip/mem.h" +#include "lwip/pbuf.h" +#include "lwip/sys.h" +#include +#include +#include +#include "netif/etharp.h" +#include "netif/ppp_oe.h" + +#include "lwipthread.h" + +#define PERIODIC_TIMER_ID 1 +#define FRAME_RECEIVED_ID 2 + +/** + * Stack area for the LWIP-MAC thread. + */ +WORKING_AREA(wa_lwip_thread, LWIP_THREAD_STACK_SIZE); + +/* + * Initialization. + */ +static void low_level_init(struct netif *netif) { + /* set MAC hardware address length */ + netif->hwaddr_len = ETHARP_HWADDR_LEN; + + /* maximum transfer unit */ + netif->mtu = 1500; + + /* device capabilities */ + /* don't set NETIF_FLAG_ETHARP if this device is not an Ethernet one */ + netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; + + /* Do whatever else is needed to initialize interface. */ +} + +/* + * Transmits a frame. + */ +static err_t low_level_output(struct netif *netif, struct pbuf *p) { + struct pbuf *q; + MACTransmitDescriptor td; + + (void)netif; + if (macWaitTransmitDescriptor(ÐD1, &td, MS2ST(LWIP_SEND_TIMEOUT)) != RDY_OK) + return ERR_TIMEOUT; + +#if ETH_PAD_SIZE + pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ +#endif + + /* Iterates through the pbuf chain. */ + for(q = p; q != NULL; q = q->next) + macWriteTransmitDescriptor(&td, (uint8_t *)q->payload, (size_t)q->len); + macReleaseTransmitDescriptor(&td); + +#if ETH_PAD_SIZE + pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ +#endif + + LINK_STATS_INC(link.xmit); + + return ERR_OK; +} + +/* + * Receives a frame. + */ +static struct pbuf *low_level_input(struct netif *netif) { + MACReceiveDescriptor rd; + struct pbuf *p, *q; + u16_t len; + + (void)netif; + if (macWaitReceiveDescriptor(ÐD1, &rd, TIME_IMMEDIATE) == RDY_OK) { + len = (u16_t)rd.size; + +#if ETH_PAD_SIZE + len += ETH_PAD_SIZE; /* allow room for Ethernet padding */ +#endif + + /* We allocate a pbuf chain of pbufs from the pool. */ + p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); + + if (p != NULL) { + +#if ETH_PAD_SIZE + pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ +#endif + + /* Iterates through the pbuf chain. */ + for(q = p; q != NULL; q = q->next) + macReadReceiveDescriptor(&rd, (uint8_t *)q->payload, (size_t)q->len); + macReleaseReceiveDescriptor(&rd); + +#if ETH_PAD_SIZE + pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ +#endif + + LINK_STATS_INC(link.recv); + } + else { + macReleaseReceiveDescriptor(&rd); + LINK_STATS_INC(link.memerr); + LINK_STATS_INC(link.drop); + } + return p; + } + return NULL; +} + +/* + * Initialization. + */ +static err_t ethernetif_init(struct netif *netif) { +#if LWIP_NETIF_HOSTNAME + /* Initialize interface hostname */ + netif->hostname = "lwip"; +#endif /* LWIP_NETIF_HOSTNAME */ + + /* + * Initialize the snmp variables and counters inside the struct netif. + * The last argument should be replaced with your link speed, in units + * of bits per second. + */ + NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LWIP_LINK_SPEED); + + netif->state = NULL; + netif->name[0] = LWIP_IFNAME0; + netif->name[1] = LWIP_IFNAME1; + /* We directly use etharp_output() here to save a function call. + * You can instead declare your own function an call etharp_output() + * from it if you have to do some checks before sending (e.g. if link + * is available...) */ + netif->output = etharp_output; + netif->linkoutput = low_level_output; + + /* initialize the hardware */ + low_level_init(netif); + + return ERR_OK; +} + +/** + * @brief LWIP handling thread. + * + * @param[in] p pointer to a @p lwipthread_opts structure or @p NULL + * @return The function does not return. + */ +msg_t lwip_thread(void *p) { + EvTimer evt; + EventListener el0, el1; + struct ip_addr ip, gateway, netmask; + static struct netif thisif; + static const MACConfig mac_config = {thisif.hwaddr}; + + chRegSetThreadName("lwipthread"); + + /* Initializes the thing.*/ + sys_init(); + mem_init(); + memp_init(); + pbuf_init(); + netif_init(); + ip_init(); + tcpip_init(NULL, NULL); + + /* TCP/IP parameters, runtime or compile time.*/ + if (p) { + struct lwipthread_opts *opts = p; + unsigned i; + + for (i = 0; i < 6; i++) + thisif.hwaddr[i] = opts->macaddress[i]; + ip.addr = opts->address; + gateway.addr = opts->gateway; + netmask.addr = opts->netmask; + } + else { + thisif.hwaddr[0] = LWIP_ETHADDR_0; + thisif.hwaddr[1] = LWIP_ETHADDR_1; + thisif.hwaddr[2] = LWIP_ETHADDR_2; + thisif.hwaddr[3] = LWIP_ETHADDR_3; + thisif.hwaddr[4] = LWIP_ETHADDR_4; + thisif.hwaddr[5] = LWIP_ETHADDR_5; + LWIP_IPADDR(&ip); + LWIP_GATEWAY(&gateway); + LWIP_NETMASK(&netmask); + } + macStart(ÐD1, &mac_config); + netif_add(&thisif, &ip, &netmask, &gateway, NULL, ethernetif_init, tcpip_input); + + netif_set_default(&thisif); + netif_set_up(&thisif); + + /* Setup event sources.*/ + evtInit(&evt, S2ST(5)); + evtStart(&evt); + chEvtRegisterMask(&evt.et_es, &el0, PERIODIC_TIMER_ID); + chEvtRegisterMask(macGetReceiveEventSource(ÐD1), &el1, FRAME_RECEIVED_ID); + chEvtAddFlags(PERIODIC_TIMER_ID | FRAME_RECEIVED_ID); + + /* Goes to the final priority after initialization.*/ + chThdSetPriority(LWIP_THREAD_PRIORITY); + + while (TRUE) { + eventmask_t mask = chEvtWaitAny(ALL_EVENTS); + if (mask & PERIODIC_TIMER_ID) { + bool_t current_link_status = macPollLinkStatus(ÐD1); + if (current_link_status != netif_is_link_up(&thisif)) { + if (current_link_status) + tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up, + &thisif, 0); + else + tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down, + &thisif, 0); + } + } + if (mask & FRAME_RECEIVED_ID) { + struct pbuf *p; + while ((p = low_level_input(&thisif)) != NULL) { + struct eth_hdr *ethhdr = p->payload; + switch (htons(ethhdr->type)) { + /* IP or ARP packet? */ + case ETHTYPE_IP: + case ETHTYPE_ARP: +#if PPPOE_SUPPORT + /* PPPoE packet? */ + case ETHTYPE_PPPOEDISC: + case ETHTYPE_PPPOE: +#endif /* PPPOE_SUPPORT */ + /* full packet send to tcpip_thread to process */ + if (thisif.input(p, &thisif) == ERR_OK) + break; + LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); + default: + pbuf_free(p); + } + } + } + } + return 0; +} + +/** @} */ diff --git a/os/various/lwip_bindings/lwipthread.h b/os/various/lwip_bindings/lwipthread.h new file mode 100644 index 000000000..7934c0342 --- /dev/null +++ b/os/various/lwip_bindings/lwipthread.h @@ -0,0 +1,135 @@ +/* + ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, + 2011,2012 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS/RT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + --- + + A special exception to the GPL can be applied should you wish to distribute + a combined work that includes ChibiOS/RT, without being obliged to provide + the source code for any proprietary components. See the file exception.txt + for full details of how and when the exception can be applied. +*/ + +/** + * @file lwipthread.h + * @brief LWIP wrapper thread macros and structures. + * @addtogroup LWIP_THREAD + * @{ + */ + +#ifndef _LWIPTHREAD_H_ +#define _LWIPTHREAD_H_ + +/** @brief MAC thread priority.*/ +#ifndef LWIP_THREAD_PRIORITY +#define LWIP_THREAD_PRIORITY LOWPRIO +#endif + +/** @brief IP Address. */ +#if !defined(LWIP_IPADDR) || defined(__DOXYGEN__) +#define LWIP_IPADDR(p) IP4_ADDR(p, 192, 168, 1, 20) +#endif + +/** @brief IP Gateway. */ +#if !defined(LWIP_GATEWAY) || defined(__DOXYGEN__) +#define LWIP_GATEWAY(p) IP4_ADDR(p, 192, 168, 1, 1) +#endif + +/** @brief IP netmask. */ +#if !defined(LWIP_NETMASK) || defined(__DOXYGEN__) +#define LWIP_NETMASK(p) IP4_ADDR(p, 255, 255, 255, 0) +#endif + +/** @brief MAC thread stack size. */ +#if !defined(LWIP_THREAD_STACK_SIZE) || defined(__DOXYGEN__) +#define LWIP_THREAD_STACK_SIZE 512 +#endif + +/** @brief Transmission timeout. */ +#if !defined(LWIP_SEND_TIMEOUT) || defined(__DOXYGEN__) +#define LWIP_SEND_TIMEOUT 50 +#endif + +/** @brief Link speed. */ +#if !defined(LWIP_LINK_SPEED) || defined(__DOXYGEN__) +#define LWIP_LINK_SPEED 100000000 +#endif + +/** @brief MAC Address byte 0. */ +#if !defined(LWIP_ETHADDR_0) || defined(__DOXYGEN__) +#define LWIP_ETHADDR_0 0xC2 +#endif + +/** @brief MAC Address byte 1. */ +#if !defined(LWIP_ETHADDR_1) || defined(__DOXYGEN__) +#define LWIP_ETHADDR_1 0xAF +#endif + +/** @brief MAC Address byte 2. */ +#if !defined(LWIP_ETHADDR_2) || defined(__DOXYGEN__) +#define LWIP_ETHADDR_2 0x51 +#endif + +/** @brief MAC Address byte 3. */ +#if !defined(LWIP_ETHADDR_3) || defined(__DOXYGEN__) +#define LWIP_ETHADDR_3 0x03 +#endif + +/** @brief MAC Address byte 4. */ +#if !defined(LWIP_ETHADDR_4) || defined(__DOXYGEN__) +#define LWIP_ETHADDR_4 0xCF +#endif + +/** @brief MAC Address byte 5. */ +#if !defined(LWIP_ETHADDR_5) || defined(__DOXYGEN__) +#define LWIP_ETHADDR_5 0x46 +#endif + +/** @brief Interface name byte 0. */ +#if !defined(LWIP_IFNAME0) || defined(__DOXYGEN__) +#define LWIP_IFNAME0 'm' +#endif + +/** @brief Interface name byte 1. */ +#if !defined(LWIP_IFNAME1) || defined(__DOXYGEN__) +#define LWIP_IFNAME1 's' +#endif + +/** + * @brief Runtime TCP/IP settings. + */ +struct lwipthread_opts { + uint8_t *macaddress; + uint32_t address; + uint32_t netmask; + uint32_t gateway; +}; + +extern WORKING_AREA(wa_lwip_thread, LWIP_THREAD_STACK_SIZE); + +#ifdef __cplusplus +extern "C" { +#endif + msg_t lwip_thread(void *p); +#ifdef __cplusplus +} +#endif + +#endif /* _LWIPTHREAD_H_ */ + +/** @} */ diff --git a/os/various/lwip_bindings/readme.txt b/os/various/lwip_bindings/readme.txt new file mode 100644 index 000000000..8a7a89e8c --- /dev/null +++ b/os/various/lwip_bindings/readme.txt @@ -0,0 +1,6 @@ +This directory contains the ChibiOS/RT "official" bindings with the lwIP +TCP/IP stack: http://savannah.nongnu.org/projects/lwip + +In order to use FatFS within ChibiOS/RT project, unzip FatFS under +./ext/lwip-1.4.0 then include $(CHIBIOS)/os/various/lwip_bindings/lwip.mk +in your makefile. diff --git a/readme.txt b/readme.txt index 272757e79..1aca87b07 100644 --- a/readme.txt +++ b/readme.txt @@ -79,6 +79,8 @@ ***************************************************************************** *** 2.4.2 *** +- FIX: Fixed lwIP-related files missing from version 2.4.1 (bug 3533887). + - FIX: Fixed timeout related race condition in STM32 I2C driver (bug 3530043). - FIX: Fixed wrong macro check in STM32 MAC driver (bug 3527179). - FIX: Fixed error in STM32L-Discovery board.h file (bug 3526918).