⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lwip.c

📁 uCOSII_lwip_lpc1768
💻 C
📖 第 1 页 / 共 2 页
字号:
	 * P1.4 - ENET_TX_EN
	 * P1.8 - ENET_CRS
	 * P1.9 - ENET_RXD0
	 * P1.10 - ENET_RXD1
	 * P1.14 - ENET_RX_ER
	 * P1.15 - ENET_REF_CLK
	 * P1.16 - ENET_MDC
	 * P1.17 - ENET_MDIO
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 1;

	PinCfg.Pinnum = 0;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 1;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 4;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 8;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 9;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 10;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 14;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 15;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 16;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 17;
	PINSEL_ConfigPin(&PinCfg);

	Emac_Config.Mode = EMAC_MODE_AUTO;
	Emac_Config.pbEMAC_Addr = EMACAddr;
	/* Initialize EMAC module with given parameter */
	while (EMAC_Init(&Emac_Config) == 0)
	{
		/* Delay for a while then continue initializing EMAC module */
		printf("Error during initializing EMAC, restart after a while \r\n");
		for (i = 0x100000; i; i--);
	}
	/* Enable all interrupt */
	EMAC_IntCmd((EMAC_INT_RX_OVERRUN | EMAC_INT_TX_DONE | EMAC_INT_RX_DONE ) , ENABLE);
	/* preemption = 1, sub-priority = 1 */
	NVIC_SetPriority(ENET_IRQn, ((0x01<<3)|0x01));
	NVIC_EnableIRQ(ENET_IRQn);
	printf("Initialize EMAC complete \r\n");
}

/*******************************************************************************
* Function Name  : ethernet_send
* Description    : transmit an Ethernet frame to MAC/DMA controller
* Input          : None
* Output         : None
* Return         : None
* Attention		 : None
*******************************************************************************/
int ethernet_send(void *pPacket, int size)
{
	EMAC_PACKETBUF_Type TxPack;

	/* Check size */
	if(size == 0)
	{
		return 0;
	}

	/* check Tx Slot is available */
	if (EMAC_CheckTransmitIndex() == FALSE)
	{
		return -1;
	}

	size = MIN(size,EMAC_MAX_PACKET_SIZE);

	/* Setup Tx Packet buffer */
	TxPack.ulDataLen = size;
	TxPack.pbDataBuf = (uint32_t *)pPacket;
	EMAC_WritePacketBuffer(&TxPack);
	EMAC_UpdateTxProduceIndex();

	return 0;
}

/*******************************************************************************
* Function Name  : Init_lwIP
* Description    : Init_lwIP initialize the LwIP
* Input          : None
* Output         : None
* Return         : None
* Attention		 : None
*******************************************************************************/
void Init_lwIP(void)
{
    struct ip_addr ipaddr;
    struct ip_addr netmask;
    struct ip_addr gw;
    uint8_t macaddress[6]={ emacETHADDR0, emacETHADDR1, emacETHADDR2, emacETHADDR3, emacETHADDR4, emacETHADDR5 };

    sys_sem_t sem;
    
    sys_init();
    
    /* Initializes the dynamic memory heap defined by MEM_SIZE.*/
    mem_init();
    
    /* Initializes the memory pools defined by MEMP_NUM_x.*/
    memp_init();
    
    pbuf_init();	
    netif_init();
    
	USART_Configuration();

    printf("TCP/IP initializing... \r\n");
    sem = sys_sem_new(0);
    tcpip_init(TcpipInitDone, &sem);
    sys_sem_wait(sem);
    sys_sem_free(sem);
    printf("TCP/IP initialized. \r\n");
      
#if LWIP_DHCP
    /* 启用DHCP服务器 */
    ipaddr.addr = 0;
    netmask.addr = 0;
    gw.addr = 0;
#else
    /* 启用静态IP */
    IP4_ADDR(&ipaddr, emacIPADDR0, emacIPADDR1, emacIPADDR2, emacIPADDR3 );
    IP4_ADDR(&netmask,emacNET_MASK0, emacNET_MASK1, emacNET_MASK2, emacNET_MASK3 );
    IP4_ADDR(&gw, emacGATEWAY_ADDR0, emacGATEWAY_ADDR1, emacGATEWAY_ADDR2, emacGATEWAY_ADDR3 );
#endif
    
    
    netif_add(&_netif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
    netif_set_default(&_netif);
    
#if LWIP_DHCP
    dhcp_start(&_netif);
#endif
    netif_set_up(&_netif);
}

/*******************************************************************************
* Function Name  : Display_IPAddress
* Description    : Display_IPAddress Display IP Address
* Input          : None
* Output         : None
* Return         : None
* Attention		 : None
*******************************************************************************/
void Display_IPAddress(void)
{
    if(IPaddress != _netif.ip_addr.addr)
    {   
	    /* IP 地址发生改变*/
        __IO uint8_t iptab[4];
        uint8_t iptxt[20];
        
        /* read the new IP address */
        IPaddress = _netif.ip_addr.addr;
        
        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]);
        
        list_if();
        
        /* Display the new IP address */
#if LWIP_DHCP
        if(_netif.flags & NETIF_FLAG_DHCP)
        {   
		    /* IP地址由DHCP指定 */
            /* Display the IP address */
		    GUI_Text( ( MAX_X - ( strlen("  IP assigned   ") * 8 ) ) / 2 , MAX_Y/2 - 40,  "  IP assigned   ", White, Black);
		    GUI_Text( ( MAX_X - ( strlen("by DHCP server  ") * 8 ) ) / 2 , MAX_Y/2 - 20,  " by DHCP server ", White, Black);
		    GUI_Text( ( MAX_X - ( strlen((void*)iptxt) * 8 ) ) / 2       , MAX_Y/2     ,       iptxt        , White, Black);	
		    GUI_Text( ( MAX_X - ( strlen("                ") * 8 ) ) / 2 , MAX_Y/2 + 20,  "                ", White, Black);
        }
        else
#endif  
        /* 静态IP地址 */
        {   
		    /* Display the IP address */
		    GUI_Text( ( MAX_X - ( strlen(" Static IP Addr ") * 8 ) ) / 2 , MAX_Y/2 - 40,  " Static IP Addr ", White, Black);
		    GUI_Text( ( MAX_X - ( strlen("                ") * 8 ) ) / 2 , MAX_Y/2 - 20,  "                ", White, Black);
		    GUI_Text( ( MAX_X - ( strlen((void*)iptxt) * 8 ) ) / 2       , MAX_Y/2     ,       iptxt        , White, Black);
		    GUI_Text( ( MAX_X - ( strlen("                ") * 8 ) ) / 2 , MAX_Y/2 + 20,  "                ", White, Black);

        }
    }
#if LWIP_DHCP
    else if(IPaddress == 0)
    {   
	    /* 等待DHCP分配IP */
		GUI_Text( ( MAX_X - ( strlen("   Looking for  ") * 8 ) ) / 2 , MAX_Y/2 - 40,  "   Looking for  ", White, Black);
		GUI_Text( ( MAX_X - ( strlen("   DHCP server  ") * 8 ) ) / 2 , MAX_Y/2 - 20,  "   DHCP server  ", White, Black);
		GUI_Text( ( MAX_X - ( strlen(" please wait... ") * 8 ) ) / 2 , MAX_Y/2     ,  " please wait... ", White, Black);
		GUI_Text( ( MAX_X - ( strlen("                ") * 8 ) ) / 2 , MAX_Y/2 + 20,  "                ", White, Black);
        
        /* If no response from a DHCP server for MAX_DHCP_TRIES times */
        /* stop the dhcp client and set a static IP address */
        if(_netif.dhcp->tries > MAX_DHCP_TRIES) 
        {   
		    /* 超出DHCP重试次数,改用静态IP设置 */
            struct ip_addr ipaddr;
            struct ip_addr netmask;
            struct ip_addr gw;
            
		    GUI_Text( ( MAX_X - ( strlen("  DHCP timeout  ") * 8 ) ) / 2, MAX_Y/2 + 20, "  DHCP timeout  ", White, Black);

            dhcp_stop(&_netif);
            
            IP4_ADDR(&ipaddr, emacIPADDR0, emacIPADDR1, emacIPADDR2, emacIPADDR3 );
            IP4_ADDR(&netmask,emacNET_MASK0, emacNET_MASK1, emacNET_MASK2, emacNET_MASK3 );
            IP4_ADDR(&gw, emacGATEWAY_ADDR0, emacGATEWAY_ADDR1, emacGATEWAY_ADDR2, emacGATEWAY_ADDR3 );
            
            netif_set_addr(&_netif, &ipaddr , &netmask, &gw);
            
            list_if();
        }
    }
#endif
}

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
	/* wait for current transmission complete - THR must be empty */
	while (UART_CheckBusy(LPC_UART2) == SET);
	
	UART_SendByte(LPC_UART2, ch);
	
	return ch;
}

/*********************************************************************************************************
      END FILE
*********************************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -