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

📄 main.c

📁 W3510A PPPoE code for network use
💻 C
字号:

/*
********************************************************************************
File Include Section
********************************************************************************
*/

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/eeprom.h>
#include <stdio.h> 
#include <string.h>
#include "mcu.h"
#include "delay.h"
#include "serial.h"
#include "socket.h"

/*
********************************************************************************
Define Part
********************************************************************************
*/
#define	MAX_BUF_SIZE	2048		/* maximum size of Rx buffer. */

/*
********************************************************************************
Local Variable Declaration Section
********************************************************************************
*/
uint8 * rx_buf = (uint8 *)(0x7000);	/* Rx buffer for Application */

/*
*******************************************************************************
Function Prototype Declaration Part
*******************************************************************************
*/
void init_sock(SOCKET s, uint16 port, uint8 flag, uint8 udp_mode, uint8 client_mode) ;

/*
********************************************************************************
Function Implementation Part
********************************************************************************
*/

int16 main(void)
{
	SOCKET i;
	int16 len;		/* size of rx data */
	uint8 ip[6];
	uint16 lport;
	uint8 tcp_flag;
	uint16 loop_idx;
	uint16 dummy;
	
	mcu_init();
	uart_init(0, 7);		// Serial Port Initialize
	//uart_init(0, 2);		// Serial Port Initialize
	printf("\r\nATmega128 EVB TCP Server Program.\r\n");

	sei();			/* enable interrupts */
	iinchip_init();

   lport = 3000;
	ip[0] = 0x00; ip[1] = 0x01; ip[2] = 0x02; ip[3] = 0x03; ip[4] = 0x04; ip[5] = 0x65;
	setMACAddr(ip);
	ip[0] = 192; ip[1] = 168; ip[2] = 1; ip[3] = 1;
	setgateway(ip);
	ip[0] = 255; ip[1] = 255; ip[2] = 255; ip[3] = 0;
	setsubmask(ip);
	ip[0] = 192; ip[1] = 168; ip[2] = 0; ip[3] = 31;
	setIP(ip);
	sysinit(0x55,0x55);

/* -------------------------------------------------
 * adsl connection code begin
 * -------------------------------------------------*/
	uint8 pppmac[6], pppsessionid[2], pppip[4];
	uint8 pppid[128];
	uint8 ppppasswd[128];
	uint8 pppid_len;
	uint8 ppppasswd_len;
	uint8 ret;
	uint8 ppp_state;
	uint8 scanf_ret;
	printf("for adsl input 'a'");
	if (uart0_getchar() == 'a')
	{
AUTH_FAIL:
		printf("<<Setup PPPoE connection>>\r\n");

		/* 1. Get the USER ID for adsl connection */
		printf("Enter user id : ");
		scanf_ret = scanf("%s", pppid);
		printf("%s\r\n",pppid);
		pppid_len = strlen(pppid);

		/* 2. Get the Password for adsl connection */
		printf("Enter user password : ");
		scanf_ret = scanf("%s", ppppasswd);
		printf("%s\r\n",ppppasswd);
		ppppasswd_len = strlen(ppppasswd);

		//printf("%d %d\r\n",pppid_len,ppppasswd_len);

		i = 0;
		/* 3. Disconnect the previous adsl connection */
		pppterm(pppmac, pppsessionid);

		/* 4. Start to connect. If ret is 1, then adsl connection is established. */
		while ((ret = pppinit( pppid, pppid_len, ppppasswd, ppppasswd_len)) != 1)
		{
			switch (ret)
			{
				/* if ret is 2, authentication fail */
				case 2 : 
					getDestMAC(0, pppmac);
					getDestPort(0, pppsessionid);
					goto AUTH_FAIL; 
					break;
				default : break;
			}				
			i++;
   		/*  */
			if (i == 5) break;
		}
		
		/* 5. save information about adsl connection */
		if (i != 5)
		{
			ppp_state = IINCHIP_READ(INT_REG); // remove already setted value of INT_REG
			printf("<<PPP Connection established>>\r\n");
			getDestMAC(0, pppmac);
			getDestPort(0, pppsessionid);
			getLocalAddr(pppip);
			printf("Get IP ADDRESS : ");
			for (i = 0; i < 3; i++) printf("%d.", pppip[i]);
			printf("%d", pppip[i]);
			printf("\r\n");
			setIP(pppip);
		}
		else
		{
			IINCHIP_WRITE(TMODE,0x00);
		}
	}
/* -------------------------------------------------
 * adsl connection code end
 * -------------------------------------------------*/


	// display network information
	printf("\r\n====================================\r\n");
	printf("        Network Information\r\n");
	printf("====================================\r\n");

	printf("MAC ADDRESS      : ");
	for (i = 0; i < 5; i++)	printf("%.2X.", IINCHIP_READ(SRC_HA_PTR+i));
	printf("%.2X", IINCHIP_READ(SRC_HA_PTR+i));
	printf("\r\n");
	
	printf("SUBNET MASK      : ");
	for (i = 0; i < 3; i++) printf("%d.", IINCHIP_READ(SUBNET_MASK_PTR+i));
	printf("%d", IINCHIP_READ(SUBNET_MASK_PTR+i));
	printf("\r\n");

	printf("G/W IP ADDRESS   : ");
	for (i = 0; i < 3; i++) printf("%d.", IINCHIP_READ(GATEWAY_PTR+i));
	printf("%d", IINCHIP_READ(GATEWAY_PTR+i));
	printf("\r\n");

	printf("LOCAL IP ADDRESS : ");
	for (i = 0; i < 3; i++) printf("%d.", IINCHIP_READ(SRC_IP_PTR+i));
	printf("%d", IINCHIP_READ(SRC_IP_PTR+i));
	printf("\r\n");
	printf("====================================\r\n");

	setRCR(0x05); // timeout count is 5
	sysinit(0x55,0x55);

	for (i = 0 ; i < MAX_SOCK_NUM ; i++ )
	{
		init_sock(i, lport, 0x00 ,0 ,0);
	}

	/* Loop-back Service */	
	while (1)
	{
		for (i = 0 ; i < MAX_SOCK_NUM ; i++ )
		{
			switch (IINCHIP_READ(SOCK_STATUS(i)))
			{
			case SOCK_ESTABLISHED:						/* if connection is established */
				if ((len = select(i, SEL_RECV)) > 0) 			/* check Rx data */
				{
					if (len > MAX_BUF_SIZE) len = MAX_BUF_SIZE;	/* if Rx data size is lager than MAX_BUF_SIZE, */
											/* the data size to read is MAX_BUF_SIZE. */
					len = recv(i, rx_buf, len);			/* read the received data */
					send(i, rx_buf, len); /* send the received data */
				}
				break;
			case SOCK_CLOSE_WAIT:                           		/* If the client request to close */
				printf("CLOSE_WAIT : %d\r\n", i);
				disconnect(i); /* close the socket */
			case SOCK_CLOSED:                                               /* if a socket is closed */
				printf("CLOSED : %d %.2x %.2x\r\n", i, IINCHIP_READ(INT_STATUS(i)), IINCHIP_READ(SOCK_STATUS(i)));
				init_sock(i, lport, 0, 0, 0);						/* reinitialize the socket */
				break;
			default :
				break;
			}
		}
	}		
}

/*
********************************************************************************
*              Re-initialization function for the disconnected channel.
*
* Description: Waits in the server mode after re-initialization for the disconnected channel.
* Arguments  : s - is a socket number
* Returns    : None.
* Note       : 
********************************************************************************
*/
void init_sock(SOCKET s, uint16 port, uint8 flag, uint8 udp_mode, uint8 client_mode) 
{
	uint8 sel;
	uint8 ip[4];
	uint16 sport;
	int i;

	printf("socket %d : ", s);
	socket(s, SOCK_STREAM, port, flag);	/* Create a new socket */
	printf("%.2x ok\r\n", IINCHIP_READ(SOCK_STATUS(s)));
	printf("listen : ");
	listen(s);           			/* Server Mode */
	printf("%d ok..\r\n", s);
}

⌨️ 快捷键说明

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