1.LWIP源码下载
官网:https://savannah.nongnu.org/projects/lwip/
版本:lwip-2.2.0
2.移植
2.1LWIP目录
2.1.1.porttable目录
2.1.1.1.cc.h
#ifndef CC_H_
#define CC_H_
#define SYS_ARCH_DECL_PROTECT(lev)
#define SYS_ARCH_PROTECT(lev)
#define SYS_ARCH_UNPROTECT(lev)
typedef int sys_prot_t;
#endif
2.1.1.2.lwipopts.h
#ifndef __LWIPOPTS_H__
#define __LWIPOPTS_H__
/* ---------- NO SYS options ---------- */
#define NO_SYS 1
/* ---------- Core locking options ---------- */
/* ---------- Memory options ---------- */
/* MEM_ALIGNMENT: should be set to the alignment of the CPU for which
lwIP is compiled.
4 byte alignment -> define MEM_ALIGNMENT to 4,
2 byte alignment -> define MEM_ALIGNMENT to 2.
*/
#define MEM_ALIGNMENT 4
/* MEM_SIZE: the size of the heap memory. If the application will send
a lot of data that needs to be copied, this should be set high. */
#define MEM_SIZE 2048
/* ---------- Pbuf options ---------- */
///* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */
#define PBUF_POOL_SIZE 10
///* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */
#define PBUF_POOL_BUFSIZE 2048
/* ---------- ARP options ---------- */
#define LWIP_ARP 1
/* ---------- IP options ---------- */
#define LWIP_IPV4 1
/* ---------- ICMP options ---------- */
#define LWIP_ICMP 1
/* ---------- RAW options ---------- */
#define LWIP_RAW 0
/* ---------- DHCP options ---------- */
#define LWIP_DHCP 0
/* ---------- IGMP options ----------*/
#define LWIP_IGMP 0
/*---------- DNS options -----------*/
#define LWIP_DNS 0
/* ---------- UDP options ---------- */
#define LWIP_UDP 0
/* ---------- TCP options ---------- */
#define LWIP_TCP 0
/* ---------- THREAD options ---------- */
#if !NO_SYS
#define TCPIP_THREAD_PRIO 3 // ref freertos:[low 0-6 high]
#define TCPIP_THREAD_STACKSIZE 2048 // stack size in bytes for the new thread (may be ignored by ports)
#define TCPIP_MBOX_SIZE 24 // number of messages in this mbox
#define DEFAULT_RAW_RECVMBOX_SIZE TCPIP_MBOX_SIZE
#define DEFAULT_UDP_RECVMBOX_SIZE TCPIP_MBOX_SIZE
#define DEFAULT_TCP_RECVMBOX_SIZE TCPIP_MBOX_SIZE
#define DEFAULT_ACCEPTMBOX_SIZE TCPIP_MBOX_SIZE
#endif
/* ---------- Sequential layer options ---------- */
#define LWIP_NETCONN (NO_SYS==0)
/* ---------- SOCKET options ---------- */
#define LWIP_SOCKET (NO_SYS==0)
#define LWIP_SO_RCVTIMEO (NO_SYS==0)
#define LWIP_SO_SNDTIMEO (NO_SYS==0)
/*---------- Statistics options ----------*/
#define LWIP_STATS 0
/*---------- Checksum options ----------*/
/*---------- IPv6 options ----------*/
/*---------- Hook options ----------*/
/* ---------- Debugging options ---------- */
#define LWIP_DEBUG LWIP_DBG_ON
/* ---------- OTHER options ---------- */
#if !NO_SYS
#define LWIP_PROVIDE_ERRNO
#endif
#define LWIP_TIMEVAL_PRIVATE 0
/* Self-define data. */
/* LWIP MAC address define. */
#define LWIP_MAC_ADDR 0x11, 0x22, 0x33, 0x44, 0x55, 0x66
/* LWIP net host name define. */
#define LWIP_NETIF_HOSTNAME 1
#define LWIP_NETIF_HOSTNAME_TEXT "LwIP_Net"
/* LWIP use IPV4 address. */
#define LWIP_IPV4 1
/* Ethernet link speed define. */
#define LWIP_LINK_SPEED_IN_BPS 100000000
/* LWIP gateway, IP address and netmask define. */
#define LWIP_INIT_IPADDR(addr) IP4_ADDR((addr), 192,192,10,40)
#define LWIP_INIT_NETMASK(addr) IP4_ADDR((addr), 255,255,255,0)
#define LWIP_INIT_GW(addr) IP4_ADDR((addr), 192,192,10,254)
#endif /* __LWIPOPTS_H__ */
2.1.1.3.sys_arch.c
#include "lwip/arch.h"
void timer_mdelay(u32_t delay)
{
unsigned long count=0;
while(count < (delay * 0xffff))
{
count++;
}
}
u32_t sys_now(void)
{
timer_mdelay(1);
return 0;
}
2.1.1.4.ethernetif.c
/**
* @file
* Ethernet Interface Skeleton
*
*/
/*
* 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 <adam@sics.se>
*
*/
/*
* This file is a skeleton for developing Ethernet network interface
* drivers for lwIP. Add code to the low_level functions and do a
* search-and-replace for the word "ethernetif" to replace it with
* something that better describes your network interface.
*/
#include "lwip/opt.h"
#if 1 /* don't build, this is only a skeleton, see previous comment */
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/stats.h"
#include "lwip/snmp.h"
#include "lwip/ethip6.h"
#include "lwip/etharp.h"
#include "netif/ppp/pppoe.h"
/* Define those to better describe your network interface. */
#define IFNAME0 'e'
#define IFNAME1 'n'
struct netif gnetif;
/**
* Helper struct to hold private data used to operate your ethernet interface.
* Keeping the ethernet address of the MAC in this struct is not necessary
* as it is already kept in the struct netif.
* But this is only an example, anyway...
*/
struct ethernetif {
struct eth_addr *ethaddr;
/* Add whatever per-interface state that is needed here. */
};
/* Forward declarations. */
static void ethernetif_input(struct netif *netif);
/**
* In this function, the hardware should be initialized.
* Called from ethernetif_init().
*
* @param netif the already initialized lwip network interface structure
* for this ethernetif
*/
static void
low_level_init(struct netif *netif)
{
struct ethernetif *ethernetif = netif->state;
/* set MAC hardware address length */
netif->hwaddr_len = ETHARP_HWADDR_LEN;
/* set MAC hardware address */
netif->hwaddr[0] = 0x11;
netif->hwaddr[1] = 0x22;
netif->hwaddr[2] = 0x33;
netif->hwaddr[3] = 0x44;
netif->hwaddr[4] = 0x55;
netif->hwaddr[5] = 0x66;
/* 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;
#if LWIP_IPV6 && LWIP_IPV6_MLD
/*
* For hardware/netifs that implement MAC filtering.
* All-nodes link-local is handled by default, so we must let the hardware know
* to allow multicast packets in.
* Should set mld_mac_filter previously. */
if (netif->mld_mac_filter != NULL) {
ip6_addr_t ip6_allnodes_ll;
ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
}
#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
/* Do whatever else is needed to initialize interface. */
}
void send_data_from(u32_t *payload, u16_t len)
{
pg_gmac_send_one_frame_data(payload, len);
}
void read_data_into(void *payload, u16_t len)
{
pg_gmac_receive_one_frame_data(payload, len);
}
/**
* This function should do the actual transmission of the packet. The packet is
* contained in the pbuf that is passed to the function. This pbuf
* might be chained.
*
* @param netif the lwip network interface structure for this ethernetif
* @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
* @return ERR_OK if the packet could be sent
* an err_t value if the packet couldn't be sent
*
* @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
* strange results. You might consider waiting for space in the DMA queue
* to become available since the stack doesn't retry to send a packet
* dropped because of memory failure (except for the TCP timers).
*/
static err_t
low_level_output(struct netif *netif, struct pbuf *p)
{
struct ethernetif *ethernetif = netif->state;
struct pbuf *q;
//initiate_transfer();
#if ETH_PAD_SIZE
pbuf_remove_header(p, ETH_PAD_SIZE); /* drop the padding word */
#endif
for (q = p; q != NULL; q = q->next) {
/* Send the data from the pbuf to the interface, one pbuf at a
time. The size of the data in each pbuf is kept in the ->len
variable. */
send_data_from(q->payload, q->len);
}
//signal that packet should be sent();
MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
if (((u8_t *)p->payload)[0] & 1) {
/* broadcast or multicast packet*/
MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
} else {
/* unicast packet */
MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
}
/* increase ifoutdiscards or ifouterrors on error */
#if ETH_PAD_SIZE
pbuf_add_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif
LINK_STATS_INC(link.xmit);
return ERR_OK;
}
/**
* Should allocate a pbuf and transfer the bytes of the incoming
* packet from the interface into the pbuf.
*
* @param netif the lwip network interface structure for this ethernetif
* @return a pbuf filled with the received packet (including MAC header)
* NULL on memory error
*/
static struct pbuf *
low_level_input(struct netif *netif)
{
struct ethernetif *ethernetif = netif->state;
struct pbuf *p, *q;
u16_t len;
/* Obtain the size of the packet and put it into the "len"
variable. */
len = 2048;
#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_remove_header(p, ETH_PAD_SIZE); /* drop the padding word */
#endif
/* We iterate over the pbuf chain until we have read the entire
* packet into the pbuf. */
for (q = p; q != NULL; q = q->next) {
/* Read enough bytes to fill this pbuf in the chain. The
* available data in the pbuf is given by the q->len
* variable.
* This does not necessarily have to be a memcpy, you can also preallocate
* pbufs for a DMA-enabled MAC and after receiving truncate it to the
* actually received size. In this case, ensure the tot_len member of the
* pbuf is the sum of the chained pbuf len members.
*/
read_data_into(q->payload, q->len);
}
//acknowledge that packet has been read();
MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
if (((u8_t *)p->payload)[0] & 1) {
/* broadcast or multicast packet*/
MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
} else {
/* unicast packet*/
MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
}
#if ETH_PAD_SIZE
pbuf_add_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif
LINK_STATS_INC(link.recv);
} else {
//drop packet();
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.drop);
MIB2_STATS_NETIF_INC(netif, ifindiscards);
}
return p;
}
/**
* This function should be called when a packet is ready to be read
* from the interface. It uses the function low_level_input() that
* should handle the actual reception of bytes from the network
* interface. Then the type of the received packet is determined and
* the appropriate input function is called.
*
* @param netif the lwip network interface structure for this ethernetif
*/
static void
ethernetif_input(struct netif *netif)
{
struct ethernetif *ethernetif;
struct eth_hdr *ethhdr;
struct pbuf *p;
ethernetif = netif->state;
/* move received packet into a new pbuf */
p = low_level_input(netif);
/* if no packet could be read, silently ignore this */
if (p != NULL) {
/* pass all packets to ethernet_input, which decides what packets it supports */
if (netif->input(p, netif) != ERR_OK) {
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
pbuf_free(p);
p = NULL;
}
}
}
/**
* Should be called at the beginning of the program to set up the
* network interface. It calls the function low_level_init() to do the
* actual setup of the hardware.
*
* This function should be passed as a parameter to netif_add().
*
* @param netif the lwip network interface structure for this ethernetif
* @return ERR_OK if the loopif is initialized
* ERR_MEM if private data couldn't be allocated
* any other err_t on error
*/
err_t
ethernetif_init(struct netif *netif)
{
struct ethernetif *ethernetif;
LWIP_ASSERT("netif != NULL", (netif != NULL));
// ethernetif = mem_malloc(sizeof(struct ethernetif));
// if (ethernetif == NULL) {
// LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
// return ERR_MEM;
// }
#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.
*/
MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
netif->state = ethernetif;
netif->name[0] = IFNAME0;
netif->name[1] = 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...) */
#if LWIP_IPV4
netif->output = etharp_output;
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#endif /* LWIP_IPV6 */
netif->linkoutput = low_level_output;
//ethernetif->ethaddr = (struct eth_addr *) & (netif->hwaddr[0]);
/* initialize the hardware */
low_level_init(netif);
return ERR_OK;
}
void lwips_init(void)
{
#if LWIP_IPV4
ip4_addr_t ipaddr, netmask, gw;
#if (!LWIP_DHCP) && (!LWIP_AUTOIP)
LWIP_INIT_GW(&gw);
LWIP_INIT_IPADDR(&ipaddr);
LWIP_INIT_NETMASK(&netmask);
#endif /* (!LWIP_DHCP) && (!LWIP_AUTOIP) */
#endif /* LWIP_IPV4 */
/* LwIP service init process. */
lwip_init();
/* Config netif layer parameters. */
netif_set_default(netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, netif_input));
/* Check to set netif up flag. */
if (netif_is_link_up(&gnetif))
{
/* If netif link is fully configured, this function must be called. */
netif_set_up(&gnetif);
}else{
/* If netif link is down, this function must be called. */
netif_set_down(&gnetif);
}
}
void LwIPTestProcess(void)
{
/* Ethernet data receiving process. */
ethernetif_input(&gnetif);
/* Check LwIP timeout event. */
sys_check_timeouts();
}
#endif /* 0 */
2.1.2.src目录
1.新建src目录
将lwip-2.2.0源码下的src目录下文件全部复制到src目录下。
2.2.数据收发底层实现
2.2.1.接收数据
略
2.2.2.发送数据
略
2.3.main.c
int main(void)
{
gmac_init();//gmac控制器初始化——reset、gate、clock、sycctrl、mio、mac等
phy_init();//phy初始化——speed、duplex、auto-negotiation、loopback等
lwips_init();
while(1) {
LwIPTestProcess();
}
}