LwIP移植和使用
LwIP移植和使用
本手册基于lwip-1.4.x编写,本人没有移植过1.4.0之前的版本,更早的版本或许有差别。如果看官发现问题欢迎联系<QQ: 937431539 email: 937431539@qq.com>
本文系个人原创,你可以转载,修改,重新发布,但请保留作者信息。
LwIP官网是:http://savannah.nongnu.org/projects/lwip/
你可以从这里获取源代码。当然也可以从Git获取源代码:
git clone git://git.savannah.nongnu.org/lwip.git
LwIP以BSD协议发布源代码,我们可以自由的使用,修改,发布或不发布源代码。
附件中有我移植的文件,可以用来参考。祝你移植顺利。
移植
1)新建几个头文件:
include/lwipopts.h // lwip配置文件
include/arch/cc.h // 平台相关。类型定义,大小端设置,内存对齐等
include/arch/perf.h // 平台相关的性能测量实现(没用)
include/arch/sys_arch.h // RTOS抽象层。信号量,mbox等类型定义,函数声明
lwipopts.h // lwip配置文件,详见附件
cc.h //类型定义,大小端设置,内存对齐等
#ifndef __CC_H__
#define __CC_H__ #include <stdint.h> /* Types based on stdint.h */
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 uintptr_t mem_ptr_t; /* Define (sn)printf formatters for these lwIP types */
#define U16_F "hu"
#define S16_F "hd"
#define X16_F "hx"
#define U32_F "lu"
#define S32_F "ld"
#define X32_F "lx"
#define SZT_F "uz" /* 选择小端模式 */
#define BYTE_ORDER LITTLE_ENDIAN /* Use LWIP error codes */
#define LWIP_PROVIDE_ERRNO /* 内存对齐 */
#if defined(__arm__) && defined(__ARMCC_VERSION)
/* Keil uVision4 tools */
#define PACK_STRUCT_BEGIN __packed
#define PACK_STRUCT_STRUCT
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(fld) fld
#define ALIGNED(n) __align(n) #endif
perf.h // 两个宏定义为空即可
#ifndef __PERF_H__
#define __PERF_H__ #define PERF_START /* null definition */
#define PERF_STOP(x) /* null definition */ #endif /* END __PERF_H__ */
sys_arch.h
RTOS抽象层的类型定义,函数声明,详细内容见 doc/sys_arch.h
2)建立RTOS抽象层文件:
port/sys_arch.c // RTOS抽象层实现
为了屏蔽不同RTOS在信号量,互斥锁,消息,任务创建等OS原语使用上的差别,lwip构造了一个RTOS的抽象层,规定了OS原语的数据类型名称和对应方法名称。我们要做的就是根据所用RTOS的api去实现这些原语。
比如移植lwip到raw-os上,信号量的移植:
类型定义,宏定义在sys_arch.h中
struct _sys_sem
{
RAW_SEMAPHORE *sem;
}; typedef struct _sys_sem sys_sem_t; // sys_sem_t是lwip的信号量类型名
#define SYS_SEM_NULL NULL
#define sys_sem_valid(sema) (((sema) != NULL) && ((sema)->sem != NULL))
#define sys_sem_set_invalid(sema) ((sema)->sem = NULL)
err_t sys_sem_new(sys_sem_t *sem, u8_t count)
{
RAW_SEMAPHORE *semaphore_ptr = ;
if (sem == NULL)
{
RAW_ASSERT();
} semaphore_ptr = port_malloc(sizeof(RAW_SEMAPHORE));
if(semaphore_ptr == )
{
RAW_ASSERT();
} //这是raw-os的API
raw_semaphore_create(semaphore_ptr, (RAW_U8 *)"name_ptr", count);
sem->sem = semaphore_ptr; return ERR_OK;
} void sys_sem_free(sys_sem_t *sem)
{
if((sem == NULL) || (sem->sem == NULL))
{
RAW_ASSERT();
} raw_semaphore_delete(sem->sem); //这是raw-os的API raw_memset(sem->sem, sizeof(RAW_SEMAPHORE), );
port_free(sem->sem);
sem->sem = NULL;
}
还有几个函数就不一一列举了,如有疑问看doc/sys_arch.txt
3)修改网卡框架文件:
netif/ethernetif.c
该文件是作者提供的网卡驱动和lwip的接口框架。
该文件中要改动的函数只有3个:
static void low_level_init(struct netif *netif);
static err_t low_level_output(struct netif *netif, struct pbuf *p);
static struct pbuf *low_level_input(struct netif *netif);
/* 你可以给网卡起个名字 */
/* Define those to better describe your network interface. */
#define IFNAME0 'e'
#define IFNAME1 '0' /**
* 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.
// 在这里添加网卡的私有数据,比如和网卡相关的信号量,互斥锁,
// 网卡状态等等,这不是必须的
};
3个网卡相关的函数只要改动红色部分,需根据具体的网卡驱动函数改动
static void low_level_init(struct netif *netif)
{
struct ethernetif *ethernetif = netif->state; /* set MAC hardware address length */
netif->hwaddr_len = ETHARP_HWADDR_LEN; /* 设置MAC地址, 必须与网卡初始化的地址相同 */
netif->hwaddr[0] = ;
netif->hwaddr[1] = ;
netif->hwaddr[2] = ;
netif->hwaddr[3] = ;
netif->hwaddr[4] = ;
netif->hwaddr[5] = ; /* maximum transfer unit */
netif->mtu = ; /* 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; /* 在这里添加其他初始化代码(如真正的网卡初始化, phy初始化等) */
} 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_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(); #if ETH_PAD_SIZE
pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif LINK_STATS_INC(link.xmit); return ERR_OK;
} 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 = ; // 获取将要接收的数据长度 #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 /* 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(); #if ETH_PAD_SIZE
pbuf_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);
} return p;
}
LwIP的使用
LwIP的初始化:
LwIP的初始化必须在RTOS启动之后才可以进行, 因为它的初始化代码使用了一些OS提供的功能!!!
初始化代码示例:
extern err_t ethernetif_init(struct netif *netif);
struct netif lpc1788_netif;
ip_addr_t e0ip, e0mask, e0gw; /* tcpip_init使用的回调函数,用于判断tcpip_init初始化完成 */
static void tcpip_init_done(void *pdat)
{
*(int *)pdat = ;
} void ethernetif_input(struct netif *netif);
// 一直调用ethernetif_input函数,从网卡读取数据
static void lwip_read_task(void *netif)
{
while()
{
ethernetif_input(netif);
}
} void init_lwip()
{
struct netif *pnetif = NULL;
int flag = ; tcpip_init(tcpip_init_done, &flag); // lwip协议栈的初始化
while(flag); IP4_ADDR(&e0ip, ,,,); // 设置网卡ip
IP4_ADDR(&e0mask, ,,,); // 设置子网掩码
IP4_ADDR(&e0gw, ,,,); // 设置网关 //给lwip添加网卡
pnetif = netif_add(&lpc1788_netif, &e0ip, &e0mask, &e0gw,
NULL, ethernetif_init, tcpip_input);
netif_set_default(pnetif); // 设置该网卡为默认网卡
netif_set_up(&lpc1788_netif); // 启动网卡,可以唤醒DHCP等服务 // 创建一个任务。这个任务负责不停的调用ethernetif_input函数从网卡读取数据
raw_task_create(&lwip_read_obj, (RAW_U8 *)"lwip_read", &lpc1788_netif,
CONFIG_RAW_PRIO_MAX - , , lwip_read_stk,
LWIP_READ_STK_SIZE , lwip_read_task, );
}
附件:
http://pan.baidu.com/s/1gdfz1zd
LwIP移植和使用的更多相关文章
- 关于lwip移植到ucsos-ii平台的遇到的问题(一)
移植的步骤参照<Day_Day_Up笔记之uCOS-II_LwIP_在_STM32F107_上移植>,<uCOS平台下的LwIP移植笔记>,<嵌入式网络那些事>. ...
- lwip移植到stm32上-enc28j60,103mcu(2)
前面小玩了一下ucos和lwip,但是都还不是真正的网络多任务,真正的网络多任务应该是什么样子的呢?应该是有一个专门的任务负责网络的通讯,他负责将数据发送出去,将数据接收回来,而其他的需要用到网络的任 ...
- lwip 移植
一.源码目录结构 api . core.netif. include core下又有IPV4 . IPV6 . SNMP 和.c文件 include下又有IPV4.IPV6.LWIP.netif ne ...
- LwIP移植uCos+stm32f407
LwIP同操作系统一起工作的时候模型如下: 1.TCP/IP协议栈和应用程序以分离的任务运行 2.应用同协议栈沟通是通过API函数调用(API函数调用事实上就是通过OS自带的进程间通信机制,由应用程序 ...
- LWIP移植文件介绍
在介绍文件之前首先介绍一下DMA描述符 stm32以太网模块接收/发送FIFO和内存之间的以太网传输是通过以太网DMA使用DMA描述符完成的,一共有两个描述符列表:一个用于接收,一个用于发送, 两个列 ...
- LWIP移植
- LwIP学习笔记——STM32 ENC28J60移植与入门
0.前言 去年(2013年)的整理了LwIP相关代码,并在STM32上"裸奔"成功.一直没有时间深入整理,在这里借博文整理总结.LwIP的移植过程细节很多,博文也不可能一一 ...
- lwip协议栈移植(1)
lwip移植分为两类: 1,只移植内核核心,用户应用程序编写只能基于raw/callback api进行 2,移植内核核心和上层API函数模块,用户可以使用所有三种API编程,即 raw/callba ...
- LWIP总结
介绍 Lwip,light weight IP:是由Adam Dunkels 开发的一个小型开源的TCP/IP协议栈:目前已经为全球共同开发的开源协议:支持TCPIP协议族的核心协议:包括:ARP/I ...
随机推荐
- Tomcat7服务器删除后重新安装失败问题
学习JSP时,需要修改conf下的配置文件,确发现无论如何修改权限都无法保存,所以决定卸载重装到D盘,卸载时又弹出一个对话框,具体内容没看,但是就是卸载不了,所以打算手动删除. 删除后,重装时安装失败 ...
- 洛谷P1288 取数游戏II[博弈论]
题目描述 有一个取数的游戏.初始时,给出一个环,环上的每条边上都有一个非负整数.这些整数中至少有一个0.然后,将一枚硬币放在环上的一个节点上.两个玩家就是以这个放硬币的节点为起点开始这个游戏,两人轮流 ...
- MyBatis 智能标签
使用Where 只能标签 检索部门Y2162Dept 数据库已存在表Y2162Dept 实现动态查询 Deptno Deptname 赋值 不赋值 不赋值 赋值 赋值 赋值 不赋值 不赋值 <! ...
- SQL-语句实现九九乘法表
下面用while 和 if 条件写的SQL语句的四种九九乘法表 sql语句实现--x 左下角九九乘法表 DECLARE @I INT ,@J INT,@S VARCHAR(100) SET @I=1 ...
- SharePoint 2013技巧分享系列 - 隐藏Blog和Apps左侧导航菜单
企业内部网中,不需要员工创建Blog或者创建,安装SharePoint应用,因此需要在员工个人Web页面需要隐藏Blog或者Apps导航菜单, 其步骤设置如下: 该技巧适合SharePoint 201 ...
- 大流量网站性能优化:一步一步打造一个适合自己的BigRender插件
BigRender 当一个网站越来越庞大,加载速度越来越慢的时候,开发者们不得不对其进行优化,谁愿意访问一个需要等待 10 秒,20 秒才能出现的网页呢? 常见的也是相对简单易行的一个优化方案是 图片 ...
- 关于 MonoDevelop on Linux 单步调试问题的解决
在 MonoDevelop 中默认是关闭对外部程序集(.dll)的调试,可通过如下步骤来解决这个问题. 通过菜单[Edit]-[Preferences]-[Debugger]进入到调试器的设置页,把“ ...
- 关于dll
今日看到一个不带dllmain的dll,忽然觉得有点奇怪,然后查了一下,原来dll还可以不需要dllmain,甚至可以自己定义入口 先mark以下的资料,有空再总结一下...同时dll劫持,有必要亲身 ...
- Poisson泊松分布
PMF 若随机变量\(K\)的概率质量函数PMF为 \[ P(K = k) = e^ {-\lambda} \frac {\lambda^k}{k!} \] 则称:\(K \sim Poisson(\ ...
- Windows7SP1补丁包(Win7补丁汇总) 32位/64位版 更新截至2016年11月
Windows7SP1(64位)补丁包(Win7补丁汇总)更新到本月最新.包含Windows7SP1中文版所有重要补丁,可离线安装,适用于Windows 7 SP1 64位 简体中文系统.包含Inte ...