仓库迁移
This commit is contained in:
393
source/eth/ethernetif--.c
Normal file
393
source/eth/ethernetif--.c
Normal file
@@ -0,0 +1,393 @@
|
||||
/**
|
||||
* @file
|
||||
* Ethernet Interface for standalone applications (without RTOS) - works only for
|
||||
* ethernet polling mode (polling for ethernet frame reception)
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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>
|
||||
*
|
||||
*/
|
||||
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/mem.h"
|
||||
#include "netif/etharp.h"
|
||||
#include "ethernetif.h"
|
||||
#include "stm32f4x7_eth.h"
|
||||
#include "stm32f4xx.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Network interface name */
|
||||
#define IFNAME0 's'
|
||||
#define IFNAME1 't'
|
||||
|
||||
|
||||
/* Ethernet Rx & Tx DMA Descriptors */
|
||||
extern ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB];
|
||||
|
||||
/* Ethernet Driver Receive buffers */
|
||||
extern uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE];
|
||||
|
||||
/* Ethernet Driver Transmit buffers */
|
||||
extern uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE];
|
||||
|
||||
/* Global pointers to track current transmit and receive descriptors */
|
||||
extern ETH_DMADESCTypeDef *DMATxDescToSet;
|
||||
extern ETH_DMADESCTypeDef *DMARxDescToGet;
|
||||
|
||||
/* Global pointer for last received frame infos */
|
||||
extern ETH_DMA_Rx_Frame_infos *DMA_RX_FRAME_infos;
|
||||
|
||||
/**
|
||||
* In this function, the hardware should be initialized.
|
||||
* Called from ethernetif_init().
|
||||
*
|
||||
* @param netif the already initialized lwip network interface structure
|
||||
* for this ethernetif
|
||||
*/
|
||||
static void low_level_init(struct netif *netif)
|
||||
{
|
||||
#ifdef CHECKSUM_BY_HARDWARE
|
||||
int i;
|
||||
#endif
|
||||
/* set MAC hardware address length */
|
||||
netif->hwaddr_len = ETHARP_HWADDR_LEN;
|
||||
|
||||
/* set MAC hardware address */
|
||||
netif->hwaddr[0] = 0xaa;
|
||||
netif->hwaddr[1] = 0xcc;
|
||||
netif->hwaddr[2] = 0xbb;
|
||||
netif->hwaddr[3] = 0xdd;
|
||||
netif->hwaddr[4] = 0xee;
|
||||
netif->hwaddr[5] = 0xcd;
|
||||
|
||||
/* initialize MAC address in ethernet MAC */
|
||||
ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr);
|
||||
|
||||
/* 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;
|
||||
|
||||
/* Initialize Tx Descriptors list: Chain Mode */
|
||||
ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
|
||||
/* Initialize Rx Descriptors list: Chain Mode */
|
||||
ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
|
||||
|
||||
#ifdef CHECKSUM_BY_HARDWARE
|
||||
/* Enable the TCP, UDP and ICMP checksum insertion for the Tx frames */
|
||||
for(i=0; i<ETH_TXBUFNB; i++)
|
||||
{
|
||||
ETH_DMATxDescChecksumInsertionConfig(&DMATxDscrTab[i], ETH_DMATxDesc_ChecksumTCPUDPICMPFull);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* Note: TCP, UDP, ICMP checksum checking for received frame are enabled in DMA config */
|
||||
|
||||
/* Enable MAC and DMA transmission and reception */
|
||||
ETH_Start();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ETH_IRQHandler(void)
|
||||
{
|
||||
|
||||
/* Frame received */
|
||||
if ( ETH_GetDMAFlagStatus(ETH_DMA_FLAG_R) == SET)
|
||||
{
|
||||
/* Give the semaphore to wakeup LwIP task */
|
||||
xSemaphoreGiveFromISR( s_xSemaphore, &xHigherPriorityTaskWoken );
|
||||
}
|
||||
|
||||
/* Clear the interrupt flags. */
|
||||
/* Clear the Eth DMA Rx IT pending bits */
|
||||
ETH_DMAClearITPendingBit(ETH_DMA_IT_R);
|
||||
ETH_DMAClearITPendingBit(ETH_DMA_IT_NIS);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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 Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
|
||||
* strange results. You might consider waiting for space in the DMA queue
|
||||
* to become availale since the stack doesn't retry to send a packet
|
||||
* dropped because of memory failure (except for the TCP timers).
|
||||
*/
|
||||
|
||||
static err_t low_level_output(struct netif *netif, struct pbuf *p)
|
||||
{
|
||||
err_t errval;
|
||||
struct pbuf *q;
|
||||
u8 *buffer = (u8 *)(DMATxDescToSet->Buffer1Addr);
|
||||
__IO ETH_DMADESCTypeDef *DmaTxDesc;
|
||||
uint16_t framelength = 0;
|
||||
uint32_t bufferoffset = 0;
|
||||
uint32_t byteslefttocopy = 0;
|
||||
uint32_t payloadoffset = 0;
|
||||
|
||||
DmaTxDesc = DMATxDescToSet;
|
||||
bufferoffset = 0;
|
||||
|
||||
/* copy frame from pbufs to driver buffers */
|
||||
for(q = p; q != NULL; q = q->next)
|
||||
{
|
||||
/* Is this buffer available? If not, goto error */
|
||||
if((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET)
|
||||
{
|
||||
errval = ERR_BUF;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* Get bytes in current lwIP buffer */
|
||||
byteslefttocopy = q->len;
|
||||
payloadoffset = 0;
|
||||
|
||||
/* Check if the length of data to copy is bigger than Tx buffer size*/
|
||||
while( (byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE )
|
||||
{
|
||||
/* Copy data to Tx buffer*/
|
||||
memcpy( (u8_t*)((u8_t*)buffer + bufferoffset), (u8_t*)((u8_t*)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset) );
|
||||
|
||||
/* Point to next descriptor */
|
||||
DmaTxDesc = (ETH_DMADESCTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
|
||||
|
||||
/* Check if the buffer is available */
|
||||
if((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET)
|
||||
{
|
||||
errval = ERR_USE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
buffer = (u8 *)(DmaTxDesc->Buffer1Addr);
|
||||
|
||||
byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset);
|
||||
payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset);
|
||||
framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset);
|
||||
bufferoffset = 0;
|
||||
}
|
||||
|
||||
/* Copy the remaining bytes */
|
||||
memcpy( (u8_t*)((u8_t*)buffer + bufferoffset), (u8_t*)((u8_t*)q->payload + payloadoffset), byteslefttocopy );
|
||||
bufferoffset = bufferoffset + byteslefttocopy;
|
||||
framelength = framelength + byteslefttocopy;
|
||||
}
|
||||
|
||||
/* Note: padding and CRC for transmitted frame
|
||||
are automatically inserted by DMA */
|
||||
|
||||
/* Prepare transmit descriptors to give to DMA*/
|
||||
ETH_Prepare_Transmit_Descriptors(framelength);
|
||||
|
||||
errval = ERR_OK;
|
||||
|
||||
error:
|
||||
|
||||
/* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */
|
||||
if ((ETH->DMASR & ETH_DMASR_TUS) != (uint32_t)RESET)
|
||||
{
|
||||
/* Clear TUS ETHERNET DMA flag */
|
||||
ETH->DMASR = ETH_DMASR_TUS;
|
||||
|
||||
/* Resume DMA transmission*/
|
||||
ETH->DMATPDR = 0;
|
||||
}
|
||||
return errval;
|
||||
}
|
||||
/**
|
||||
* Should allocate a pbuf and transfer the bytes of the incoming
|
||||
* packet from the interface into the pbuf.
|
||||
*
|
||||
* @param netif the lwip network interface structure for this ethernetif
|
||||
* @return a pbuf filled with the received packet (including MAC header)
|
||||
* NULL on memory error
|
||||
*/
|
||||
static struct pbuf * low_level_input(struct netif *netif)
|
||||
{
|
||||
struct pbuf *p, *q;
|
||||
uint32_t len;
|
||||
FrameTypeDef frame;
|
||||
u8 *buffer;
|
||||
__IO ETH_DMADESCTypeDef *DMARxDesc;
|
||||
uint32_t bufferoffset = 0;
|
||||
uint32_t payloadoffset = 0;
|
||||
uint32_t byteslefttocopy = 0;
|
||||
uint32_t i=0;
|
||||
|
||||
/* get received frame */
|
||||
frame = ETH_Get_Received_Frame();
|
||||
|
||||
/* Obtain the size of the packet and put it into the "len" variable. */
|
||||
len = frame.length;
|
||||
buffer = (u8 *)frame.buffer;
|
||||
|
||||
/* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
|
||||
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
|
||||
|
||||
if (p != NULL)
|
||||
{
|
||||
DMARxDesc = frame.descriptor;
|
||||
bufferoffset = 0;
|
||||
for(q = p; q != NULL; q = q->next)
|
||||
{
|
||||
byteslefttocopy = q->len;
|
||||
payloadoffset = 0;
|
||||
|
||||
/* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
|
||||
while( (byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE )
|
||||
{
|
||||
/* Copy data to pbuf*/
|
||||
memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
|
||||
|
||||
/* Point to next descriptor */
|
||||
DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
|
||||
buffer = (unsigned char *)(DMARxDesc->Buffer1Addr);
|
||||
|
||||
byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
|
||||
payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
|
||||
bufferoffset = 0;
|
||||
}
|
||||
/* Copy remaining data in pbuf */
|
||||
memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), byteslefttocopy);
|
||||
bufferoffset = bufferoffset + byteslefttocopy;
|
||||
}
|
||||
}
|
||||
|
||||
/* Release descriptors to DMA */
|
||||
DMARxDesc =frame.descriptor;
|
||||
|
||||
/* Set Own bit in Rx descriptors: gives the buffers back to DMA */
|
||||
for (i=0; i<DMA_RX_FRAME_infos->Seg_Count; i++)
|
||||
{
|
||||
DMARxDesc->Status = ETH_DMARxDesc_OWN;
|
||||
DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
|
||||
}
|
||||
|
||||
/* Clear Segment_Count */
|
||||
DMA_RX_FRAME_infos->Seg_Count =0;
|
||||
|
||||
/* When Rx Buffer unavailable flag is set: clear it and resume reception */
|
||||
if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)
|
||||
{
|
||||
/* Clear RBUS ETHERNET DMA flag */
|
||||
ETH->DMASR = ETH_DMASR_RBUS;
|
||||
/* Resume DMA reception */
|
||||
ETH->DMARPDR = 0;
|
||||
}
|
||||
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
|
||||
*/
|
||||
err_t ethernetif_input(struct netif *netif)
|
||||
{
|
||||
err_t err;
|
||||
struct pbuf *p;
|
||||
|
||||
/* move received packet into a new pbuf */
|
||||
p = low_level_input(netif);
|
||||
|
||||
/* no packet could be read, silently ignore this */
|
||||
if (p == NULL) return ERR_MEM;
|
||||
|
||||
/* entry point to the LwIP stack */
|
||||
err = netif->input(p, netif);
|
||||
|
||||
if (err != ERR_OK)
|
||||
{
|
||||
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
|
||||
pbuf_free(p);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ethernetif_init(struct netif *netif)
|
||||
{
|
||||
LWIP_ASSERT("netif != NULL", (netif != NULL));
|
||||
|
||||
#if LWIP_NETIF_HOSTNAME
|
||||
/* Initialize interface hostname */
|
||||
netif->hostname = "lwip";
|
||||
#endif /* LWIP_NETIF_HOSTNAME */
|
||||
|
||||
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;
|
||||
|
||||
/* initialize the hardware */
|
||||
low_level_init(netif);
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
|
461
source/eth/ethernetif.c
Normal file
461
source/eth/ethernetif.c
Normal file
@@ -0,0 +1,461 @@
|
||||
/**
|
||||
* @file
|
||||
* Ethernet Interface Skeleton
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is a skeleton for developing Ethernet network interface
|
||||
* drivers for lwIP. Add code to the low_level functions and do a
|
||||
* search-and-replace for the word "ethernetif" to replace it with
|
||||
* something that better describes your network interface.
|
||||
*/
|
||||
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/def.h"
|
||||
#include "lwip/mem.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/timers.h"
|
||||
#include "netif/etharp.h"
|
||||
#include "lwip/err.h"
|
||||
#include "ethernetif.h"
|
||||
|
||||
#include "rtthread.h"
|
||||
#include "stm32f4x7_eth.h"
|
||||
#include <string.h>
|
||||
#include "dev_flash.h"
|
||||
|
||||
|
||||
#define netifMTU (1500)
|
||||
#define netifINTERFACE_TASK_STACK_SIZE ( 1024 )
|
||||
#define netifINTERFACE_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
|
||||
#define netifGUARD_BLOCK_TIME ( 250 )
|
||||
/* The time to block waiting for input. */
|
||||
#define emacBLOCK_TIME_WAITING_FOR_INPUT ( ( portTickType ) 100 )
|
||||
|
||||
/* Define those to better describe your network interface. */
|
||||
#define IFNAME0 's'
|
||||
#define IFNAME1 't'
|
||||
|
||||
|
||||
static struct netif *s_pxNetIf = NULL;
|
||||
static rt_sem_t s_xSemaphore = NULL;
|
||||
|
||||
|
||||
/* Ethernet Rx & Tx DMA Descriptors */
|
||||
extern ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB];
|
||||
|
||||
/* Ethernet Receive buffers */
|
||||
extern uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE];
|
||||
|
||||
/* Ethernet Transmit buffers */
|
||||
extern uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE];
|
||||
|
||||
/* Global pointers to track current transmit and receive descriptors */
|
||||
extern ETH_DMADESCTypeDef *DMATxDescToSet;
|
||||
extern ETH_DMADESCTypeDef *DMARxDescToGet;
|
||||
|
||||
/* Global pointer for last received frame infos */
|
||||
extern ETH_DMA_Rx_Frame_infos *DMA_RX_FRAME_infos;
|
||||
|
||||
|
||||
static void ethernetif_input( void * pvParameters );
|
||||
static void arp_timer(void *arg);
|
||||
|
||||
|
||||
/**
|
||||
* In this function, the hardware should be initialized.
|
||||
* Called from ethernetif_init().
|
||||
*
|
||||
* @param netif the already initialized lwip network interface structure
|
||||
* for this ethernetif
|
||||
*/
|
||||
static void low_level_init(struct netif *netif)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
/* set netif MAC hardware address length */
|
||||
netif->hwaddr_len = ETHARP_HWADDR_LEN;
|
||||
|
||||
/* set netif MAC hardware address */
|
||||
const uint8_t *mac=sys_param()->mac;
|
||||
netif->hwaddr[0] = mac[0];
|
||||
netif->hwaddr[1] = mac[1];
|
||||
netif->hwaddr[2] = mac[2];
|
||||
netif->hwaddr[3] = mac[3];
|
||||
netif->hwaddr[4] = mac[4];
|
||||
netif->hwaddr[5] = mac[5];
|
||||
|
||||
/* set netif maximum transfer unit */
|
||||
netif->mtu = 1500;
|
||||
|
||||
/* Accept broadcast address and ARP traffic */
|
||||
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
|
||||
|
||||
s_pxNetIf =netif;
|
||||
|
||||
/* create binary semaphore used for informing ethernetif of frame reception */
|
||||
if (s_xSemaphore == NULL)
|
||||
{
|
||||
//vSemaphoreCreateBinary(s_xSemaphore);
|
||||
s_xSemaphore=rt_sem_create("eth_recv",0,RT_IPC_FLAG_PRIO);
|
||||
//xSemaphoreTake( s_xSemaphore, 0);
|
||||
//rt_sem_take(s_xSemaphore,0);
|
||||
}
|
||||
|
||||
/* initialize MAC address in ethernet MAC */
|
||||
ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr);
|
||||
|
||||
/* Initialize Tx Descriptors list: Chain Mode */
|
||||
ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
|
||||
/* Initialize Rx Descriptors list: Chain Mode */
|
||||
ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
|
||||
|
||||
/* Enable Ethernet Rx interrrupt */
|
||||
{
|
||||
for(i=0; i<ETH_RXBUFNB; i++)
|
||||
{
|
||||
ETH_DMARxDescReceiveITConfig(&DMARxDscrTab[i], ENABLE);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CHECKSUM_BY_HARDWARE
|
||||
/* Enable the checksum insertion for the Tx frames */
|
||||
{
|
||||
for(i=0; i<ETH_TXBUFNB; i++)
|
||||
{
|
||||
ETH_DMATxDescChecksumInsertionConfig(&DMATxDscrTab[i], ETH_DMATxDesc_ChecksumTCPUDPICMPFull);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* create the task that handles the ETH_MAC */
|
||||
// xTaskCreate(ethernetif_input, (signed char*) "Eth_if", netifINTERFACE_TASK_STACK_SIZE, NULL,
|
||||
// netifINTERFACE_TASK_PRIORITY,NULL);
|
||||
rt_thread_t t= rt_thread_create("eth_recv_t",ethernetif_input,NULL,netifINTERFACE_TASK_STACK_SIZE,
|
||||
19,20);
|
||||
rt_thread_startup(t);
|
||||
/* Enable MAC and DMA transmission and reception */
|
||||
ETH_Start();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ETH_IRQHandler(void)
|
||||
{
|
||||
rt_interrupt_enter();
|
||||
/* Frame received */
|
||||
if ( ETH_GetDMAFlagStatus(ETH_DMA_FLAG_R) == SET)
|
||||
{
|
||||
/* Give the semaphore to wakeup LwIP task */
|
||||
//xSemaphoreGiveFromISR( s_xSemaphore, &xHigherPriorityTaskWoken );
|
||||
if(s_xSemaphore!=NULL)
|
||||
{
|
||||
rt_sem_release(s_xSemaphore);
|
||||
}
|
||||
}
|
||||
|
||||
/* Clear the interrupt flags. */
|
||||
/* Clear the Eth DMA Rx IT pending bits */
|
||||
ETH_DMAClearITPendingBit(ETH_DMA_IT_R);
|
||||
ETH_DMAClearITPendingBit(ETH_DMA_IT_NIS);
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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 Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
|
||||
* strange results. You might consider waiting for space in the DMA queue
|
||||
* to become availale since the stack doesn't retry to send a packet
|
||||
* dropped because of memory failure (except for the TCP timers).
|
||||
*/
|
||||
|
||||
static err_t low_level_output(struct netif *netif, struct pbuf *p)
|
||||
{
|
||||
static rt_mutex_t xTxSemaphore = NULL;
|
||||
struct pbuf *q;
|
||||
u8 *buffer ;
|
||||
__IO ETH_DMADESCTypeDef *DmaTxDesc;
|
||||
uint16_t framelength = 0;
|
||||
uint32_t bufferoffset = 0;
|
||||
uint32_t byteslefttocopy = 0;
|
||||
uint32_t payloadoffset = 0;
|
||||
|
||||
if (xTxSemaphore == NULL)
|
||||
{
|
||||
//vSemaphoreCreateBinary (xTxSemaphore);
|
||||
xTxSemaphore=rt_mutex_create("eth_out_mut",RT_IPC_FLAG_PRIO);
|
||||
}
|
||||
|
||||
//if (xSemaphoreTake(xTxSemaphore, netifGUARD_BLOCK_TIME))
|
||||
if(rt_mutex_take(xTxSemaphore,RT_WAITING_FOREVER)==RT_EOK)
|
||||
{
|
||||
DmaTxDesc = DMATxDescToSet;
|
||||
buffer = (u8 *)(DmaTxDesc->Buffer1Addr);
|
||||
bufferoffset = 0;
|
||||
|
||||
for(q = p; q != NULL; q = q->next)
|
||||
{
|
||||
if((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET)
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* Get bytes in current lwIP buffer */
|
||||
byteslefttocopy = q->len;
|
||||
payloadoffset = 0;
|
||||
|
||||
/* Check if the length of data to copy is bigger than Tx buffer size*/
|
||||
while( (byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE )
|
||||
{
|
||||
/* Copy data to Tx buffer*/
|
||||
memcpy( (u8_t*)((u8_t*)buffer + bufferoffset), (u8_t*)((u8_t*)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset) );
|
||||
|
||||
/* Point to next descriptor */
|
||||
DmaTxDesc = (ETH_DMADESCTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
|
||||
|
||||
/* Check if the buffer is available */
|
||||
if((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET)
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
|
||||
buffer = (u8 *)(DmaTxDesc->Buffer1Addr);
|
||||
|
||||
byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset);
|
||||
payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset);
|
||||
framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset);
|
||||
bufferoffset = 0;
|
||||
}
|
||||
|
||||
/* Copy the remaining bytes */
|
||||
memcpy( (u8_t*)((u8_t*)buffer + bufferoffset), (u8_t*)((u8_t*)q->payload + payloadoffset), byteslefttocopy );
|
||||
bufferoffset = bufferoffset + byteslefttocopy;
|
||||
framelength = framelength + byteslefttocopy;
|
||||
}
|
||||
|
||||
/* Prepare transmit descriptors to give to DMA*/
|
||||
ETH_Prepare_Transmit_Descriptors(framelength);
|
||||
|
||||
/* Give semaphore and exit */
|
||||
error:
|
||||
|
||||
//xSemaphoreGive(xTxSemaphore);
|
||||
rt_mutex_release(xTxSemaphore);
|
||||
}
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should allocate a pbuf and transfer the bytes of the incoming
|
||||
* packet from the interface into the pbuf.
|
||||
*
|
||||
* @param netif the lwip network interface structure for this ethernetif
|
||||
* @return a pbuf filled with the received packet (including MAC header)
|
||||
* NULL on memory error
|
||||
*/
|
||||
static struct pbuf * low_level_input(struct netif *netif)
|
||||
{
|
||||
struct pbuf *p= NULL, *q;
|
||||
u32_t len;
|
||||
FrameTypeDef frame;
|
||||
u8 *buffer;
|
||||
__IO ETH_DMADESCTypeDef *DMARxDesc;
|
||||
uint32_t bufferoffset = 0;
|
||||
uint32_t payloadoffset = 0;
|
||||
uint32_t byteslefttocopy = 0;
|
||||
uint32_t i=0;
|
||||
|
||||
/* get received frame */
|
||||
frame = ETH_Get_Received_Frame_interrupt();
|
||||
|
||||
/* Obtain the size of the packet and put it into the "len" variable. */
|
||||
len = frame.length;
|
||||
buffer = (u8 *)frame.buffer;
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
/* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
|
||||
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
|
||||
}
|
||||
|
||||
if (p != NULL)
|
||||
{
|
||||
DMARxDesc = frame.descriptor;
|
||||
bufferoffset = 0;
|
||||
for(q = p; q != NULL; q = q->next)
|
||||
{
|
||||
byteslefttocopy = q->len;
|
||||
payloadoffset = 0;
|
||||
|
||||
/* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
|
||||
while( (byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE )
|
||||
{
|
||||
/* Copy data to pbuf*/
|
||||
memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
|
||||
|
||||
/* Point to next descriptor */
|
||||
DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
|
||||
buffer = (unsigned char *)(DMARxDesc->Buffer1Addr);
|
||||
|
||||
byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
|
||||
payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
|
||||
bufferoffset = 0;
|
||||
}
|
||||
|
||||
/* Copy remaining data in pbuf */
|
||||
memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), byteslefttocopy);
|
||||
bufferoffset = bufferoffset + byteslefttocopy;
|
||||
}
|
||||
|
||||
/* Release descriptors to DMA */
|
||||
DMARxDesc =frame.descriptor;
|
||||
|
||||
/* Set Own bit in Rx descriptors: gives the buffers back to DMA */
|
||||
for (i=0; i<DMA_RX_FRAME_infos->Seg_Count; i++)
|
||||
{
|
||||
DMARxDesc->Status = ETH_DMARxDesc_OWN;
|
||||
DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
|
||||
}
|
||||
|
||||
/* Clear Segment_Count */
|
||||
DMA_RX_FRAME_infos->Seg_Count =0;
|
||||
/* added for test*/
|
||||
}
|
||||
|
||||
/* When Rx Buffer unavailable flag is set: clear it and resume reception */
|
||||
if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)
|
||||
{
|
||||
/* Clear RBUS ETHERNET DMA flag */
|
||||
ETH->DMASR = ETH_DMASR_RBUS;
|
||||
/* Resume DMA reception */
|
||||
ETH->DMARPDR = 0;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This function is the ethernetif_input task, it is processed 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
|
||||
*/
|
||||
void ethernetif_input( void * pvParameters )
|
||||
{
|
||||
struct pbuf *p;
|
||||
|
||||
for( ;; )
|
||||
{
|
||||
//if (xSemaphoreTake( s_xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT)==pdTRUE)
|
||||
if(rt_sem_take(s_xSemaphore,RT_WAITING_FOREVER)==RT_EOK)
|
||||
{
|
||||
TRY_GET_NEXT_FRAME:
|
||||
p = low_level_input( s_pxNetIf );
|
||||
if (p != NULL)
|
||||
{
|
||||
if (ERR_OK != s_pxNetIf->input( p, s_pxNetIf))
|
||||
{
|
||||
pbuf_free(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
goto TRY_GET_NEXT_FRAME;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ethernetif_init(struct netif *netif)
|
||||
{
|
||||
LWIP_ASSERT("netif != NULL", (netif != NULL));
|
||||
|
||||
#if LWIP_NETIF_HOSTNAME
|
||||
/* Initialize interface hostname */
|
||||
netif->hostname = "lwip";
|
||||
#endif /* LWIP_NETIF_HOSTNAME */
|
||||
|
||||
netif->name[0] = IFNAME0;
|
||||
netif->name[1] = IFNAME1;
|
||||
|
||||
netif->output = etharp_output;
|
||||
netif->linkoutput = low_level_output;
|
||||
|
||||
/* initialize the hardware */
|
||||
low_level_init(netif);
|
||||
|
||||
etharp_init();
|
||||
sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
static void arp_timer(void *arg)
|
||||
{
|
||||
etharp_tmr();
|
||||
sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
|
||||
}
|
||||
|
11
source/eth/ethernetif.h
Normal file
11
source/eth/ethernetif.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef __ETHERNETIF_H__
|
||||
#define __ETHERNETIF_H__
|
||||
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/netif.h"
|
||||
|
||||
err_t ethernetif_init(struct netif *netif);
|
||||
//err_t ethernetif_input(struct netif *netif);
|
||||
|
||||
#endif
|
238
source/eth/lwipopts.h
Normal file
238
source/eth/lwipopts.h
Normal file
@@ -0,0 +1,238 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file lwipopts.h
|
||||
* @author MCD Application Team
|
||||
* @version V1.1.0
|
||||
* @date 31-July-2013
|
||||
* @brief lwIP Options Configuration.
|
||||
* This file is based on Utilities\lwip_v1.4.1\src\include\lwip\opt.h
|
||||
* and contains the lwIP configuration for the STM32F4x7 demonstration.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
|
||||
*
|
||||
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
|
||||
* You may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.st.com/software_license_agreement_liberty_v2
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef __LWIPOPTS_H__
|
||||
#define __LWIPOPTS_H__
|
||||
|
||||
/**
|
||||
* SYS_LIGHTWEIGHT_PROT==1: if you want inter-task protection for certain
|
||||
* critical regions during buffer allocation, deallocation and memory
|
||||
* allocation and deallocation.
|
||||
*/
|
||||
#define SYS_LIGHTWEIGHT_PROT 1
|
||||
|
||||
/**
|
||||
* NO_SYS==1: Provides VERY minimal functionality. Otherwise,
|
||||
* use lwIP facilities.
|
||||
*/
|
||||
#define NO_SYS 0
|
||||
|
||||
/**
|
||||
* NO_SYS_NO_TIMERS==1: Drop support for sys_timeout when NO_SYS==1
|
||||
* Mainly for compatibility to old versions.
|
||||
*/
|
||||
#define NO_SYS_NO_TIMERS 1
|
||||
|
||||
/* ---------- Memory options ---------- */
|
||||
/* MEM_ALIGNMENT: should be set to the alignment of the CPU for which
|
||||
lwIP is compiled. 4 byte alignment -> define MEM_ALIGNMENT to 4, 2
|
||||
byte alignment -> define MEM_ALIGNMENT to 2. */
|
||||
#define MEM_ALIGNMENT 4
|
||||
|
||||
/* MEM_SIZE: the size of the heap memory. If the application will send
|
||||
a lot of data that needs to be copied, this should be set high. */
|
||||
#define MEM_SIZE (10*1024)
|
||||
|
||||
/* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application
|
||||
sends a lot of data out of ROM (or other static memory), this
|
||||
should be set high. */
|
||||
#define MEMP_NUM_PBUF 50
|
||||
/* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One
|
||||
per active UDP "connection". */
|
||||
#define MEMP_NUM_UDP_PCB 6
|
||||
/* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP
|
||||
connections. */
|
||||
#define MEMP_NUM_TCP_PCB 10
|
||||
/* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP
|
||||
connections. */
|
||||
#define MEMP_NUM_TCP_PCB_LISTEN 6
|
||||
/* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP
|
||||
segments. */
|
||||
#define MEMP_NUM_TCP_SEG 12
|
||||
/* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active
|
||||
timeouts. */
|
||||
#define MEMP_NUM_SYS_TIMEOUT 10
|
||||
|
||||
|
||||
/* ---------- Pbuf options ---------- */
|
||||
/* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */
|
||||
#define PBUF_POOL_SIZE 10
|
||||
|
||||
/* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */
|
||||
#define PBUF_POOL_BUFSIZE 1500
|
||||
|
||||
|
||||
/* ---------- TCP options ---------- */
|
||||
#define LWIP_TCP 1
|
||||
#define TCP_TTL 255
|
||||
|
||||
/* Controls if TCP should queue segments that arrive out of
|
||||
order. Define to 0 if your device is low on memory. */
|
||||
#define TCP_QUEUE_OOSEQ 0
|
||||
|
||||
/* TCP Maximum segment size. */
|
||||
#define TCP_MSS (1500 - 40) /* TCP_MSS = (Ethernet MTU - IP header size - TCP header size) */
|
||||
|
||||
/* TCP sender buffer space (bytes). */
|
||||
#define TCP_SND_BUF (4*TCP_MSS)
|
||||
|
||||
/* TCP_SND_QUEUELEN: TCP sender buffer space (pbufs). This must be at least
|
||||
as much as (2 * TCP_SND_BUF/TCP_MSS) for things to work. */
|
||||
|
||||
#define TCP_SND_QUEUELEN (2* TCP_SND_BUF/TCP_MSS)
|
||||
|
||||
/* TCP receive window. */
|
||||
#define TCP_WND (2*TCP_MSS)
|
||||
|
||||
|
||||
/* ---------- ICMP options ---------- */
|
||||
#define LWIP_ICMP 1
|
||||
|
||||
|
||||
/* ---------- DHCP options ---------- */
|
||||
/* Define LWIP_DHCP to 1 if you want DHCP configuration of
|
||||
interfaces. DHCP is not implemented in lwIP 0.5.1, however, so
|
||||
turning this on does currently not work. */
|
||||
#define LWIP_DHCP 1
|
||||
|
||||
|
||||
/* ---------- UDP options ---------- */
|
||||
#define LWIP_UDP 1
|
||||
#define UDP_TTL 255
|
||||
|
||||
|
||||
/* ---------- Statistics options ---------- */
|
||||
#define LWIP_STATS 0
|
||||
#define LWIP_PROVIDE_ERRNO 1
|
||||
|
||||
/* ---------- link callback options ---------- */
|
||||
/* LWIP_NETIF_LINK_CALLBACK==1: Support a callback function from an interface
|
||||
* whenever the link changes (i.e., link down)
|
||||
*/
|
||||
#define LWIP_NETIF_LINK_CALLBACK 1
|
||||
/*
|
||||
--------------------------------------
|
||||
---------- Checksum options ----------
|
||||
--------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
The STM32F4x7 allows computing and verifying the IP, UDP, TCP and ICMP checksums by hardware:
|
||||
- To use this feature let the following define uncommented.
|
||||
- To disable it and process by CPU comment the the checksum.
|
||||
*/
|
||||
#define CHECKSUM_BY_HARDWARE
|
||||
|
||||
|
||||
#ifdef CHECKSUM_BY_HARDWARE
|
||||
/* CHECKSUM_GEN_IP==0: Generate checksums by hardware for outgoing IP packets.*/
|
||||
#define CHECKSUM_GEN_IP 0
|
||||
/* CHECKSUM_GEN_UDP==0: Generate checksums by hardware for outgoing UDP packets.*/
|
||||
#define CHECKSUM_GEN_UDP 0
|
||||
/* CHECKSUM_GEN_TCP==0: Generate checksums by hardware for outgoing TCP packets.*/
|
||||
#define CHECKSUM_GEN_TCP 0
|
||||
/* CHECKSUM_CHECK_IP==0: Check checksums by hardware for incoming IP packets.*/
|
||||
#define CHECKSUM_CHECK_IP 0
|
||||
/* CHECKSUM_CHECK_UDP==0: Check checksums by hardware for incoming UDP packets.*/
|
||||
#define CHECKSUM_CHECK_UDP 0
|
||||
/* CHECKSUM_CHECK_TCP==0: Check checksums by hardware for incoming TCP packets.*/
|
||||
#define CHECKSUM_CHECK_TCP 0
|
||||
/* CHECKSUM_CHECK_ICMP==0: Check checksums by hardware for incoming ICMP packets.*/
|
||||
#define CHECKSUM_GEN_ICMP 0
|
||||
#else
|
||||
/* CHECKSUM_GEN_IP==1: Generate checksums in software for outgoing IP packets.*/
|
||||
#define CHECKSUM_GEN_IP 1
|
||||
/* CHECKSUM_GEN_UDP==1: Generate checksums in software for outgoing UDP packets.*/
|
||||
#define CHECKSUM_GEN_UDP 1
|
||||
/* CHECKSUM_GEN_TCP==1: Generate checksums in software for outgoing TCP packets.*/
|
||||
#define CHECKSUM_GEN_TCP 1
|
||||
/* CHECKSUM_CHECK_IP==1: Check checksums in software for incoming IP packets.*/
|
||||
#define CHECKSUM_CHECK_IP 1
|
||||
/* CHECKSUM_CHECK_UDP==1: Check checksums in software for incoming UDP packets.*/
|
||||
#define CHECKSUM_CHECK_UDP 1
|
||||
/* CHECKSUM_CHECK_TCP==1: Check checksums in software for incoming TCP packets.*/
|
||||
#define CHECKSUM_CHECK_TCP 1
|
||||
/* CHECKSUM_CHECK_ICMP==1: Check checksums by hardware for incoming ICMP packets.*/
|
||||
#define CHECKSUM_GEN_ICMP 1
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
----------------------------------------------
|
||||
---------- Sequential layer options ----------
|
||||
----------------------------------------------
|
||||
*/
|
||||
/**
|
||||
* LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c)
|
||||
*/
|
||||
#define LWIP_NETCONN 1
|
||||
|
||||
/*
|
||||
------------------------------------
|
||||
---------- Socket options ----------
|
||||
------------------------------------
|
||||
*/
|
||||
/**
|
||||
* LWIP_SOCKET==1: Enable Socket API (require to use sockets.c)
|
||||
*/
|
||||
#define LWIP_SOCKET 0
|
||||
|
||||
|
||||
/*
|
||||
----------------------------------------
|
||||
---------- Lwip Debug options ----------
|
||||
----------------------------------------
|
||||
*/
|
||||
//#define LWIP_DEBUG 1
|
||||
|
||||
|
||||
|
||||
#define MEM_LIBC_MALLOC 1
|
||||
|
||||
#define MEMP_MEM_MALLOC 1
|
||||
|
||||
#define DEFAULT_RAW_RECVMBOX_SIZE 4
|
||||
#define DEFAULT_TCP_RECVMBOX_SIZE 4
|
||||
#define DEFAULT_UDP_RECVMBOX_SIZE 4
|
||||
#define DEFAULT_ACCEPTMBOX_SIZE 10
|
||||
#define DEFAULT_THREAD_STACKSIZE 1024
|
||||
#define DEFAULT_THREAD_PRIO 25
|
||||
#define TCPIP_THREAD_STACKSIZE 2048
|
||||
#define TCPIP_THREAD_PRIO 10
|
||||
#define TCPIP_MBOX_SIZE 10
|
||||
#define sys_msleep rt_thread_mdelay
|
||||
#define LWIP_TCP_KEEPALIVE 1
|
||||
#define TCP_KEEPIDLE_DEFAULT 3000 //3秒内双方无数据则发起保活探测
|
||||
#define TCP_KEEPINTVL_DEFAULT 1000 //1秒发送一次保活探测
|
||||
#define TCP_KEEPCNT_DEFAULT 3 //3次探测无响应则断开
|
||||
//#define TCP_MAXIDLE (TCP_KEEPCNT_DEFAULT * TCP_KEEPINTVL_DEFAULT)
|
||||
|
||||
#endif /* __LWIPOPTS_H__ */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
308
source/eth/netconf.c
Normal file
308
source/eth/netconf.c
Normal file
@@ -0,0 +1,308 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file netconf.c
|
||||
* @author MCD Application Team
|
||||
* @version V1.1.0
|
||||
* @date 31-July-2013
|
||||
* @brief Network connection configuration
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
|
||||
*
|
||||
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
|
||||
* You may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.st.com/software_license_agreement_liberty_v2
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "lwip/tcpip.h"
|
||||
#include "stm32f4xx.h"
|
||||
#include "lwip/mem.h"
|
||||
#include "lwip/memp.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "lwip/tcp_impl.h"
|
||||
#include "lwip/udp.h"
|
||||
#include "netif/etharp.h"
|
||||
#include "lwip/dhcp.h"
|
||||
#include "ethernetif.h"
|
||||
#include "stm32f4x7_eth_bsp.h"
|
||||
#include "netconf.h"
|
||||
#include <stdio.h>
|
||||
#include "dev_flash.h"
|
||||
|
||||
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
#define MAX_DHCP_TRIES 4
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
struct netif gnetif;
|
||||
uint32_t TCPTimer = 0;
|
||||
uint32_t ARPTimer = 0;
|
||||
uint32_t IPaddress = 0;
|
||||
|
||||
#ifdef USE_DHCP
|
||||
uint32_t DHCPfineTimer = 0;
|
||||
uint32_t DHCPcoarseTimer = 0;
|
||||
__IO uint8_t DHCP_state;
|
||||
#endif
|
||||
extern __IO uint32_t EthStatus;
|
||||
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
void LwIP_DHCP_Process_Handle(void);
|
||||
/**
|
||||
* @brief Initializes the lwIP stack
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void LwIP_Init(void)
|
||||
{
|
||||
struct ip_addr ipaddr;
|
||||
struct ip_addr netmask;
|
||||
struct ip_addr gw;
|
||||
#ifndef USE_DHCP
|
||||
uint8_t iptab[4] = {0};
|
||||
uint8_t iptxt[20];
|
||||
#endif
|
||||
|
||||
/* Initializes the dynamic memory heap defined by MEM_SIZE.*/
|
||||
//mem_init();
|
||||
|
||||
/* Initializes the memory pools defined by MEMP_NUM_x.*/
|
||||
// memp_init();
|
||||
|
||||
tcpip_init(NULL,NULL);
|
||||
|
||||
#ifdef USE_DHCP
|
||||
ipaddr.addr = 0;
|
||||
netmask.addr = 0;
|
||||
gw.addr = 0;
|
||||
#else
|
||||
const uint8_t *ip=sys_param()->local_ip;
|
||||
IP4_ADDR(&ipaddr, ip[0], ip[1], ip[2], ip[3]);
|
||||
IP4_ADDR(&netmask, 255, 255 , 255, 0);
|
||||
IP4_ADDR(&gw, ip[0], ip[1], ip[2], 1);
|
||||
#endif
|
||||
|
||||
/* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
|
||||
struct ip_addr *netmask, struct ip_addr *gw,
|
||||
void *state, err_t (* init)(struct netif *netif),
|
||||
err_t (* input)(struct pbuf *p, struct netif *netif))
|
||||
|
||||
Adds your network interface to the netif_list. Allocate a struct
|
||||
netif and pass a pointer to this structure as the first argument.
|
||||
Give pointers to cleared ip_addr structures when using DHCP,
|
||||
or fill them with sane numbers otherwise. The state pointer may be NULL.
|
||||
|
||||
The init function pointer must point to a initialization function for
|
||||
your ethernet netif interface. The following code illustrates it's use.*/
|
||||
netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
|
||||
|
||||
/* Registers the default network interface.*/
|
||||
netif_set_default(&gnetif);
|
||||
|
||||
if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG))
|
||||
{
|
||||
/* Set Ethernet link flag */
|
||||
gnetif.flags |= NETIF_FLAG_LINK_UP;
|
||||
|
||||
/* When the netif is fully configured this function must be called.*/
|
||||
netif_set_up(&gnetif);
|
||||
#ifdef USE_DHCP
|
||||
DHCP_state = DHCP_START;
|
||||
#endif /* USE_DHCP */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* When the netif link is down this function must be called.*/
|
||||
netif_set_down(&gnetif);
|
||||
#ifdef USE_DHCP
|
||||
DHCP_state = DHCP_LINK_DOWN;
|
||||
#endif /* USE_DHCP */
|
||||
}
|
||||
|
||||
/* Set the link callback function, this function is called on change of link status*/
|
||||
netif_set_link_callback(&gnetif, ETH_link_callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Called when a frame is received
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void LwIP_Pkt_Handle(void)
|
||||
{
|
||||
/* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
|
||||
//ethernetif_input(&gnetif);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LwIP periodic tasks
|
||||
* @param localtime the current LocalTime value
|
||||
* @retval None
|
||||
*/
|
||||
void LwIP_Periodic_Handle(__IO uint32_t localtime)
|
||||
{
|
||||
#if LWIP_TCP
|
||||
/* TCP periodic process every 250 ms */
|
||||
if (localtime - TCPTimer >= TCP_TMR_INTERVAL)
|
||||
{
|
||||
TCPTimer = localtime;
|
||||
tcp_tmr();
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ARP periodic process every 5s */
|
||||
if ((localtime - ARPTimer) >= ARP_TMR_INTERVAL)
|
||||
{
|
||||
ARPTimer = localtime;
|
||||
etharp_tmr();
|
||||
}
|
||||
|
||||
#ifdef USE_DHCP
|
||||
/* Fine DHCP periodic process every 500ms */
|
||||
if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS)
|
||||
{
|
||||
DHCPfineTimer = localtime;
|
||||
dhcp_fine_tmr();
|
||||
if ((DHCP_state != DHCP_ADDRESS_ASSIGNED) &&
|
||||
(DHCP_state != DHCP_TIMEOUT) &&
|
||||
(DHCP_state != DHCP_LINK_DOWN))
|
||||
{
|
||||
/* toggle LED1 to indicate DHCP on-going process */
|
||||
STM_EVAL_LEDToggle(LED1);
|
||||
|
||||
/* process DHCP state machine */
|
||||
LwIP_DHCP_Process_Handle();
|
||||
}
|
||||
}
|
||||
|
||||
/* DHCP Coarse periodic process every 60s */
|
||||
if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS)
|
||||
{
|
||||
DHCPcoarseTimer = localtime;
|
||||
dhcp_coarse_tmr();
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_DHCP
|
||||
/**
|
||||
* @brief LwIP_DHCP_Process_Handle
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void LwIP_DHCP_Process_Handle()
|
||||
{
|
||||
struct ip_addr ipaddr;
|
||||
struct ip_addr netmask;
|
||||
struct ip_addr gw;
|
||||
uint8_t iptab[4] = {0};
|
||||
uint8_t iptxt[20];
|
||||
|
||||
switch (DHCP_state)
|
||||
{
|
||||
case DHCP_START:
|
||||
{
|
||||
DHCP_state = DHCP_WAIT_ADDRESS;
|
||||
dhcp_start(&gnetif);
|
||||
/* IP address should be set to 0
|
||||
every time we want to assign a new DHCP address */
|
||||
IPaddress = 0;
|
||||
#ifdef USE_LCD
|
||||
LCD_DisplayStringLine(Line4, (uint8_t*)" Looking for ");
|
||||
LCD_DisplayStringLine(Line5, (uint8_t*)" DHCP server ");
|
||||
LCD_DisplayStringLine(Line6, (uint8_t*)" please wait... ");
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
|
||||
case DHCP_WAIT_ADDRESS:
|
||||
{
|
||||
/* Read the new IP address */
|
||||
IPaddress = gnetif.ip_addr.addr;
|
||||
|
||||
if (IPaddress!=0)
|
||||
{
|
||||
DHCP_state = DHCP_ADDRESS_ASSIGNED;
|
||||
|
||||
/* Stop DHCP */
|
||||
dhcp_stop(&gnetif);
|
||||
|
||||
#ifdef USE_LCD
|
||||
iptab[0] = (uint8_t)(IPaddress >> 24);
|
||||
iptab[1] = (uint8_t)(IPaddress >> 16);
|
||||
iptab[2] = (uint8_t)(IPaddress >> 8);
|
||||
iptab[3] = (uint8_t)(IPaddress);
|
||||
|
||||
sprintf((char*)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]);
|
||||
|
||||
LCD_ClearLine(Line4);
|
||||
LCD_ClearLine(Line5);
|
||||
LCD_ClearLine(Line6);
|
||||
|
||||
/* Display the IP address */
|
||||
LCD_DisplayStringLine(Line7, (uint8_t*)"IP address assigned ");
|
||||
LCD_DisplayStringLine(Line8, (uint8_t*)" by a DHCP server ");
|
||||
LCD_DisplayStringLine(Line9, iptxt);
|
||||
#endif
|
||||
STM_EVAL_LEDOn(LED1);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* DHCP timeout */
|
||||
if (gnetif.dhcp->tries > MAX_DHCP_TRIES)
|
||||
{
|
||||
DHCP_state = DHCP_TIMEOUT;
|
||||
|
||||
/* Stop DHCP */
|
||||
dhcp_stop(&gnetif);
|
||||
|
||||
/* Static address used */
|
||||
IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
|
||||
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
|
||||
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
|
||||
netif_set_addr(&gnetif, &ipaddr , &netmask, &gw);
|
||||
|
||||
#ifdef USE_LCD
|
||||
LCD_DisplayStringLine(Line7, (uint8_t*)" DHCP timeout ");
|
||||
|
||||
iptab[0] = IP_ADDR3;
|
||||
iptab[1] = IP_ADDR2;
|
||||
iptab[2] = IP_ADDR1;
|
||||
iptab[3] = IP_ADDR0;
|
||||
|
||||
sprintf((char*)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]);
|
||||
|
||||
LCD_ClearLine(Line4);
|
||||
LCD_ClearLine(Line5);
|
||||
LCD_ClearLine(Line6);
|
||||
|
||||
LCD_DisplayStringLine(Line8, (uint8_t*)" Static IP address ");
|
||||
LCD_DisplayStringLine(Line9, iptxt);
|
||||
#endif
|
||||
STM_EVAL_LEDOn(LED1);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
63
source/eth/netconf.h
Normal file
63
source/eth/netconf.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file netconf.h
|
||||
* @author MCD Application Team
|
||||
* @version V1.1.0
|
||||
* @date 31-July-2013
|
||||
* @brief This file contains all the functions prototypes for the netconf.c
|
||||
* file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
|
||||
*
|
||||
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
|
||||
* You may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.st.com/software_license_agreement_liberty_v2
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __NETCONF_H
|
||||
#define __NETCONF_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32f4xx.h"
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
#define DHCP_START 1
|
||||
#define DHCP_WAIT_ADDRESS 2
|
||||
#define DHCP_ADDRESS_ASSIGNED 3
|
||||
#define DHCP_TIMEOUT 4
|
||||
#define DHCP_LINK_DOWN 5
|
||||
//#define USE_DHCP
|
||||
#define RMII_MODE
|
||||
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void LwIP_Init(void);
|
||||
void LwIP_Pkt_Handle(void);
|
||||
void LwIP_Periodic_Handle(__IO uint32_t localtime);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __NETCONF_H */
|
||||
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
2873
source/eth/stm32f4x7_eth.c
Normal file
2873
source/eth/stm32f4x7_eth.c
Normal file
File diff suppressed because it is too large
Load Diff
1890
source/eth/stm32f4x7_eth.h
Normal file
1890
source/eth/stm32f4x7_eth.h
Normal file
File diff suppressed because it is too large
Load Diff
527
source/eth/stm32f4x7_eth_bsp.c
Normal file
527
source/eth/stm32f4x7_eth_bsp.c
Normal file
@@ -0,0 +1,527 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32f4x7_eth_bsp.c
|
||||
* @author MCD Application Team
|
||||
* @version V1.1.0
|
||||
* @date 31-July-2013
|
||||
* @brief STM32F4x7 Ethernet hardware configuration.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
|
||||
*
|
||||
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
|
||||
* You may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.st.com/software_license_agreement_liberty_v2
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "lwip/opt.h"
|
||||
#include "stm32f4x7_eth.h"
|
||||
#include "stm32f4x7_eth_bsp.h"
|
||||
#include "stm32f4xx.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "netconf.h"
|
||||
#include "lwip/dhcp.h"
|
||||
#include "dev_flash.h"
|
||||
|
||||
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
ETH_InitTypeDef ETH_InitStructure;
|
||||
__IO uint32_t EthStatus = 0;
|
||||
__IO uint8_t EthLinkStatus = 0;
|
||||
extern struct netif gnetif;
|
||||
#ifdef USE_DHCP
|
||||
extern __IO uint8_t DHCP_state;
|
||||
#endif /* LWIP_DHCP */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
static void ETH_GPIO_Config(void);
|
||||
static void ETH_MACDMA_Config(void);
|
||||
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void ETH_NVIC_Config(void)
|
||||
{
|
||||
NVIC_InitTypeDef NVIC_InitStructure;
|
||||
|
||||
/* Enable the Ethernet global Interrupt */
|
||||
NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 12 ;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief ETH_BSP_Config
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void ETH_BSP_Config(void)
|
||||
{
|
||||
RCC_ClocksTypeDef RCC_Clocks;
|
||||
|
||||
/***************************************************************************
|
||||
NOTE:
|
||||
When using Systick to manage the delay in Ethernet driver, the Systick
|
||||
must be configured before Ethernet initialization and, the interrupt
|
||||
priority should be the highest one.
|
||||
*****************************************************************************/
|
||||
|
||||
/* Configure Systick clock source as HCLK */
|
||||
// SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
|
||||
|
||||
// /* SystTick configuration: an interrupt every 10ms */
|
||||
// RCC_GetClocksFreq(&RCC_Clocks);
|
||||
// SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
|
||||
|
||||
// /* Set Systick interrupt priority to 0*/
|
||||
// NVIC_SetPriority (SysTick_IRQn, 0);
|
||||
|
||||
/* Configure the GPIO ports for ethernet pins */
|
||||
ETH_GPIO_Config();
|
||||
|
||||
ETH_NVIC_Config();
|
||||
|
||||
/* Configure the Ethernet MAC/DMA */
|
||||
ETH_MACDMA_Config();
|
||||
|
||||
/* Get Ethernet link status*/
|
||||
if(ETH_ReadPHYRegister(DP83848_PHY_ADDRESS, PHY_SR) & 1)
|
||||
{
|
||||
EthStatus |= ETH_LINK_FLAG;
|
||||
}
|
||||
|
||||
/* Configure the PHY to generate an interrupt on change of link status */
|
||||
// Eth_Link_PHYITConfig(DP83848_PHY_ADDRESS);
|
||||
|
||||
/* Configure the EXTI for Ethernet link status. */
|
||||
// Eth_Link_EXTIConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configures the Ethernet Interface
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void ETH_MACDMA_Config(void)
|
||||
{
|
||||
/* Enable ETHERNET clock */
|
||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx |
|
||||
RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
|
||||
|
||||
/* Reset ETHERNET on AHB Bus */
|
||||
ETH_DeInit();
|
||||
|
||||
/* Software reset */
|
||||
ETH_SoftwareReset();
|
||||
|
||||
/* Wait for software reset */
|
||||
while (ETH_GetSoftwareResetStatus() == SET);
|
||||
|
||||
/* ETHERNET Configuration --------------------------------------------------*/
|
||||
/* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
|
||||
ETH_StructInit(Ð_InitStructure);
|
||||
|
||||
/* Fill ETH_InitStructure parametrs */
|
||||
/*------------------------ MAC -----------------------------------*/
|
||||
ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
|
||||
// ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
|
||||
// ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
|
||||
// ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
|
||||
|
||||
ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
|
||||
ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
|
||||
ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
|
||||
ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
|
||||
ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
|
||||
ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
|
||||
ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
|
||||
ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
|
||||
#ifdef CHECKSUM_BY_HARDWARE
|
||||
ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
|
||||
#endif
|
||||
|
||||
/*------------------------ DMA -----------------------------------*/
|
||||
|
||||
/* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
|
||||
the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
|
||||
if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
|
||||
ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
|
||||
ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
|
||||
ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
|
||||
|
||||
ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
|
||||
ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
|
||||
ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
|
||||
ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
|
||||
ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
|
||||
ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
|
||||
ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
|
||||
ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1;
|
||||
|
||||
/* Configure Ethernet */
|
||||
EthStatus = ETH_Init(Ð_InitStructure, DP83848_PHY_ADDRESS);
|
||||
|
||||
|
||||
/* Enable the Ethernet Rx Interrupt */
|
||||
ETH_DMAITConfig(ETH_DMA_IT_NIS | ETH_DMA_IT_R, ENABLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configures the different GPIO ports.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void ETH_GPIO_Config(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
|
||||
/* Enable GPIOs clocks */
|
||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
|
||||
RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOI |
|
||||
RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOH |
|
||||
RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF, ENABLE);
|
||||
|
||||
/* Enable SYSCFG clock */
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
||||
|
||||
/*NET_NRST control, Configure PE3 */
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
|
||||
GPIO_Init(GPIOE, &GPIO_InitStructure);
|
||||
GPIO_ResetBits(GPIOE, GPIO_Pin_3);
|
||||
for(u16 i=0;i<1000;i++)
|
||||
;
|
||||
GPIO_SetBits(GPIOE, GPIO_Pin_3);
|
||||
|
||||
/* Configure MCO (PA8) */
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
/* MII/RMII Media interface selection --------------------------------------*/
|
||||
#ifdef MII_MODE /* Mode MII with STM324xx-EVAL */
|
||||
#ifdef PHY_CLOCK_MCO
|
||||
|
||||
|
||||
/* Output HSE clock (25MHz) on MCO pin (PA8) to clock the PHY */
|
||||
RCC_MCO1Config(RCC_MCO1Source_HSE, RCC_MCO1Div_1);
|
||||
#endif /* PHY_CLOCK_MCO */
|
||||
|
||||
SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_MII);
|
||||
#elif defined RMII_MODE /* Mode RMII with STM324xx-EVAL */
|
||||
|
||||
SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
|
||||
#endif
|
||||
|
||||
/* Ethernet pins configuration ************************************************/
|
||||
/*
|
||||
ETH_MDIO -------------------------> PA2
|
||||
ETH_MDC --------------------------> PC1
|
||||
ETH_PPS_OUT ----------------------> PB5
|
||||
ETH_MII_CRS ----------------------> PH2
|
||||
ETH_MII_COL ----------------------> PH3
|
||||
ETH_MII_RX_ER --------------------> PI10
|
||||
ETH_MII_RXD2 ---------------------> PH6
|
||||
ETH_MII_RXD3 ---------------------> PH7
|
||||
ETH_MII_TX_CLK -------------------> PC3
|
||||
ETH_MII_TXD2 ---------------------> PC2
|
||||
ETH_MII_TXD3 ---------------------> PB8
|
||||
ETH_MII_RX_CLK/ETH_RMII_REF_CLK---> PA1
|
||||
ETH_MII_RX_DV/ETH_RMII_CRS_DV ----> PA7
|
||||
ETH_MII_RXD0/ETH_RMII_RXD0 -------> PC4
|
||||
ETH_MII_RXD1/ETH_RMII_RXD1 -------> PC5
|
||||
ETH_MII_TX_EN/ETH_RMII_TX_EN -----> PG11
|
||||
ETH_MII_TXD0/ETH_RMII_TXD0 -------> PG13
|
||||
ETH_MII_TXD1/ETH_RMII_TXD1 -------> PG14
|
||||
*/
|
||||
|
||||
/* Configure PA1, PA2 and PA7 */
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH);
|
||||
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH);
|
||||
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH);
|
||||
|
||||
/* Configure PB11, PB12, PB13 */
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
|
||||
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_ETH);
|
||||
GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_ETH);
|
||||
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_ETH);
|
||||
|
||||
/* Configure PC1, PC4 and PC5 */
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5;
|
||||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||||
GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH);
|
||||
GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH);
|
||||
GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configure the PHY to generate an interrupt on change of link status.
|
||||
* @param PHYAddress: external PHY address
|
||||
* @retval None
|
||||
*/
|
||||
uint32_t Eth_Link_PHYITConfig(uint16_t PHYAddress)
|
||||
{
|
||||
uint16_t tmpreg = 0;
|
||||
|
||||
/* Read MICR register */
|
||||
tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_MICR);
|
||||
|
||||
/* Enable output interrupt events to signal via the INT pin */
|
||||
tmpreg |= (uint16_t)(PHY_MICR_INT_EN | PHY_MICR_INT_OE);
|
||||
if(!(ETH_WritePHYRegister(PHYAddress, PHY_MICR, tmpreg)))
|
||||
{
|
||||
/* Return ERROR in case of write timeout */
|
||||
return ETH_ERROR;
|
||||
}
|
||||
|
||||
/* Read MISR register */
|
||||
tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_MISR);
|
||||
|
||||
/* Enable Interrupt on change of link status */
|
||||
tmpreg |= (uint16_t)PHY_MISR_LINK_INT_EN;
|
||||
if(!(ETH_WritePHYRegister(PHYAddress, PHY_MISR, tmpreg)))
|
||||
{
|
||||
/* Return ERROR in case of write timeout */
|
||||
return ETH_ERROR;
|
||||
}
|
||||
/* Return SUCCESS */
|
||||
return ETH_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief EXTI configuration for Ethernet link status.
|
||||
* @param PHYAddress: external PHY address
|
||||
* @retval None
|
||||
*/
|
||||
void Eth_Link_EXTIConfig(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
EXTI_InitTypeDef EXTI_InitStructure;
|
||||
NVIC_InitTypeDef NVIC_InitStructure;
|
||||
|
||||
/* Enable the INT (PB14) Clock */
|
||||
RCC_AHB1PeriphClockCmd(ETH_LINK_GPIO_CLK, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
||||
|
||||
/* Configure INT pin as input */
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||
GPIO_InitStructure.GPIO_Pin = ETH_LINK_PIN;
|
||||
GPIO_Init(ETH_LINK_GPIO_PORT, &GPIO_InitStructure);
|
||||
|
||||
/* Connect EXTI Line to INT Pin */
|
||||
SYSCFG_EXTILineConfig(ETH_LINK_EXTI_PORT_SOURCE, ETH_LINK_EXTI_PIN_SOURCE);
|
||||
|
||||
/* Configure EXTI line */
|
||||
EXTI_InitStructure.EXTI_Line = ETH_LINK_EXTI_LINE;
|
||||
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
||||
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
|
||||
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
||||
EXTI_Init(&EXTI_InitStructure);
|
||||
|
||||
/* Enable and set the EXTI interrupt to priority 1*/
|
||||
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Ethernet link status.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void Eth_Link_ITHandler(uint16_t PHYAddress)
|
||||
{
|
||||
/* Check whether the link interrupt has occurred or not */
|
||||
if(((ETH_ReadPHYRegister(PHYAddress, PHY_MISR)) & PHY_LINK_STATUS) != 0)
|
||||
{
|
||||
if((ETH_ReadPHYRegister(PHYAddress, PHY_SR) & 1))
|
||||
{
|
||||
netif_set_link_up(&gnetif);
|
||||
}
|
||||
else
|
||||
{
|
||||
EthLinkStatus = 1;
|
||||
netif_set_link_down(&gnetif);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Link callback function, this function is called on change of link status.
|
||||
* @param The network interface
|
||||
* @retval None
|
||||
*/
|
||||
void ETH_link_callback(struct netif *netif)
|
||||
{
|
||||
__IO uint32_t timeout = 0;
|
||||
uint32_t tmpreg;
|
||||
uint16_t RegValue;
|
||||
struct ip_addr ipaddr;
|
||||
struct ip_addr netmask;
|
||||
struct ip_addr gw;
|
||||
#ifndef USE_DHCP
|
||||
uint8_t iptab[4] = {0};
|
||||
uint8_t iptxt[20];
|
||||
#endif /* USE_DHCP */
|
||||
|
||||
if(netif_is_link_up(netif))
|
||||
{
|
||||
/* Restart the auto-negotiation */
|
||||
if(ETH_InitStructure.ETH_AutoNegotiation != ETH_AutoNegotiation_Disable)
|
||||
{
|
||||
/* Reset Timeout counter */
|
||||
timeout = 0;
|
||||
|
||||
/* Enable auto-negotiation */
|
||||
ETH_WritePHYRegister(DP83848_PHY_ADDRESS, PHY_BCR, PHY_AutoNegotiation);
|
||||
|
||||
/* Wait until the auto-negotiation will be completed */
|
||||
do
|
||||
{
|
||||
timeout++;
|
||||
} while (!(ETH_ReadPHYRegister(DP83848_PHY_ADDRESS, PHY_BSR) & PHY_AutoNego_Complete) && (timeout < (uint32_t)PHY_READ_TO));
|
||||
|
||||
/* Reset Timeout counter */
|
||||
timeout = 0;
|
||||
|
||||
/* Read the result of the auto-negotiation */
|
||||
RegValue = ETH_ReadPHYRegister(DP83848_PHY_ADDRESS, PHY_SR);
|
||||
|
||||
/* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */
|
||||
if((RegValue & PHY_DUPLEX_STATUS) != (uint16_t)RESET)
|
||||
{
|
||||
/* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
|
||||
ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
|
||||
ETH_InitStructure.ETH_Mode = ETH_Mode_HalfDuplex;
|
||||
}
|
||||
/* Configure the MAC with the speed fixed by the auto-negotiation process */
|
||||
if(RegValue & PHY_SPEED_STATUS)
|
||||
{
|
||||
/* Set Ethernet speed to 10M following the auto-negotiation */
|
||||
ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Set Ethernet speed to 100M following the auto-negotiation */
|
||||
ETH_InitStructure.ETH_Speed = ETH_Speed_100M;
|
||||
}
|
||||
|
||||
/*------------------------ ETHERNET MACCR Re-Configuration --------------------*/
|
||||
/* Get the ETHERNET MACCR value */
|
||||
tmpreg = ETH->MACCR;
|
||||
|
||||
/* Set the FES bit according to ETH_Speed value */
|
||||
/* Set the DM bit according to ETH_Mode value */
|
||||
tmpreg |= (uint32_t)(ETH_InitStructure.ETH_Speed | ETH_InitStructure.ETH_Mode);
|
||||
|
||||
/* Write to ETHERNET MACCR */
|
||||
ETH->MACCR = (uint32_t)tmpreg;
|
||||
|
||||
_eth_delay_(ETH_REG_WRITE_DELAY);
|
||||
tmpreg = ETH->MACCR;
|
||||
ETH->MACCR = tmpreg;
|
||||
}
|
||||
|
||||
/* Restart MAC interface */
|
||||
ETH_Start();
|
||||
|
||||
#ifdef USE_DHCP
|
||||
ipaddr.addr = 0;
|
||||
netmask.addr = 0;
|
||||
gw.addr = 0;
|
||||
DHCP_state = DHCP_START;
|
||||
#else
|
||||
const uint8_t *ip=sys_param()->local_ip;
|
||||
IP4_ADDR(&ipaddr, ip[0], ip[1], ip[2], ip[3]);
|
||||
IP4_ADDR(&netmask, 255, 255 , 255, 0);
|
||||
IP4_ADDR(&gw, ip[0], ip[1], ip[2], 1);
|
||||
#endif /* USE_DHCP */
|
||||
|
||||
netif_set_addr(&gnetif, &ipaddr , &netmask, &gw);
|
||||
|
||||
/* When the netif is fully configured this function must be called.*/
|
||||
netif_set_up(&gnetif);
|
||||
|
||||
EthLinkStatus = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ETH_Stop();
|
||||
#ifdef USE_DHCP
|
||||
DHCP_state = DHCP_LINK_DOWN;
|
||||
dhcp_stop(netif);
|
||||
#endif /* USE_DHCP */
|
||||
|
||||
/* When the netif link is down this function must be called.*/
|
||||
netif_set_down(&gnetif);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
84
source/eth/stm32f4x7_eth_bsp.h
Normal file
84
source/eth/stm32f4x7_eth_bsp.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32f4x7_eth_bsp.h
|
||||
* @author MCD Application Team
|
||||
* @version V1.1.0
|
||||
* @date 31-July-2013
|
||||
* @brief Header for stm32f4x7_eth_bsp.c file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
|
||||
*
|
||||
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
|
||||
* You may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.st.com/software_license_agreement_liberty_v2
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __STM32F4x7_ETH_BSP_H
|
||||
#define __STM32F4x7_ETH_BSP_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#if defined (USE_STM324xG_EVAL)
|
||||
#include "stm324xg_eval.h"
|
||||
#include "stm324xg_eval_lcd.h"
|
||||
|
||||
#elif defined (USE_STM324x7I_EVAL)
|
||||
#include "stm324x7i_eval.h"
|
||||
// #include "stm324x7i_eval_lcd.h"
|
||||
|
||||
#else
|
||||
#define error "Please select first the Evaluation board used in your application (in Project Options)"
|
||||
#endif
|
||||
|
||||
#include "lwip/netif.h"
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
#define DP83848_PHY_ADDRESS ((uint16_t) 0x01) /* Relative to STM324xG-EVAL Board */
|
||||
|
||||
/* Specific defines for EXTI line, used to manage Ethernet link status */
|
||||
#define ETH_LINK_EXTI_LINE EXTI_Line14
|
||||
#define ETH_LINK_EXTI_PORT_SOURCE EXTI_PortSourceGPIOB
|
||||
#define ETH_LINK_EXTI_PIN_SOURCE EXTI_PinSource14
|
||||
#define ETH_LINK_EXTI_IRQn EXTI15_10_IRQn
|
||||
/* PB14 */
|
||||
#define ETH_LINK_PIN GPIO_Pin_14
|
||||
#define ETH_LINK_GPIO_PORT GPIOB
|
||||
#define ETH_LINK_GPIO_CLK RCC_AHB1Periph_GPIOB
|
||||
|
||||
/* Ethernet Flags for EthStatus variable */
|
||||
#define ETH_INIT_FLAG 0x01 /* Ethernet Init Flag */
|
||||
#define ETH_LINK_FLAG 0x10 /* Ethernet Link Flag */
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void ETH_BSP_Config(void);
|
||||
uint32_t Eth_Link_PHYITConfig(uint16_t PHYAddress);
|
||||
void Eth_Link_EXTIConfig(void);
|
||||
void Eth_Link_ITHandler(uint16_t PHYAddress);
|
||||
void ETH_link_callback(struct netif *netif);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __STM32F4x7_ETH_BSP_H */
|
||||
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
120
source/eth/stm32f4x7_eth_conf.h
Normal file
120
source/eth/stm32f4x7_eth_conf.h
Normal file
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32f4x7_eth_conf.h
|
||||
* @author MCD Application Team
|
||||
* @version V1.1.0
|
||||
* @date 31-July-2013
|
||||
* @brief Configuration file for the STM32F4x7 Ethernet driver.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
|
||||
*
|
||||
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
|
||||
* You may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.st.com/software_license_agreement_liberty_v2
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __STM32F4x7_ETH_CONF_H
|
||||
#define __STM32F4x7_ETH_CONF_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32f4xx.h"
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
|
||||
/* Uncomment the line below when using time stamping and/or IPv4 checksum offload */
|
||||
#define USE_ENHANCED_DMA_DESCRIPTORS
|
||||
|
||||
/* Uncomment the line below if you want to use user defined Delay function
|
||||
(for precise timing), otherwise default _eth_delay_ function defined within
|
||||
the Ethernet driver is used (less precise timing) */
|
||||
#define USE_Delay
|
||||
|
||||
#ifdef USE_Delay
|
||||
#include "rtthread.h" /* Header file where the Delay function prototype is exported */
|
||||
#define _eth_delay_ rt_thread_mdelay /* User can provide more timing precise _eth_delay_ function
|
||||
in this example Systick is configured with an interrupt every 10 ms*/
|
||||
#else
|
||||
#define _eth_delay_ ETH_Delay /* Default _eth_delay_ function with less precise timing */
|
||||
#endif
|
||||
|
||||
|
||||
/*This define allow to customize configuration of the Ethernet driver buffers */
|
||||
#define CUSTOM_DRIVER_BUFFERS_CONFIG
|
||||
|
||||
#ifdef CUSTOM_DRIVER_BUFFERS_CONFIG
|
||||
/* Redefinition of the Ethernet driver buffers size and count */
|
||||
#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */
|
||||
#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */
|
||||
#define ETH_RXBUFNB 4 /* 4 Rx buffers of size ETH_RX_BUF_SIZE */
|
||||
#define ETH_TXBUFNB 4 /* 4 Tx buffers of size ETH_TX_BUF_SIZE */
|
||||
#endif
|
||||
|
||||
|
||||
/* PHY configuration section **************************************************/
|
||||
#ifdef USE_Delay
|
||||
/* PHY Reset delay */
|
||||
#define PHY_RESET_DELAY ((uint32_t)0x000000FF)
|
||||
/* PHY Configuration delay */
|
||||
#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF)
|
||||
/* Delay when writing to Ethernet registers*/
|
||||
#define ETH_REG_WRITE_DELAY ((uint32_t)0x00000001)
|
||||
#else
|
||||
/* PHY Reset delay */
|
||||
#define PHY_RESET_DELAY ((uint32_t)0x000FFFFF)
|
||||
/* PHY Configuration delay */
|
||||
#define PHY_CONFIG_DELAY ((uint32_t)0x00FFFFFF)
|
||||
/* Delay when writing to Ethernet registers*/
|
||||
#define ETH_REG_WRITE_DELAY ((uint32_t)0x0000FFFF)
|
||||
#endif
|
||||
|
||||
/******************* PHY Extended Registers section : ************************/
|
||||
|
||||
/* These values are relatives to DP83848 PHY and change from PHY to another,
|
||||
so the user have to update this value depending on the used external PHY */
|
||||
|
||||
/* The DP83848 PHY status register */
|
||||
#define PHY_SR ((uint16_t)0x10) /* PHY status register Offset */
|
||||
#define PHY_SPEED_STATUS ((uint16_t)0x0002) /* PHY Speed mask */
|
||||
#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /* PHY Duplex mask */
|
||||
|
||||
/* The DP83848 PHY: MII Interrupt Control Register */
|
||||
#define PHY_MICR ((uint16_t)0x11) /* MII Interrupt Control Register */
|
||||
#define PHY_MICR_INT_EN ((uint16_t)0x0002) /* PHY Enable interrupts */
|
||||
#define PHY_MICR_INT_OE ((uint16_t)0x0001) /* PHY Enable output interrupt events */
|
||||
|
||||
/* The DP83848 PHY: MII Interrupt Status and Misc. Control Register */
|
||||
#define PHY_MISR ((uint16_t)0x12) /* MII Interrupt Status and Misc. Control Register */
|
||||
#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /* Enable Interrupt on change of link status */
|
||||
#define PHY_LINK_STATUS ((uint16_t)0x2000) /* PHY link status interrupt mask */
|
||||
|
||||
/* Note : Common PHY registers are defined in stm32f4x7_eth.h file */
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __STM32F4x7_ETH_CONF_H */
|
||||
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
Reference in New Issue
Block a user