Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested
Fork of F7_Ethernet by
lwip/netif/slipif.c@0:d26c1b55cfca, 2016-06-19 (annotated)
- Committer:
- DieterGraef
- Date:
- Sun Jun 19 16:23:40 2016 +0000
- Revision:
- 0:d26c1b55cfca
Ethernet Library for Nucleo stm32f746ZG and Disco stm32f746NG works under arm and gcc environment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DieterGraef | 0:d26c1b55cfca | 1 | /** |
DieterGraef | 0:d26c1b55cfca | 2 | * @file |
DieterGraef | 0:d26c1b55cfca | 3 | * SLIP Interface |
DieterGraef | 0:d26c1b55cfca | 4 | * |
DieterGraef | 0:d26c1b55cfca | 5 | */ |
DieterGraef | 0:d26c1b55cfca | 6 | |
DieterGraef | 0:d26c1b55cfca | 7 | /* |
DieterGraef | 0:d26c1b55cfca | 8 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
DieterGraef | 0:d26c1b55cfca | 9 | * All rights reserved. |
DieterGraef | 0:d26c1b55cfca | 10 | * |
DieterGraef | 0:d26c1b55cfca | 11 | * Redistribution and use in source and binary forms, with or without |
DieterGraef | 0:d26c1b55cfca | 12 | * modification, are permitted provided that the following conditions |
DieterGraef | 0:d26c1b55cfca | 13 | * are met: |
DieterGraef | 0:d26c1b55cfca | 14 | * 1. Redistributions of source code must retain the above copyright |
DieterGraef | 0:d26c1b55cfca | 15 | * notice, this list of conditions and the following disclaimer. |
DieterGraef | 0:d26c1b55cfca | 16 | * 2. Redistributions in binary form must reproduce the above copyright |
DieterGraef | 0:d26c1b55cfca | 17 | * notice, this list of conditions and the following disclaimer in the |
DieterGraef | 0:d26c1b55cfca | 18 | * documentation and/or other materials provided with the distribution. |
DieterGraef | 0:d26c1b55cfca | 19 | * 3. Neither the name of the Institute nor the names of its contributors |
DieterGraef | 0:d26c1b55cfca | 20 | * may be used to endorse or promote products derived from this software |
DieterGraef | 0:d26c1b55cfca | 21 | * without specific prior written permission. |
DieterGraef | 0:d26c1b55cfca | 22 | * |
DieterGraef | 0:d26c1b55cfca | 23 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
DieterGraef | 0:d26c1b55cfca | 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
DieterGraef | 0:d26c1b55cfca | 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
DieterGraef | 0:d26c1b55cfca | 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
DieterGraef | 0:d26c1b55cfca | 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
DieterGraef | 0:d26c1b55cfca | 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
DieterGraef | 0:d26c1b55cfca | 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
DieterGraef | 0:d26c1b55cfca | 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
DieterGraef | 0:d26c1b55cfca | 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
DieterGraef | 0:d26c1b55cfca | 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
DieterGraef | 0:d26c1b55cfca | 33 | * SUCH DAMAGE. |
DieterGraef | 0:d26c1b55cfca | 34 | * |
DieterGraef | 0:d26c1b55cfca | 35 | * This file is built upon the file: src/arch/rtxc/netif/sioslip.c |
DieterGraef | 0:d26c1b55cfca | 36 | * |
DieterGraef | 0:d26c1b55cfca | 37 | * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com> |
DieterGraef | 0:d26c1b55cfca | 38 | * Simon Goldschmidt |
DieterGraef | 0:d26c1b55cfca | 39 | * |
DieterGraef | 0:d26c1b55cfca | 40 | * Usage: This netif can be used in three ways: |
DieterGraef | 0:d26c1b55cfca | 41 | * 1) For NO_SYS==0, an RX thread can be used which blocks on sio_read() |
DieterGraef | 0:d26c1b55cfca | 42 | * until data is received. |
DieterGraef | 0:d26c1b55cfca | 43 | * 2) In your main loop, call slipif_poll() to check for new RX bytes, |
DieterGraef | 0:d26c1b55cfca | 44 | * completed packets are fed into netif->input(). |
DieterGraef | 0:d26c1b55cfca | 45 | * 3) Call slipif_received_byte[s]() from your serial RX ISR and |
DieterGraef | 0:d26c1b55cfca | 46 | * slipif_process_rxqueue() from your main loop. ISR level decodes |
DieterGraef | 0:d26c1b55cfca | 47 | * packets and puts completed packets on a queue which is fed into |
DieterGraef | 0:d26c1b55cfca | 48 | * the stack from the main loop (needs SYS_LIGHTWEIGHT_PROT for |
DieterGraef | 0:d26c1b55cfca | 49 | * pbuf_alloc to work on ISR level!). |
DieterGraef | 0:d26c1b55cfca | 50 | * |
DieterGraef | 0:d26c1b55cfca | 51 | */ |
DieterGraef | 0:d26c1b55cfca | 52 | |
DieterGraef | 0:d26c1b55cfca | 53 | /* |
DieterGraef | 0:d26c1b55cfca | 54 | * This is an arch independent SLIP netif. The specific serial hooks must be |
DieterGraef | 0:d26c1b55cfca | 55 | * provided by another file. They are sio_open, sio_read/sio_tryread and sio_send |
DieterGraef | 0:d26c1b55cfca | 56 | */ |
DieterGraef | 0:d26c1b55cfca | 57 | |
DieterGraef | 0:d26c1b55cfca | 58 | #include "netif/slipif.h" |
DieterGraef | 0:d26c1b55cfca | 59 | #include "lwip/opt.h" |
DieterGraef | 0:d26c1b55cfca | 60 | |
DieterGraef | 0:d26c1b55cfca | 61 | #if LWIP_HAVE_SLIPIF |
DieterGraef | 0:d26c1b55cfca | 62 | |
DieterGraef | 0:d26c1b55cfca | 63 | #include "lwip/def.h" |
DieterGraef | 0:d26c1b55cfca | 64 | #include "lwip/pbuf.h" |
DieterGraef | 0:d26c1b55cfca | 65 | #include "lwip/stats.h" |
DieterGraef | 0:d26c1b55cfca | 66 | #include "lwip/snmp.h" |
DieterGraef | 0:d26c1b55cfca | 67 | #include "lwip/sio.h" |
DieterGraef | 0:d26c1b55cfca | 68 | #include "lwip/sys.h" |
DieterGraef | 0:d26c1b55cfca | 69 | |
DieterGraef | 0:d26c1b55cfca | 70 | #define SLIP_END 0xC0 /* 0300: start and end of every packet */ |
DieterGraef | 0:d26c1b55cfca | 71 | #define SLIP_ESC 0xDB /* 0333: escape start (one byte escaped data follows) */ |
DieterGraef | 0:d26c1b55cfca | 72 | #define SLIP_ESC_END 0xDC /* 0334: following escape: original byte is 0xC0 (END) */ |
DieterGraef | 0:d26c1b55cfca | 73 | #define SLIP_ESC_ESC 0xDD /* 0335: following escape: original byte is 0xDB (ESC) */ |
DieterGraef | 0:d26c1b55cfca | 74 | |
DieterGraef | 0:d26c1b55cfca | 75 | /** Maximum packet size that is received by this netif */ |
DieterGraef | 0:d26c1b55cfca | 76 | #ifndef SLIP_MAX_SIZE |
DieterGraef | 0:d26c1b55cfca | 77 | #define SLIP_MAX_SIZE 1500 |
DieterGraef | 0:d26c1b55cfca | 78 | #endif |
DieterGraef | 0:d26c1b55cfca | 79 | |
DieterGraef | 0:d26c1b55cfca | 80 | /** Define this to the interface speed for SNMP |
DieterGraef | 0:d26c1b55cfca | 81 | * (sio_fd is the sio_fd_t returned by sio_open). |
DieterGraef | 0:d26c1b55cfca | 82 | * The default value of zero means 'unknown'. |
DieterGraef | 0:d26c1b55cfca | 83 | */ |
DieterGraef | 0:d26c1b55cfca | 84 | #ifndef SLIP_SIO_SPEED |
DieterGraef | 0:d26c1b55cfca | 85 | #define SLIP_SIO_SPEED(sio_fd) 0 |
DieterGraef | 0:d26c1b55cfca | 86 | #endif |
DieterGraef | 0:d26c1b55cfca | 87 | |
DieterGraef | 0:d26c1b55cfca | 88 | enum slipif_recv_state { |
DieterGraef | 0:d26c1b55cfca | 89 | SLIP_RECV_NORMAL, |
DieterGraef | 0:d26c1b55cfca | 90 | SLIP_RECV_ESCAPE, |
DieterGraef | 0:d26c1b55cfca | 91 | }; |
DieterGraef | 0:d26c1b55cfca | 92 | |
DieterGraef | 0:d26c1b55cfca | 93 | struct slipif_priv { |
DieterGraef | 0:d26c1b55cfca | 94 | sio_fd_t sd; |
DieterGraef | 0:d26c1b55cfca | 95 | /* q is the whole pbuf chain for a packet, p is the current pbuf in the chain */ |
DieterGraef | 0:d26c1b55cfca | 96 | struct pbuf *p, *q; |
DieterGraef | 0:d26c1b55cfca | 97 | u8_t state; |
DieterGraef | 0:d26c1b55cfca | 98 | u16_t i, recved; |
DieterGraef | 0:d26c1b55cfca | 99 | #if SLIP_RX_FROM_ISR |
DieterGraef | 0:d26c1b55cfca | 100 | struct pbuf *rxpackets; |
DieterGraef | 0:d26c1b55cfca | 101 | #endif |
DieterGraef | 0:d26c1b55cfca | 102 | }; |
DieterGraef | 0:d26c1b55cfca | 103 | |
DieterGraef | 0:d26c1b55cfca | 104 | /** |
DieterGraef | 0:d26c1b55cfca | 105 | * Send a pbuf doing the necessary SLIP encapsulation |
DieterGraef | 0:d26c1b55cfca | 106 | * |
DieterGraef | 0:d26c1b55cfca | 107 | * Uses the serial layer's sio_send() |
DieterGraef | 0:d26c1b55cfca | 108 | * |
DieterGraef | 0:d26c1b55cfca | 109 | * @param netif the lwip network interface structure for this slipif |
DieterGraef | 0:d26c1b55cfca | 110 | * @param p the pbuf chaing packet to send |
DieterGraef | 0:d26c1b55cfca | 111 | * @param ipaddr the ip address to send the packet to (not used for slipif) |
DieterGraef | 0:d26c1b55cfca | 112 | * @return always returns ERR_OK since the serial layer does not provide return values |
DieterGraef | 0:d26c1b55cfca | 113 | */ |
DieterGraef | 0:d26c1b55cfca | 114 | err_t |
DieterGraef | 0:d26c1b55cfca | 115 | slipif_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr) |
DieterGraef | 0:d26c1b55cfca | 116 | { |
DieterGraef | 0:d26c1b55cfca | 117 | struct slipif_priv *priv; |
DieterGraef | 0:d26c1b55cfca | 118 | struct pbuf *q; |
DieterGraef | 0:d26c1b55cfca | 119 | u16_t i; |
DieterGraef | 0:d26c1b55cfca | 120 | u8_t c; |
DieterGraef | 0:d26c1b55cfca | 121 | |
DieterGraef | 0:d26c1b55cfca | 122 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
DieterGraef | 0:d26c1b55cfca | 123 | LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); |
DieterGraef | 0:d26c1b55cfca | 124 | LWIP_ASSERT("p != NULL", (p != NULL)); |
DieterGraef | 0:d26c1b55cfca | 125 | |
DieterGraef | 0:d26c1b55cfca | 126 | LWIP_UNUSED_ARG(ipaddr); |
DieterGraef | 0:d26c1b55cfca | 127 | |
DieterGraef | 0:d26c1b55cfca | 128 | LWIP_DEBUGF(SLIP_DEBUG, ("slipif_output(%"U16_F"): sending %"U16_F" bytes\n", (u16_t)netif->num, p->tot_len)); |
DieterGraef | 0:d26c1b55cfca | 129 | priv = netif->state; |
DieterGraef | 0:d26c1b55cfca | 130 | |
DieterGraef | 0:d26c1b55cfca | 131 | /* Send pbuf out on the serial I/O device. */ |
DieterGraef | 0:d26c1b55cfca | 132 | /* Start with packet delimiter. */ |
DieterGraef | 0:d26c1b55cfca | 133 | sio_send(SLIP_END, priv->sd); |
DieterGraef | 0:d26c1b55cfca | 134 | |
DieterGraef | 0:d26c1b55cfca | 135 | for (q = p; q != NULL; q = q->next) { |
DieterGraef | 0:d26c1b55cfca | 136 | for (i = 0; i < q->len; i++) { |
DieterGraef | 0:d26c1b55cfca | 137 | c = ((u8_t *)q->payload)[i]; |
DieterGraef | 0:d26c1b55cfca | 138 | switch (c) { |
DieterGraef | 0:d26c1b55cfca | 139 | case SLIP_END: |
DieterGraef | 0:d26c1b55cfca | 140 | /* need to escape this byte (0xC0 -> 0xDB, 0xDC) */ |
DieterGraef | 0:d26c1b55cfca | 141 | sio_send(SLIP_ESC, priv->sd); |
DieterGraef | 0:d26c1b55cfca | 142 | sio_send(SLIP_ESC_END, priv->sd); |
DieterGraef | 0:d26c1b55cfca | 143 | break; |
DieterGraef | 0:d26c1b55cfca | 144 | case SLIP_ESC: |
DieterGraef | 0:d26c1b55cfca | 145 | /* need to escape this byte (0xDB -> 0xDB, 0xDD) */ |
DieterGraef | 0:d26c1b55cfca | 146 | sio_send(SLIP_ESC, priv->sd); |
DieterGraef | 0:d26c1b55cfca | 147 | sio_send(SLIP_ESC_ESC, priv->sd); |
DieterGraef | 0:d26c1b55cfca | 148 | break; |
DieterGraef | 0:d26c1b55cfca | 149 | default: |
DieterGraef | 0:d26c1b55cfca | 150 | /* normal byte - no need for escaping */ |
DieterGraef | 0:d26c1b55cfca | 151 | sio_send(c, priv->sd); |
DieterGraef | 0:d26c1b55cfca | 152 | break; |
DieterGraef | 0:d26c1b55cfca | 153 | } |
DieterGraef | 0:d26c1b55cfca | 154 | } |
DieterGraef | 0:d26c1b55cfca | 155 | } |
DieterGraef | 0:d26c1b55cfca | 156 | /* End with packet delimiter. */ |
DieterGraef | 0:d26c1b55cfca | 157 | sio_send(SLIP_END, priv->sd); |
DieterGraef | 0:d26c1b55cfca | 158 | return ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 159 | } |
DieterGraef | 0:d26c1b55cfca | 160 | |
DieterGraef | 0:d26c1b55cfca | 161 | /** |
DieterGraef | 0:d26c1b55cfca | 162 | * Handle the incoming SLIP stream character by character |
DieterGraef | 0:d26c1b55cfca | 163 | * |
DieterGraef | 0:d26c1b55cfca | 164 | * @param netif the lwip network interface structure for this slipif |
DieterGraef | 0:d26c1b55cfca | 165 | * @param c received character (multiple calls to this function will |
DieterGraef | 0:d26c1b55cfca | 166 | * return a complete packet, NULL is returned before - used for polling) |
DieterGraef | 0:d26c1b55cfca | 167 | * @return The IP packet when SLIP_END is received |
DieterGraef | 0:d26c1b55cfca | 168 | */ |
DieterGraef | 0:d26c1b55cfca | 169 | static struct pbuf* |
DieterGraef | 0:d26c1b55cfca | 170 | slipif_rxbyte(struct netif *netif, u8_t c) |
DieterGraef | 0:d26c1b55cfca | 171 | { |
DieterGraef | 0:d26c1b55cfca | 172 | struct slipif_priv *priv; |
DieterGraef | 0:d26c1b55cfca | 173 | struct pbuf *t; |
DieterGraef | 0:d26c1b55cfca | 174 | |
DieterGraef | 0:d26c1b55cfca | 175 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
DieterGraef | 0:d26c1b55cfca | 176 | LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); |
DieterGraef | 0:d26c1b55cfca | 177 | |
DieterGraef | 0:d26c1b55cfca | 178 | priv = netif->state; |
DieterGraef | 0:d26c1b55cfca | 179 | |
DieterGraef | 0:d26c1b55cfca | 180 | switch (priv->state) { |
DieterGraef | 0:d26c1b55cfca | 181 | case SLIP_RECV_NORMAL: |
DieterGraef | 0:d26c1b55cfca | 182 | switch (c) { |
DieterGraef | 0:d26c1b55cfca | 183 | case SLIP_END: |
DieterGraef | 0:d26c1b55cfca | 184 | if (priv->recved > 0) { |
DieterGraef | 0:d26c1b55cfca | 185 | /* Received whole packet. */ |
DieterGraef | 0:d26c1b55cfca | 186 | /* Trim the pbuf to the size of the received packet. */ |
DieterGraef | 0:d26c1b55cfca | 187 | pbuf_realloc(priv->q, priv->recved); |
DieterGraef | 0:d26c1b55cfca | 188 | |
DieterGraef | 0:d26c1b55cfca | 189 | LINK_STATS_INC(link.recv); |
DieterGraef | 0:d26c1b55cfca | 190 | |
DieterGraef | 0:d26c1b55cfca | 191 | LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet (%"U16_F" bytes)\n", priv->recved)); |
DieterGraef | 0:d26c1b55cfca | 192 | t = priv->q; |
DieterGraef | 0:d26c1b55cfca | 193 | priv->p = priv->q = NULL; |
DieterGraef | 0:d26c1b55cfca | 194 | priv->i = priv->recved = 0; |
DieterGraef | 0:d26c1b55cfca | 195 | return t; |
DieterGraef | 0:d26c1b55cfca | 196 | } |
DieterGraef | 0:d26c1b55cfca | 197 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 198 | case SLIP_ESC: |
DieterGraef | 0:d26c1b55cfca | 199 | priv->state = SLIP_RECV_ESCAPE; |
DieterGraef | 0:d26c1b55cfca | 200 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 201 | } /* end switch (c) */ |
DieterGraef | 0:d26c1b55cfca | 202 | break; |
DieterGraef | 0:d26c1b55cfca | 203 | case SLIP_RECV_ESCAPE: |
DieterGraef | 0:d26c1b55cfca | 204 | /* un-escape END or ESC bytes, leave other bytes |
DieterGraef | 0:d26c1b55cfca | 205 | (although that would be a protocol error) */ |
DieterGraef | 0:d26c1b55cfca | 206 | switch (c) { |
DieterGraef | 0:d26c1b55cfca | 207 | case SLIP_ESC_END: |
DieterGraef | 0:d26c1b55cfca | 208 | c = SLIP_END; |
DieterGraef | 0:d26c1b55cfca | 209 | break; |
DieterGraef | 0:d26c1b55cfca | 210 | case SLIP_ESC_ESC: |
DieterGraef | 0:d26c1b55cfca | 211 | c = SLIP_ESC; |
DieterGraef | 0:d26c1b55cfca | 212 | break; |
DieterGraef | 0:d26c1b55cfca | 213 | } |
DieterGraef | 0:d26c1b55cfca | 214 | priv->state = SLIP_RECV_NORMAL; |
DieterGraef | 0:d26c1b55cfca | 215 | break; |
DieterGraef | 0:d26c1b55cfca | 216 | } /* end switch (priv->state) */ |
DieterGraef | 0:d26c1b55cfca | 217 | |
DieterGraef | 0:d26c1b55cfca | 218 | /* byte received, packet not yet completely received */ |
DieterGraef | 0:d26c1b55cfca | 219 | if (priv->p == NULL) { |
DieterGraef | 0:d26c1b55cfca | 220 | /* allocate a new pbuf */ |
DieterGraef | 0:d26c1b55cfca | 221 | LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n")); |
DieterGraef | 0:d26c1b55cfca | 222 | priv->p = pbuf_alloc(PBUF_LINK, (PBUF_POOL_BUFSIZE - PBUF_LINK_HLEN), PBUF_POOL); |
DieterGraef | 0:d26c1b55cfca | 223 | |
DieterGraef | 0:d26c1b55cfca | 224 | if (priv->p == NULL) { |
DieterGraef | 0:d26c1b55cfca | 225 | LINK_STATS_INC(link.drop); |
DieterGraef | 0:d26c1b55cfca | 226 | LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n")); |
DieterGraef | 0:d26c1b55cfca | 227 | /* don't process any further since we got no pbuf to receive to */ |
DieterGraef | 0:d26c1b55cfca | 228 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 229 | } |
DieterGraef | 0:d26c1b55cfca | 230 | |
DieterGraef | 0:d26c1b55cfca | 231 | if (priv->q != NULL) { |
DieterGraef | 0:d26c1b55cfca | 232 | /* 'chain' the pbuf to the existing chain */ |
DieterGraef | 0:d26c1b55cfca | 233 | pbuf_cat(priv->q, priv->p); |
DieterGraef | 0:d26c1b55cfca | 234 | } else { |
DieterGraef | 0:d26c1b55cfca | 235 | /* p is the first pbuf in the chain */ |
DieterGraef | 0:d26c1b55cfca | 236 | priv->q = priv->p; |
DieterGraef | 0:d26c1b55cfca | 237 | } |
DieterGraef | 0:d26c1b55cfca | 238 | } |
DieterGraef | 0:d26c1b55cfca | 239 | |
DieterGraef | 0:d26c1b55cfca | 240 | /* this automatically drops bytes if > SLIP_MAX_SIZE */ |
DieterGraef | 0:d26c1b55cfca | 241 | if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) { |
DieterGraef | 0:d26c1b55cfca | 242 | ((u8_t *)priv->p->payload)[priv->i] = c; |
DieterGraef | 0:d26c1b55cfca | 243 | priv->recved++; |
DieterGraef | 0:d26c1b55cfca | 244 | priv->i++; |
DieterGraef | 0:d26c1b55cfca | 245 | if (priv->i >= priv->p->len) { |
DieterGraef | 0:d26c1b55cfca | 246 | /* on to the next pbuf */ |
DieterGraef | 0:d26c1b55cfca | 247 | priv->i = 0; |
DieterGraef | 0:d26c1b55cfca | 248 | if (priv->p->next != NULL && priv->p->next->len > 0) { |
DieterGraef | 0:d26c1b55cfca | 249 | /* p is a chain, on to the next in the chain */ |
DieterGraef | 0:d26c1b55cfca | 250 | priv->p = priv->p->next; |
DieterGraef | 0:d26c1b55cfca | 251 | } else { |
DieterGraef | 0:d26c1b55cfca | 252 | /* p is a single pbuf, set it to NULL so next time a new |
DieterGraef | 0:d26c1b55cfca | 253 | * pbuf is allocated */ |
DieterGraef | 0:d26c1b55cfca | 254 | priv->p = NULL; |
DieterGraef | 0:d26c1b55cfca | 255 | } |
DieterGraef | 0:d26c1b55cfca | 256 | } |
DieterGraef | 0:d26c1b55cfca | 257 | } |
DieterGraef | 0:d26c1b55cfca | 258 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 259 | } |
DieterGraef | 0:d26c1b55cfca | 260 | |
DieterGraef | 0:d26c1b55cfca | 261 | /** Like slipif_rxbyte, but passes completed packets to netif->input |
DieterGraef | 0:d26c1b55cfca | 262 | * |
DieterGraef | 0:d26c1b55cfca | 263 | * @param netif The lwip network interface structure for this slipif |
DieterGraef | 0:d26c1b55cfca | 264 | * @param data received character |
DieterGraef | 0:d26c1b55cfca | 265 | */ |
DieterGraef | 0:d26c1b55cfca | 266 | static void |
DieterGraef | 0:d26c1b55cfca | 267 | slipif_rxbyte_input(struct netif *netif, u8_t c) |
DieterGraef | 0:d26c1b55cfca | 268 | { |
DieterGraef | 0:d26c1b55cfca | 269 | struct pbuf *p; |
DieterGraef | 0:d26c1b55cfca | 270 | p = slipif_rxbyte(netif, c); |
DieterGraef | 0:d26c1b55cfca | 271 | if (p != NULL) { |
DieterGraef | 0:d26c1b55cfca | 272 | if (netif->input(p, netif) != ERR_OK) { |
DieterGraef | 0:d26c1b55cfca | 273 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 274 | } |
DieterGraef | 0:d26c1b55cfca | 275 | } |
DieterGraef | 0:d26c1b55cfca | 276 | } |
DieterGraef | 0:d26c1b55cfca | 277 | |
DieterGraef | 0:d26c1b55cfca | 278 | #if SLIP_USE_RX_THREAD |
DieterGraef | 0:d26c1b55cfca | 279 | /** |
DieterGraef | 0:d26c1b55cfca | 280 | * The SLIP input thread. |
DieterGraef | 0:d26c1b55cfca | 281 | * |
DieterGraef | 0:d26c1b55cfca | 282 | * Feed the IP layer with incoming packets |
DieterGraef | 0:d26c1b55cfca | 283 | * |
DieterGraef | 0:d26c1b55cfca | 284 | * @param nf the lwip network interface structure for this slipif |
DieterGraef | 0:d26c1b55cfca | 285 | */ |
DieterGraef | 0:d26c1b55cfca | 286 | static void |
DieterGraef | 0:d26c1b55cfca | 287 | slipif_loop_thread(void *nf) |
DieterGraef | 0:d26c1b55cfca | 288 | { |
DieterGraef | 0:d26c1b55cfca | 289 | u8_t c; |
DieterGraef | 0:d26c1b55cfca | 290 | struct netif *netif = (struct netif *)nf; |
DieterGraef | 0:d26c1b55cfca | 291 | struct slipif_priv *priv = (struct slipif_priv *)netif->state; |
DieterGraef | 0:d26c1b55cfca | 292 | |
DieterGraef | 0:d26c1b55cfca | 293 | while (1) { |
DieterGraef | 0:d26c1b55cfca | 294 | if (sio_read(priv->sd, &c, 1) > 0) { |
DieterGraef | 0:d26c1b55cfca | 295 | slipif_rxbyte_input(netif, c); |
DieterGraef | 0:d26c1b55cfca | 296 | } |
DieterGraef | 0:d26c1b55cfca | 297 | } |
DieterGraef | 0:d26c1b55cfca | 298 | } |
DieterGraef | 0:d26c1b55cfca | 299 | #endif /* SLIP_USE_RX_THREAD */ |
DieterGraef | 0:d26c1b55cfca | 300 | |
DieterGraef | 0:d26c1b55cfca | 301 | /** |
DieterGraef | 0:d26c1b55cfca | 302 | * SLIP netif initialization |
DieterGraef | 0:d26c1b55cfca | 303 | * |
DieterGraef | 0:d26c1b55cfca | 304 | * Call the arch specific sio_open and remember |
DieterGraef | 0:d26c1b55cfca | 305 | * the opened device in the state field of the netif. |
DieterGraef | 0:d26c1b55cfca | 306 | * |
DieterGraef | 0:d26c1b55cfca | 307 | * @param netif the lwip network interface structure for this slipif |
DieterGraef | 0:d26c1b55cfca | 308 | * @return ERR_OK if serial line could be opened, |
DieterGraef | 0:d26c1b55cfca | 309 | * ERR_MEM if no memory could be allocated, |
DieterGraef | 0:d26c1b55cfca | 310 | * ERR_IF is serial line couldn't be opened |
DieterGraef | 0:d26c1b55cfca | 311 | * |
DieterGraef | 0:d26c1b55cfca | 312 | * @note netif->num must contain the number of the serial port to open |
DieterGraef | 0:d26c1b55cfca | 313 | * (0 by default). If netif->state is != NULL, it is interpreted as an |
DieterGraef | 0:d26c1b55cfca | 314 | * u8_t pointer pointing to the serial port number instead of netif->num. |
DieterGraef | 0:d26c1b55cfca | 315 | * |
DieterGraef | 0:d26c1b55cfca | 316 | */ |
DieterGraef | 0:d26c1b55cfca | 317 | err_t |
DieterGraef | 0:d26c1b55cfca | 318 | slipif_init(struct netif *netif) |
DieterGraef | 0:d26c1b55cfca | 319 | { |
DieterGraef | 0:d26c1b55cfca | 320 | struct slipif_priv *priv; |
DieterGraef | 0:d26c1b55cfca | 321 | u8_t sio_num; |
DieterGraef | 0:d26c1b55cfca | 322 | |
DieterGraef | 0:d26c1b55cfca | 323 | LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)netif->num)); |
DieterGraef | 0:d26c1b55cfca | 324 | |
DieterGraef | 0:d26c1b55cfca | 325 | /* Allocate private data */ |
DieterGraef | 0:d26c1b55cfca | 326 | priv = (struct slipif_priv *)mem_malloc(sizeof(struct slipif_priv)); |
DieterGraef | 0:d26c1b55cfca | 327 | if (!priv) { |
DieterGraef | 0:d26c1b55cfca | 328 | return ERR_MEM; |
DieterGraef | 0:d26c1b55cfca | 329 | } |
DieterGraef | 0:d26c1b55cfca | 330 | |
DieterGraef | 0:d26c1b55cfca | 331 | netif->name[0] = 's'; |
DieterGraef | 0:d26c1b55cfca | 332 | netif->name[1] = 'l'; |
DieterGraef | 0:d26c1b55cfca | 333 | netif->output = slipif_output; |
DieterGraef | 0:d26c1b55cfca | 334 | netif->mtu = SLIP_MAX_SIZE; |
DieterGraef | 0:d26c1b55cfca | 335 | netif->flags |= NETIF_FLAG_POINTTOPOINT; |
DieterGraef | 0:d26c1b55cfca | 336 | |
DieterGraef | 0:d26c1b55cfca | 337 | /* netif->state or netif->num contain the port number */ |
DieterGraef | 0:d26c1b55cfca | 338 | if (netif->state != NULL) { |
DieterGraef | 0:d26c1b55cfca | 339 | sio_num = *(u8_t*)netif->state; |
DieterGraef | 0:d26c1b55cfca | 340 | } else { |
DieterGraef | 0:d26c1b55cfca | 341 | sio_num = netif->num; |
DieterGraef | 0:d26c1b55cfca | 342 | } |
DieterGraef | 0:d26c1b55cfca | 343 | /* Try to open the serial port. */ |
DieterGraef | 0:d26c1b55cfca | 344 | priv->sd = sio_open(sio_num); |
DieterGraef | 0:d26c1b55cfca | 345 | if (!priv->sd) { |
DieterGraef | 0:d26c1b55cfca | 346 | /* Opening the serial port failed. */ |
DieterGraef | 0:d26c1b55cfca | 347 | mem_free(priv); |
DieterGraef | 0:d26c1b55cfca | 348 | return ERR_IF; |
DieterGraef | 0:d26c1b55cfca | 349 | } |
DieterGraef | 0:d26c1b55cfca | 350 | |
DieterGraef | 0:d26c1b55cfca | 351 | /* Initialize private data */ |
DieterGraef | 0:d26c1b55cfca | 352 | priv->p = NULL; |
DieterGraef | 0:d26c1b55cfca | 353 | priv->q = NULL; |
DieterGraef | 0:d26c1b55cfca | 354 | priv->state = SLIP_RECV_NORMAL; |
DieterGraef | 0:d26c1b55cfca | 355 | priv->i = 0; |
DieterGraef | 0:d26c1b55cfca | 356 | priv->recved = 0; |
DieterGraef | 0:d26c1b55cfca | 357 | #if SLIP_RX_FROM_ISR |
DieterGraef | 0:d26c1b55cfca | 358 | priv->rxpackets = NULL; |
DieterGraef | 0:d26c1b55cfca | 359 | #endif |
DieterGraef | 0:d26c1b55cfca | 360 | |
DieterGraef | 0:d26c1b55cfca | 361 | netif->state = priv; |
DieterGraef | 0:d26c1b55cfca | 362 | |
DieterGraef | 0:d26c1b55cfca | 363 | /* initialize the snmp variables and counters inside the struct netif */ |
DieterGraef | 0:d26c1b55cfca | 364 | NETIF_INIT_SNMP(netif, snmp_ifType_slip, SLIP_SIO_SPEED(priv->sd)); |
DieterGraef | 0:d26c1b55cfca | 365 | |
DieterGraef | 0:d26c1b55cfca | 366 | #if SLIP_USE_RX_THREAD |
DieterGraef | 0:d26c1b55cfca | 367 | /* Create a thread to poll the serial line. */ |
DieterGraef | 0:d26c1b55cfca | 368 | sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif, |
DieterGraef | 0:d26c1b55cfca | 369 | SLIPIF_THREAD_STACKSIZE, SLIPIF_THREAD_PRIO); |
DieterGraef | 0:d26c1b55cfca | 370 | #endif /* SLIP_USE_RX_THREAD */ |
DieterGraef | 0:d26c1b55cfca | 371 | return ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 372 | } |
DieterGraef | 0:d26c1b55cfca | 373 | |
DieterGraef | 0:d26c1b55cfca | 374 | /** |
DieterGraef | 0:d26c1b55cfca | 375 | * Polls the serial device and feeds the IP layer with incoming packets. |
DieterGraef | 0:d26c1b55cfca | 376 | * |
DieterGraef | 0:d26c1b55cfca | 377 | * @param netif The lwip network interface structure for this slipif |
DieterGraef | 0:d26c1b55cfca | 378 | */ |
DieterGraef | 0:d26c1b55cfca | 379 | void |
DieterGraef | 0:d26c1b55cfca | 380 | slipif_poll(struct netif *netif) |
DieterGraef | 0:d26c1b55cfca | 381 | { |
DieterGraef | 0:d26c1b55cfca | 382 | u8_t c; |
DieterGraef | 0:d26c1b55cfca | 383 | struct slipif_priv *priv; |
DieterGraef | 0:d26c1b55cfca | 384 | |
DieterGraef | 0:d26c1b55cfca | 385 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
DieterGraef | 0:d26c1b55cfca | 386 | LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); |
DieterGraef | 0:d26c1b55cfca | 387 | |
DieterGraef | 0:d26c1b55cfca | 388 | priv = (struct slipif_priv *)netif->state; |
DieterGraef | 0:d26c1b55cfca | 389 | |
DieterGraef | 0:d26c1b55cfca | 390 | while (sio_tryread(priv->sd, &c, 1) > 0) { |
DieterGraef | 0:d26c1b55cfca | 391 | slipif_rxbyte_input(netif, c); |
DieterGraef | 0:d26c1b55cfca | 392 | } |
DieterGraef | 0:d26c1b55cfca | 393 | } |
DieterGraef | 0:d26c1b55cfca | 394 | |
DieterGraef | 0:d26c1b55cfca | 395 | #if SLIP_RX_FROM_ISR |
DieterGraef | 0:d26c1b55cfca | 396 | /** |
DieterGraef | 0:d26c1b55cfca | 397 | * Feeds the IP layer with incoming packets that were receive |
DieterGraef | 0:d26c1b55cfca | 398 | * |
DieterGraef | 0:d26c1b55cfca | 399 | * @param netif The lwip network interface structure for this slipif |
DieterGraef | 0:d26c1b55cfca | 400 | */ |
DieterGraef | 0:d26c1b55cfca | 401 | void |
DieterGraef | 0:d26c1b55cfca | 402 | slipif_process_rxqueue(struct netif *netif) |
DieterGraef | 0:d26c1b55cfca | 403 | { |
DieterGraef | 0:d26c1b55cfca | 404 | struct slipif_priv *priv; |
DieterGraef | 0:d26c1b55cfca | 405 | SYS_ARCH_DECL_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 406 | |
DieterGraef | 0:d26c1b55cfca | 407 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
DieterGraef | 0:d26c1b55cfca | 408 | LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); |
DieterGraef | 0:d26c1b55cfca | 409 | |
DieterGraef | 0:d26c1b55cfca | 410 | priv = (struct slipif_priv *)netif->state; |
DieterGraef | 0:d26c1b55cfca | 411 | |
DieterGraef | 0:d26c1b55cfca | 412 | SYS_ARCH_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 413 | while (priv->rxpackets != NULL) { |
DieterGraef | 0:d26c1b55cfca | 414 | struct pbuf *p = priv->rxpackets; |
DieterGraef | 0:d26c1b55cfca | 415 | #if SLIP_RX_QUEUE |
DieterGraef | 0:d26c1b55cfca | 416 | /* dequeue packet */ |
DieterGraef | 0:d26c1b55cfca | 417 | struct pbuf *q = p; |
DieterGraef | 0:d26c1b55cfca | 418 | while ((q->len != q->tot_len) && (q->next != NULL)) { |
DieterGraef | 0:d26c1b55cfca | 419 | q = q->next; |
DieterGraef | 0:d26c1b55cfca | 420 | } |
DieterGraef | 0:d26c1b55cfca | 421 | priv->rxpackets = q->next; |
DieterGraef | 0:d26c1b55cfca | 422 | q->next = NULL; |
DieterGraef | 0:d26c1b55cfca | 423 | #else /* SLIP_RX_QUEUE */ |
DieterGraef | 0:d26c1b55cfca | 424 | priv->rxpackets = NULL; |
DieterGraef | 0:d26c1b55cfca | 425 | #endif /* SLIP_RX_QUEUE */ |
DieterGraef | 0:d26c1b55cfca | 426 | SYS_ARCH_UNPROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 427 | if (netif->input(p, netif) != ERR_OK) { |
DieterGraef | 0:d26c1b55cfca | 428 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 429 | } |
DieterGraef | 0:d26c1b55cfca | 430 | SYS_ARCH_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 431 | } |
DieterGraef | 0:d26c1b55cfca | 432 | } |
DieterGraef | 0:d26c1b55cfca | 433 | |
DieterGraef | 0:d26c1b55cfca | 434 | /** Like slipif_rxbyte, but queues completed packets. |
DieterGraef | 0:d26c1b55cfca | 435 | * |
DieterGraef | 0:d26c1b55cfca | 436 | * @param netif The lwip network interface structure for this slipif |
DieterGraef | 0:d26c1b55cfca | 437 | * @param data Received serial byte |
DieterGraef | 0:d26c1b55cfca | 438 | */ |
DieterGraef | 0:d26c1b55cfca | 439 | static void |
DieterGraef | 0:d26c1b55cfca | 440 | slipif_rxbyte_enqueue(struct netif *netif, u8_t data) |
DieterGraef | 0:d26c1b55cfca | 441 | { |
DieterGraef | 0:d26c1b55cfca | 442 | struct pbuf *p; |
DieterGraef | 0:d26c1b55cfca | 443 | struct slipif_priv *priv = (struct slipif_priv *)netif->state; |
DieterGraef | 0:d26c1b55cfca | 444 | SYS_ARCH_DECL_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 445 | |
DieterGraef | 0:d26c1b55cfca | 446 | p = slipif_rxbyte(netif, data); |
DieterGraef | 0:d26c1b55cfca | 447 | if (p != NULL) { |
DieterGraef | 0:d26c1b55cfca | 448 | SYS_ARCH_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 449 | if (priv->rxpackets != NULL) { |
DieterGraef | 0:d26c1b55cfca | 450 | #if SLIP_RX_QUEUE |
DieterGraef | 0:d26c1b55cfca | 451 | /* queue multiple pbufs */ |
DieterGraef | 0:d26c1b55cfca | 452 | struct pbuf *q = p; |
DieterGraef | 0:d26c1b55cfca | 453 | while(q->next != NULL) { |
DieterGraef | 0:d26c1b55cfca | 454 | q = q->next; |
DieterGraef | 0:d26c1b55cfca | 455 | } |
DieterGraef | 0:d26c1b55cfca | 456 | q->next = p; |
DieterGraef | 0:d26c1b55cfca | 457 | } else { |
DieterGraef | 0:d26c1b55cfca | 458 | #else /* SLIP_RX_QUEUE */ |
DieterGraef | 0:d26c1b55cfca | 459 | pbuf_free(priv->rxpackets); |
DieterGraef | 0:d26c1b55cfca | 460 | } |
DieterGraef | 0:d26c1b55cfca | 461 | { |
DieterGraef | 0:d26c1b55cfca | 462 | #endif /* SLIP_RX_QUEUE */ |
DieterGraef | 0:d26c1b55cfca | 463 | priv->rxpackets = p; |
DieterGraef | 0:d26c1b55cfca | 464 | } |
DieterGraef | 0:d26c1b55cfca | 465 | SYS_ARCH_UNPROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 466 | } |
DieterGraef | 0:d26c1b55cfca | 467 | } |
DieterGraef | 0:d26c1b55cfca | 468 | |
DieterGraef | 0:d26c1b55cfca | 469 | /** |
DieterGraef | 0:d26c1b55cfca | 470 | * Process a received byte, completed packets are put on a queue that is |
DieterGraef | 0:d26c1b55cfca | 471 | * fed into IP through slipif_process_rxqueue(). |
DieterGraef | 0:d26c1b55cfca | 472 | * |
DieterGraef | 0:d26c1b55cfca | 473 | * This function can be called from ISR if SYS_LIGHTWEIGHT_PROT is enabled. |
DieterGraef | 0:d26c1b55cfca | 474 | * |
DieterGraef | 0:d26c1b55cfca | 475 | * @param netif The lwip network interface structure for this slipif |
DieterGraef | 0:d26c1b55cfca | 476 | * @param data received character |
DieterGraef | 0:d26c1b55cfca | 477 | */ |
DieterGraef | 0:d26c1b55cfca | 478 | void |
DieterGraef | 0:d26c1b55cfca | 479 | slipif_received_byte(struct netif *netif, u8_t data) |
DieterGraef | 0:d26c1b55cfca | 480 | { |
DieterGraef | 0:d26c1b55cfca | 481 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
DieterGraef | 0:d26c1b55cfca | 482 | LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); |
DieterGraef | 0:d26c1b55cfca | 483 | slipif_rxbyte_enqueue(netif, data); |
DieterGraef | 0:d26c1b55cfca | 484 | } |
DieterGraef | 0:d26c1b55cfca | 485 | |
DieterGraef | 0:d26c1b55cfca | 486 | /** |
DieterGraef | 0:d26c1b55cfca | 487 | * Process multiple received byte, completed packets are put on a queue that is |
DieterGraef | 0:d26c1b55cfca | 488 | * fed into IP through slipif_process_rxqueue(). |
DieterGraef | 0:d26c1b55cfca | 489 | * |
DieterGraef | 0:d26c1b55cfca | 490 | * This function can be called from ISR if SYS_LIGHTWEIGHT_PROT is enabled. |
DieterGraef | 0:d26c1b55cfca | 491 | * |
DieterGraef | 0:d26c1b55cfca | 492 | * @param netif The lwip network interface structure for this slipif |
DieterGraef | 0:d26c1b55cfca | 493 | * @param data received character |
DieterGraef | 0:d26c1b55cfca | 494 | * @param len Number of received characters |
DieterGraef | 0:d26c1b55cfca | 495 | */ |
DieterGraef | 0:d26c1b55cfca | 496 | void |
DieterGraef | 0:d26c1b55cfca | 497 | slipif_received_bytes(struct netif *netif, u8_t *data, u8_t len) |
DieterGraef | 0:d26c1b55cfca | 498 | { |
DieterGraef | 0:d26c1b55cfca | 499 | u8_t i; |
DieterGraef | 0:d26c1b55cfca | 500 | u8_t *rxdata = data; |
DieterGraef | 0:d26c1b55cfca | 501 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
DieterGraef | 0:d26c1b55cfca | 502 | LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); |
DieterGraef | 0:d26c1b55cfca | 503 | |
DieterGraef | 0:d26c1b55cfca | 504 | for (i = 0; i < len; i++, rxdata++) { |
DieterGraef | 0:d26c1b55cfca | 505 | slipif_rxbyte_enqueue(netif, *rxdata); |
DieterGraef | 0:d26c1b55cfca | 506 | } |
DieterGraef | 0:d26c1b55cfca | 507 | } |
DieterGraef | 0:d26c1b55cfca | 508 | #endif /* SLIP_RX_FROM_ISR */ |
DieterGraef | 0:d26c1b55cfca | 509 | |
DieterGraef | 0:d26c1b55cfca | 510 | #endif /* LWIP_HAVE_SLIPIF */ |