main.c

来自「opentcp_mcf5282原代码」· C语言 代码 · 共 247 行

C
247
字号
/*
 * File:		main.c
 * Purpose:		Main process
 *
 */

#include "src/common/m5282evb.h"
#include "src/common/stdlib.h"
#include "src/opentcp/opentcp.h"


NIF	fec_nif;

static unsigned char mac[6] = {0x00, 0x06, 0x70, 0x00, 0x00, 0x01};

/* IP Address */
#define IP1 192
#define IP2 168
#define IP3 1
#define IP4 123

/* Subnet Mask */
#define SM1 255
#define SM2 255
#define SM3 255
#define SM4 0

/* Default Gateway */
#define GW1 192
#define GW2 168
#define GW3 1
#define GW4 1

/* No LEDs to blink on the M5282LITE board. */
#undef HAS_LEDS

/********************************************************************/

#ifdef __GNUC__ /* { */
void __main(void) {}
#endif /* } __GNUC__ */

int main (void)
{
	int16 len;
	int16 reset_timer;
	int16 packet_status;
#ifdef HAS_LEDS /* { */
	int16 alive_timer;
	uint16 round;
#endif /* } HAS_LEDS */

	/* Enable interrupts */
	mcf5xxx_wr_sr(0x2000);

	printf("\nMCF5282 Initialization Complete!\n");

	/* Init Opentcp	*/

	eth_stub_init();
	init_timerpool();
	arpinit();
	tcp_init();
	udp_init();
	init_http();
	init_bootpc(0);
	init_tftps();
	smtpc_init();
	pop3c_init();


#ifdef HAS_LEDS /* { */
	/* Set DTOUT0,1,2,3 to act as GPIO */
	MCF5282_GPIO_PTCPAR = 0x0;
	MCF5282_GPIO_PTDPAR = 0x0;

	/* Set Data Direction of DTOUT0,1,2,3 to Output */
	MCF5282_GPIO_DDRTC = 0x0F;
	MCF5282_GPIO_DDRTD = 0x0F;
#endif /* } HAS_LEDS */


	/* Set 10 ms timer for OpenTCP timers	*/

	MCF5282_PIT0_PCSR  = MCF5282_PIT_PCSR_PRE(0x0E);	/* Divide system clock/2 by 32768 --> 1,024 ms/tic	*/
	MCF5282_PIT0_PCSR |= MCF5282_PIT_PCSR_OVW | MCF5282_PIT_PCSR_PIE |
	                     MCF5282_PIT_PCSR_PIF | MCF5282_PIT_PCSR_RLD |
	                     MCF5282_PIT_PCSR_EN;

	MCF5282_PIT0_PMR = 10;						/* Interrupt every 10 ms	*/

	MCF5282_INTC0_ICR55 = MCF5282_INTC_ICR_IL(TIMER_NETWORK_LEVEL);
	MCF5282_INTC0_IMRH &= ~MCF5282_INTC_IMRH_INT55;

	/* Enable FEC Rx Frame interrupts to ColdFire core */
	MCF5282_INTC0_ICR27 = MCF5282_INTC_ICR_IL(FEC_LEVEL);
	MCF5282_INTC0_IMRL &= 	~(MCF5282_INTC_IMRL_INT27 | MCF5282_INTC_IMRL_MASKALL);


	/* Initialize network device */
	fec_init(&fec_nif);

	/* Write ethernet address in the NIF structure */
	fec_nif.hwa[0] = mac[0];
	fec_nif.hwa[1] = mac[1];
	fec_nif.hwa[2] = mac[2];
	fec_nif.hwa[3] = mac[3];
	fec_nif.hwa[4] = mac[4];
	fec_nif.hwa[5] = mac[5];

	/* Initialize Network Buffers */
	nbuf_init();

	/* Register protocol handlers	*/

	nif_bind_protocol(&fec_nif, FRAME_IP, opentcp_fechandler, "IP PROTOCOL");
	nif_bind_protocol(&fec_nif, FRAME_ARP, opentcp_fechandler, "ARP PROTOCOL");

	/* Start ethernet	*/

	fec_nif.reset(&fec_nif);
	fec_nif.start(&fec_nif);

	/* Set OpenTCP localmachine	*/

	localmachine.localip = ((UINT32)IP1)<<24 | ((UINT32)IP2)<<16 | ((UINT32)IP3)<<8 | IP4;
	localmachine.localHW[0] = mac[0];
	localmachine.localHW[1] = mac[1];
	localmachine.localHW[2] = mac[2];
	localmachine.localHW[3] = mac[3];
	localmachine.localHW[4] = mac[4];
	localmachine.localHW[5] = mac[5];
	localmachine.defgw   = ((UINT32)GW1)<<24 | ((UINT32)GW2)<<16 | ((UINT32)GW3)<<8 | GW4;
	localmachine.netmask = ((UINT32)SM1)<<24 | ((UINT32)SM2)<<16 | ((UINT32)SM3)<<8 | SM4;
	localmachine.tos = 0;

	printf("IP Address:   %d.%d.%d.%d\n", IP1, IP2, IP3, IP4);
	printf("Subnet Mask: %d.%d.%d.%d\n", SM1, SM2, SM3, SM4);
	printf("Gateway:      %d.%d.%d.%d\n", GW1, GW2, GW3, GW4);

	/* Enable some apps	*/

	bootpc_enable();

#ifdef HAS_LEDS /* { */
	alive_timer = get_timer();
	init_timer(alive_timer, 1*TIMERTIC);
	round = 0;
#endif /* } HAS_LEDS */
	reset_timer = get_timer();
	init_timer(reset_timer, 30*TIMERTIC);


	while(1)	/* Idle */
	{
	
		packet_status = eth_stub_isframe(&fec_nif);

#if 0		
		if(check_timer(reset_timer) == 0)
		{
			DEBUGOUT("Resetting FEC\n");
			
			fec_nif.reset(&fec_nif);
			nbuf_init();
			eth_stub_init();
			fec_nif.start(&fec_nif);
			init_timer(reset_timer, 30*TIMERTIC);
		}
		
#endif	
		if(packet_status > 0)
		{

			switch(otcp_rxframe.protocol)
			{
				case PROTOCOL_ARP:

					Process_ARP(&otcp_rxframe);
					break;

				case PROTOCOL_IP:

					len = ProcessIPIn(&otcp_rxframe);

    				if(len < 0)
    					break;

    				if( ReceivedIPPacket.protocol == IP_ICMP)
    					ProcessICMPIn(&ReceivedIPPacket, len);
    				else if(ReceivedIPPacket.protocol == IP_TCP)
    					ProcessTCPIn(&ReceivedIPPacket, len);
    				else if(ReceivedIPPacket.protocol == IP_UDP)
    					ProcessUDPIn(&ReceivedIPPacket, len);

					break;

				default:
					break;

			}

			eth_stub_dumpframe(&fec_nif);

		}


		/* Run apps	*/
		https_run();
		//bootpc_run();
		tftps_run();
		smtpc_run();
		pop3c_run();

		arpmanage();
		tcp_poll();


#ifdef HAS_LEDS /* { */
		/* Show we are alive	*/

		if(check_timer(alive_timer) == 0)
		{
			if(round & 0x01)
			{
				/* Write 0 to turn LEDs on */
				MCF5282_GPIO_PORTTD = 0x0A;
				MCF5282_GPIO_PORTTC = 0x0A;
			}
			else
			{
				/* Write 1 to turn LEDs off */
				MCF5282_GPIO_PORTTD = 0x0F;
				MCF5282_GPIO_PORTTC = 0x0F;

			}

			round++;
			init_timer(alive_timer, 1*TIMERTIC);
		}
#endif /* } HAS_LEDS */

	}
}

/********************************************************************/

⌨️ 快捷键说明

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