⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 世纪民生公司的带网络功能的单片机CS6209开发http服务器的演示源代码。
💻 C
字号:
/*
 * 
 * main.c
 * 
 * Part of the Myson Century CS620X	demo program.
 *
 * Authors: LY Lin, WM Wang, IJ Chen, WH Lee
 *
 * main.c contains main program.
 *
 * This	program	was developed using the Keil 8051 C uVision 2 system.
 * The Keil compiler MUST be used if working with Myson Century supplied
 * firmware.
 *
*/


#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 "hwi2c.h"
#include "rs232.h"
#include "http.h"
#include "620xenet.h"
#include "rtc_api.h"
#include "iap.h"
char ipfromwhere;  /* 1=i2c, 2=flash, 3=config.c*/										

void do_poll(GENFRAME *gfp);
void do_receive(GENFRAME *gfp);
void init_MAC(char net_from_flag);
int wheregetip(char from_eeprom_flag);
void netconfig (NODE *np, char net_from_flag);
void CS620X_init(void);
extern tcpdebug ;				 /*Set to enable TCP debug message display*/
extern statedebug ;				 /*Set to enable TCP state display*/

void main(void)
{
    int	n;
    unsigned char cTmp;
    char k=0;
    char IP_from_eeprom_flag;
    GENFRAME *gfp;
    TSOCK *ts;

    /* Debug flags.*/
    tcpdebug = 0;	/*Set to enable TCP debug message display*/
    statedebug = 0;	/*Set to enable TCP state display*/

	/*
	disable RTC register write protect.
	*/
	XBYTE[RTCMASK]=0x55;
	XBYTE[RTCMASK]=0xaa;
    /*
	Initialize the system: RS232 and Clock . This function 
	must be called before any other. 
    */
    CS620X_init();

    /*
	Initialize the RS232 driver with RX FIFO. If you don't want to use the Keil
	in-system debugger, don't call this function.
    */
     RS232_intr_init();

    /* System sign-on message. */
    printf("CS6209C0  Demo : HTTP demo \n");

    /* Initialize timer2 for TCP/IP timout functions. */
    init_timer2() ;

    /* Check for serial eeprom on evaluation board. */
    IP_from_eeprom_flag = detect_24256();
  
    if (!IP_from_eeprom_flag)
	printf("\nCannot find 24C256 EEPROM.\n");

    wheregetip(IP_from_eeprom_flag);

    /* 
	Initialize the MAC address and start the network interface.
	After calling this function, the network interface can receive 
	and transmit packets.
    */
    init_MAC (ipfromwhere);  
    /*Atfter this line , the net interface can receive and transmit packets.*/
    /* 
	Initialize the genframe structure. 
	Driver type is Ethernet.
    */
    genframe.g.dtype = DTYPE_ETHER;

    /* set a pointer to the genframe structure for later use. */
    gfp= &genframe;			

    /* Initialize GENFRAME buffer. */
    gfp->buff=&nic_xmit_buf[0];
    gfp_buff_flag=0;
	
    /* 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;
    }

    /* Set the IP Address, Gateway Address, and Netmask. */		
    netconfig (&locnode,ipfromwhere);   

    /* Print network parameters on the rs232 interface. */
    show_menu (&locnode,0);				     

    /* Initialize HTTP server and file system for it.*/	
    http_init ();
    //init_rtc();
    /* Main	loop... */	
    while (1)			
    {
        /* Check RS232 and set IP,Gateway,Net Mask if necessary */
	 poll_set_ip();
	 /* Waits for the incoming packets,also handles requests. */
	 do_receive(gfp);					

	 if (!RS232_rx_empty())
    	 {
    	       cTmp = RS232_rx_getchar();
	       putchar(cTmp);

	 }
	 /* Poll	net	drivers	.*/
	 do_poll(gfp);
    	 if (!RS232_rx_empty())
    	 {
    	       cTmp = RS232_rx_getchar();
		 putchar(cTmp);

	 }			
		 
    }
	
}
/************************************************************************
/*	Function Name : init_MAC											*
/*																		*
/*	Arguments : 														*
/*			char from_eeprom_flag:Set to read MAC address from EEPROM.	*
/*																		*
/*	Return : None.														*
/*																		*
/*																		*
/*  Comment :															*
/*			This function sets the MAC address, then starts the network *
/*			interface. Attempts to read network parameters from serial	*
/*			eeprom if the 'from_eeprom_flag' is set, otherwise defaults	*
/*			to config.c.												*
/*																		*
/************************************************************************/
void init_MAC(char net_from_flag)
{
      unsigned char mac[6];
      int i;


	if (net_from_flag==1)
	{
		c256_pageread(I2C_MAC_START_ADDR,mac,MACLEN);//mac
		for(i = 0; i<6; i++)
			my_mac_addr[i] = mac[i];

	}
	else if(net_from_flag==2)
	{
		IAP_pageread(FLASH_MAC_START_ADDR, mac, MACLEN);
		for(i = 0; i<6; i++)
			my_mac_addr[i] = mac[i];

	}

	init_ethernet();
		
}		

int wheregetip(char from_eeprom_flag)
{
	unsigned char mac[6];
	unsigned char	tempip[6];
	unsigned char temptftp[4];

	if (from_eeprom_flag)
	{
		c256_pageread(I2C_MAC_START_ADDR,mac,MACLEN);//mac
		c256_pageread(I2C_IP_START_ADDR,&tempip[0],4);
		c256_pageread(I2C_TFTP_SERVER_ADDR, &temptftp[0], 4);
		
		if(((mac[0]&0x01) == 0) && (tempip[0]<248) && (temptftp[0]<248))
		{
			ipfromwhere = 1;   /*from I2C*/
			printf("Read network configuration from I2C \n");
			return 1;
		}
		else
			printf("Read network configuration from I2C error! \n");
	}


	IAP_pageread(FLASH_MAC_START_ADDR, mac, MACLEN);
	IAP_pageread(FLASH_IP_START_ADDR, &tempip[0], 4);
	IAP_pageread(FLASH_TFTP_SERVER_ADDR, &temptftp[0], 4);

	//printf("mac[0] : %bx  IP[0]: %bx  tftp[0]:%bx \n", mac[0], tempip[0], temptftp[0]);
	
	
	if(((mac[0]&0x01) == 0) && (tempip[0]<248) && (temptftp[0]<248))
	{
		ipfromwhere = 2;  /*from flash*/
		printf("Read network configuration from flash \n");
		return 1;
	}
	else
	{
		ipfromwhere = 3;
		printf("Read network configuration from flash error! \n");
		printf("Read network configuration from config.c \n");
	}
	return 1;
}

/************************************************************************
/*	Function Name : netconfig											*
/*																		*
/*	Arguments : 														*
/*			NODE *np:Point to NODE to return settings. 					*
/*			char from_eeprom_flag:Set to read setting from EEPROM.		*
/*																		*
/*	Return : None.														*
/*																		*
/*																		*
/*  Comment :															*
/*			This function get the: IP address, netmask, and Gateway for	*
/*			the local node.Attempts to read network parameters from 	*
/*			serial eeprom if the from_eeprom_flag is set, otherwise 	*
/*			defaults to config.c.										*
/*																		*
/************************************************************************/
void netconfig (NODE *np,char net_from_flag)
{

	unsigned char	tempip[6];
	unsigned char	tempmask[6];
	unsigned char	tempgate[6];
	
      /* MAC address. */
	memcpy(np->mac,my_mac_addr,MACLEN);
  

	/* Read the network configuration from the serial EEPROM. */
	if (net_from_flag == 1)
	{
		c256_pageread(I2C_IP_START_ADDR,&tempip[0],4);
		c256_pageread(I2C_NET_MASK_START_ADDR,&tempmask[0],4);
       	c256_pageread(I2C_GATEWAY_START_ADDR,&tempgate[0],4);

		np->gate=((LWORD)tempgate[0]<<24)+((LWORD)tempgate[1]<<16)+((LWORD)tempgate[2]<<8)+tempgate[3];
		np->ip=((LWORD)tempip[0]<<24)+((LWORD)tempip[1]<<16)+((LWORD)tempip[2]<<8)+tempip[3];  
		np->mask=((LWORD)tempmask[0]<<24)+((LWORD)tempmask[1]<<16)+((LWORD)tempmask[2]<<8)+tempmask[3];

	}
	else if(net_from_flag == 2)
	{
		IAP_pageread(FLASH_IP_START_ADDR, &tempip[0], 4);
		IAP_pageread(FLASH_NET_MASK_START_ADDR, &tempmask[0], 4);
		IAP_pageread(FLASH_GATEWAY_START_ADDR, &tempgate[0], 4);
		
		np->gate=((LWORD)tempgate[0]<<24)+((LWORD)tempgate[1]<<16)+((LWORD)tempgate[2]<<8)+tempgate[3];
		np->ip=((LWORD)tempip[0]<<24)+((LWORD)tempip[1]<<16)+((LWORD)tempip[2]<<8)+tempip[3];  
		np->mask=((LWORD)tempmask[0]<<24)+((LWORD)tempmask[1]<<16)+((LWORD)tempmask[2]<<8)+tempmask[3];
		
	}
	else
	{
		np->ip = atoip (my_ip_addr); 	
		np->gate=atoip (my_gateway_addr);
		np->mask = atoip (my_ip_mask); 	 	   
	}

}

/************************************************************************
/*	Function Name : CS620X_init											*
/*																		*
/*	Arguments : 														*
/*			None.														*
/*	Return : 															*
/*			None.														*
/*																		*
/*  Comment :															*
/*			This function sets up the  clocking and RS232 intrface.	*
/*																		*
/************************************************************************/
void CS620X_init(void)
{
  PCON = PCON | 0x80;
	/*
 	  The following code cannot run when using the Keil in-system debugger.
	  Undefine mon51 when burning code into FLASH memory. 
 	*/
#ifndef mon51
  SCON = 0x5A;
  TMOD = 0x22;
  TCON = 0x40;
  TH1 = 0xF9;     /* Baud Rate = 38400Kbs. */
  TL1 = 0xFF;
  IE = 0xA0;
  /* Use the "stretch MOVX select bits" to make MOVX 2 machine cycles. */
  CKCON = CKCON & 0xFB;
#endif
  
  XBYTE[NIC_TCR] =  XBYTE[NIC_TCR] | 0x80; //fullduplex = 1

  TI = 1;

}

/************************************************************************
/*	Function Name : do_receive											*
/*																		*
/*	Arguments : 														*
/*			GENFRAME *gfp:Point to GENFRAME.							*
/*	Return : 															*
/*			None.														*
/*																		*		 
/*  Comment :															*
/*			This function check for TCP/IP socket and send data if 		*
/*			required.													*
/*																		*
/************************************************************************/
void do_poll(GENFRAME *gfp)
{
    tcp_poll(tsocks, NSOCKS, gfp);
}
/************************************************************************
/*	Function Name : do_receive											*
/*																		*
/*	Arguments : 														*
/*			GENFRAME *gfp:Point to GENFRAME.							*
/*	Return : 															*
/*			None.														*
/*																		*
/*  Comment :															*
/*			This function 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;

	/* Any incoming frames? */
    if ((rxlen=get_frame(gfp)) > 0)
    {
        ip = getframe_datap(gfp);

		/* Check for ARP. */
        if (is_arp(gfp, rxlen))
        {
			/* ARP request? */
            arp = getframe_datap(gfp);
            if (arp->op==ARPREQ && arp->dip==locnode.ip)
            {
	            /* Make ARP response. */
                node.ip = arp->sip;
                memcpy(node.mac, arp->smac, MACLEN);
                txlen = make_arp(gfp, &locnode, &node, ARPRESP);
				/* Send out the packet. */
                put_frame(gfp, txlen);
            }

			/* Check for ARP response? */
            if (arp->op==ARPRESP && arp->dip==locnode.ip)
            {
                arp_receive(tsocks, NSOCKS, gfp);
            }
        }
		/* IP datagram? */
        else if ((rxlen=is_ip(gfp, rxlen))!=0 &&    
                 ip->i.dip==locnode.ip || ip->i.dip==BCASTIP)
        {
            getip_srce(gfp, &node);

			/* ICMP protocol? */
            if ((len=is_icmp(ip, rxlen))!=0)
            {
				/* ECHO request? */
                icmp = (ICMPKT *)ip;
                if (icmp->c.type==ICREQ)
                {
					/* Create ICMP response packet. */
                    len = (WORD)maxi(len, 0);
                    txlen = make_icmp(gfp, &locnode, &node, ICREP,
                                      icmp->c.i_code, (WORD)len);

					/* Send out the packet. */
                    put_frame(gfp, txlen);
                }
            }
			/* TCP datagram? */
            else if ((len=is_tcp(ip, rxlen))!=0)
            {
                tcp_receive(tsocks, NSOCKS, gfp, len);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -