将LAN8720移植到试验工程(F407 + SPL + LWIP)后,发现不能响应ICMP,后来单步第三方工程,发现要自己在任务中收包处理才行。
中间版本的试验工程,移植lwip时好乱(为了编译过, 头文件包含太乱了)。
花了2天,重新从(F4 + ucosii)的模板工程上重新弄了一次。
不带操作系统,能响应ping.
test04_f407_spl_ucosii_lwip_template_2020_0309_1821.zip
STM32F4x7_ETH_Driver来自ST官方的en.stsw-stm32070
将\STM32F4x7_ETH_LwIP_V1.1.1\Libraries\STM32F4x7_ETH_Driver拷贝到工程下的目录 \src\STM32F4x7_ETH_Driver
将stm32f4x7_eth_conf_template.h改名为stm32f4x7_eth_conf.h
stm32f4x7_eth_conf.h中有关PHY的地方改为LAN87220的参数。
/**
******************************************************************************
* @file stm32f4x7_eth_conf_template.h
* @author MCD Application Team
* @version V1.1.0
* @date 31-July-2013
* @brief Configuration file for the STM32F4x7xx Ethernet driver.
* This file should be copied to the application folder and renamed to
* stm32f4x7_eth_conf.h
******************************************************************************
* @attention
*
* © COPYRIGHT 2013 STMicroelectronics
*
* 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 "main.h" /* Header file where the Delay function prototype is exported */
void Delay(uint32_t nCount);
#define _eth_delay_ Delay /* User can provide more timing precise _eth_delay_ function
ex. use Systick with time base of 10 ms (as done in the provided
STM32F4x7xx demonstrations) */
#else
#define _eth_delay_ ETH_Delay /* Default _eth_delay_ function with less precise timing */
#endif
/* Uncomment the line below to allow custom 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 20 /* 20 Rx buffers of size ETH_RX_BUF_SIZE */
#define ETH_TXBUFNB 5 /* 5 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)0x1F) /* PHY status register Offset */
#define PHY_SPEED_STATUS ((uint16_t)0x0004) /* PHY Speed mask */
#define PHY_DUPLEX_STATUS ((uint16_t)0x0010) /* 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****/
将lwip-2.1.2\src*拷贝到工程目录 \src\lwip
删掉\src\lwip\apps
删掉\src\lwip\include\lwip\apps
在MDK工程的新建group, 将所有\src\lwip\的*.h/*.c都导入.
不编译 \src\lwip\netif\bridgeif_fdb.c
在MDK/c++选项中,加入include路径。
.\src\cm4;.\src\spl;.\src\user;.\src\bsp;.\src\ucos\src;.\src\ucos\port;.\src\ucos\app;.\src\lwip;.\src\lwip\include;.\src\lwip\include\lwip;.\src\lwip\arch;.\src\STM32F4x7_ETH_Driver\inc
在\src\lwip新建目录arch, 加入移植相关的文件.
移植相关文件来自en.stsw-stm32070, 因为LAN8720是RMII接口,和en.stsw-stm32070的MII接口的DP83848CVV很像, 改动量不大. DP83848CVV的移植相关文件可以全部拷贝过来再修改寄存器地址, PHY接口等参数.
将这些文件都放在新建的lwip/arch目录中,是因为这些都是和移植, PHY相关的内容, 只要移植LWIP就会动这些文件。方便维护。
\lwip\arch\lwipopts.h 的内容是lwip的配置宏, 用来覆盖lwip/opt.h中的配置宏.
这个配置文件, 尽量不要多配置, 需要啥才配置啥.
配置前,先看看opt.h, 如果里面有合理的配置宏. lwipopts.h中就不要多写宏.
可以依赖编译器(编译不过了)或要微调lwip性能(增大内存用量或tcp/ip连接数),才添加必要的宏来覆盖opt.h中的宏.
// @file \lwip\arch\lwipopts.h
// lwip/opt.h 包含 lwip/arch/lwipopts.h
#if !defined(__LWIP_ARCH_LWIPOPTS_H__)
#define __LWIP_ARCH_LWIPOPTS_H__
// lwip/opt.h 需要的东西补充在这里
// 这里主要是配置选项
// 1 = 无操作系统
// 0 = 有操作系统
#define NO_SYS 1
/**
* NO_SYS_NO_TIMERS==1: Drop support for sys_timeout when NO_SYS==1
* Mainly for compatibility to old versions.
*/
// (1 == NO_SYS) && (1 == NO_SYS_NO_TIMERS)时, 不需要时基(LWIP_TIMERS)支持
#define NO_SYS_NO_TIMERS 1
// 0 = 不需要 lwip的NETCONN API
// 1 = 需要 lwip的NETCONN API
#define LWIP_NETCONN 0
// 0 = 不需要 lwip的SOCKET API
// 1 = 需要 lwip的SOCKET API
#define LWIP_SOCKET 0
// 0 = 不需要任务间的保护
// 1 = 需要任务间的保护
#define SYS_LIGHTWEIGHT_PROT 0
#define LWIP_IPV4 1
#define LWIP_IPV6 0
#define LWIP_TCP 1
/**
* 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
#endif // #if !defined(__LWIP_ARCH_LWIPOPTS_H__)
\lwip\arch\cc.h 的内容是lwip需要的数据类型定义, 按照需要来添加宏来响应lwip中需要的数据类型
// @file \lwip\arch\cc.h
// lwip/arch.h 包含 lwip/arch/cc.h
// lwip/opt.h 包含 lwip/debug.h
// lwip/debug.h 包含 lwip/arch/cc.h
#if !defined(__LWIP_ARCH_CC_H__)
#define __LWIP_ARCH_CC_H__
// arch.h 需要的东西补充在这里
// 这里主要是数据类型定义
typedef int sys_prot_t;
#endif // #if !defined(__LWIP_ARCH_CC_H__)
\lwip\arch\sys_arch.h 的内容用来覆盖lwip/sys.h的宏, 按需添加
// @file \lwip\arch\sys_arch.h
// lwip/sys.h 包含 lwip/arch/sys_arch.h
#if !defined(__LWIP_ARCH_SYS_ARCH_H__)
#define __LWIP_ARCH_SYS_ARCH_H__
// 这里主要是和移植(平台)相关的内容
// sys.h 需要的内容 补充在这里
#endif // #if !defined(__LWIP_ARCH_SYS_ARCH_H__)
arch/sio.c 是用来响应SIO协议. 虽然用不到,但是也写一组空实现, 保证能编译通过.
这样移植起来方便, 不用特意去删掉什么不用的实现.
MDK编译完,在连接前,会将用不到的函数去掉,不会占用程序区的空间. 所以不用担心。
我看好多人移植,都会将不需要的文件都去掉,这何苦. 编译器将这活都干了。
// @file \src\lwip\arch\sio.c
// @brief
// This is the interface to the platform specific serial IO module
// It needs to be implemented by those platforms which need SLIP or PPP
#include "lwip/sio.h"
sio_fd_t sio_open(u8_t devnum)
{
return NULL;
}
void sio_send(u8_t c, sio_fd_t fd)
{
}
u8_t sio_recv(sio_fd_t fd)
{
return 0;
}
u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
{
return 0;
}
u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
{
return 0;
}
u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len)
{
return 0;
}
void sio_read_abort(sio_fd_t fd)
{
}
\lwip\arch\netconf.h是ST官方的实现,没动。
/**
******************************************************************************
* @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
*
* © COPYRIGHT 2013 STMicroelectronics
*
* 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 ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
#define DHCP_START 1
#define DHCP_WAIT_ADDRESS 2
#define DHCP_ADDRESS_ASSIGNED 3
#define DHCP_TIMEOUT 4
#define DHCP_LINK_DOWN 5
/* 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****/
lwip\arch\netconf.c 是官方实现.
要改动2处:
// for TCP_TMR_INTERVAL declare
// #include "lwip/tcp_impl.h"
#include "lwip/priv/tcp_priv.h"
use ip4_addr, can’t use ip_addr, 否则编译不过, e.g.
void LwIP_Init(void)
{
struct ip4_addr ipaddr; // is ip4_addr not ip_addr
struct ip4_addr netmask;
struct ip4_addr gw;
#ifndef USE_DHCP
// uint8_t iptab[4] = {0};
// uint8_t iptxt[20];
#endif
/**
******************************************************************************
* @file netconf.c
* @author MCD Application Team
* @version V1.1.0
* @date 31-July-2013
* @brief Network connection configuration
******************************************************************************
* @attention
*
* © COPYRIGHT 2013 STMicroelectronics
*
* 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/mem.h"
#include "lwip/memp.h"
#include "lwip/tcp.h"
#include "lwip/opt.h"
// for TCP_TMR_INTERVAL declare
// #include "lwip/tcp_impl.h"
#include "lwip/priv/tcp_priv.h"
// use ip4_addr, can't use ip_addr
#include "lwip/udp.h"
#include "netif/etharp.h"
#include "lwip/dhcp.h"
#include "ethernetif.h"
#include "main.h"
#include "netconf.h"
#include
/* 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 ip4_addr ipaddr;
struct ip4_addr netmask;
struct ip4_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();
#ifdef USE_DHCP
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
#else
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);
#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;
#else
#ifdef USE_LCD
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_DisplayStringLine(Line8, (uint8_t*)" Static IP address ");
LCD_DisplayStringLine(Line9, iptxt);
#endif
#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 LCD Text Color */
// LCD_SetTextColor(Red);
/* Display message on the LCD */
// LCD_DisplayStringLine(Line5, (uint8_t*)" Network Cable is ");
// LCD_DisplayStringLine(Line6, (uint8_t*)" not connected ");
/* Set the LCD Text Color */
// LCD_SetTextColor(White);
}
/* 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****/
lwip/arch/stm32f4x7_eth_bsp.h 官方文件,将PHY地址换成自己硬件连接对应的值.
/**
******************************************************************************
* @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
*
* © COPYRIGHT 2013 STMicroelectronics
*
* 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
// #error "Please select first the Evaluation board used in your application (in Project Options)"
// #endif
#include "netif.h"
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
#define LAN8720_PHY_ADDRESS ((uint16_t) 0x00)
/* 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****/
lwip/arch/stm32f4x7_eth_bsp.c 官方实现
ETH_BSP_Config()中判断PHY上线状态的代码要改.
/* Configure the Ethernet MAC/DMA */
ETH_MACDMA_Config();
/* Read PHY status register: Get Ethernet link status */
rc1 = ETH_ReadPHYRegister(LAN8720_PHY_ADDRESS, PHY_SR); // 原版的实现
rc2 = ETH_ReadPHYRegister(LAN8720_PHY_ADDRESS, PHY_BSR); // 第三方的实现
// 原版的判断不好使,第三方的判断好使, 结合在一起用。有时间再去看LAN8720的数据表
if ((rc1 & 0x01) || ((rc2 & 0x04) > 0))
{
EthStatus |= ETH_LINK_FLAG; // 在硬件连接没问题的情况下, 必须到这里才正确.
}
ETH_GPIO_Config()要改成RMII方式的PHY, 要定义RMII_MODE, 不定义MII_MODE
要初始化RMII接口的GPIO, 不是MII接口的GPIO.
去掉不用的函数 Eth_Link_PHYITConfig(), Eth_Link_EXTIConfig()
驱动Eth_Link_ITHandler()中的MII寄存器读写
去掉ETH_link_callback()中的LCD代码, 将PHY地址和寄存器都改成RMII接口的。
增加 void Delay(uint32_t nCount) 函数,用来响应lwip的PHY操作延时. 这个函数只能叫Delay, 这是在lwip中规定的。为了不动lwip的实现,只能在自己的实现中,实现Delay()。
/**
******************************************************************************
* @file stm32f4x7_eth_bsp.c
* @author MCD Application Team
* @version V1.1.0
* @date 31-July-2013
* @brief STM32F4x7 Ethernet hardware configuration.
******************************************************************************
* @attention
*
* © COPYRIGHT 2013 STMicroelectronics
*
* 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 "main.h"
#include "netif.h"
#include "netconf.h"
#include "lwip/dhcp.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ETH_InitTypeDef ETH_InitStructure;
__IO uint32_t EthStatus = 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 ---------------------------------------------------------*/
/**
* @brief ETH_BSP_Config
* @param None
* @retval None
*/
void ETH_BSP_Config(void)
{
uint16_t rc1 = 0;
uint16_t rc2 = 0;
/* Configure the GPIO ports for ethernet pins */
ETH_GPIO_Config();
/* Configure the Ethernet MAC/DMA */
ETH_MACDMA_Config();
/* Read PHY status register: Get Ethernet link status */
rc1 = ETH_ReadPHYRegister(LAN8720_PHY_ADDRESS, PHY_SR); // 原版的实现
rc2 = ETH_ReadPHYRegister(LAN8720_PHY_ADDRESS, PHY_BSR); // 第三方的实现
// 原版的判断不好使,第三方的判断好使, 结合在一起用。有时间再去看LAN8720的数据表
if ((rc1 & 0x01) || ((rc2 & 0x04) > 0))
{
EthStatus |= ETH_LINK_FLAG; // 在硬件连接没问题的情况下, 必须到这里才正确.
}
// /* Configure the PHY to generate an interrupt on change of link status */
// Eth_Link_PHYITConfig(LAN8720_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, LAN8720_PHY_ADDRESS);
}
/**
* @brief Configures the different GPIO ports.
* @param None
* @retval None
*/
void ETH_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOs clocks */
// RMII所在的管脚时钟为PA, PC, PG
// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
// RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOI |
// RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOH |
// RCC_AHB1Periph_GPIOF, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOG, ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
// 不需要时钟输出
/* 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 */
// 必须执行RMII接口的配置才行, 所以必须要定义RMII_MODE宏,注释掉MII_MODE宏
SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
#endif
// STM324xx-EVAL + MII + DP83848CVV
// 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 PB5 and PB8 */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_8;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
// GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_ETH);
// GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_ETH);
// /* Configure PC1, PC2, PC3, PC4 and PC5 */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
// GPIO_Init(GPIOC, &GPIO_InitStructure);
// GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH);
// GPIO_PinAFConfig(GPIOC, GPIO_PinSource2, GPIO_AF_ETH);
// GPIO_PinAFConfig(GPIOC, GPIO_PinSource3, GPIO_AF_ETH);
// GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH);
// GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH);
//
// /* Configure PG11, PG14 and PG13 */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14;
// GPIO_Init(GPIOG, &GPIO_InitStructure);
// GPIO_PinAFConfig(GPIOG, GPIO_PinSource11, GPIO_AF_ETH);
// GPIO_PinAFConfig(GPIOG, GPIO_PinSource13, GPIO_AF_ETH);
// GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_ETH);
// /* Configure PH2, PH3, PH6, PH7 */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_6 | GPIO_Pin_7;
// GPIO_Init(GPIOH, &GPIO_InitStructure);
// GPIO_PinAFConfig(GPIOH, GPIO_PinSource2, GPIO_AF_ETH);
// GPIO_PinAFConfig(GPIOH, GPIO_PinSource3, GPIO_AF_ETH);
// GPIO_PinAFConfig(GPIOH, GPIO_PinSource6, GPIO_AF_ETH);
// GPIO_PinAFConfig(GPIOH, GPIO_PinSource7, GPIO_AF_ETH);
// /* Configure PI10 */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
// GPIO_Init(GPIOI, &GPIO_InitStructure);
// GPIO_PinAFConfig(GPIOI, GPIO_PinSource10, GPIO_AF_ETH);
// STM32F407ZG + RMII + LAN8720
//
// ETH_MDIO -------------------------> PA2
// ETH_MDC --------------------------> PC1
//
// 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
// 可以看出RMII接口比MII接口少了很多硬件连线
// 只能初始化必要的RMII接口的GPIO, 如果还初始化剩下的MII接口的GPIO, 程序就不对了。
// 管脚初始化
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
// ETH_MDIO -------------------------> PA2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH);
// ETH_MDC --------------------------> PC1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH);
// ETH_MII_RX_CLK/ETH_RMII_REF_CLK---> PA1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH);
// ETH_MII_RX_DV/ETH_RMII_CRS_DV ----> PA7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH);
// ETH_MII_RXD0/ETH_RMII_RXD0 -------> PC4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH);
// ETH_MII_RXD1/ETH_RMII_RXD1 -------> PC5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH);
// ETH_MII_TX_EN/ETH_RMII_TX_EN -----> PG11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource11, GPIO_AF_ETH);
// ETH_MII_TXD0/ETH_RMII_TXD0 -------> PG13
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource13, GPIO_AF_ETH);
// ETH_MII_TXD1/ETH_RMII_TXD1 -------> PG14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, 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 (1)
{
if((ETH_ReadPHYRegister(PHYAddress, PHY_SR) & 1))
{
netif_set_link_up(&gnetif);
}
else
{
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 ip4_addr ipaddr;
struct ip4_addr netmask;
struct ip4_addr gw;
#ifndef USE_DHCP
// uint8_t iptab[4] = {0};
// uint8_t iptxt[20];
#endif /* USE_DHCP */
/* Clear LCD */
// LCD_ClearLine(Line4);
// LCD_ClearLine(Line5);
// LCD_ClearLine(Line6);
// LCD_ClearLine(Line7);
// LCD_ClearLine(Line8);
// LCD_ClearLine(Line9);
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(LAN8720_PHY_ADDRESS, PHY_BCR, PHY_AutoNegotiation);
/* Wait until the auto-negotiation will be completed */
do
{
timeout++;
} while (!(ETH_ReadPHYRegister(LAN8720_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(LAN8720_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
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);
#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);
#ifdef USE_LCD
/* Set the LCD Text Color */
LCD_SetTextColor(Green);
/* Display message on the LCD */
LCD_DisplayStringLine(Line5, (uint8_t*)" Network Cable is ");
LCD_DisplayStringLine(Line6, (uint8_t*)" now connected ");
/* Set the LCD Text Color */
LCD_SetTextColor(White);
#ifndef USE_DHCP
/* Display static IP address */
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_DisplayStringLine(Line8, (uint8_t*)" Static IP address ");
LCD_DisplayStringLine(Line9, iptxt);
/* Clear LCD */
LCD_ClearLine(Line5);
LCD_ClearLine(Line6);
#endif /* USE_DHCP */
#endif /* USE_LCD */
}
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);
#ifdef USE_LCD
/* Set the LCD Text Color */
LCD_SetTextColor(Red);
/* Display message on the LCD */
LCD_DisplayStringLine(Line5, (uint8_t*)" Network Cable is ");
LCD_DisplayStringLine(Line6, (uint8_t*)" unplugged ");
/* Set the LCD Text Color */
LCD_SetTextColor(White);
#endif /* USE_LCD */
}
}
/**
* @brief Inserts a delay time.
* @param nCount: number of 10ms periods to wait for.
* @retval None
*/
void Delay(uint32_t nCount)
{
INT32U timingdelay = 0;
/* Capture the current local time */
// OSTimeGet() return ms value
timingdelay = OSTimeGet() + nCount * 10; // nCount 's unit is 10ms
/* wait until the desired delay finish */
while(timingdelay > OSTimeGet()) {
}
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\lwip\arch\ethernetif.h 官方实现, 没动.
#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
\lwip\arch\ethernetif.c 官方实现,没动
/**
* @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
*
*/
#include "lwip/opt.h"
#include "lwip/mem.h"
#include "netif/etharp.h"
#include "ethernetif.h"
#include "stm32f4x7_eth.h"
#include "main.h"
#include
/* 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] = MAC_ADDR0;
netif->hwaddr[1] = MAC_ADDR1;
netif->hwaddr[2] = MAC_ADDR2;
netif->hwaddr[3] = MAC_ADDR3;
netif->hwaddr[4] = MAC_ADDR4;
netif->hwaddr[5] = MAC_ADDR5;
/* 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();
}
/**
* 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;
}
将需要的公共头文件封在一个.h中
初始化tick为1ms, 这个功能,要放在程序入口处或由SystemInit()来完成。
不要在程序中再次调用(e.g. ETH_BSP_Config()中再调用初始化tick), 会引起维护者的混乱(如果他改的地方是在ETH_BSP_Config()之前,他就懵了, 因为他代码没写错,tick还是10ms一次).
lwip带操作系统(NO_SYS)的意思是是加并行保护,而不是"是否在rtos中运行"。在rtos中,也可以用不带操作系统的方式(NO_SYS = 1)运行lwip
在ucosii中启动一个任务, 初始化PHY, 初始化LWIP, 在一个死循环中收包,处理包。
如果没有自己收包,处理包,是没有网络响应的(e.g. ping不通).
/**
******************************************************************************
* @file Project/STM32F4xx_StdPeriph_Templates/main.c
* @author MCD Application Team
* @version V1.8.0
* @date 04-November-2016
* @brief Main program body
******************************************************************************
* @attention
*
* © COPYRIGHT 2016 STMicroelectronics
*
* 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 "main.h"
#include "arch/netconf.h"
// add ITM info
// @ref http://www.keil.com/support/man/docs/jlink/jlink_trace_itm_viewer.htm
#define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))
#define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n)))
#define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000+4*n)))
#define DEMCR (*((volatile unsigned long *)(0xE000EDFC)))
#define TRCENA 0x01000000
int fputc(int ch, FILE *f) {
if (DEMCR & TRCENA) {
while (ITM_Port32(0) == 0);
ITM_Port8(0) = ch;
}
return(ch);
}
#define TASK1_TASK_PRIO 10
#define TASK1_STK_SIZE 128
OS_STK TASK1_STK[TASK1_STK_SIZE];
void task1_proc(void* arg);
// /** @addtogroup Template_Project
// * @{
// */
// /* Private typedef -----------------------------------------------------------*/
// /* Private define ------------------------------------------------------------*/
// /* Private macro -------------------------------------------------------------*/
// /* Private variables ---------------------------------------------------------*/
// static __IO uint32_t uwTimingDelay;
// RCC_ClocksTypeDef RCC_Clocks;
/* Private function prototypes -----------------------------------------------*/
// static void Delay(__IO uint32_t nTime);
/* Private functions ---------------------------------------------------------*/
// int main_bk(void)
// {
// GPIO_InitTypeDef GPIO_InitStructure;
//
// /*!< At this stage the microcontroller clock setting is already configured,
// this is done through SystemInit() function which is called from startup
// files before to branch to application main.
// To reconfigure the default setting of SystemInit() function,
// refer to system_stm32f4xx.c file */
// /* SysTick end of count event each 10ms */
// RCC_GetClocksFreq(&RCC_Clocks);
// SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
//
// /* Add your application code here */
// /* Insert 50 ms delay */
// Delay(5);
//
// /* Output HSE clock on MCO1 pin(PA8) ****************************************/
// /* Enable the GPIOA peripheral */
// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//
// /* Configure MCO1 pin(PA8) in alternate function */
// 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_UP;
// GPIO_Init(GPIOA, &GPIO_InitStructure);
//
// /* HSE clock selected to output on MCO1 pin(PA8)*/
// RCC_MCO1Config(RCC_MCO1Source_HSE, RCC_MCO1Div_1);
//
//
// /* Output SYSCLK/4 clock on MCO2 pin(PC9) ***********************************/
// /* Enable the GPIOACperipheral */
// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
//
// /* Configure MCO2 pin(PC9) in alternate function */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
// 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_UP;
// GPIO_Init(GPIOC, &GPIO_InitStructure);
//
// /* SYSCLK/4 clock selected to output on MCO2 pin(PC9)*/
// RCC_MCO2Config(RCC_MCO2Source_SYSCLK, RCC_MCO2Div_4);
//
//
// /* Infinite loop */
// while (1)
// {
// }
// return EXIT_SUCCESS;
// }
/**
* @brief Main program
* @param None
* @retval None
*/
void my_systick_setting()
{
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_Div8 /*SysTick_CLKSource_HCLK*/);
/* SystTick configuration: an interrupt every 10ms */
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000 /*100*/); // 1ms一次tick
/* Set Systick interrupt priority to 0*/
NVIC_SetPriority (SysTick_IRQn, 0); // tick中断优先级最高
}
void my_init()
{
my_systick_setting();
printf("<< my_init()\n");
}
void app_run()
{
OS_CPU_SR cpu_sr = 0; // for OS_ENTER_CRITICAL()/OS_EXIT_CRITICAL()
INT8U rc = 0;
OSInit();
OS_ENTER_CRITICAL(); // os_cpu.h
rc = OSTaskCreate(task1_proc, (void*)0, (OS_STK*)&TASK1_STK[TASK1_STK_SIZE-1], TASK1_TASK_PRIO);
if (OS_ERR_NONE != rc) {
printf("err : OSTaskCreate(task1_proc), rc = %d\n", rc);
}
OS_EXIT_CRITICAL();
OSStart();
}
int main(void)
{
my_init();
app_run();
return EXIT_SUCCESS;
}
void task1_proc(void* arg)
{
int i_heart_beat = 0;
INT32U ui_tm_begin = 0;
INT32U ui_tm_now = 0;
printf(">> task1_proc\n");
ETH_BSP_Config();
LwIP_Init();
printf("ETH packet recive loop..., i_heart_beat = %d\n", i_heart_beat);
ui_tm_begin = OSTimeGet(); // ms
do {
ui_tm_now = OSTimeGet();
// 打印心跳信息(10秒一次)
if ((ui_tm_now - ui_tm_begin) >= (1000 * 10)) {
ui_tm_begin = OSTimeGet();
printf("ETH packet recive loop..., i_heart_beat = %d\n", ++i_heart_beat);
}
// 收包操作要自己发起, 否则没有通讯(e.g. ping)响应
if (ETH_CheckFrameReceived()) { // 收包不是阻塞的
/* process received ethernet packet */
LwIP_Pkt_Handle(); // 如果有包, 处理
}
// OSTimeDlyHMSM(0, 0, 1, 0);
} while (1);
}
/**
* @brief Inserts a delay time.
* @param nTime: specifies the delay time length, in milliseconds.
* @retval None
*/
// void Delay(__IO uint32_t nTime)
// {
// uwTimingDelay = nTime;
// do {
// } while (uwTimingDelay > 0);
// }
/**
* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
// void TimingDelay_Decrement(void)
// {
// if (uwTimingDelay > 0x00) {
// uwTimingDelay--;
// }
// }
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
// @file \src\user\my_common.h
#if !defined(__MY_COMMON_H__)
#define __MY_COMMON_H__
#include
#include
#include
#include
#include "stm32f4xx.h"
#include "ucos_ii.h"
#include "stm32f4x7_eth.h"
#include "stm32f4x7_eth_bsp.h"
/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */
#define MAC_ADDR0 2
#define MAC_ADDR1 0
#define MAC_ADDR2 0
#define MAC_ADDR3 0
#define MAC_ADDR4 0
#define MAC_ADDR5 0
/*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
#define IP_ADDR0 192
#define IP_ADDR1 168
#define IP_ADDR2 1
#define IP_ADDR3 10
/*NETMASK*/
#define NETMASK_ADDR0 255
#define NETMASK_ADDR1 255
#define NETMASK_ADDR2 255
#define NETMASK_ADDR3 0
/*Gateway Address*/
#define GW_ADDR0 192
#define GW_ADDR1 168
#define GW_ADDR2 1
#define GW_ADDR3 1
#define RMII_MODE
#endif // #if !defined(__MY_COMMON_H__)
等下一步,就将NO_SYS改成0, 用带操作系统的方式运行lwip.
MDK工程中,如果打开的文件多了,以前搞不清当前文件是哪个,TAB页的颜色不像VS那样有高亮,不得不挨个点TAB页,来强迫自己找到在TAB页中已经打开的文件.