[DemoCode下载] 新唐NUC472系列UIP_TCP_Client DemoCode

[复制链接]
 楼主| YWBIN 发表于 2015-9-10 11:17 | 显示全部楼层 |阅读模式
如下程序为新唐NUC472系列上通过UIP实现的TCP客户端的DemoCode,有需要的朋友可以参考下
 楼主| YWBIN 发表于 2015-9-10 11:18 | 显示全部楼层
程序

NUC472_UIP_TCP_Client.rar

2.88 MB, 下载次数: 126

huangcunxiake 发表于 2015-9-10 11:52 | 显示全部楼层
UIP实现的TCP?照顾好听472就搞定了吗,这个自带UIP接口?
gejigeji521 发表于 2015-9-10 21:01 | 显示全部楼层
TCP在472的操作真不会,不知道需要系统不
玛尼玛尼哄 发表于 2015-9-11 11:00 | 显示全部楼层
通过某个协议完成TCP的操作。
Roderman_z 发表于 2015-9-13 20:35 | 显示全部楼层
谢谢楼主分享的UIP实现TCP的程序
 楼主| YWBIN 发表于 2015-9-15 17:17 | 显示全部楼层
gejigeji521 发表于 2015-9-10 21:01
TCP在472的操作真不会,不知道需要系统不

操作系统可用可不用,上述的程序是没加操作系统的。
z1234zz 发表于 2016-4-13 12:01 | 显示全部楼层
谢谢楼主!!!!!1
yiyigirl2014 发表于 2017-3-25 13:55 | 显示全部楼层
  1. #ifndef __TCP_CLIENT_H__
  2. #define __TCP_CLIENT_H__

  3. #include "uipopt.h"

  4. #define UIP_APPCALL tcp_client_appcall

  5. typedef u8_t uip_tcp_appstate_t;

  6. extern void tcp_client_init(void);

  7. #endif
yiyigirl2014 发表于 2017-3-25 13:56 | 显示全部楼层
  1. #include "tcp_client.h"
  2. #include "uip.h"

  3. void tcp_client_init(void)
  4. {
  5.     uip_ipaddr_t ipaddr;

  6.     uip_ipaddr(&ipaddr, 192,168,0,3);
  7.     uip_connect(&ipaddr, HTONS(80));
  8. }

  9. void tcp_client_appcall(void)
  10. {
  11.     if(uip_connected())
  12.                 {
  13.                     printf("tcp client connected !\n");
  14.                           uip_send("hello server !", strlen("hello server !"));
  15.                 }
  16.                 else if(uip_newdata())
  17.                 {
  18.                     uip_send(uip_appdata, uip_len);
  19.                 }
  20.                 else if(uip_closed())
  21.                 {
  22.                     printf("tcp client closed !\n");
  23.                 }
  24. }
yiyigirl2014 发表于 2017-3-25 13:56 | 显示全部楼层
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 8 $
  5. * $Date: 14/05/30 5:59p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    This Ethernet sample tends to get a DHCP lease from DHCP
  7. *           server. And use 192.168.10.10 as IP address it failed to
  8. *           get a lease. After IP address configured, this sample can
  9. *           reply to PING packets.
  10. *
  11. * @note
  12. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  13. *****************************************************************************/
  14. #include <stdio.h>
  15. #include "NUC472_442.h"

  16. #include "uip.h"
  17. #include "uip_arp.h"
  18. #include "timer.h"

  19. #define PLLCON_SETTING      CLK_PLLCON_84MHz_XTAL
  20. #define PLL_CLOCK           84000000

  21. #define BUF ((struct uip_eth_hdr *)&uip_buf[0])

  22. #ifndef NULL
  23. #define NULL (void *)0
  24. #endif /* NULL */


  25. // Our MAC address
  26. uint8_t g_au8MacAddr[6] = {0x00, 0x00, 0x00, 0x59, 0x16, 0x88};

  27. clock_time_t g_Time = 0;

  28. /**
  29.   * @brief  This function handles SysTick Handler.
  30.   * @param  None
  31.   * @retval None
  32.   */
  33. void SysTick_Handler(void)
  34. {
  35.     g_Time++;
  36.     if (g_Time == 0xFFFFFFFF)
  37.     {
  38.         g_Time = 0;
  39.     }      
  40. }

  41. /**
  42.   * @brief  EMAC Tx interrupt handler.
  43.   * @param  None
  44.   * [url=home.php?mod=space&uid=266161]@return[/url] None
  45.   */
  46. void EMAC_TX_IRQHandler(void)
  47. {
  48.     // Clean up Tx resource occupied by previous sent packet(s)
  49.     EMAC_SendPktDone();
  50. }

  51. void SYS_Init(void)
  52. {
  53.     /*---------------------------------------------------------------------------------------------------------*/
  54.     /* Init System Clock                                                                                       */
  55.     /*---------------------------------------------------------------------------------------------------------*/
  56.     /* Unlock protected registers */
  57.     SYS_UnlockReg();

  58.     /* Enable External XTAL (4~24 MHz) */
  59.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  60.     /* Waiting for 12MHz clock ready */
  61.     CLK_WaitClockReady( CLK_STATUS_HXTSTB_Msk);

  62.     /* Switch HCLK clock source to HXT */
  63.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HXT,CLK_CLKDIV0_HCLK(1));

  64.     /* Set PLL to power down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/
  65.     CLK->PLLCTL |= CLK_PLLCTL_PD_Msk;

  66.     /* Set PLL frequency */
  67.     CLK->PLLCTL = CLK_PLLCTL_84MHz_HXT;

  68.     /* Waiting for clock ready */
  69.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  70.     /* Switch HCLK clock source to PLL */
  71.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL,CLK_CLKDIV0_HCLK(1));

  72.     /* Enable IP clock */
  73.     CLK_EnableModuleClock(UART0_MODULE);
  74.     CLK_EnableModuleClock(EMAC_MODULE);

  75.     /* Select IP clock source */
  76.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

  77.     // Configure MDC clock rate to HCLK / (127 + 1) = 656 kHz if system is running at 84 MHz
  78.     CLK_SetModuleClock(EMAC_MODULE, 0, CLK_CLKDIV3_EMAC(127));

  79.     /* Update System Core Clock */
  80.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
  81.     SystemCoreClockUpdate();


  82.     /*---------------------------------------------------------------------------------------------------------*/
  83.     /* Init I/O Multi-function                                                                                 */
  84.     /*---------------------------------------------------------------------------------------------------------*/
  85.     /* Set GPG multi-function pins for UART0 RXD and TXD */
  86.     SYS->GPG_MFPL = SYS_GPG_MFPL_PG1MFP_UART0_RXD | SYS_GPG_MFPL_PG2MFP_UART0_TXD ;
  87.     // Configure RMII pins
  88.     SYS->GPC_MFPL = SYS_GPC_MFPL_PC0MFP_EMAC_REFCLK |
  89.                     SYS_GPC_MFPL_PC1MFP_EMAC_MII_RXERR |
  90.                     SYS_GPC_MFPL_PC2MFP_EMAC_MII_RXDV |
  91.                     SYS_GPC_MFPL_PC3MFP_EMAC_MII_RXD1 |
  92.                     SYS_GPC_MFPL_PC4MFP_EMAC_MII_RXD0 |
  93.                     SYS_GPC_MFPL_PC6MFP_EMAC_MII_TXD0 |
  94.                     SYS_GPC_MFPL_PC7MFP_EMAC_MII_TXD1;


  95.     SYS->GPC_MFPH = SYS_GPC_MFPH_PC8MFP_EMAC_MII_TXEN;
  96.     // Enable high slew rate on all RMII pins
  97.     PC->SLEWCTL |= 0x1DF;

  98.     // Configure MDC, MDIO at PB14 & PB15
  99.     SYS->GPB_MFPH = SYS_GPB_MFPH_PB14MFP_EMAC_MII_MDC | SYS_GPB_MFPH_PB15MFP_EMAC_MII_MDIO;

  100.     /* Lock protected registers */
  101.     SYS_LockReg();
  102. }

  103. int main(void)
  104. {
  105.     int i;
  106.     uip_ipaddr_t ipaddr;
  107.     struct timer periodic_timer, arp_timer;
  108.        
  109.     SYS_Init();
  110.     UART_Open(UART0, 115200);

  111.     // Select RMII interface by default
  112.     EMAC_Open(g_au8MacAddr);

  113.     NVIC_EnableIRQ(EMAC_TX_IRQn);
  114.     //NVIC_EnableIRQ(EMAC_RX_IRQn);

  115.     EMAC_ENABLE_RX();
  116.     EMAC_ENABLE_TX();
  117.        
  118.     SysTick_Config(SystemCoreClock / 1000);  

  119.     timer_set(&periodic_timer, CLOCK_SECOND / 2);
  120.     timer_set(&arp_timer, CLOCK_SECOND * 10);
  121.   
  122.     uip_init();

  123.     uip_ipaddr(ipaddr, 192,168,0,2);
  124.     uip_sethostaddr(ipaddr);
  125.     uip_ipaddr(ipaddr, 192,168,0,1);
  126.     uip_setdraddr(ipaddr);
  127.     uip_ipaddr(ipaddr, 255,255,255,0);
  128.     uip_setnetmask(ipaddr);

  129.     tcp_client_init();
  130.   
  131.     while(1)
  132.     {
  133.         EMAC_RecvPkt(uip_buf, ( uint32_t *)&uip_len);
  134.         if(uip_len == 0)
  135.         {
  136.             EMAC_RecvPktDone();
  137.         }
  138.         if(uip_len > 0)
  139.                                 {
  140.             if(BUF->type == htons(UIP_ETHTYPE_IP))
  141.                                                 {
  142.                 uip_arp_ipin();
  143.                 uip_input();
  144.                 /* If the above function invocation resulted in data that
  145.                 should be sent out on the network, the global variable
  146.                 uip_len is set to a value > 0. */
  147.                 if(uip_len > 0)
  148.                                                                 {
  149.                     uip_arp_out();
  150.                     //tapdev_send();
  151.                     EMAC_SendPkt(uip_buf, uip_len);
  152.                 }
  153.             }
  154.                                                 else if(BUF->type == htons(UIP_ETHTYPE_ARP))
  155.                                                 {
  156.                 uip_arp_arpin();
  157.                 /* If the above function invocation resulted in data that
  158.                 should be sent out on the network, the global variable
  159.                 uip_len is set to a value > 0. */
  160.                 if(uip_len > 0)
  161.                                                                 {
  162.                     EMAC_SendPkt(uip_buf, uip_len);
  163.                 }
  164.             }
  165.             EMAC_RecvPktDone();
  166.         }
  167.                                 else if(timer_expired(&periodic_timer))
  168.                                 {
  169.             timer_reset(&periodic_timer);
  170.             for(i = 0; i < UIP_CONNS; i++)
  171.                                           {
  172.                 uip_periodic(i);
  173.                 /* If the above function invocation resulted in data that
  174.                 should be sent out on the network, the global variable
  175.                 uip_len is set to a value > 0. */
  176.                 if(uip_len > 0)
  177.                                                                 {
  178.                     uip_arp_out();
  179.                     EMAC_SendPkt(uip_buf, uip_len);
  180.                 }
  181.             }
  182. #if UIP_UDP
  183.             for(i = 0; i < UIP_UDP_CONNS; i++)
  184.                                                 {
  185.                 uip_udp_periodic(i);
  186.                 /* If the above function invocation resulted in data that
  187.                 should be sent out on the network, the global variable
  188.                 uip_len is set to a value > 0. */
  189.                 if(uip_len > 0)
  190.                                                                 {
  191.                     uip_arp_out();
  192.                     EMAC_SendPkt(uip_buf, uip_len);
  193.                 }
  194.             }
  195. #endif /* UIP_UDP */
  196.       
  197.             /* Call the ARP timer function every 10 seconds. */
  198.             if(timer_expired(&arp_timer))
  199.                                                 {
  200.                 timer_reset(&arp_timer);
  201.                 uip_arp_timer();
  202.             }
  203.         }
  204.     }
  205.     return 0;
  206. }


  207. void uip_log(char *m)
  208. {
  209.   printf("uIP log message: %s\n", m);
  210. }



  211. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

21

主题

46

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部

21

主题

46

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部