📄 stellarisif.c
字号:
*
*/
while((iBuf + 4) <= q->len) {
HWREG(ETH_BASE + MAC_O_DATA) = *pulBuf++;
iBuf += 4;
}
/**
* Check if leftover data in the pbuf and save it in the gather
* buffer for the next time.
*
*/
while(iBuf < q->len) {
/* 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);
}
}
/* Send any leftover data to the FIFO. */
HWREG(ETH_BASE + MAC_O_DATA) = ulGather;
/* Wakeup the transmitter. */
HWREG(ETH_BASE + MAC_O_TR) = MAC_TR_NEWTX;
/* Dereference the pbuf from the queue. */
pbuf_free(p);
LINK_STATS_INC(link.xmit);
return(ERR_OK);
}
/**
* This function with either place the packet into the Stellaris transmit fifo,
* or will place the packet in the interface PBUF Queue for subsequent
* transmission when the transmitter becomes idle.
*
* @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
*
*/
static err_t
low_level_output(struct netif *netif, struct pbuf *p)
{
struct ethernetif *ethernetif = netif->state;
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);
/**
* Bump the reference count on the pbuf to prevent it from being
* freed till we are done with it.
*
*/
pbuf_ref(p);
/**
* If the transmitter is idle, and there is nothing on the queue,
* send the pbuf now.
*
*/
if(PBUF_QUEUE_EMPTY(ðernetif->txq) &&
((HWREG(ETH_BASE + MAC_O_TR) & MAC_TR_NEWTX) == 0)) {
low_level_transmit(netif, p);
}
/* Otherwise place the pbuf on the transmit queue. */
else {
/* Add to transmit packet queue */
if(!enqueue_packet(p, &(ethernetif->txq))) {
/* if no room on the queue, free the pbuf reference and return error. */
pbuf_free(p);
SYS_ARCH_UNPROTECT(lev);
return (ERR_MEM);
}
}
/* Return to prior interrupt state and return. */
SYS_ARCH_UNPROTECT(lev);
return ERR_OK;
}
/**
* This function will read a single packet from the Stellaris ethernet
* interface, if available, and return a pointer to a pbuf. The timestamp
* of the packet will be placed into the pbuf structure.
*
* @param netif the lwip network interface structure for this ethernetif
* @return pointer to pbuf packet if available, NULL otherswise.
*/
static struct pbuf *
low_level_receive(struct netif *netif)
{
struct pbuf *p, *q;
u16_t len;
u32_t temp;
int i;
unsigned long *ptr;
#if LWIP_PTPD
u32_t time_s, time_ns;
/* Get the current timestamp if PTPD is enabled */
lwIPHostGetTime(&time_s, &time_ns);
#endif
/* Check if a packet is available, if not, return NULL packet. */
if((HWREG(ETH_BASE + MAC_O_NP) & MAC_NP_NPR_M) == 0) {
return(NULL);
}
/**
* Obtain the size of the packet and put it into the "len" variable.
* Note: The length returned in the FIFO length position includes the
* two bytes for the length + the 4 bytes for the FCS.
*
*/
temp = HWREG(ETH_BASE + MAC_O_DATA);
len = temp & 0xFFFF;
/* We allocate a pbuf chain of pbufs from the pool. */
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
/* If a pbuf was allocated, read the packet into the pbuf. */
if(p != NULL) {
/* Place the first word into the first pbuf location. */
*(unsigned long *)p->payload = temp;
p->payload = (char *)(p->payload) + 4;
p->len -= 4;
/* Process all but the last buffer in the pbuf chain. */
q = p;
while(q != NULL) {
/* Setup a byte pointer into the payload section of the pbuf. */
ptr = q->payload;
/**
* Read data from FIFO into the current pbuf
* (assume pbuf length is modulo 4)
*
*/
for(i = 0; i < q->len; i += 4) {
*ptr++ = HWREG(ETH_BASE + MAC_O_DATA);
}
/* Link in the next pbuf in the chain. */
q = q->next;
}
/* Restore the first pbuf parameters to their original values. */
p->payload = (char *)(p->payload) - 4;
p->len += 4;
/* Adjust the link statistics */
LINK_STATS_INC(link.recv);
#if LWIP_PTPD
/* Place the timestamp in the PBUF */
p->time_s = time_s;
p->time_ns = time_ns;
#endif
}
/* If no pbuf available, just drain the RX fifo. */
else {
for(i = 4; i < len; i+=4) {
temp = HWREG(ETH_BASE + MAC_O_DATA);
}
/* Adjust the link statistics */
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.drop);
}
return(p);
}
/**
* This function should be called when a packet is ready to be read
* from the interface. It uses the function low_level_input() that
* should handle the actual reception of bytes from the network
* interface. Then the type of the received packet is determined and
* the appropriate input function is called.
*
* @param netif the lwip network interface structure for this ethernetif
*/
int
stellarisif_input(struct netif *netif)
{
struct ethernetif *ethernetif;
struct pbuf *p;
int count = 0;
ethernetif = netif->state;
/* move received packet into a new pbuf */
while((p = dequeue_packet(ðernetif->rxq)) != NULL) {
count++;
/* process the packet. */
if (ethernet_input(p, netif)!=ERR_OK) {
LWIP_DEBUGF(NETIF_DEBUG, ("stellarisif_input: input error\n"));
pbuf_free(p);
p = NULL;
}
}
return(count);
}
/**
* Should be called at the beginning of the program to set up the
* network interface. It calls the function low_level_init() to do the
* actual setup of the hardware.
*
* This function should be passed as a parameter to netif_add().
*
* @param netif the lwip network interface structure for this ethernetif
* @return ERR_OK if the loopif is initialized
* ERR_MEM if private data couldn't be allocated
* any other err_t on error
*/
err_t
stellarisif_init(struct netif *netif)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
netif->hostname = "lwip";
#endif /* LWIP_NETIF_HOSTNAME */
/*
* Initialize the snmp variables and counters inside the struct netif.
* The last argument should be replaced with your link speed, in units
* of bits per second.
*/
NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 1000000);
netif->state = ðernetif_data;
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
/* We directly use etharp_output() here to save a function call.
* You can instead declare your own function an call etharp_output()
* from it if you have to do some checks before sending (e.g. if link
* is available...) */
netif->output = etharp_output;
netif->linkoutput = low_level_output;
ethernetif_data.ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
ethernetif_data.txq.qread = ethernetif_data.txq.qwrite = 0;
ethernetif_data.txq.overflow = 0;
ethernetif_data.rxq.qread = ethernetif_data.rxq.qwrite = 0;
ethernetif_data.rxq.overflow = 0;
/* initialize the hardware */
low_level_init(netif);
return ERR_OK;
}
/**
* Process tx and rx packets at the low-level interrupt.
*
* Should be called from the Stellaris Ethernet Interrupt Handler. This
* function will read packets from the Stellaris Ethernet fifo and place them
* into a pbuf queue. If the transmitter is idle and there is at least one packet
* on the transmit queue, it will place it in the transmit fifo and start the
* transmitter.
*
*/
void
stellarisif_interrupt(struct netif *netif)
{
struct ethernetif *ethernetif;
struct pbuf *p;
/* setup pointer to the if state data */
ethernetif = netif->state;
/**
* Process the transmit and receive queues as long as there is receive
* data available
*
*/
p = low_level_receive(netif);
while(p != NULL) {
/* Add the rx packet to the rx queue */
if(!enqueue_packet(p, ðernetif->rxq)) {
/* Could not place the packet on the queue, bail out. */
pbuf_free(p);
break;
}
/* Check if TX fifo is empty and packet available */
if((HWREG(ETH_BASE + MAC_O_TR) & MAC_TR_NEWTX) == 0) {
p = dequeue_packet(ðernetif->txq);
if(p != NULL) {
low_level_transmit(netif, p);
}
}
/* Read another packet from the RX fifo */
p = low_level_receive(netif);
}
/* One more check of the transmit queue/fifo */
if((HWREG(ETH_BASE + MAC_O_TR) & MAC_TR_NEWTX) == 0) {
p = dequeue_packet(ðernetif->txq);
if(p != NULL) {
low_level_transmit(netif, p);
}
}
}
#if NETIF_DEBUG
/* Print an IP header by using LWIP_DEBUGF
* @param p an IP packet, p->payload pointing to the IP header
*/
void
stellarisif_debug_print(struct pbuf *p)
{
struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
u16_t *plen = (u16_t *)p->payload;
LWIP_DEBUGF(NETIF_DEBUG, ("ETH header:\n"));
LWIP_DEBUGF(NETIF_DEBUG, ("Packet Length:%5"U16_F" \n",*plen));
LWIP_DEBUGF(NETIF_DEBUG, ("Destination: %02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"\n",
ethhdr->dest.addr[0],
ethhdr->dest.addr[1],
ethhdr->dest.addr[2],
ethhdr->dest.addr[3],
ethhdr->dest.addr[4],
ethhdr->dest.addr[5]));
LWIP_DEBUGF(NETIF_DEBUG, ("Source: %02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"\n",
ethhdr->src.addr[0],
ethhdr->src.addr[1],
ethhdr->src.addr[2],
ethhdr->src.addr[3],
ethhdr->src.addr[4],
ethhdr->src.addr[5]));
LWIP_DEBUGF(NETIF_DEBUG, ("Packet Type:0x%04"U16_F" \n", ethhdr->type));
}
#endif /* NETIF_DEBUG */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -