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

📄 enet_lwip.c

📁 最新版IAR FOR ARM(EWARM)5.11中的代码例子
💻 C
📖 第 1 页 / 共 2 页
字号:
//*****************************************************************************
//
// enet_lwip.c - Sample WebServer Application using lwIP.
//
// Copyright (c) 2007 Luminary Micro, Inc.  All rights reserved.
// 
// Software License Agreement
// 
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
// 
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws.  All rights are reserved.  You may not combine
// this software with "viral" open-source software in order to form a larger
// program.  Any use in violation of the foregoing restrictions may subject
// the user to criminal sanctions under applicable laws, as well as to civil
// liability for the breach of the terms and conditions of this license.
// 
// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 1952 of the Stellaris Peripheral Driver Library.
//
//*****************************************************************************

#include "../../../hw_memmap.h"
#include "../../../hw_types.h"
#include "../../../hw_ints.h"
#include "../../../src/ethernet.h"
#include "../../../src/interrupt.h"
#include "../../../src/sysctl.h"
#include "../../../src/systick.h"
#include "../../../src/flash.h"
#include "../../../src/gpio.h"
#include "../../../utils/diag.h"
#include "../../../utils/ustdlib.h"
#include "../rit128x96x4.h"
#include "lwip/opt.h"
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/sys.h"
#include "lwip/stats.h"
#include "netif/etharp.h"

//*****************************************************************************
//
//! \addtogroup ek_lm3s6965_revc_list
//! <h1>Ethernet with lwIP (enet_lwip)</h1>
//!
//! This example application demonstrates the operation of the Stellaris
//! Ethernet controller using the lwIP TCP/IP Stack.  DHCP is used to obtain an
//! Ethernet address.  If DHCP times out without obtaining an address, a static
//! IP address will be used.  The DHCP timeout and the default static IP are
//! easily configurable using macros.  The address that is selected will be
//! shown on the OLED display.
//!
//! The file system code will first check to see if an SD card has been plugged
//! into the microSD slot.  If so, all file requests from the web server will
//! be directed to the SD card.  Otherwise, a default set of pages served up
//! by an internal file system will be used.
//!
//! For additional details on lwIP, refer to the lwIP web page at:
//! http://www.sics.se/~adam/lwip/
//
//*****************************************************************************

//*****************************************************************************
//
// Defines for setting up the system clock.
//
//*****************************************************************************
#define SYSTICKHZ               100
#define SYSTICKMS               (1000 / SYSTICKHZ)
#define SYSTICKUS               (1000000 / SYSTICKHZ)
#define SYSTICKNS               (1000000000 / SYSTICKHZ)

//*****************************************************************************
//
// A set of flags.  The flag bits are defined as follows:
//
//     0 -> An indicator that a SysTick interrupt has occurred.
//     1 -> An RX Packet has been received.
//     2 -> An RX Packet has been received.
//
//*****************************************************************************
#define FLAG_SYSTICK            0
#define FLAG_RXPKT              1
#define FLAG_TXPKT              2
static volatile unsigned long g_ulFlags;

//*****************************************************************************
//
// External Application references.
//
//*****************************************************************************
extern void httpd_init(void);
extern err_t ethernetif_init(struct netif *netif);
extern void ethernetif_input(struct netif *netif);
extern void fs_init(void);
extern void fs_tick(unsigned long ulTickMS);

//*****************************************************************************
//
// Counters/Timers for lwIP.
//
//*****************************************************************************
unsigned long g_ulTCPFastTimer = 0;
unsigned long g_ulTCPSlowTimer = 0;
unsigned long g_ulARPTimer = 0;
unsigned long g_ulDHCPCoarseTimer = 0;
unsigned long g_ulDHCPFineTimer = 0;
static struct netif g_sEMAC_if;
unsigned long g_ulDHCPTimeoutTimer = 0;

//*****************************************************************************
//
// Default TCP/IP Address Configuration.  Static IP Configuration is used if
// DHCP times out.  Note:  This is in the Link Local address range
// (169.254.x.y).
//
//*****************************************************************************
//
// The Default IP address to be used.
//
#ifndef DEFAULT_IPADDR0
#define DEFAULT_IPADDR0 169
#endif

#ifndef DEFAULT_IPADDR1
#define DEFAULT_IPADDR1 254
#endif

#ifndef DEFAULT_IPADDR2
#define DEFAULT_IPADDR2 19
#endif

#ifndef DEFAULT_IPADDR3
#define DEFAULT_IPADDR3 63
#endif

//
// The Default Gateway address to be used.
//
#ifndef DEFAULT_GATEWAY_ADDR0
#define DEFAULT_GATEWAY_ADDR0 0
#endif

#ifndef DEFAULT_GATEWAY_ADDR1
#define DEFAULT_GATEWAY_ADDR1 0
#endif

#ifndef DEFAULT_GATEWAY_ADDR2
#define DEFAULT_GATEWAY_ADDR2 0
#endif

#ifndef DEFAULT_GATEWAY_ADDR3
#define DEFAULT_GATEWAY_ADDR3 0
#endif

//
// The Default Network mask to be used.
//
#ifndef DEFAULT_NET_MASK0
#define DEFAULT_NET_MASK0 255
#endif

#ifndef DEFAULT_NET_MASK1
#define DEFAULT_NET_MASK1 255
#endif

#ifndef DEFAULT_NET_MASK2
#define DEFAULT_NET_MASK2 0
#endif

#ifndef DEFAULT_NET_MASK3
#define DEFAULT_NET_MASK3 0
#endif

//*****************************************************************************
//
// Timeout for DHCP address request (in seconds).
//
//*****************************************************************************
#ifndef DHCP_EXPIRE_TIMER_SECS
#define DHCP_EXPIRE_TIMER_SECS      45
#endif

//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif

//*****************************************************************************
//
// The interrupt handler for the SysTick interrupt.
//
//*****************************************************************************
void
SysTickIntHandler(void)
{
    //
    // Indicate that a SysTick interrupt has occurred.
    //
    HWREGBITW(&g_ulFlags, FLAG_SYSTICK) = 1;
}

//*****************************************************************************
//
// Ethernet Interrupt handler.
//
//*****************************************************************************
void
EthernetIntHandler(void)
{
    unsigned long ulTemp;

    //
    // Read and Clear the interrupt.
    //
    ulTemp = EthernetIntStatus(ETH_BASE, false);
    EthernetIntClear(ETH_BASE, ulTemp);

    //
    // Check if RX Interrupt Occurred.
    //
    if(ulTemp & ETH_INT_RX)
    {
        //
        // Indicate that an RX Packet has been received.
        //
        HWREGBITW(&g_ulFlags, FLAG_RXPKT) = 1;

        //
        // Disable Ethernet RX Packet Interrupt.
        //
        EthernetIntDisable(ETH_BASE, ETH_INT_RX);
    }

    //
    // Check if TX Interrupt Occurred.
    //
    if(ulTemp & ETH_INT_TX)
    {
        //
        // Indicate that an RX Packet has been received.
        //
        HWREGBITW(&g_ulFlags, FLAG_TXPKT) = 1;

        //
        // Disable Ethernet RX Packet Interrupt.
        //
        EthernetIntDisable(ETH_BASE, ETH_INT_TX);
    }
}

//*****************************************************************************
//
// Display an lwIP type IP Address.
//
//*****************************************************************************
void
DisplayIPAddress(unsigned long ipaddr, unsigned long ulCol,
                 unsigned long ulRow)
{
    char pucBuf[16];
    int iIndex = 0;
    int iTemp;
    unsigned char *pucTemp = (unsigned char *)&ipaddr;

    //
    // Convert the "long" IP Address into a string, one byte at a time.
    //
    iTemp = usprintf(&pucBuf[iIndex], "%d.", pucTemp[0]);
    iIndex += iTemp;
    iTemp = usprintf(&pucBuf[iIndex], "%d.", pucTemp[1]);
    iIndex += iTemp;
    iTemp = usprintf(&pucBuf[iIndex], "%d.", pucTemp[2]);
    iIndex += iTemp;
    iTemp = usprintf(&pucBuf[iIndex], "%d", pucTemp[3]);
    iIndex += iTemp;
    pucBuf[iIndex] = 0;

    //
    // Display the string.
    //
    RIT128x96x4StringDraw(pucBuf, ulCol, ulRow, 15);
}

//*****************************************************************************
//
// Should be called by the top-level application to perform the needed lwIP
// TCP/IP initialization
//
//*****************************************************************************
void
lwip_init(void)
{
    struct ip_addr xIpAddr, xNetMast, xGateway;

    //
    // Low-Level initialization of the lwIP stack modules.
    //
    stats_init();
    sys_init();
    mem_init();
    memp_init();
    pbuf_init();
    etharp_init();
    ip_init();
    udp_init();
    tcp_init();
    netif_init();

    //
    // Create, Configure and Add the Ethernet Controller Interface.
    //
    IP4_ADDR(&xIpAddr,0,0,0,0);
    IP4_ADDR(&xNetMast,0,0,0,0);
    IP4_ADDR(&xGateway,0,0,0,0);
    netif_add(&g_sEMAC_if,
              &xIpAddr,
              &xNetMast,
              &xGateway,
              NULL,
              ethernetif_init,
              ip_input);
    netif_set_default(&g_sEMAC_if);
    dhcp_start(&g_sEMAC_if);
    RIT128x96x4Enable(1000000);
    RIT128x96x4StringDraw("Waiting for DHCP", 0, 16, 15);
    RIT128x96x4StringDraw("<                   > ", 0, 24, 15);
    RIT128x96x4Disable();

    //
    // Bring the interface up.
    //
    netif_set_up(&g_sEMAC_if);
}

//*****************************************************************************
//
// Should be called by the top-level application every system tick, with the
// number of MS per system tick, to run lwIP timers, etc.
//
//*****************************************************************************

⌨️ 快捷键说明

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