263 lines
7.8 KiB
C
263 lines
7.8 KiB
C
|
#include "includes.h"
|
|||
|
#include "Lan8720.h"
|
|||
|
#include "gd32f4xx.h"
|
|||
|
//#include "gd32f4xx_gpio.h"
|
|||
|
#include "pdebug.h"
|
|||
|
#include "bsp_os.h"
|
|||
|
//#include "gd32f4xx_enet.h"
|
|||
|
#include "gd32f4xx_it.h"
|
|||
|
#include "lwip/tcpip.h"
|
|||
|
#include "lwip/raw.h"
|
|||
|
#include "lwip/icmp.h"
|
|||
|
#include "lwip/dhcp.h"
|
|||
|
#include "lwip/netif.h"
|
|||
|
#include "lwip/sys.h"
|
|||
|
#include "lwip/timers.h"
|
|||
|
#include "lwip/inet_chksum.h"
|
|||
|
#include "lwip/init.h"
|
|||
|
#include "netif/etharp.h"
|
|||
|
|
|||
|
unsigned char g_local_ip[4] = {192,168,8,200}; //<2F><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>ip
|
|||
|
unsigned char g_local_mac[] = {0x42, 0x00, 0x00, 0x00, 0x00, 0x01}; //Ĭ<><C4AC>mac<61><63>ַ42:00:00:00:00:01
|
|||
|
unsigned char g_netmask[4] = {255,255,255,0}; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
unsigned char g_gateway[4] = {172,16,19,254}; //<2F><><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
//unsigned char gCfgLoca_MAC[] = {0x02, 0x0A, 0x0D, 0x0E, 0x0F, 0x06};
|
|||
|
unsigned char gCfgLoca_MAC[] = {0x42, 0x00, 0x00, 0x00, 0x00, 0x01};
|
|||
|
|
|||
|
uint8_t gCfgLoca_MAC[];
|
|||
|
|
|||
|
__IO uint32_t enet_init_status = 0;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̫<EFBFBD><CCAB><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>״̬<D7B4>ж<EFBFBD>
|
|||
|
char Enet_init_status;
|
|||
|
struct netif fsl_netif0;
|
|||
|
extern err_t ethernetif_init(struct netif *netif);
|
|||
|
|
|||
|
extern err_t ethernetif_input(struct netif *netif);
|
|||
|
|
|||
|
|
|||
|
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
static void Lan8720_gpio_Init(void)
|
|||
|
{
|
|||
|
//IO<49><4F><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ʹ<EFBFBD><CAB9>
|
|||
|
rcu_periph_clock_enable(RCU_GPIOA);
|
|||
|
rcu_periph_clock_enable(RCU_GPIOB);
|
|||
|
rcu_periph_clock_enable(RCU_GPIOC);
|
|||
|
rcu_periph_clock_enable(RCU_GPIOD);
|
|||
|
rcu_periph_clock_enable(RCU_GPIOG);
|
|||
|
rcu_periph_clock_enable(RCU_GPIOH);
|
|||
|
rcu_periph_clock_enable(RCU_GPIOI);
|
|||
|
//ʹ<><CAB9>ϵͳʱ<CDB3><CAB1>
|
|||
|
rcu_periph_clock_enable(RCU_SYSCFG);
|
|||
|
|
|||
|
//ѡ<><D1A1>RMIIģʽ
|
|||
|
syscfg_enet_phy_interface_config(SYSCFG_ENET_PHY_RMII);
|
|||
|
|
|||
|
//PA1<41><31>REF_CLK ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD><C5A3>ⲿPHY<48>ṩ50M
|
|||
|
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
|
|||
|
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
|
|||
|
|
|||
|
//PA2: ETH_MDIO
|
|||
|
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_2);
|
|||
|
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_2);
|
|||
|
|
|||
|
//PA7: ETH_RMII_CRS_DV
|
|||
|
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7);
|
|||
|
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
|
|||
|
//<2F><><EFBFBD>Ÿ<EFBFBD><C5B8><EFBFBD>
|
|||
|
gpio_af_set(GPIOA, GPIO_AF_11, GPIO_PIN_1);
|
|||
|
gpio_af_set(GPIOA, GPIO_AF_11, GPIO_PIN_2);
|
|||
|
gpio_af_set(GPIOA, GPIO_AF_11, GPIO_PIN_7);
|
|||
|
|
|||
|
//PB11: ETH_RMII_TX_EN
|
|||
|
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
|
|||
|
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
|
|||
|
|
|||
|
//PB12: ETH_RMII_TXD0
|
|||
|
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_12);
|
|||
|
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_12);
|
|||
|
|
|||
|
//PB13: ETH_RMII_TXD1
|
|||
|
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13);
|
|||
|
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_13);
|
|||
|
|
|||
|
gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_11);
|
|||
|
gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_12);
|
|||
|
gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_13);
|
|||
|
|
|||
|
//PC1: ETH_MDC
|
|||
|
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
|
|||
|
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
|
|||
|
|
|||
|
//PC4: ETH_RMII_RXD0
|
|||
|
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_4);
|
|||
|
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_4);
|
|||
|
|
|||
|
//PC5: ETH_RMII_RXD1
|
|||
|
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_5);
|
|||
|
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_5);
|
|||
|
|
|||
|
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_1);
|
|||
|
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_4);
|
|||
|
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_5);
|
|||
|
}
|
|||
|
|
|||
|
//<2F><>ʼ<EFBFBD><CABC>MAC/DMA
|
|||
|
static void Enet_MAC_DMA_config(void)
|
|||
|
{
|
|||
|
ErrStatus reval_state = ERROR;
|
|||
|
|
|||
|
rcu_periph_clock_enable(RCU_ENET);//ʹ<><CAB9><EFBFBD><EFBFBD>̫<EFBFBD><CCAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
rcu_periph_clock_enable(RCU_ENETTX);//ʹ<>ܷ<EFBFBD><DCB7><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
rcu_periph_clock_enable(RCU_ENETRX);//ʹ<>ܽ<EFBFBD><DCBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
|||
|
enet_deinit();//<2F><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>AHB
|
|||
|
|
|||
|
reval_state = enet_software_reset();//<2F>ȴ<EFBFBD>ʱ<EFBFBD>Ӹ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
|
|||
|
if(ERROR == reval_state){//<2F><>λʧ<CEBB><CAA7>
|
|||
|
while(1){
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//<2F><>̫<EFBFBD><CCAB><EFBFBD>豸<EFBFBD><E8B1B8>ʼ<EFBFBD><CABC>
|
|||
|
#ifdef CHECKSUM_BY_HARDWARE//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
enet_init_status = enet_init(ENET_AUTO_NEGOTIATION, ENET_AUTOCHECKSUM_DROP_FAILFRAMES, ENET_BROADCAST_FRAMES_PASS);
|
|||
|
#else //PHY<48>Զ<EFBFBD>Э<EFBFBD>̡<EFBFBD><CCA1><EFBFBD><EFBFBD><EFBFBD>IP֡У<D6A1>鹦<EFBFBD>ܡ<EFBFBD><DCA1><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD>յ<EFBFBD><D5B5>Ĺ㲥֡ ENET_100M_FULLDUPLEX/ENET_AUTO_NEGOTIATION
|
|||
|
enet_init_status = enet_init(ENET_AUTO_NEGOTIATION, ENET_NO_AUTOCHECKSUM, ENET_BROADCAST_FRAMES_PASS);
|
|||
|
#endif /* CHECKSUM_BY_HARDWARE */
|
|||
|
/*ע<><D7A2><EFBFBD><EFBFBD>ǰʹ<C7B0>õ<EFBFBD>LAN8720PHYоƬֻ<C6AC><D6BB>ʹ<EFBFBD><CAB9><EFBFBD>Զ<EFBFBD>Э<EFBFBD><D0AD>ģʽ<C4A3><CABD><EFBFBD>ֶ<EFBFBD>ģʽ<C4A3><EFBFBD>ͨ<EFBFBD>ţ<EFBFBD>LAN8720<32><30><EFBFBD><EFBFBD>bug<75><67>*/
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//<2F>ж<EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
static void Nvic_configuration(void)
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>Ƕ<EFBFBD><EFBFBD><D7BB><EFBFBD>ַ,,,ʹ<><CAB9>Flash base address<73><73><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>λӦ<CEBB>ó<EFBFBD><C3B3><EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bootʹ<74>ú<EFBFBD>ʹ<EFBFBD>ܣ<EFBFBD>
|
|||
|
// nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x0);
|
|||
|
//ʹ<><CAB9><EFBFBD><EFBFBD>̫<EFBFBD><CCAB><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
nvic_irq_enable(ENET_IRQn, 2, 3);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void Lan8720_Init(void)
|
|||
|
{
|
|||
|
|
|||
|
// <20>ж<EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Nvic_configuration();
|
|||
|
|
|||
|
// RMIIģʽ<C4A3><CABD>ʱ<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Lan8720_gpio_Init();
|
|||
|
|
|||
|
//MAC/DMA<4D><41><EFBFBD><EFBFBD>
|
|||
|
Enet_MAC_DMA_config();
|
|||
|
|
|||
|
if(0 == enet_init_status){//<2F><>ʼ<EFBFBD><CABC>DMAʧ<41><CAA7>
|
|||
|
Enet_init_status = 0;
|
|||
|
term_printf("\n Lan8720 Init fail 0 \r\n\r\n");
|
|||
|
}else{
|
|||
|
term_printf("\n Lan8720 Init success 1 \r\n\r\n");
|
|||
|
Enet_init_status = 1;
|
|||
|
}
|
|||
|
//<2F><>̫<EFBFBD><CCAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
enet_interrupt_enable(ENET_DMA_INT_NIE);
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ʹ<EFBFBD><CAB9>
|
|||
|
enet_interrupt_enable(ENET_DMA_INT_RIE);
|
|||
|
|
|||
|
BSP_IntVectSet(77,ENET_IRQHandler);
|
|||
|
BSP_IntEn(77);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
unsigned int Lwip_init(void)
|
|||
|
{
|
|||
|
ip_addr_t fsl_netif0_ipaddr, fsl_netif0_netmask, fsl_netif0_gw;
|
|||
|
pdebug(DEBUG_LEVEL_INFO,"tcp ip init begin!\r\n");
|
|||
|
tcpip_init(NULL, NULL);
|
|||
|
OSTimeDlyHMSM(0, 0, 0, 100);
|
|||
|
pdebug(DEBUG_LEVEL_INFO,"tcp ip init ok!\r\n");
|
|||
|
|
|||
|
#if LWIP_DHCP
|
|||
|
fsl_netif0_ipaddr.addr=0;
|
|||
|
fsl_netif0_netmask.addr=0;
|
|||
|
fsl_netif0_gw.addr=0;
|
|||
|
#else
|
|||
|
IP4_ADDR(&fsl_netif0_ipaddr, g_local_ip[0],g_local_ip[1],g_local_ip[2],g_local_ip[3]);
|
|||
|
IP4_ADDR(&fsl_netif0_netmask, g_netmask[0],g_netmask[1],g_netmask[2],g_netmask[3]);
|
|||
|
IP4_ADDR(&fsl_netif0_gw, g_gateway[0],g_gateway[1],g_gateway[2],g_gateway[3]);
|
|||
|
|
|||
|
u8_t *ip = (u8_t*)&fsl_netif0_ipaddr.addr;
|
|||
|
pdebug(DEBUG_LEVEL_INFO,"set net ip: %u.%u.%u.%u \r\n",ip[0], ip[1],ip[2], ip[3]);
|
|||
|
|
|||
|
ip = (u8_t*)&fsl_netif0_netmask.addr;
|
|||
|
pdebug(DEBUG_LEVEL_INFO,"set net mask: %u.%u.%u.%u \r\n",ip[0], ip[1],ip[2], ip[3]);
|
|||
|
|
|||
|
ip = (u8_t*)&fsl_netif0_gw.addr;
|
|||
|
pdebug(DEBUG_LEVEL_INFO,"set net gw: %u.%u.%u.%u \r\n",ip[0], ip[1],ip[2], ip[3]);
|
|||
|
#endif
|
|||
|
|
|||
|
netif_add(&fsl_netif0, &fsl_netif0_ipaddr, &fsl_netif0_netmask, &fsl_netif0_gw, NULL, ethernetif_init, ethernet_input);
|
|||
|
netif_set_default(&fsl_netif0);
|
|||
|
netif_set_up(&fsl_netif0);
|
|||
|
#if LWIP_DHCP
|
|||
|
dhcp_start(&fsl_netif0);
|
|||
|
int i = sys_now();
|
|||
|
int time = 0;
|
|||
|
while((fsl_netif0.dhcp->offered_ip_addr.addr == 0) || (fsl_netif0.dhcp->offered_gw_addr.addr == 0) || (fsl_netif0.dhcp->offered_sn_mask.addr == 0))
|
|||
|
{
|
|||
|
if(sys_now()- i > 8000) {
|
|||
|
i = sys_now();
|
|||
|
dhcp_start(&fsl_netif0);
|
|||
|
// <20><>ʱ3<CAB1><33>
|
|||
|
time++;
|
|||
|
if (time > 3) {
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
OSTimeDlyHMSM(0, 0, 0, 100);
|
|||
|
}
|
|||
|
|
|||
|
dhcp_fine_tmr();
|
|||
|
netif_set_addr(&fsl_netif0, &(fsl_netif0.dhcp->offered_ip_addr), &(fsl_netif0.dhcp->offered_sn_mask), &(fsl_netif0.dhcp->offered_gw_addr));
|
|||
|
netif_set_default(&fsl_netif0);
|
|||
|
netif_set_up(&fsl_netif0);
|
|||
|
|
|||
|
u8_t *ip = (u8_t*)&fsl_netif0.ip_addr.addr;
|
|||
|
ip = (u8_t*)&fsl_netif0.dhcp->offered_sn_mask;
|
|||
|
ip = (u8_t*)&fsl_netif0.dhcp->offered_gw_addr;
|
|||
|
|
|||
|
#endif
|
|||
|
term_printf("\n Lwip Init success \r\n\r\n");
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/// <20><>̫<EFBFBD><CCAB>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
///
|
|||
|
/// <20><>ʼ<EFBFBD><CABC><EFBFBD>ײ<EFBFBD>mac<61><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>phyоƬ<D0BE><C6AC>OSLwip,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
/// @param none
|
|||
|
/// @param none
|
|||
|
/// @return none
|
|||
|
/// @note <20><EFBFBD><DEB8><EFBFBD>־
|
|||
|
/// gkl<6B><6C>2017-05-26<32><36><EFBFBD><EFBFBD>
|
|||
|
/// LH<4C><48>2022-05-12<31><EFBFBD>
|
|||
|
void init_enet_module()
|
|||
|
{
|
|||
|
// <20><>ʼ<EFBFBD><CABC>mac<61><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>phyоƬ
|
|||
|
Lan8720_Init();
|
|||
|
// <20><>ʼ<EFBFBD><CABC>Lwip_Init<69><74><EFBFBD>ع<EFBFBD><D8B9><EFBFBD>ģ<EFBFBD><C4A3>
|
|||
|
Lwip_init();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|