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

📄 main.c

📁 this is a driver for the davicom ethernet controller DM9000. this driver help in developing pc or em
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 2006, Invector Embedded Technologies
 * All rights reserved. 
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met: 
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution. 
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.  
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
 *
 * This file is part of the uIP TCP/IP stack.
 *
 * Author:    Tony Pemberton, Pontus Oldberg
 *
 */

/*
 * Notes on debug usage in this module.
 *
 *  The include file iet_debug.h defines a number of (A-C) inclusive macros
 *  that can be used to enter debug printouts into the code. When no PRINT_X
 *  macros are defined during compile time no debug prints are present in the
 *  resulting binary.
 *
 *  In this module I have classified the three levels of debug prints in the
 *  following classes:
 *    - A = System level printouts, important stuff that is always needed
 *          when debugging the system.
 *    - B = Informative prints, used to trace program flow etc during execution.
 *    - C = Informative prints. Should be used on repetetive code that spams
 *          the output but is sometimes useful.
 */

#include "system.h"       // System description
#include "main.h"         // Locals
#include "iet_debug.h"    // Debug macros

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void Timer0_ISR (void) interrupt TF0_VECTOR;
void Timer1_ISR (void) interrupt TF1_VECTOR;
void Timer2_ISR (void) interrupt TF2_VECTOR;
void Timer3_ISR (void) interrupt TF3_VECTOR;
void Timer4_ISR (void) interrupt TF4_VECTOR;

void Timer0_Init (void);
void Timer1_Init (int counts);
void Timer2_Init (int counts);
void Timer3_Init (int counts);
void Timer4_Init (int counts);

void cleanup( void );
extern void config();

//-----------------------------------------------------------------------------
// Static data definitions
//-----------------------------------------------------------------------------
static data u16_t half_Sec;
static data u16_t ten_Secs;
static data u8_t  led;        // Used for advanced LED blinking.
static data u16_t LedTimer;   // Separate timer for the led

bit TX_EventPending;	        // the DM9000 hardware receive event
bit ARP_EventPending;	        // trigger the arp timer event
bit LED_EventPending;         // For triggering LED changes.

bit uartByteAvail;		        // True when character is available.
bit Transmitting;    	        // True when transmitting a character.
bit uart_tx_buf_full;

// ---------------------------------------------------------------------------
//							main()
//
//	Simple Web Server application.
// ---------------------------------------------------------------------------
void main(void)
{ 
	unsigned int i;
  
  config();                 // Configure the MCU

	half_Sec = UIP_TX_TIMER;
	ten_Secs = UIP_ARP_TIMER;
  LedTimer = LED_TIMER;

  led = 0x06;

	Timer0_Init ();	          // 10 mSec interrupt rate

  CUart_init(BAUD_115200);  // Set the Uart up for operation
                            // Uses Timer1 for baudrate

  uip_init();               // Initialise the uIP TCP/IP stack.
  httpd_init();             // Initialise the webserver app.
  if (InitDM9000())         // Initialise the device driver.
    cleanup();              // exit if init failed
  uip_arp_init();           // Initialise the ARP cache.

  TX_EventPending = FALSE;	// False to poll the DM9000 receive hardware
  ARP_EventPending = FALSE;	// clear the arp timer event flag

	EA = TRUE;                // Enable interrupts

  A_(printf_small("\x0a\x0d");)
  A_(printf_small("Invector Embedded Technologies Debug system output v. 1.001\x0a\x0d");)
  A_(printf_small("System: IET902X, 20MHz system clock, DM9000E Ethernet Controller\x0a\x0d");)
  A_(printf_small("Global TCP/IP application data pointer (uip_appdata) = %x\x0a\x0d", (unsigned int) uip_appdata);)

  while(1)
  {
    // Loops here until either a packet has been received or
    // TX_EventPending (half a sec) to scan for anything to send or
    // ARP_EventPending (10 secs) to send an ARP packet		
    uip_len = DM9000_receive();

    // If uip_len is 0, no packet has been received
    if (uip_len == 0)
    {
      // Test for anything to be sent to 'net
      if (TX_EventPending)  
      { 
        TX_EventPending = FALSE;  // First clear flag if set

        // UIP_CONNS - nominally 10 - is the maximum simultaneous
        // connections allowed. Scan through all 10
        C_(printf_small("Time for connection periodic management\x0a\x0d");)
        for (i = 0; i < UIP_CONNS; i++)
        {
          uip_periodic(i);
          // If uip_periodic(i) finds that data that needs to be
          // transmitted to the network, the global variable uip_len
          // will be set to a value > 0.
          if (uip_len > 0)
          {
            /* The uip_arp_out() function should be called when an IP packet should be sent out on the
               Ethernet. This function creates an Ethernet header before the IP header in the uip_buf buffer.
               The Ethernet header will have the correct Ethernet MAC destination address filled in if an
               ARP table entry for the destination IP address (or the IP address of the default router)
               is present. If no such table entry is found, the IP packet is overwritten with an ARP request
               and we rely on TCP to retransmit the packet that was overwritten. In any case, the
               uip_len variable holds the length of the Ethernet frame that should be transmitted. 
            */
            uip_arp_out();
            DM9000_transmit();
          }
        }

#if UIP_UDP
        C_(printf_small("Time for udp periodic management\x0a\x0d");)
        for (i = 0; i < UIP_UDP_CONNS; i++)
        {
          uip_udp_periodic(i);
          // If the above function invocation resulted in data that
          // should be sent out on the network, the global variable
          // uip_len is set to a value > 0.
          if (uip_len > 0)
          {
            uip_arp_out();
            DM9000_transmit();
          }
        }
#endif /* UIP_UDP */
      }
      // Call the ARP timer function every 10 seconds. Flush dead entries
      if (ARP_EventPending) 
      {
        B_(printf_small("ARP house keeping.\x0a\x0d");)
        ARP_EventPending = FALSE;
        uip_arp_timer();
      }
    }
    // Packet Received (uip_len != 0) Process incoming       
    else 
    {
      B_(printf_small("Received incomming data package.\x0a\x0d");)
      if (BUF->type == htons(UIP_ETHTYPE_IP))
      {
        B_(printf_small("IP Package received.\x0a\x0d");)
        // Received an IP packet
        uip_arp_ipin();
        uip_input();
        // If the above function invocation resulted in data that
        // should be sent out on the network, the global variable
        // uip_len is set to a value > 0.
        if (uip_len > 0)

⌨️ 快捷键说明

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