📄 main.c1
字号:
//**********************************************************************
//
// main.c
//
// Myson-Century CS620X Embedded HTTP Server demo program.
//
// This demo program shows how to use the CS620X TCP socket interface
// and HTTP server support functions to receive HTTP requests and to
// generate HTTP responses.
//
// This program is developed using the Keil 8051 C uVision 2 system.
// If you are using a different 8051 C compiler, the program can be
// easily adapted.
//
// This program has to be linked with the 620xlib.LIB library
// supplied by Myson-Century. If you are not using the Keil C
// development system, please contact the Myson-Century support engineer
// for a different version of the library.
//
//***********************************************************************
#define VERSION "0.01"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "hpserver.h"
#include "timer.h"
#include "ether.h"
#include "netutil.h"
#include "net.h"
#include "ip.h"
#include "tcp.h"
#include "udp.h"
#include "config.h"
#include "utility.h"
#include "libif.h"
#include "swi2c.h"
#include "rs232.h"
#include "smtp.h"
#include "cs00.h"
void do_poll(GENFRAME *gfp);
void do_receive(GENFRAME *gfp);
void init_MAC(char from_eeprom_flag);
WORD netconfig (NODE *np,char IP_from_eeprom_flag);
void CS620X_init(void);
extern tcpdebug ; // TCP debug message
extern statedebug ; // TCP state display
st_mail mysmtp;
void main(void)
{
int n;
char k=0;
char IP_from_eeprom_flag;
GENFRAME *gfp;
TSOCK *ts;
char flag=0xff;
extern code unsigned char pq[];
// set debug flags
tcpdebug = 0; // TCP debug message
statedebug = 0; // TCP state display
//Init system
CS620X_init(); //Init RS232 ,Clock PLL .This function must be call first.
RS232_intr_init(); //Init Rs232 driver with RX FIFO . If you don't want to use
//MYSON RS232 driver ,don't call this function.
printf("CS6208 Demo v" VERSION "\n"); /* Sign on */
init_timer2() ; //Init timer2 for TCP/IP timeout functions
IP_from_eeprom_flag=detect_24256();//Detect if there are 24C256 EEPROM on board
if (!IP_from_eeprom_flag)
printf("\nCan't Find 24C256 EEPROM\n");
init_MAC(IP_from_eeprom_flag); //init MAC Address and start net interface.
//atfter this line , the net interface can receive and transmit Packets
//Init genframe
genframe.g.dtype = DTYPE_ETHER; /* Set frame driver type */
gfp = &genframe; /* Get pointer to frame */
// initialize the sockets
for (n = 0, ts = &tsocks[0]; n < NSOCKS; n++, ts++)
{
ts->index = n+1;
ts->txb.len = _CBUFFLEN_;
ts->txb.b_data = &tsock_txb [n*_CBUFFLEN_];
ts->rxb.b_data_length = 0;
#if NO_SOCKET_RXB
ts->rxb.len = 0;
#else
ts->rxb.len = _CBUFFLEN_;
ts->rxb.b_data = &tsock_rxb [n*_CBUFFLEN_];
#endif
ts->state=TCP_CLOSED;
}
netconfig (&locnode,IP_from_eeprom_flag); //Config IP Address,Gateway,Net mask for CS6208
show_menu(&locnode,0);//show IP in rs232 .
/*
Place code there to initialize applications
*/
tsocks[NSOCKS-1].loc=locnode;
tsocks[NSOCKS-1].rem=locnode;
tsocks[NSOCKS-1].rem.ip=atoip("172.16.200.1");
tsocks[NSOCKS-1].rem.port=25;
mysmtp.socket=&tsocks[NSOCKS-1];
mysmtp.host_name="A";
mysmtp.sender_EMAIL_addr="lin_jy@myson.com.tw";
mysmtp.recv_EMAIL_addr="lin_jy@myson.com.tw";
mysmtp.priority="3";
mysmtp.number_of_cc_EMAIL_addr=0;
mysmtp.subject="TEST";
mysmtp.char_set="BIG5";
mysmtp.text="This Mail is send by CS6208";
mysmtp.text_length=strlen(mysmtp.text);
mysmtp.attach_file_name="cs00.jpg";
mysmtp.attach_file_buffer=cs00_jpg;
mysmtp.attach_file_size=CS00_SIZE;
send_mail(&mysmtp);
while (1) /* Main loop.. */
{
/*
Place code there to execuate tasks
*/
if ((get_smtp_status(&mysmtp)==SMTP_STOP)&&(flag&0x1))
{
printf("\nSMTP send mail OK");
flag&=~0xf;
}
else if ((get_smtp_status(&mysmtp)==SMTP_ERROR)&&(flag&0x2))
{
printf("\nSMTP send mail ERROR");
printf("\nlast command=%s return code=%s",mysmtp.last_command,mysmtp.last_error_code);
flag&=~0x2;
}
else if ((get_smtp_status(&mysmtp)==SMTP_TCP_ERROR)&&(flag&0x4))
{
printf("\nSMTP socket ERROR");
flag&=~0x4;
}
else if ((get_smtp_status(&mysmtp)==SMTP_START)&&(flag&0x8))
{
printf("\nSMTP Connect to server Start send mail");
flag&=~0x8;
}
do_receive(gfp); /* Receive frames */
do_poll(gfp); /* Poll net drivers */
}
}
//Get MAC address and set it to CS6208 , then start net interface.
void init_MAC(char from_eeprom_flag)
{
if (from_eeprom_flag)
c256_pageread(0x7ff4,my_mac_addr,MACLEN);//mac
open_net();
}
// get IP address, netmask, and gateway for local node
WORD netconfig (NODE *np,char IP_from_eeprom_flag)
{
if (IP_from_eeprom_flag)
{
unsigned char temp[6];
c256_pageread(0x7ff0,&temp[0],4);//ip
np->ip=((LWORD)temp[0]<<24)+((LWORD)temp[1]<<16)+((LWORD)temp[2]<<8)+temp[3];
memcpy(np->mac,my_mac_addr,MACLEN);//mac
c256_pageread(0x7ffa,&temp[0],4);//mask
np->mask=((LWORD)temp[0]<<24)+((LWORD)temp[1]<<16)+((LWORD)temp[2]<<8)+temp[3];
c256_pageread(0x7fec,&temp[0],4);//Gateway
np->gate=((LWORD)temp[0]<<24)+((LWORD)temp[1]<<16)+((LWORD)temp[2]<<8)+temp[3];
}
else
{
memcpy (np->mac, my_mac_addr, MACLEN);
np->ip=atoip (my_ip_addr); // default ip
// set network mask
np->mask=atoip (my_ip_mask);
// set network gateway address
np->gate=atoip (my_gateway_addr);
// initialize the network interface
}
return(1);
}
//xdata unsigned char pllfc _at_ PLLFC;
void CS620X_init(void)
{
// set the phase lock loop (PLL) register to pump up CPU clock
// CUP clock=20Mhz*(pllfc&0xf)/(pllfc>>4);
//pllfc=0x4A;
PCON = PCON | 0x80;
SCON=0x5A;
TMOD=0x22;
TCON=0x40;
TH1=0xFF; //BR:115200
TL1=0xFF;
IE = 0xA0;
TI = 1;
// use the "stretch MOVX select bits" to make MOVX 2 machine cycles
CKCON = CKCON & 0xF8;
}
/* Poll the network interface to keep it alive */
void do_poll(GENFRAME *gfp)
{
tcp_poll(tsocks, NSOCKS, gfp);
}
/* Check for incoming packets, send response if required */
void do_receive(GENFRAME *gfp)
{
NODE node;
ARPKT *arp;
IPKT *ip;
ICMPKT *icmp;
int rxlen, txlen, len;
if ((rxlen=get_frame(gfp)) > 0) /* Any incoming frames? */
{
ip = getframe_datap(gfp);
if (is_arp(gfp, rxlen))
{ /* ARP response? */
arp = getframe_datap(gfp);
if (arp->op==ARPREQ && arp->dip==locnode.ip)
{ /* ARP request? */
node.ip = arp->sip; /* Make ARP response */
memcpy(node.mac, arp->smac, MACLEN);
txlen = make_arp(gfp, &locnode, &node, ARPRESP);
put_frame(gfp, txlen); /* Send packet */
}
if (arp->op==ARPRESP && arp->dip==locnode.ip)
{ /* ARP response? */
arp_receive(tsocks, NSOCKS, gfp);
}
}
else if ((rxlen=is_ip(gfp, rxlen))!=0 && /* IP datagram? */
ip->i.dip==locnode.ip || ip->i.dip==BCASTIP)
{
getip_srce(gfp, &node);
if ((len=is_icmp(ip, rxlen))!=0) /* ICMP? */
{
icmp = (ICMPKT *)ip;
if (icmp->c.type==ICREQ) /* Echo request? */
{
len = (WORD)maxi(len, 0); /* Make response */
txlen = make_icmp(gfp, &locnode, &node, ICREP,
icmp->c.i_code, (WORD)len);
put_frame(gfp, txlen); /* Send packet */
}
}
else if ((len=is_tcp(ip, rxlen))!=0) /* TCP? */
{
tcp_receive(tsocks, NSOCKS, gfp, len);
}
else if ((len=is_udp(ip, rxlen))!=0) /* UDP? */
{
udp_receive(gfp, maxi(len, 0));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -