打印
[DemoCode下载]

新唐NUC472系列UIP_TCP_Client DemoCode

[复制链接]
2748|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
YWBIN|  楼主 | 2015-9-10 11:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如下程序为新唐NUC472系列上通过UIP实现的TCP客户端的DemoCode,有需要的朋友可以参考下
沙发
YWBIN|  楼主 | 2015-9-10 11:18 | 只看该作者
程序

NUC472_UIP_TCP_Client.rar

2.88 MB

使用特权

评论回复
板凳
huangcunxiake| | 2015-9-10 11:52 | 只看该作者
UIP实现的TCP?照顾好听472就搞定了吗,这个自带UIP接口?

使用特权

评论回复
地板
gejigeji521| | 2015-9-10 21:01 | 只看该作者
TCP在472的操作真不会,不知道需要系统不

使用特权

评论回复
5
玛尼玛尼哄| | 2015-9-11 11:00 | 只看该作者
通过某个协议完成TCP的操作。

使用特权

评论回复
6
Roderman_z| | 2015-9-13 20:35 | 只看该作者
谢谢楼主分享的UIP实现TCP的程序

使用特权

评论回复
7
YWBIN|  楼主 | 2015-9-15 17:17 | 只看该作者
gejigeji521 发表于 2015-9-10 21:01
TCP在472的操作真不会,不知道需要系统不

操作系统可用可不用,上述的程序是没加操作系统的。

使用特权

评论回复
8
z1234zz| | 2016-4-13 12:01 | 只看该作者
谢谢楼主!!!!!1

使用特权

评论回复
9
yiyigirl2014| | 2017-3-25 13:55 | 只看该作者
#ifndef __TCP_CLIENT_H__
#define __TCP_CLIENT_H__

#include "uipopt.h"

#define UIP_APPCALL tcp_client_appcall

typedef u8_t uip_tcp_appstate_t;

extern void tcp_client_init(void);

#endif

使用特权

评论回复
10
yiyigirl2014| | 2017-3-25 13:56 | 只看该作者
#include "tcp_client.h"
#include "uip.h"

void tcp_client_init(void)
{
    uip_ipaddr_t ipaddr;

    uip_ipaddr(&ipaddr, 192,168,0,3);
    uip_connect(&ipaddr, HTONS(80));
}

void tcp_client_appcall(void)
{
    if(uip_connected())
                {
                    printf("tcp client connected !\n");
                          uip_send("hello server !", strlen("hello server !"));
                }
                else if(uip_newdata())
                {
                    uip_send(uip_appdata, uip_len);
                }
                else if(uip_closed())
                {
                    printf("tcp client closed !\n");
                }
}

使用特权

评论回复
11
yiyigirl2014| | 2017-3-25 13:56 | 只看该作者
/******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url]     main.c
* [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
* $Revision: 8 $
* $Date: 14/05/30 5:59p $
* [url=home.php?mod=space&uid=247401]@brief[/url]    This Ethernet sample tends to get a DHCP lease from DHCP
*           server. And use 192.168.10.10 as IP address it failed to
*           get a lease. After IP address configured, this sample can
*           reply to PING packets.
*
* @note
* Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "NUC472_442.h"

#include "uip.h"
#include "uip_arp.h"
#include "timer.h"

#define PLLCON_SETTING      CLK_PLLCON_84MHz_XTAL
#define PLL_CLOCK           84000000

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

#ifndef NULL
#define NULL (void *)0
#endif /* NULL */


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

clock_time_t g_Time = 0;

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
    g_Time++;
    if (g_Time == 0xFFFFFFFF)
    {
        g_Time = 0;
    }      
}

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

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);

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

    // 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();
}

int main(void)
{
    int i;
    uip_ipaddr_t ipaddr;
    struct timer periodic_timer, arp_timer;
       
    SYS_Init();
    UART_Open(UART0, 115200);

    // Select RMII interface by default
    EMAC_Open(g_au8MacAddr);

    NVIC_EnableIRQ(EMAC_TX_IRQn);
    //NVIC_EnableIRQ(EMAC_RX_IRQn);

    EMAC_ENABLE_RX();
    EMAC_ENABLE_TX();
       
    SysTick_Config(SystemCoreClock / 1000);  

    timer_set(&periodic_timer, CLOCK_SECOND / 2);
    timer_set(&arp_timer, CLOCK_SECOND * 10);
  
    uip_init();

    uip_ipaddr(ipaddr, 192,168,0,2);
    uip_sethostaddr(ipaddr);
    uip_ipaddr(ipaddr, 192,168,0,1);
    uip_setdraddr(ipaddr);
    uip_ipaddr(ipaddr, 255,255,255,0);
    uip_setnetmask(ipaddr);

    tcp_client_init();
  
    while(1)
    {
        EMAC_RecvPkt(uip_buf, ( uint32_t *)&uip_len);
        if(uip_len == 0)
        {
            EMAC_RecvPktDone();
        }
        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();
                    EMAC_SendPkt(uip_buf, uip_len);
                }
            }
                                                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)
                                                                {
                    EMAC_SendPkt(uip_buf, uip_len);
                }
            }
            EMAC_RecvPktDone();
        }
                                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();
                    EMAC_SendPkt(uip_buf, uip_len);
                }
            }
#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();
                    EMAC_SendPkt(uip_buf, uip_len);
                }
            }
#endif /* UIP_UDP */
      
            /* Call the ARP timer function every 10 seconds. */
            if(timer_expired(&arp_timer))
                                                {
                timer_reset(&arp_timer);
                uip_arp_timer();
            }
        }
    }
    return 0;
}


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



/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

21

主题

46

帖子

1

粉丝