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

📄 main.c

📁 用于以太网开发
💻 C
字号:
 /**************************************************************************                      
 *							 
 *       Copyright (C) 2003 Freescale Semiconductor, Inc. 
 *           and 2000-2002 Viola Systems Ltd. 
 *       All Rights Reserved
 *				 
 * File Name      : Main.c
 * Project Name   : Connector_App.mcp
 * Description    : This file contains main() for the Connector_App demo 
 *                    code for the MC9S12NE64.
 *                  It does initializations and calls functions in the
 *                    Connector_App.c application code. The Connector_App.c 
 *                    file contains code for IO communication and UDP
 *                    protocol functions on the MC9S12NE64.
 * 
 * *** NOTE: This project is for demonstration purposes only. It excludes
 *             portions of the complete OpenTCP Stack for other protocols, 
 *             such as http, bootp, dns, etc. The complete version of this 
 *             stack is also included in the MC9S12NE64 Resource CD.
 *                 
 * *** NOTE: All header files are not included in the Connector_App.mcp
 *             project due to the 32-file limit of CodeWarrior Special  
 *             Edition. However, all the header files are included in your 
 *             working folder and can be viewed by highlighting it's name 
 *             in a source file, right clicking, and choosing "Find and 
 *             Open File..." You may also just type the name of the file 
 *						 in any source file and do this.
 *
 * Version : 1.0
 * Date    : 07/06/04
 *
 ***************************************************************************/

#include <debug.h>
#include <config.h>
#include <datatypes.h>
#include <timers.h>
#include <system.h>
#include <ethernet.h>
#include <arp.h>
#include <ip.h>
#include <tcp_ip.h>

#include "mBuf.h"

#include "address.h"

/* Including used modules for compiling procedure */
#include "Cpu.h"

/* Network Interface definition. Must be somewhere so why not here? :-)*/
struct netif localmachine;

extern void RTI_Enable (void);
extern void porth_isr_handler (void);

extern	tU08	gotlink; 


//============================================================
tU08 OldSwitchValue=255;
tU16 Pot=0;
tU16 OldPot=1050;
tU08 OldB1=255; 
tU08 OldB2=255;

//============================================================
#pragma CODE_SEG NON_BANKED
interrupt void PortHInterrupt (void)
{			   
  porth_isr_handler();
}
#pragma CODE_SEG DEFAULT


//============================================================
//Initialize ATD
//============================================================
void ATD_init(void)
{
    ATDCTL2 =  ATDCTL2_ADPU_MASK | ATDCTL2_AFFC_MASK;
    ATDCTL3_S1C = 8;    // 8 ch seq. 
    ATDCTL3_FIFO = 0;    // no FIFO
    ATDCTL3_FRZ = 3;     // Freeze immediately in BDM  
    ATDCTL4 =  ATDCTL4_PRS2_MASK |ATDCTL4_PRS1_MASK | ATDCTL4_PRS0_MASK;
    ATDCTL4 = ATDCTL4 & ~ATDCTL4_SRES8_MASK; //10 bit
    ATDCTL5 = ATDCTL5_SCAN_MASK;
}


//============================================================
// Initialize Port for LEDs, Switch, and Buttons
//============================================================
void demoinit(void) 
 {
     //LEDS
    DDRG_DDRG0 = 1;
    DDRG_DDRG1 = 1;
 		PTG_PTG0 = 1;	//turn off		
 		PTG_PTG1 = 1;	//turn off		

    //SWITCH (RUN/LOAD)		 0:input
    DDRG_DDRG4 = 0;

    //BUTTON2 
    DDRH_DDRH4 = 0;
    PIEH_PIEH4 = 1;      //PIEH4 Interrupt Enable
 }


//============================================================
/* main */
//============================================================
void main(void)
{
  INT16 len;

    /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
    PE_low_level_init();
    /*** End of Processor Expert internal initialization.                    ***/
	
	/* initialize processor-dependant stuff (I/O ports, timers...).
	 * Most important things to do in this function as far as the TCP/IP 
	 * stack concerns:
	 *  - initializing some timer so it executes decrement_timers
	 * 	on every 10ms (TODO: Throw out this dependency from several files
	 *	so that frequency can be adjusted more freely!!!)
	 *  - not mess too much with ports allocated for Ethernet controller
	 */
    init();
    demoinit();
    ATD_init();

   	/* Set our network information. This is for static configuration.
     * if using BOOTP or DHCP this will be a bit different.
     */
   	/* IP address */
 	  localmachine.localip = *((UINT32 *)ip_address);
   	/* Default gateway */
   	localmachine.defgw   = *((UINT32 *)ip_gateway);
   	/* Subnet mask */
   	localmachine.netmask = *((UINT32 *)ip_netmask);

   	/* Ethernet (MAC) address */
   	localmachine.localHW[0] = hard_addr[0];
   	localmachine.localHW[1] = hard_addr[1];
   	localmachine.localHW[2] = hard_addr[2];
   	localmachine.localHW[3] = hard_addr[3];
   	localmachine.localHW[4] = hard_addr[4];
   	localmachine.localHW[5] = hard_addr[5];

	
	/* Init system services		*/    
	timer_pool_init();
		
    /* Initialize all buffer descriptors */
	mBufInit ();

	/*interrupts can be enabled AFTER timer pool has been initialized */
    	
  /* Initialize all network layers	*/
  EtherInit();
  
  //Initialize required network protocols 	
  arp_init();
  udp_init();
	udp_demo_init();
	
	//Enable RTI
	RTI_Enable ();

	/* main loop */
	DEBUGOUT(">>>>>>>>>Entering to MAIN LOOP>>>>>>>>>\n\r");
	for (;;)
	{
	  if (gotlink) {
	  
	  	/* take care of watchdog stuff */
   		/* Try to receive Ethernet Frame	*/    	
   		if( NETWORK_CHECK_IF_RECEIVED() == TRUE ) {  		   		
   			switch( received_frame.protocol)
			  {
   				case PROTOCOL_ARP:
					  process_arp (&received_frame);
	    			break;
   				case PROTOCOL_IP:
   					len = process_ip_in(&received_frame);
  					if(len < 0)
     						break;
     					switch (received_ip_packet.protocol)
  					  {
     						case IP_ICMP:
      						process_icmp_in (&received_ip_packet, len);
  							  break;
     						case IP_UDP:
                  process_udp_in (&received_ip_packet,len);   							
                  break;
    						case IP_TCP:
      						process_tcp_in (&received_ip_packet, len);				
     							break;
     						default:
  	  						break;
    					}
       				break;
				  default:
   				  break;
			  }
			/* discard received frame */    		
   			NETWORK_RECEIVE_END();
   		}
    arp_manage();
 
		/* Application main loops */
		/* TCP/IP stack Periodic tasks here... */
    udp_demo_run();
	  } 
	  else {
     		PTG_PTG0 = 1;	//turn off LED1		
     		PTG_PTG1 = 1;	//turn off LED2		  
	  }
	}
}

⌨️ 快捷键说明

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