求教大侠们关于一点关于串口以太网的问题,小弟十分感谢!
我用的是motorola的mc9s12ne64,功能是串口转以太网。以前是把ip和物理地址固化进去的,现在想把程序改成可以通过串口精灵之类的界面给板子设定ip,可是总是无法成功,附上程序,真诚的希望可以可以得到大家的帮助,谢谢啊!
*****************************************************************************
* (c) freescale inc. 2004 all rights reserved
*
* file name : debug.c
*
* purpose: diagnostics output through sci
*
*
* description: implementation of the diagnostics output - enabled when
* eth_debug keyword is defined (-deth_debug)
* _sci_base must be set to sci0 or sci1
* (see compiler command line arguments)
*
* version : 2.1
* date : 02/04/04
*
*****************************************************************************/
//#include <stdio.h>
#include "ne64debug.h"
#include "mc9s12ne64.h"
#include "eeprom.h"
/*************shell fun used sci0*******************/
extern tu08 hard_addr[6];
extern tu08 prot_addr[4];
extern tu08 netw_mask[4];
extern tu08 dfgw_addr[4];
extern tu08 brcs_addr[4];
extern tu08 demo_rmthost_ip[4];
extern tu16 demo_local_port;
extern tu16 demo_rmthost_prt;
tu08 tcpip_mode=0;
tu32 sci_baud;
tu16 sci_base;
tu08 sci_mode;
tu08 sci_baudtemp;
tu08 prot_addrf,netw_maskf,dfgw_addrf,demo_rmthost_ipf,
demo_local_portf,demo_rmthost_prtf,mac_addf,
sci_modef,set_baudf,tcp_modef;
/******************************/
#ifdef eth_debug
#define _base 0x0000 /** < base on register map */
#define eclk 25000000 /** < this is busclk */
#define reg(x) *((volatile tu08 *)(_base+x))
#define regw(x) *((volatile tu16 *)(_base+x))
//#define _sci_base sci0
//#ifndef _sci_base
//#error _sci_base not defined, use sci0 or sci1
//#endif
#define sci0 0x00c8
#define sci1 0x00d0
#define scibd regw(sci_base+0x00)
#define scicr1 reg(sci_base+0x02)
#define scicr2 reg(sci_base+0x03)
#define scisr1 reg(sci_base+0x04)
#define scidrl reg(sci_base+0x07)
//===================================================
void initdebug(void)
{
#define baud_rate 9600
#define baud_div eclk/16/baud_rate
sci_base = sci0;
scibd= baud_div;
scicr1= 0;
//scicr2= sci1cr2_te_mask | sci1cr2_re_mask | sci1cr2_rwu_mask ;
scicr2 = 0x2c;
}
//===================================================
/* send string via sci */
//===================================================
void debugt(ts08 * s)
{
while (*s != 0)
{
while (!(scisr1 & 0x80));
scidrl=*s;
s++;
}
}
//===================================================
/* send string via sci for protocol display*/
//===================================================
void debugs(tu08 s)
{
while (!(scisr1 & 0x80));
scidrl=s;
}
//===================================================
/* convert 8bit number to 2 hexadecimal ascii characters */
//===================================================
ts08 cvt[16]={ '0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f' };
//===================================================
void ctoh (ts08 * s, tu08 c)
{
*s= cvt[c / 16];
s++;
*s= cvt[c % 16];
}
//===================================================
/* send 16bit number in hexa via sci */
//===================================================
void debugi(tu16 i)
{
ts08 s[6];
s[0]=' ';
ctoh(s+1,(tu08)(i / 256));
ctoh(s+3,i % 256);
s[5]=0;
debugt(s);
}
//===================================================
/* send 8bit number in hexa via sci */
//===================================================
void debugc(tu08 c)
{
ts08 s[4];
s[0]=' ';
ctoh(s+1,c);
s[3]=0;
debugt(s);
}
//===================================================
/* send newline and return characters via sci */
//===================================================
void debugnl(void)
{
debugt("\r\n");
}
/*********************shell function**************************/
//===================================================
/* shell write data to ipaddress/net mask/route receive buffer */
//===================================================
tu08 shell_recedata(tu08 *p)
{
tu08 dataok=0,s;
tu08 i,j=0;
tu08 bufftemp[3];
for(i=0;i <4;)
{
while(!(scisr1 & 0x20));
s = scidrl;
if((s !='.')&&(s !=0x0d)){
if(('0' <= s)&&(s <='9')){
if(j==3){
debugs(0x07);
// dataok=1;
// break;
} else{
debugs(s);
s = s&0x0f;
bufftemp[j]=s;
j++;
}
} else if(s==0x08)
{
if(j!=0){
debugs(0x08);
j--;
} else {
debugs(0x07);
}
} else{
debugs(0x07);
//dataok = 1;
//break;
}
// bufftemp[j]=s;
// j++;
} else
{
if((s=='.')&&(i!=3)&&(j!=0))
{
debugt(".");
if(j==1)p[i]=bufftemp[0];
if(j==2)p[i]=bufftemp[1]+bufftemp[0]*10;
if((j==3)&&(bufftemp[0] <3))p[i]=bufftemp[2]+bufftemp[1]*10+bufftemp[0]*100;
else if((j==3)&&(bufftemp[0]> 3))dataok=1;
i++;
j=0;
}else if((s==0x0d)&&(i==3)&&(j!=0))
{
if(j==1)p[i]=bufftemp[0];
if(j==2)p[i]=bufftemp[1]+bufftemp[0]*10;
if((j==3)&&(bufftemp[0] <3))p[i]=bufftemp[2]+bufftemp[1]*10+bufftemp[0]*100;
else if((j==3)&&(bufftemp[0]> 3))dataok=1;
i++;
j=0;
} else if((s==0x0d)&&(i==0)&&(j==0)){
dataok=1;
return(dataok);
} else {
debugs(0x07);
// dataok = 1;
}
}
}
return(dataok);
}
//===================================================
/* shell mac address str to num funciton select via sci */
//===================================================
tu08 strtonum(tu08 s)
{
if(('0' <= s)&&(s <='9')){
s = s&0x0f;
}
else if(('a' <= s)&&(s <='f')){
s=s&0x0f;
s+=9;
}
else if(('a' <= s)&&(s <='f')){
s=s&0x0f;
s+=9;
}
else if(s==0x08){
//debugs(0x08);
return(128);
}
else return(255);
return(s);
}
//===================================================
/* shell configration sci baud_rate process via sci */
//===================================================
void shell_scibaud(void)
{
tu08 i,ss;
set_baudf=0;
for(i=0;i <1;)
{
while(!(scisr1 & 0x20));
ss = scidrl;
if(ss==0x0d) {
set_baudf=1;
break;
}
sci_baudtemp= strtonum(ss);
if(sci_baudtemp!=255)
{
debugs(ss);
i++;
}
else{
debugs(0x07);
}
}
}
//===================================================
/* set sci baud_rate process via sci */
//===================================================
void set_scibaud(void) {
switch(sci_baudtemp)
{
case 1:
sci_baud=300;
break;
case 2:
sci_baud=600;
break;
case 3:
sci_baud=1200;
break;
case 4:
sci_baud=2400;
break;
case 5:
sci_baud=4800;
break;
case 6:
sci_baud=9600;
break;
case 7:
sci_baud=19200;
break;
case 8:
sci_baud=38400;
break;
case 9:
sci_baud=57600;
break;
case 10:
sci_baud=115200;
break;
case 11:
sci_baud=230400;
break;
case 12:
sci_baud=460800;
break;
default:
break;
}
}
//===================================================
/* shell configration remote port and local port process via sci */
//===================================================
tu08 shell_cofigport(tu16 *port)
{
tu08 s,i;
tu08 bufftemp[5];
tu08 dataok=0;
for(i=0;i <5;i++){
bufftemp[i]=0;
}
for(i=0;i <6;)
{
while(!(scisr1 & 0x20));
s = scidrl;
if(s !=0x0d){
if(('0' <= s)&&(s <='9')&&(i <5)){
debugs(s);
s = s&0x0f;
bufftemp[i]=s;
i++;
} else if(s==0x08)
{
if(i!=0){
debugs(0x08);
i--;
} else {
debugs(0x07);
}
} else{
debugs(0x07);
}
} else
{
if(i==4){
*port=bufftemp[0]*1000;
*port+=bufftemp[1]*100;
*port+=bufftemp[2]*10;
*port+=bufftemp[3];
if((*port) <1024) {
debugs(0x07);
} else return(0);
} else if(i==5)
{
*port=bufftemp[0]*10000;
*port+=bufftemp[1]*1000;
*port+=bufftemp[2]*100;
*port+=bufftemp[3]*10;
*port+=bufftemp[4];
if((bufftemp[0]> 6)&&((*port) <1024)) {
//i=5;
debugs(0x07);
} else return(0);
} else if(i==0){
dataok=1;
return (dataok);
}else debugs(0x07);
}
}
}
//===================================================
/* shell funciton configration process via sci */
//===================================================
void shell_func(void)
{
tu08 i,ss;
//tu08 hard_addrf,prot_addrf;
// tu08 netw_maskf,dfgw_addrf;
tu08 hard_addrtemp[12];
//help ifo
debugnl();
debugt(" help information,\r\n");
debugt("the follow is the info should be configed\r\n");
debugt("mac address by 0x00-0xff\r\n");
debugt("ip address by 0-255\r\n");
debugt("net mask by 0-255\r\n");
debugt("default route by 0-255\r\n");
debugt("demo_rmthost_ip by 0-255\r\n");
debugt("tcpip_mode by 0,1,2\r\n");
debugt("remote host port by 1024-50000\r\n");
debugt("local host port by 1024-50000\r\n");
debugt("sci_mode select by 0,1\r\n");
debugt("sci baud_rate 1-c\r\n");
/* debugt("you can choose the figure to confige the parameter separately\r\n");
debugt("or input 's' to confige the patameter step by step\r\n");
while(!(scisr1 & 0x20));
s = scidrl;
s = s&0x0f;*/
//mac address configration
// debugnl();
debugnl();
debugt("mac address (000000000000):");
mac_addf=0;
for(i=0;i <12;)
{
while(!(scisr1 & 0x20));
ss = scidrl;
if((ss==0x0d)&&(i==0)) {
mac_addf=1;
break;
}
hard_addrtemp[i]= strtonum(ss);
if(hard_addrtemp[i]!=255)
{
if(hard_addrtemp[i]==128){
if((i!=0)&&(i%2)){
debugs(0x08);
i--;
} else{
debugs(0x07);
}
}else {
debugs(ss);
i++;
if((i!=0)&&(i!=12)){
if(!(i%2))
debugt("-");
}
}
} else{
debugs(0x07);
//hard_addrf = 1;
}
}
// if(hard_addrf==0){
for(i=0;i <6;i++){
hard_addr[i]=hard_addrtemp[i*2]*16+ hard_addrtemp[i*2+1];
}
// }
//ip address configration
debugnl();
debugt("ip address (0.0.0.0):");
prot_addrf=shell_recedata(prot_addr);
//net mask configration
debugnl();
debugt("net mask (0.0.0.0):");
netw_maskf=shell_recedata(netw_mask);
//default route configration
debugnl();
debugt("default route (0.0.0.0):");
dfgw_addrf=shell_recedata(dfgw_addr);
//demo_rmthost_ip configration
debugnl();
debugt("demo_rmthost_ip (0.0.0.0):");
demo_rmthost_ipf=shell_recedata(demo_rmthost_ip);
//tcpip_mode seleck
debugnl();
debugt("tcpip_mode select:\r\n");
debugt(" 0:tcpip_mode_udp\r\n");
debugt(" 1:tcpip_mode_tcp_client\r\n");
debugt(" 2:tcpip_mode_tcp_server\r\n");
tcp_modef=0;
for(i=0;i <1;)
{
while(!(scisr1 & 0x20));
ss = scidrl;
if(ss==0x0d) {
tcp_modef=1;
break;
}
if(('0' <=ss)&&(ss <='2'))
{
debugnl();
debugs(ss);
ss = ss&0x0f;
tcpip_mode =ss;
i++;
} else{
debugs(0x07);
}
}
//remote port
debugnl();
debugt("remote host port (00000) by 1024-50000:\r\n");
demo_rmthost_prtf=shell_cofigport(&demo_rmthost_prt);
//local port
debugnl();
debugt("local host port (00000) by 1024-50000:\r\n");
demo_local_portf=shell_cofigport(&demo_local_port);
// sci_mode select configration
debugnl();
debugt("sci_mode seleck:\r\n");
debugt(" 0:sci0_mode\r\n");
debugt(" 1:sci1_mode\r\n");
sci_modef=0;
for(i=0;i <1;)
{
while(!(scisr1 & 0x20));
ss = scidrl;
if(ss==0x0d) {
sci_modef=1;
break;
}
if(('0' <=ss)&&(ss <='1'))
{
debugnl();
debugs(ss);
ss = ss&0x0f;
sci_mode =ss;
i++;
} else{
debugs(0x07);
}
}
// sci baud_rate configration
debugnl();
debugt("sci baud_rate by\r\n");
debugt(" 1.300; 2.600; 3.1200; 4.2400; 5.4800; 6.9600; \r\n");
debugt(" 7.19200;8.38400;9.57600;a.115200;b.230400;c.460800.\r\n");
shell_scibaud();
set_scibaud();
}
//===================================================
/* configration info write to eeprom for save*/
//===================================================
void shell_eepromwrite(void){
tu16 eeprom_buf[3];
tu08 i;
debugnl();
debugnl();
debugt("system is going to write info to eeprom");
//ip address write to eeprom
if(!prot_addrf)
{
for(i=0;i <2;i++)
{
erase_eeprom(20+i);
eeprom_buf[i] = prot_addr[i*2+1]*256 + prot_addr[i*2];
write_eeprom(20+i,eeprom_buf[i]);
}
}
//net mask write to eeprom
if(!netw_maskf)
{
for(i=0;i <2;i++)
{
erase_eeprom(22+i);
eeprom_buf[i] = netw_mask[i*2+1]*256 + netw_mask[i*2];
write_eeprom(22+i,eeprom_buf[i]);
}
}
//default route write to eeprom
if(!dfgw_addrf)
{
for(i=0;i <2;i++)
{
erase_eeprom(24+i);
eeprom_buf[i] = dfgw_addr[i*2+1]*256 + dfgw_addr[i*2];
write_eeprom(24+i,eeprom_buf[i]);
}
}
//demo_rmthost_ip configration
if(!demo_rmthost_ipf)
{
for(i=0;i <2;i++)
{
erase_eeprom(26+i);
eeprom_buf[i] = demo_rmthost_ip[i*2+1]*256 + demo_rmthost_ip[i*2];
write_eeprom(26+i,eeprom_buf[i]);
}
}
//mac address configration
if(!mac_addf)
{
for(i=0;i <3;i++)
{
erase_eeprom(28+i);
eeprom_buf[i] = hard_addr[i*2+1]*256 + hard_addr[i*2];
write_eeprom(28+i,eeprom_buf[i]);
}
}
//tcpip_mode seleck
if(!tcp_modef)
{
erase_eeprom(31);
eeprom_buf[0]=(tu16)(tcpip_mode);
write_eeprom(31,eeprom_buf[0]);
}
//remote port
if(!demo_rmthost_prtf)
{
erase_eeprom(32);
eeprom_buf[0]= demo_rmthost_prt;
write_eeprom(32,eeprom_buf[0]);
}
//local port
if(!demo_local_portf)
{
erase_eeprom(33);
eeprom_buf[0]= demo_local_port;
write_eeprom(33,eeprom_buf[0]);
}
// sci baud_rate configration
if(!set_baudf)
{
erase_eeprom(34);
eeprom_buf[0]=(tu16)(sci_baudtemp);
write_eeprom(34,eeprom_buf[0]);
}
// sci_mode select configration
if(!sci_modef)
{
erase_eeprom(35);
eeprom_buf[0]=(tu16)(sci_mode);
write_eeprom(35,eeprom_buf[0]);
}
debugnl();
debugt("write process ok!\r\n");
}
//===================================================
/* read configration info form eeprom */
//===================================================
void shell_eepromread(void)
{
tu16 eeprom_buf[3];
tu08 i;
//ip address write to eeprom
for(i=0;i <2;i++){
eeprom_buf[i] = read_eeprom(20+i);
prot_addr[i*2+1] =(tu08)(eeprom_buf[i]/256);
prot_addr[i*2] = eeprom_buf[i]%256;
}
//net mask write to eeprom
for(i=0;i <2;i++){
eeprom_buf[i] = read_eeprom(22+i);
netw_mask[i*2+1] =(tu08)(eeprom_buf[i]/256);
netw_mask[i*2] = eeprom_buf[i]%256;
}
//default route write to eeprom
for(i=0;i <2;i++){
eeprom_buf[i] = read_eeprom(24+i);
dfgw_addr[i*2+1] =(tu08)(eeprom_buf[i]/256);
dfgw_addr[i*2] = eeprom_buf[i]%256;
}
//demo_rmthost_ip configration
for(i=0;i <2;i++){
eeprom_buf[i] = read_eeprom(26+i);
demo_rmthost_ip[i*2+1] =(tu08)(eeprom_buf[i]/256);
demo_rmthost_ip[i*2] = eeprom_buf[i]%256;
}
//mac address configration
for(i=0;i <3;i++)
{
eeprom_buf[i] = read_eeprom(28+i);
hard_addr[i*2+1] =(tu08)(eeprom_buf[i]/256);
hard_addr[i*2] = eeprom_buf[i]%256;
}
//tcpip_mode seleck
eeprom_buf[0]=read_eeprom(31);
tcpip_mode = (tu08)(eeprom_buf[0]);
//remote port
eeprom_buf[0]=read_eeprom(32);
demo_rmthost_prt = eeprom_buf[0] ;
//local port
eeprom_buf[0]=read_eeprom(33);
demo_local_port = eeprom_buf[0];
// sci baud_rate configration
eeprom_buf[0]=read_eeprom(34);
sci_baudtemp=(tu08)(eeprom_buf[0]);
set_scibaud();
// sci_mode select configration
eeprom_buf[0]=read_eeprom(35);
sci_mode=(tu08)(eeprom_buf[0]);
}
//===================================================
/* display configration info */
//===================================================
void shell_disp(void)
{
tu08 i;
if(prot_addrf==1) {
debugnl();
debugt("the info write to ip address is not matching standard\r\n");
prot_addrf=0;
}
if(netw_maskf==1) {
debugnl();
debugt("the info write to net mask is not matching standard\r\n");
netw_maskf=0;
}
if(dfgw_addrf==1) {
debugnl();
debugt("the info write to default route is not matching standard\r\n");
dfgw_addrf=0;
}
if(demo_rmthost_ipf==1) {
debugnl();
debugt("the info write to demo_rmthost_ip is not matching standard\r\n");
demo_rmthost_ipf=0;
}
//mac address configration
debugnl();
debugt("mac address (000000000000):");
for(i=0;i <6;i++){
debugc(hard_addr[i]);
}
//ip address configration
debugnl();
debugt("ip address (0.0.0.0):");
for(i=0;i <4;i++){
debugc(prot_addr[i]);
}
//net mask configration
debugnl();
debugt("net mask (0.0.0.0):");
for(i=0;i <4;i++){
debugc(netw_mask[i]);
}
//default route configration
debugnl();
debugt("default route (0.0.0.0):");
for(i=0;i <4;i++){
debugc(dfgw_addr[i]);
}
//demo_rmthost_ip configration
debugnl();
debugnl();
debugt("demo_rmthost_ip (0.0.0.0):");
for(i=0;i <4;i++){
debugc(demo_rmthost_ip[i]);
}
//tcpip_mode seleck
debugnl();
debugt("tcpip_mode seleck:");
if(tcpip_mode==0)debugt("tcpip_mode_udp");
if(tcpip_mode==1)debugt("tcpip_mode_tcp_client");
if(tcpip_mode==2)debugt("tcpip_mode_tcp_server");
//remote port
debugnl();
debugt("remote host port (00000):");
debugi(demo_rmthost_prt);
//local port
debugnl();
debugt("local host port (00000):");
debugi(demo_local_port);
// sci_mode select configration
debugnl();
debugnl();
debugt("sci_mode seleck: ");
if(sci_mode==0)debugt("sci0_mode");
if(sci_mode==1)debugt("sci1_mode");
// sci baud_rate configration
debugnl();
if(sci_baudtemp==1)debugt("sci baud_rate is 300\r\n");
if(sci_baudtemp==2)debugt("sci baud_rate is 600\r\n");
if(sci_baudtemp==3)debugt("sci baud_rate is 1200\r\n");
if(sci_baudtemp==4)debugt("sci baud_rate is 2400\r\n");
if(sci_baudtemp==5)debugt("sci baud_rate is 4800\r\n");
if(sci_baudtemp==6)debugt("sci baud_rate is 9600\r\n");
if(sci_baudtemp==7)debugt("sci baud_rate is 19200\r\n");
if(sci_baudtemp==8)debugt("sci baud_rate is 38400\r\n");
if(sci_baudtemp==9)debugt("sci baud_rate is 57600\r\n");
if(sci_baudtemp==10)debugt("sci baud_rate is 1152000\r\n");
if(sci_baudtemp==11)debugt("sci baud_rate is 230400\r\n");
if(sci_baudtemp==12)debugt("sci baud_rate is 460800\r\n");
debugnl();
}
//===================================================
/* shell process init via sci */
//===================================================
void shell_init(void)
{
uint16 i,j;
uint8 n;
tu16 dat;
debugnl();
debugnl();
debugt(" =================================\r\n");
debugt(" welcome to system configure shell\r\n");
debugt(" =================================\r\n");
debugnl();
debugt("i/o port test.... ok\r\n");
debugt("system clock initialization... ok\r\n");
/*********for eeprom******************************/
// write_eeprom(0x10,0x1278);
// dat=read_eeprom(0x10);
//sprintf(buf,"dat=%d\n",dat);debugt(buf);
if(dat==0x1278){
debugt("eeprom initialization... ok\r\n");
} else{
debugt("eeprom initialization... fault\r\n");
debugt("system initialize to a default configration\r\n");
//shell_eepromread(); //eeprom is bad,so system should apply a default configration
//a function should insert here.
for(i=0;i <6;i++) {
hard_addr[i] = 0;
}
prot_addr[0]=192; prot_addr[1]=168; prot_addr[2]=0; prot_addr[3]=2;
netw_mask[0]=255; netw_mask[1]=255; netw_mask[2]=255; netw_mask[3]=0;
dfgw_addr[0]=192; dfgw_addr[1]=168; dfgw_addr[2]=0; dfgw_addr[3]=1;
brcs_addr[0]=192; brcs_addr[1]=168; brcs_addr[2]=0; brcs_addr[3]=255;
demo_rmthost_ip[0]=192; demo_rmthost_ip[1]=168; demo_rmthost_ip[2]=0; demo_rmthost_ip[3]=3;
sci_mode=0;
tcpip_mode = 0;
demo_rmthost_prt=5001;
demo_local_port=5000;
sci_baud=9600;
shell_disp();
/***********************************************/
debugt("press any key for the begin of configuration");
for(n=0;n <10;n++){
debugt(".");
for(i=0;i <1000;i++){
for(j=0;j <8000;j++){}
if(scisr1 & 0x20)
{
scidrl;
debugnl();
shell_func();
shell_eepromwrite(); // function here implement a fun
shell_eepromread();
shell_disp(); // to configration info into eeprom
return;
}
}
}
debugnl();
debugnl();
debugt("system initialize to a previous configration\r\n");
shell_eepromread();
shell_disp();
// a fuction here should implement a fun to read info from eeprom
}
}
#endif
点击浏览该文件
发表时间:2008年1月18日17:03:57