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 ...
随机推荐
- 【Python网络编程】利用Python进行TCP、UDP套接字编程
之前实现了Java版本的TCP和UDP套接字编程的例子,于是决定结合Python的学习做一个Python版本的套接字编程实验. 流程如下: 1.一台客户机从其标准输入(键盘)读入一行字符,并通过其套接 ...
- NOIP2015斗地主[DFS 贪心]
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- ThinkPHP实现支付宝接口功能
最近做系统,需要实现在线支付功能,毫不犹豫,选择的是支付宝的接口支付功能.这里我用的是即时到帐的接口,具体实现的步骤如下:一.下载支付宝接口包下载地址:https://doc.open.alipay. ...
- 利用DotSpatial发布WMS, WFS服务
我们遇到的几个给政府部门做的GIS系统,一般都只要面子,只要好看,领导高兴得不得了,点点这里点点那里,哟,这按钮一点还会转,领导开心得跟朵花似的...要是搞个各种分析什么的全堆上来,他就嫌烦了...这 ...
- WEB安全:XSS漏洞与SQL注入漏洞介绍及解决方案
对web安全方面的知识非常薄弱,这篇文章把Xss跨站攻击和sql注入的相关知识整理了下,希望大家多多提意见. 对于防止sql注入发生,我只用过简单拼接字符串的注入及参数化查询,可以说没什么好经验,为避 ...
- docfx daylybuild
参考:https://myget.org/gallery/docfx-dev 根据对应的vs或nuget版本中添加地址. PS:daylybuild可能包含很多错误哦.
- Css--深入学习之切角
本文是作者从别的网站和文章学习了解的知识,简单做了个笔记,想要学习更多的可以参考这里:[css进阶]伪元素的妙用--单标签之美,奇思妙想 带切角的矩形: 该图来源于(奇思妙想) Css代码: .not ...
- 如何对多个属性进行transform
w3school对transform的介绍很简单 transform: none|transform-functions; transform的默认值是none 其所举的例子也只是对一个值进行过渡,其 ...
- Android Studio NDK初探
Android Studio中实现NDK开发较之前Eclipse+Cygwin,方便了很多. 本文以最简单的从C程序中获取字符串,并显示到MainActivity的TextView上为例进行NDK开发 ...
- <<< Tomcat运行报错IOException while loading persisted sessions: java.io.EOFException
解决方法:将work下面的文件清空,主要是*.ser文件,或者只是删除掉session.ser即可以解决.