跟unix中的uip-conf.h文件是一样的只是加载的应用头文件改为了hello-world模块,uip-conf.h文件是对uipopt.h文件的补充,之前在uip-0.9中这两个文件是没有分开的,是修改的uipopt.h,main.c文件编写同样比较简单,如下:
- /******************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V1.00
- * $Revision: 2 $
- * $Date: 14/11/25 10:00a $
- * @brief A uIP httpd sample for NUC472 MCU.
- *
- * @note
- * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
- *****************************************************************************/
- #include <stdio.h>
- #include <string.h>
- #include "NUC472_442.h"
- #include "uip.h"
- #include "uip_arp.h"
- #include "tapdev.h"
- #include "uip-timer.h"
-
- #define PLL_CLOCK 84000000
-
- #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
-
- // Our MAC address
- struct uip_eth_addr ethaddr = {0x00, 0x00, 0x00, 0x59, 0x16, 0x88};
-
- void SYS_Init(void)
- {
-
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Unlock protected registers */
- SYS_UnlockReg();
-
- /* Enable External XTAL (4~24 MHz) */
- CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
-
- /* Waiting for 12MHz clock ready */
- CLK_WaitClockReady( CLK_STATUS_HXTSTB_Msk);
-
- /* Switch HCLK clock source to HXT */
- CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HXT,CLK_CLKDIV0_HCLK(1));
-
- /* Set PLL to power down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/
- CLK->PLLCTL |= CLK_PLLCTL_PD_Msk;
-
- /* Set PLL frequency */
- CLK->PLLCTL = CLK_PLLCTL_84MHz_HXT;
-
- /* Waiting for clock ready */
- CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);
-
- /* Switch HCLK clock source to PLL */
- CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL,CLK_CLKDIV0_HCLK(1));
-
- /* Enable IP clock */
- CLK_EnableModuleClock(UART0_MODULE);
- CLK_EnableModuleClock(EMAC_MODULE);
- CLK_EnableModuleClock(TMR0_MODULE);
-
- /* Select IP clock source */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));
- CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);
-
- // Configure MDC clock rate to HCLK / (127 + 1) = 656 kHz if system is running at 84 MHz
- CLK_SetModuleClock(EMAC_MODULE, 0, CLK_CLKDIV3_EMAC(127));
-
- /* Update System Core Clock */
- /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
- SystemCoreClockUpdate();
-
-
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init I/O Multi-function */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Set GPG multi-function pins for UART0 RXD and TXD */
- SYS->GPG_MFPL = SYS_GPG_MFPL_PG1MFP_UART0_RXD | SYS_GPG_MFPL_PG2MFP_UART0_TXD ;
- // Configure RMII pins
- SYS->GPC_MFPL = SYS_GPC_MFPL_PC0MFP_EMAC_REFCLK |
- SYS_GPC_MFPL_PC1MFP_EMAC_MII_RXERR |
- SYS_GPC_MFPL_PC2MFP_EMAC_MII_RXDV |
- SYS_GPC_MFPL_PC3MFP_EMAC_MII_RXD1 |
- SYS_GPC_MFPL_PC4MFP_EMAC_MII_RXD0 |
- SYS_GPC_MFPL_PC6MFP_EMAC_MII_TXD0 |
- SYS_GPC_MFPL_PC7MFP_EMAC_MII_TXD1;
-
-
- SYS->GPC_MFPH = SYS_GPC_MFPH_PC8MFP_EMAC_MII_TXEN;
- // Enable high slew rate on all RMII pins
- PC->SLEWCTL |= 0x1DF;
-
- // Configure MDC, MDIO at PB14 & PB15
- SYS->GPB_MFPH = SYS_GPB_MFPH_PB14MFP_EMAC_MII_MDC | SYS_GPB_MFPH_PB15MFP_EMAC_MII_MDIO;
-
- /* Lock protected registers */
- SYS_LockReg();
-
- }
-
- // This sample application can response to ICMP ECHO packets (ping)
- // IP address is configure with DHCP, but if a lease cannot be acquired, a static IP will be used.
- int main(void)
- {
- int i;
- uip_ipaddr_t ipaddr;
- struct timer periodic_timer, arp_timer;
-
- timer_set(&periodic_timer, CLOCK_SECOND / 2);
- timer_set(&arp_timer, CLOCK_SECOND * 10);
-
- SYS_Init();
- UART_Open(UART0, 115200);
-
- clock_init();
- tapdev_init();
- uip_init();
-
- uip_ipaddr(ipaddr, 192,168,2,105);
- uip_sethostaddr(ipaddr);
- uip_ipaddr(ipaddr, 192,168,2,1);
- uip_setdraddr(ipaddr);
- uip_ipaddr(ipaddr, 255,255,255,0);
- uip_setnetmask(ipaddr);
- uip_setethaddr(ethaddr);
- hello_world_init();
- printf("NUC472 uIP sample code start\n");
-
- while(1)
- {
- uip_len = tapdev_read();
- if(uip_len > 0)
- {
- if(BUF->type == htons(UIP_ETHTYPE_IP))
- {
- uip_arp_ipin();
- uip_input();
- /* If the above function invocation resulted in data that
- should be sent out on the network, the global variable
- uip_len is set to a value > 0. */
- if(uip_len > 0)
- {
- uip_arp_out();
- tapdev_send();
- }
- }
- else if(BUF->type == htons(UIP_ETHTYPE_ARP))
- {
- uip_arp_arpin();
- /* If the above function invocation resulted in data that
- should be sent out on the network, the global variable
- uip_len is set to a value > 0. */
- if(uip_len > 0)
- {
- tapdev_send();
- }
- }
- }
- else if(timer_expired(&periodic_timer))
- {
- timer_reset(&periodic_timer);
- for(i = 0; i < UIP_CONNS; i++)
- {
- uip_periodic(i);
- /* If the above function invocation resulted in data that
- should be sent out on the network, the global variable
- uip_len is set to a value > 0. */
- if(uip_len > 0)
- {
- uip_arp_out();
- tapdev_send();
- }
- }
- #if UIP_UDP
- for(i = 0; i < UIP_UDP_CONNS; i++)
- {
- uip_udp_periodic(i);
- /* If the above function invocation resulted in data that
- should be sent out on the network, the global variable
- uip_len is set to a value > 0. */
- if(uip_len > 0)
- {
- uip_arp_out();
- tapdev_send();
- }
- }
- #endif /* UIP_UDP */
-
- /* Call the ARP timer function every 10 seconds. */
- if(timer_expired(&arp_timer))
- {
- timer_reset(&arp_timer);
- uip_arp_timer();
- }
- }
- }
- }
-
- void
- uip_log(char *m)
- {
- printf("uIP log message: %s\n", m);
- }
|