基于个人兴趣,决定实现一个和方案公司提供的uboot收发广播的功能。记录笔记如下。
SDK版本:Hi3518E_V100R001C01SPC081
1. 由于我手头的板子的Phy是RMII模式,因此先按这篇帖子进行将uboot默认的网络驱动改为RMII模式。
http://blog.csdn.net/skdkjzz/article/details/39931915
2. 然后设置好Phy的CONFIG_PHY_ADDR_U和CONFIG_PHY_ADDR_D,这个我不太懂是为什么,估计和硬件有关,各位看官可能这里不能完全照抄,我有空翻一翻Phy的规格书。编辑 u-boot-2010.06\include\configs\hi3518e.h
3. 在common目录下新建文件cmd_udp.c,新建一个uboot cmd
#include <common.h>
#include <command.h>
#include <net.h> char _ctoi(char c)
{
if(c>=''&&c<='')return c-'';
if(c>='a'&&c<='f')return c-'a'+;
return ;
} int _atoi(char* s)
{
unsigned int a=;
unsigned int b=;
int i=;
while(s[i]){
a*=;
a+=_ctoi(s[i]);
i++;
}
return a;
} int _atox(char* s)
{
unsigned int a=;
unsigned int b=;
int i=;
while(s[i]){
a<<=;
a|=_ctoi(s[i]);
i++;
if(i>=)break;
}
return a;
} int _strlen(char*s)
{
int i=;
while(s[i]){
i++;
}
return i;
} void _getmac(char* s,char* eth)
{
unsigned char a=;
int i=;
while(s[i]){
a<<=;
a|=_ctoi(s[i]);
if((i&)==){
eth[i>>]=a;
a=;
}
i++;
}
} int do_udp_broadcast(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
uchar eth[]= {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
char* UdpServerIP="255.255.255.255";
int dest_port = , src_port =;
uchar *str;
int len; switch(argc){
case :
puts("Params error, try again\nusage: [udp_b] [string] or [udp_b] [dest_port] [src_port] [string]\n");
return ;
break;
case :
str = argv[];
len = strlen(str);
break;
case :
dest_port = _atoi(argv[]);
src_port = _atoi(argv[]);
str = argv[];
len = strlen(argv[]);
break;
default:
return ;
break;
} printf("len=%d\n",len);
show_boot_progress();
if(NetLoop_UDP(eth,UdpServerIP,dest_port,src_port,str,len)<){
show_boot_progress(-);
return ;
} return ;
} U_BOOT_CMD(
udp_b, , , do_udp_broadcast,
"Send or receive UDP broadcast to/from server using UDP protocol",
"[udp_b] [string] or [udp_b] [dest_port] [src_port] [string]"
);
4. 给common目录的makefile添加
COBJS-$(CONFIG_CMD_UDP) += cmd_udp.o
5. 在net目录新建文件udp处理函数,udp.c和udp.h
udp.c
#include <common.h>
#include <watchdog.h>
#include <command.h>
#include <net.h>
#include <miiphy.h>
#include "udp.h" void
UdpHandler(uchar *pkt, unsigned dest, unsigned src, unsigned len) //这个函数是处理包,先放下,有空再写
{
//printf("handler udp packet\n");
printf("Receive udp packet: %s\n",pkt);
} void
udpSend(void)
{
uchar *pkt;
pkt = (uchar *)NetTxPacket;
pkt += NetSetEther(pkt, UdpServerEther, PROT_IP);
NetSetIP (pkt, UdpServerIP, UdpServerPort, UdpOurPort, data_len);
pkt += IP_HDR_SIZE;
memcpy(pkt, pkt_data, data_len);
(void) eth_send(NetTxPacket, (pkt - NetTxPacket) + data_len);
} void
UdpStart(const uchar *remoteeth, char *remoteip, int remoteport,int thisport, const char *data, int datalength)
{
UdpServerEther = remoteeth;
UdpServerIP = string_to_ip(remoteip);
UdpServerPort = remoteport;
UdpOurPort = thisport;
pkt_data = data;
data_len = datalength; udpSend();
printf("Send,ready to receive...\n");
}
udp.h
#ifndef __UDP_H__
#define __UDP_H__ static uchar *UdpServerEther;
static IPaddr_t UdpServerIP;
static int UdpServerPort;/* The UDP port at their end
*/
static int UdpOurPort;/* The UDP port at our end
*/
static uchar *pkt_data;
static int data_len; void UdpHandler(uchar *pkt, unsigned dest, unsigned src, unsigned len); #endif
5. 修改net目录的net.c,在最下面加入NetLoop_UDP函数,并添加udp.h头文件引用 #include "udp.h"
int
NetLoop_UDP(const uchar* remorteth, char* remoteip, int remoteport, int thisport, const char* data, int datalength)
{
bd_t *bd = gd->bd; /* XXX problem with bss workaround */
NetTxPacket = NULL; if (!NetTxPacket){
int i;
/*
* Setup packet buffers, aligned correctly.
*/
NetTxPacket = &PktBuf[] + (PKTALIGN - );
NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
for (i = ; i < PKTBUFSRX; i++) {
NetRxPackets[i] = NetTxPacket + (i+)*PKTSIZE_ALIGN;
}
} eth_halt();
if (eth_init(bd) < ) {
eth_halt();
return(-);
} restart:
eth_getenv_enetaddr("ethaddr", NetOurEther);
/*
* Start the ball rolling with the given start function. From
* here on, this code is a state machine driven by received
* packets and timer events.
*/
NetInitLoop(TFTP); switch (net_check_prereq (TFTP)) {
case :
/* network not configured */
eth_halt();
return (-); case :
NetSetHandler(UdpHandler);
UdpStart(remorteth,remoteip, remoteport, thisport, data, datalength);
break;
} /*
* Main packet reception loop. Loop receiving packets until
* someone sets `NetState' to a state that terminates.
*/
for (;;) {
/*
* Check the ethernet for a new packet. The ethernet
* receive routine will process it.
*/
eth_rx();
NetSetHandler(UdpHandler); /*
* Abort if ctrl-c was pressed.
*/
if (ctrlc()) {
eth_halt();
puts ("\nAbort by Ctrl+C\n");
return (-);
}
}
}
6. 修改net目录下的makefile,添加
COBJS-$(CONFIG_CMD_NET) += udp.o
7. 这样操作后可以通过udp_b命令发送和接收UDP广播了。实际跑在板子上能发送UDP广播,但接收不到UDP广播包。
经过查看芯片规格书之后,发现有寄存器可以屏蔽广播包,我们查看uboot网络驱动源码之后发现确实默认将UDP广播包屏蔽了。
那么我们修改\drivers\net\hisfv300下的glb.c
将
hieth_writel_bits(ld, 0, GLB_MACTCTRL, BITS_BROAD2CPU_UP);
修改为
hieth_writel_bits(ld, 1, GLB_MACTCTRL, BITS_BROAD2CPU_UP);
8. 修改部分到此结束,现在我们进行测试。我采用的测试工具是我自己用c#写的udp收发小工具,源码就不发了,很简单的小工具。
参考链接:
http://blog.csdn.net/skdkjzz/article/details/39931915