LwIP学习笔记——STM32 ENC28J60移植与入门
(换句话说,想要从0開始移植LwIP必须对操作网卡很熟悉)
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] = 'A';
netif->hwaddr[1] = 'R';
netif->hwaddr[2] = 'M';
netif->hwaddr[3] = 'N';
netif->hwaddr[4] = 'E';
netif->hwaddr[5] = 'T'; /* 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. */
enc28j60_init(netif->hwaddr); // 【1】
}
static err_t
low_level_output(struct netif *netif, struct pbuf *p)
{
struct ethernetif *ethernetif = netif->state;
struct pbuf *q; enc28j60_init_send(p->tot_len); //【1】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. */
enc28j60_writebuf( q->payload, q->len ); //【2】send data from(q->payload, q->len);
} enc28j60_start_send(); //【3】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; len = enc28j60_packet_getlen(); // 【1】 #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 for(q = p; q != NULL; q = q->next) {
enc28j60_readbuf (q->payload, q->len ); //【2】read data into(q->payload, q->len);
}
enc28j60_finish_receive(); //【3】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 {
enc28j60_finish_receive(); //【4】drop packet();
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.drop);
} return p;
}
能够使用中断方法或查询方法。
#ifndef __LWIPOPTS_H__
#define __LWIPOPTS_H__ #define SYS_LIGHTWEIGHT_PROT 0 #define NO_SYS 1 #define NO_SYS_NO_TIMERS 1 /* ---------- 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 (5*1024) /* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application
sends a lot of data out of ROM (or other static memory), this
should be set high. */
#define MEMP_NUM_PBUF 10
/* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One
per active UDP "connection". */
#define MEMP_NUM_UDP_PCB 6
/* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP
connections. */
#define MEMP_NUM_TCP_PCB 10
/* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP
connections. */
#define MEMP_NUM_TCP_PCB_LISTEN 6
/* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP
segments. */
#define MEMP_NUM_TCP_SEG 12
/* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active
timeouts. */
#define MEMP_NUM_SYS_TIMEOUT 3 /* ---------- 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 1500 /* ---------- TCP options ---------- */
#define LWIP_TCP 1
#define TCP_TTL 255 /* Controls if TCP should queue segments that arrive out of
order. Define to 0 if your device is low on memory. */
#define TCP_QUEUE_OOSEQ 0 /* TCP Maximum segment size. */
#define TCP_MSS (1500 - 40) /* TCP_MSS = (Ethernet MTU - IP header size - TCP header size) */ /* TCP sender buffer space (bytes). */
#define TCP_SND_BUF (2*TCP_MSS) /* TCP sender buffer space (pbufs). This must be at least = 2 *
TCP_SND_BUF/TCP_MSS for things to work. */
#define TCP_SND_QUEUELEN (6 * TCP_SND_BUF)/TCP_MSS /* TCP receive window. */
#define TCP_WND (2*TCP_MSS) /* ---------- ICMP options ---------- */
#define LWIP_ICMP 1 /* ---------- DHCP options ---------- */
/* Define LWIP_DHCP to 1 if you want DHCP configuration of
interfaces. DHCP is not implemented in lwIP 0.5.1, however, so
turning this on does currently not work. */
#define LWIP_DHCP 0 /* ---------- UDP options ---------- */
#define LWIP_UDP 1
#define UDP_TTL 255 /* ---------- Statistics options ---------- */
#define LWIP_STATS 0
#define LWIP_PROVIDE_ERRNO 1 /**
* LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c)
*/
#define LWIP_NETCONN 0 /**
* LWIP_SOCKET==1: Enable Socket API (require to use sockets.c)
*/
#define LWIP_SOCKET 0 #endif /* __LWIPOPTS_H__ */
void LwIP_Config (void)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw; // 调用LWIP初始化函数
lwip_init(); IP4_ADDR(&ipaddr, 192, 168, 1, 16); // 设置网络接口的ip地址
IP4_ADDR(&netmask, 255, 255, 255, 0); // 子网掩码
IP4_ADDR(&gw, 192, 168, 1, 1); // 网关 // 初始化enc28j60与LWIP的接口。參数为网络接口结构体、ip地址、
// 子网掩码、网关、网卡信息指针、初始化函数、输入函数
netif_add(&enc28j60, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input); // 把enc28j60设置为默认网卡
netif_set_default(&enc28j60); netif_set_up(&enc28j60);
}
timer_typedef tcp_timer, arp_timer; /* 设定查询定时器 ARP定时器 */
timer_set(&tcp_timer, CLOCK_SECOND / 10); // tcp处理定时器 100ms
timer_set(&arp_timer, CLOCK_SECOND * 5); // arp处理定时器 5s
while (1) { if (enc28j60_packet_getcount() != 0) {
ethernetif_input(&enc28j60);
} // TCP 定时处理
if (timer_expired(&tcp_timer)) {
timer_set(&tcp_timer, CLOCK_SECOND / 4);
tcp_tmr();
} // ARP 定时处理
if (timer_expired(&arp_timer)) {
timer_set(&arp_timer, CLOCK_SECOND * 5);
etharp_tmr();
}
}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHVrYWk4NzExMDU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHVrYWk4NzExMDU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
版权声明:本文博主原创文章,博客,未经同意,不得转载。
LwIP学习笔记——STM32 ENC28J60移植与入门的更多相关文章
- jQuery学习笔记(一):入门
jQuery学习笔记(一):入门 一.JQuery是什么 JQuery是什么?始终是萦绕在我心中的一个问题: 借鉴网上同学们的总结,可以从以下几个方面观察. 不使用JQuery时获取DOM文本的操 ...
- Oracle学习笔记之四,SQL语言入门
1. SQL语言概述 1.1 SQL语言特点 集合性,SQL可以的高层的数据结构上进行工作,工作时不是单条地处理记录,而对数据进行成组的处理. 统一性,操作任务主要包括:查询数据:插入.修改和删除数据 ...
- canvas学习笔记(下篇) -- canvas入门教程--保存状态/变形/旋转/缩放/矩阵变换/综合案例(星空/时钟/小球)
[下篇] -- 建议学习时间4小时 课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...
- canvas学习笔记(中篇) -- canvas入门教程-- 颜色/透明度/渐变色/线宽/线条样式/虚线/文本/阴影/图片/像素处理
[中篇] -- 建议学习时间4小时 课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...
- canvas学习笔记(上篇)-- canvas入门教程 -- canvas标签/方块/描边/路径/圆形/曲线
[上篇] -- 建议学习时间4小时 课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...
- C#线程学习笔记九:async & await入门二
一.异步方法返回类型 只能返回3种类型(void.Task和Task<T>). 1.1.void返回类型:调用方法执行异步方法,但又不需要做进一步的交互. class Program { ...
- pandas库学习笔记(二)DataFrame入门学习
Pandas基本介绍——DataFrame入门学习 前篇文章中,小生初步介绍pandas库中的Series结构的创建与运算,今天小生继续“死磕自己”为大家介绍pandas库的另一种最为常见的数据结构D ...
- Spark (Python版) 零基础学习笔记(一)—— 快速入门
由于Scala才刚刚开始学习,还是对python更为熟悉,因此在这记录一下自己的学习过程,主要内容来自于spark的官方帮助文档,这一节的地址为: http://spark.apache.org/do ...
- 【SSM】学习笔记(二)——SpringMVC入门
原视频链接:https://www.bilibili.com/video/BV1Fi4y1S7ix/?p=43&spm_id_from=pageDriver&vd_source=8ae ...
随机推荐
- centos安装和卸载软件
==如何卸载: 1.打开一个SHELL终端 2.因为Linux下的软件名都包括版本号,所以卸载前最好先确定这个软件的完整名称. 查找RPM包软件:rpm -qa ×××* 注意:×××指软件名称开头的 ...
- 高性能 Socket 组件 HP-Socket v3.2.1-RC2 公布
HP-Socket 是一套通用的高性能 TCP/UDP Socket 组件,包括服务端组件.client组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C+ ...
- or1200中IMMU分析(续)
下面内容摘自<步步惊芯--软核处理器内部设计分析>一书 2 IMMU中的特殊寄存器 OR1200处理器中的IMMU包括第2组特殊寄存器,如表10.1所看到的. ITLBW0MRx是指令TL ...
- CSS设计指南之理解盒子模型
原文:CSS设计指南之理解盒子模型 一.理解盒模型 每一个元素都会在页面上生成一个盒子.因此,HTML页面实际上是由一堆盒子组成的.默认情况下,每个盒子的边框不可见,背景也是透明的,所以我们不能直接看 ...
- 做一个自动修改本机IP和mac的bat文件
原文:做一个自动修改本机IP和mac的bat文件 1.ip bat修改理论探讨 前两天我突然萌生了一个念头:能不能做一个小程序来实现自动配置或修改IP和mac,达到一键搞定的目的,这样尤其适合那些带着 ...
- Uva 10131 Is Bigger Smarter? (LIS,打印路径)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意: ...
- 1.cocos2dx 3.2环境结构
1 所需软件 jdk-7u25-windows-i586.exe python-2.7.8.amd64.msi cocos2d-x-3.2.zip apache-ant-1.9.4.zi ...
- Build制作模型
#include <iostream> using namespace std; //不知道为什么事实上非常好解释的东西在网上搞的人晕头转向的,下面是我的理解. //一个基类衍生出很多详细 ...
- 11gR2 Database Services for "Policy" and "Administrator" Managed Databases (文件 ID 1481647.1)
In this Document _afrLoop=1459311711568804&id=1481647.1&displayIndex=6&_afrWindowMode= ...
- 【剑指offer】打印1到最大的n数字
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25562105 剑指offer第12题.九度OJ測试通过. 题目描写叙述: 给定一个数字N, ...