📄 stellarisif.c
字号:
/**
* @file - stellarisif.c
* lwIP Ethernet interface for Stellaris Devices
*
*/
/**
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* 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 lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
/**
* Copyright (c) 2008 Luminary Micro, Inc.
*
* This file is dervied from the ``ethernetif.c'' skeleton Ethernet network
* interface driver for lwIP.
*
*/
#include "lwip/opt.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/sys.h"
#include <lwip/stats.h>
#include <lwip/snmp.h>
#include "netif/etharp.h"
#include "netif/ppp_oe.h"
#include "stellarisif.h"
/**
* Sanity Check: This interface driver will NOT work if the following defines
* are incorrect.
*
*/
#if (PBUF_LINK_HLEN != 16)
#error "PBUF_LINK_HLEN must be 16 for this interface driver!"
#endif
#if (ETH_PAD_SIZE != 2)
#error "ETH_PAD_SIZE must be 2 for this interface driver!"
#endif
#if (!SYS_LIGHTWEIGHT_PROT)
#error "SYS_LIGHTWEIGHT_PROT must be enabled for this interface driver!"
#endif
/**
* Number of pbufs supported in low-level tx/rx pbuf queue.
*
*/
#ifndef STELLARIS_NUM_PBUF_QUEUE
#define STELLARIS_NUM_PBUF_QUEUE 20
#endif
/**
* Setup processing for PTP (IEEE-1588).
*
*/
#if LWIP_PTPD
extern void lwIPHostGetTime(u32_t *time_s, u32_t *time_ns);
#endif
/**
* Luminary Micro DriverLib Header Files required for this interface driver.
*
*/
#include "hw_memmap.h"
#include "hw_types.h"
#include "hw_ints.h"
#include "hw_ethernet.h"
#include "ethernet.h"
#include "interrupt.h"
#include "sysctl.h"
/* Define those to better describe your network interface. */
#define IFNAME0 'E'
#define IFNAME1 'T'
/* Helper struct to hold a queue of pbufs for transmit and receive. */
struct pbufq {
struct pbuf *pbuf[STELLARIS_NUM_PBUF_QUEUE];
unsigned long qwrite;
unsigned long qread;
unsigned long overflow;
};
/* Helper macros for accessing pbuf queues. */
#define PBUF_QUEUE_EMPTY(q) \
(((q)->qwrite == (q)->qread) ? true : false)
#define PBUF_QUEUE_FULL(q) \
((((((q)->qwrite + 1) % STELLARIS_NUM_PBUF_QUEUE)) == (q)->qread) ? \
true : false )
/**
* Helper struct to hold private data used to operate your ethernet interface.
* Keeping the ethernet address of the MAC in this struct is not necessary
* as it is already kept in the struct netif.
* But this is only an example, anyway...
*/
struct ethernetif {
struct eth_addr *ethaddr;
/* Add whatever per-interface state that is needed here. */
struct pbufq txq;
struct pbufq rxq;
};
/**
* Global variable for this interface's private data. Needed to allow
* the interrupt handlers access to this information outside of the
* context of the lwIP netif.
*
*/
static struct ethernetif ethernetif_data;
/**
* Pop a pbuf packet from a pbuf packet queue
*
* @param q is the packet queue from which to pop the pbuf.
*
* @return pointer to pbuf packet if available, NULL otherswise.
*/
static struct pbuf *
dequeue_packet(struct pbufq *q)
{
struct pbuf *pBuf;
SYS_ARCH_DECL_PROTECT(lev);
/**
* This entire function must run within a "critical section" to preserve
* the integrity of the transmit pbuf queue.
*
*/
SYS_ARCH_PROTECT(lev);
if(PBUF_QUEUE_EMPTY(q)) {
/* Return a NULL pointer if the queue is empty. */
pBuf = (struct pbuf *)NULL;
}
else {
/**
* The queue is not empty so return the next frame from it
* and adjust the read pointer accordingly.
*
*/
pBuf = q->pbuf[q->qread];
q->qread = ((q->qread + 1) % STELLARIS_NUM_PBUF_QUEUE);
}
/* Return to prior interrupt state and return the pbuf pointer. */
SYS_ARCH_UNPROTECT(lev);
return(pBuf);
}
/**
* Push a pbuf packet onto a pbuf packet queue
*
* @param p is the pbuf to push onto the packet queue.
* @param q is the packet queue.
*
* @return 1 if successful, 0 if q is full.
*/
static int
enqueue_packet(struct pbuf *p, struct pbufq *q)
{
SYS_ARCH_DECL_PROTECT(lev);
int ret;
/**
* This entire function must run within a "critical section" to preserve
* the integrity of the transmit pbuf queue.
*
*/
SYS_ARCH_PROTECT(lev);
if(!PBUF_QUEUE_FULL(q)) {
/**
* The queue isn't full so we add the new frame at the current
* write position and move the write pointer.
*
*/
q->pbuf[q->qwrite] = p;
q->qwrite = ((q->qwrite + 1) % STELLARIS_NUM_PBUF_QUEUE);
ret = 1;
}
else {
/**
* The stack is full so we are throwing away this value. Keep track
* of the number of times this happens.
*
*/
q->overflow++;
ret = 0;
}
/* Return to prior interrupt state and return the pbuf pointer. */
SYS_ARCH_UNPROTECT(lev);
return(ret);
}
/**
* In this function, the hardware should be initialized.
* Called from stellarisif_init().
*
* @param netif the already initialized lwip network interface structure
* for this ethernetif
*/
static void
low_level_init(struct netif *netif)
{
u32_t temp;
//struct ethernetif *ethernetif = netif->state;
/* set MAC hardware address length */
netif->hwaddr_len = ETHARP_HWADDR_LEN;
/* set MAC hardware address */
EthernetMACAddrGet(ETH_BASE, &(netif->hwaddr[0]));
/* maximum transfer unit */
netif->mtu = 1500;
/* device capabilities */
/* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
/* Do whatever else is needed to initialize interface. */
/* Disable all Ethernet Interrupts. */
EthernetIntDisable(ETH_BASE, (ETH_INT_PHY | ETH_INT_MDIO | ETH_INT_RXER |
ETH_INT_RXOF | ETH_INT_TX | ETH_INT_TXER | ETH_INT_RX));
temp = EthernetIntStatus(ETH_BASE, false);
EthernetIntClear(ETH_BASE, temp);
/* Initialize the Ethernet Controller. */
EthernetInitExpClk(ETH_BASE, SysCtlClockGet());
/*
* Configure the Ethernet Controller for normal operation.
* - Enable TX Duplex Mode
* - Enable TX Padding
* - Enable TX CRC Generation
* - Enable RX Multicast Reception
*/
EthernetConfigSet(ETH_BASE, (ETH_CFG_TX_DPLXEN |ETH_CFG_TX_CRCEN |
ETH_CFG_TX_PADEN | ETH_CFG_RX_AMULEN));
/* Enable the Ethernet Controller transmitter and receiver. */
EthernetEnable(ETH_BASE);
IntPrioritySet(INT_ETH, 0xFF); /*设置以太网中断优先级*/
/* Enable the Ethernet Interrupt handler. */
IntEnable(INT_ETH);
/* Enable Ethernet TX and RX Packet Interrupts. */
EthernetIntEnable(ETH_BASE, ETH_INT_RX | ETH_INT_TX);
}
/**
* This function should do the actual transmission of the packet. The packet is
* contained in the pbuf that is passed to the function. This pbuf might be
* chained.
*
* @param netif the lwip network interface structure for this ethernetif
* @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
* @return ERR_OK if the packet could be sent
* an err_t value if the packet couldn't be sent
* @note This function MUST be called with interrupts disabled or with the
* Stellaris Ethernet transmit fifo protected.
*/
static err_t
low_level_transmit(struct netif *netif, struct pbuf *p)
{
int iBuf;
unsigned char *pucBuf;
unsigned long *pulBuf;
struct pbuf *q;
int iGather;
unsigned long ulGather;
unsigned char *pucGather;
/**
* Fill in the first two bytes of the payload data (configured as padding
* with ETH_PAD_SIZE = 2) with the total length of the payload data
* (minus the Ethernet MAC layer header).
*
*/
*((unsigned short *)(p->payload)) = p->tot_len - 16;
/* Initialize the gather register. */
iGather = 0;
pucGather = (unsigned char *)&ulGather;
ulGather = 0;
/* Copy data from the pbuf(s) into the TX Fifo. */
for(q = p; q != NULL; q = q->next) {
/* Intialize a char pointer and index to the pbuf payload data. */
pucBuf = (unsigned char *)q->payload;
iBuf = 0;
/**
* If the gather buffer has leftover data from a previous pbuf
* in the chain, fill it up and write it to the Tx FIFO.
*
*/
while((iBuf < q->len) && (iGather != 0)) {
/* Copy a byte from the pbuf into the gather buffer. */
pucGather[iGather] = pucBuf[iBuf++];
/* Increment the gather buffer index modulo 4. */
iGather = ((iGather + 1) % 4);
}
/**
* If the gather index is 0 and the pbuf index is non-zero,
* we have a gather buffer to write into the Tx FIFO.
*
*/
if((iGather == 0) && (iBuf != 0)) {
HWREG(ETH_BASE + MAC_O_DATA) = ulGather;
ulGather = 0;
}
/* Initialze a long pointer into the pbuf for 32-bit access. */
pulBuf = (unsigned long *)&pucBuf[iBuf];
/**
* Copy words of pbuf data into the Tx FIFO, but don't go past
* the end of the pbuf.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -