如果出现图片打不开,或是显示异常,请点击下方链接阅读原文!!!

DSP:6678开发板NDK网口通信完整实现(附源码) - 子木的文章 - 知乎 https://zhuanlan.zhihu.com/p/85110735

 

//*************************************************************************************************

//写在前面:

1.已经有很多前辈做过很优秀的记录,本篇尽量讲得详细一点,能够让新手直接上手。

2.在整个调试过程中,会遇到各种各样的问题,如果遇到问题请看第四部分,大部分问题应该能解决掉,不能解决的就评论区留言。

3.我的CCS安装路径是“C:\Ti\***”,后文如果出现,请对应你自己的安装路径

 

1.开发环境:CCS 5.4 、卓岚TCP&UDP调试工具

2.开发平台: Ti TMDSEVM6678LE(TMDSEVM6678LE是开发板,程序在其他6678板卡上也运行过)

3.需要工程源文件,ccs,调试助手的评论区留言!

 

//*************************************************************************************************//

目录

一、原理

二、调试流程

三、程序执行流程与部分程序解读

四、注意事项与遇到过的问题及解决方法

五、程序源码

 

 

一、原理

1.通过UDP实现DSP与PC的通信,硬件上只需要用网线连接电脑与6678板卡即可,在程序上只是对TI已有的范例进行一些修改就能实现简单的通信。

2.板卡上电后,CCS通过JTAG连接调试板卡,则会首先从GEL的StartUp( )函数开始执行初始化,JTAG连接后执行OnTargetConnect( )函数,OnTargetConnect( )会调用Global_Default_Setup_Silent()函数中的ddr3_setup_auto_lvl_1333(0);和 configSGMIISerdes();函数完成DDR和SGMII的初始化。

3.如果要使用NDK,建立的ccs工程必须是跑sys/bios的工程。所以需要安装bios,我安装的是bios_setupwin32_6_33_06_50(正常情况下,安装ccs时已经安装了,检查下安装目录)

4. 在任何SOCKET应用程序建立之前,TCP/IP堆栈必须被正确的配置然后初始化。通常可以动态创建或者在DSP/BIOS中静态创建一个堆栈初始化任务,不管应用程序当中建立了多少SOCKET,在整个系统中只能有一个堆栈初始化任务,并且该任务会成为整个网络任务的唯一调度者,因此该任务在应用程序结束前不会返回。

堆栈初始化过程:(1)使用NC_SystemOpen()。该函数为所有网络应用程序建立堆栈和内存环境;(2)使用CfgNew()。该函数会创建一个配置句柄,利用该句柄使用CfgAddEntry()函数可以添加DHCP、DNS、HTTP等服务和配置SOCKET缓冲区大小与ARP超时参数;(3)使用NC_NetStart()。该函数根据前面的配置参数启动网络,并创建三个回调函数,分别是网络启动时只运行一次的函数、网络被关闭时只运行一次的函数、当IP地址改变时运行的函数。通常会在启动函数里面创建自己的应用程序线程,在关闭函数里面删除创建的应用程序线程。

 

 

二、调试过程

1.首先需要安装MCSDK和NDK,一般来说安装的CCS版本不是太低,都已经自动安装这些组件了。(我记得ndk_2_21_01_38这个版本是有问题的,尽量不用这个,安装了也没关系,可以在设置里面修改成其他版本。)

我ccs安装目录下文件状态

2.导入工程

在C:\Ti\mcsdk_2_01_02_06\examples\ndk\helloWorld路径下是ndk例程的官方例程,

导入工程可以分为在它源文件上进行调试,也可以复制到其他地方调试,担心破坏掉源文件,所以这里采用把文件复制到其他路径下调试。

(1)开始导入工程

(2)工程文件选择到helloWorld或者evmc6678l都可以,因为后面还得改。第3步是把文件复制到自己的工作路径下,我的是workspace

(3)可以看到此时文件标识有感叹号,这是由于路径不对,如果编译,会报错:

 
 

Description Resource Path Location Type gmake: *** No rule to make target `C:/Ti/workspace/udpHello.c', needed by `udpHello.obj'. helloworld_evmc6678l Unknown C/C++ Problem

(4)在工程上右键,点击properties

(5)在箭头处双击,进去过file找到helloWorld.c等4个c文件的位置,也就是:C:\Ti\mcsdk_2_01_02_06\examples\ndk\helloWorld里面。就完成了修改路径

(6)修改完之后是下图的状态

(7)此时右键工程选择Rebuild Project,编译就通过了。

 

3.配置仿真文件

由于需要硬件仿真,需要选择仿真器类型,创建ccxml和gel文件,如果以前写过相关的ccxml和gel,只要仿真器和板卡没变,可以直接复制过来使用,就可跳过这一步,文章最后会提供我的ccxml和gel。

(1)右键工程,选择新建Target Configuration File

(2)命名为evmc6678l.ccxml

(3)选择仿真器型号和芯片

(4)配置gel文件

【Gel文件的执行时间一般在DSP connect target之后,在download代码之前。因为gel文件通常会进行一些初始化的寄存器的设置,比如memory map,PLL和DDR初始化等。所以在download之前必须初始化这些。

gel文件的脚本都可以找到的。CCS5的版本可以在这个目录下找到大部分EVM的gel脚本

ccsv5\ccs_base\emulation\boards

gel脚本类似于C语言函数,就是运行一些函数。比如DDR初始化函数。如果DDR没有初始化,是无法将代码download到DDR中去的。执行gel脚本有两种,一种是用户自己执行,另一种是CCS5自动将gel脚本关联到相关的操作中,比如connect target就自动关联了初始化PLL,初始化DDR的gel函数。

可以找个gel脚本看看,这样更有助于理解。】

双击刚刚新建的evmc6678l.ccxml文件,再点击图中位置,

(5)gel文件在CCS安装路径下的\ccsv5\ccs_base\emulation\boards里可以找到对应芯片的gel文件,最后点击save。6678是多核芯片,这里只用到了core0,所以给0核配置就可以了。也可以直接在路径中复制过来。

4.文件处理好的状态

5.IP设置

(1)由于是采取的静态IP,需要把板卡上的拨码开关设置成下图这样,(这个拨码开关决定选择dhcp还是静态IP)

(2)需要将程序中IP部分进行相应修改

(3)为了稳定,上位机使用的是一个网上的助手软件。也可以用QT或是VS编写,后面的文章会说到。下图是助手界面,可以看到相关信息,目的IP(DSP的ip)是169.254.198.113。所以需要在DSP程序中进行修改。

(4)将原来helloWorld.c里面的ip改为助手显示的IP就行

原IP

修改后的IP段程序:

 
 

/****助手IP***/ char *HostName = "tidsp"; char *LocalIPAddr = "169.254.198.113"; char *LocalIPMask = "255.255.0.0"; // Not used when using DHCP char *GatewayIP = "169.254.1.1"; // Not used when using DHCP char *DomainName = "demo.net"; // Not used when using DHCP char *DNSServer = "0.0.0.0"; // Used when set to anything but zero

(5)因为使用的是静态IP,不需要DHCP,在helloWorld.c里面再修改下面的一段程序

修改后的程序(注释后加一个if(1)就行了):

 
 

// If the IP address is specified, manually configure IP and Gateway //#if defined(_SCBP6618X_) || defined(_EVMTCI6614_) || defined(DEVICE_K2H) || defined(DEVICE_K2K) /* SCBP6618x, EVMTCI6614, EVMK2H, EVMK2K always uses DHCP */ // if (0) //#else // if (!platform_get_switch_state(1)) //#endif if(1) { printf("IP address is specified.\n"); CI_IPNET NA; CI_ROUTE RT; IPN IPTmp;

6.硬件调试

(1)rebuild project之后连接好线。板卡需要电源线,仿真器,网线。

(2)点击view里面的Target configurations

(3)在弹出的界面右键ccxml文件,launch selected configuration

(4)选择0核,再点击连接

连接之后的打印信息:

 
 

C66xx_0: GEL Output: Setup_Memory_Map... C66xx_0: GEL Output: Setup_Memory_Map... Done. C66xx_0: GEL Output: Connecting Target... C66xx_0: GEL Output: DSP core #0 C66xx_0: GEL Output: C6678L GEL file Ver is 2.005 C66xx_0: GEL Output: Global Default Setup... C66xx_0: GEL Output: Setup Cache... C66xx_0: GEL Output: L1P = 32K C66xx_0: GEL Output: L1D = 32K C66xx_0: GEL Output: L2 = ALL SRAM C66xx_0: GEL Output: Setup Cache... Done. C66xx_0: GEL Output: Main PLL (PLL1) Setup ... C66xx_0: GEL Output: PLL in Bypass ... C66xx_0: GEL Output: PLL1 Setup for DSP @ 1000.0 MHz. C66xx_0: GEL Output: SYSCLK2 = 333.3333 MHz, SYSCLK5 = 200.0 MHz. C66xx_0: GEL Output: SYSCLK8 = 15.625 MHz. C66xx_0: GEL Output: PLL1 Setup... Done. C66xx_0: GEL Output: Power on all PSC modules and DSP domains... C66xx_0: GEL Output: Security Accelerator disabled! C66xx_0: GEL Output: Power on all PSC modules and DSP domains... Done. C66xx_0: GEL Output: PA PLL (PLL3) Setup ... C66xx_0: GEL Output: PA PLL Setup... Done. C66xx_0: GEL Output: DDR3 PLL (PLL2) Setup ... C66xx_0: GEL Output: DDR3 PLL Setup... Done. C66xx_0: GEL Output: DDR begin (1333 auto) C66xx_0: GEL Output: XMC Setup ... Done C66xx_0: GEL Output: DDR3 initialization is complete. C66xx_0: GEL Output: DDR done C66xx_0: GEL Output: DDR3 memory test... Started C66xx_0: GEL Output: DDR3 memory test... Passed C66xx_0: GEL Output: PLL and DDR Initialization completed(0) ... C66xx_0: GEL Output: configSGMIISerdes Setup... Begin C66xx_0: GEL Output: SGMII SERDES has been configured. C66xx_0: GEL Output: Enabling EDC ... C66xx_0: GEL Output: L1P error detection logic is enabled. C66xx_0: GEL Output: L2 error detection/correction logic is enabled. C66xx_0: GEL Output: MSMC error detection/correction logic is enabled. C66xx_0: GEL Output: Enabling EDC ...Done C66xx_0: GEL Output: Configuring CPSW ... C66xx_0: GEL Output: Configuring CPSW ...Done C66xx_0: GEL Output: Global Default Setup... Done.

(5)load .out文件

加载成功后的打印信息:

 
 

C66xx_0: GEL Output: Invalidate All Cache... C66xx_0: GEL Output: Invalidate All Cache... Done. C66xx_0: GEL Output: GEL Reset... C66xx_0: GEL Output: GEL Reset... Done. C66xx_0: GEL Output: Disable all EDMA3 interrupts and events.

(6)运行DSP

运行的打印信息:

 
 

[C66xx_0] QMSS successfully initialized CPPI successfully initialized PA successfully initialized TCP/IP Stack 'Hello World!' Application TCP/IP Stack 'Hello World!' Application PASS successfully initialized Ethernet subsystem successfully initialized Ethernet eventId : 48 and vectId (Interrupt) : 7 Registration of the EMAC Successful, waiting for link up .. Network Added: If-1:169.254.198.113

(7)打开助手软件

dsp的程序实现的功能是上位机向dsp发送消息,dsp收到后再回传给上位机。下图是打开助手后的状态

(8)测试结果

下图是由上位机发送一串数字,并显示接收到的消息的状态

 

三、程序执行流程与部分程序解读

1.首先执行EVM_init()函数,也就是在main()函数之前执行,具体设置在一个*.cfg文件中

 
 

void EVM_init()

2.在main函数中执行BIOS_start(),并创建进程StackTest()

 
 

int main() { /* Start the BIOS 6 Scheduler */ BIOS_start (); }

3.在进程StackTest中执行了QMSS、CPPI、PA等的初始化,完成了基本网络配置

 
 

int StackTest()

4.初始化platform

5.完成相应配置后启动NetworkOpen服务后,关联了dtask_udp_hello任务,等待UDP连接,并执行相关操作

 
 

static void NetworkOpen() { // Create our local server hHello = DaemonNew( SOCK_DGRAM, 0, 7, dtask_udp_hello, OS_TASKPRINORM, OS_TASKSTKNORM, 0, 1 ); }

6.程序开始执行udpHello.c

 

四、注意事项与遇到过的问题及解决方法

 

1.路径不要包含非ASCII字符

2.dsp每次烧写程序都需要断电重连

3.程序如果出问题了,可以在程序中用printf("**** ");打印信息。但是printf开销很大,占用资源,完整的程序中应该少用。

4.如果遇到关于platform而出现的编译问题,参考这个链接:https://blog.csdn.net/u013368345/article/details/87905638

5. 正常来说,PC是无法识别DSP的,因为没有交互,但是能够正常通信。所以在电脑上ping的话,会显示ping不通。

6.导入工程出错,选择状态栏的project方式或者在project explore界面右键导入

 

 

五、程序源码

1.helloWorld.c

 
 

/* * helloWorld_bios6.c * * TCP/IP Stack 'Hello World!' Example ported to use BIOS6 OS. * * Copyright (C) 2007, 2011 Texas Instruments Incorporated - http://www.ti.com/ * * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution. * * Neither the name of Texas Instruments Incorporated nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ //-------------------------------------------------------------------------- // IP Stack 'Hello World!' Example // // This is a skeleton application, intended to provide application // programmers with a basic Stack setup, to which they can start // adding their code. // // To test it as is, use with helloWorld.exe from \winapps directory // #include <stdio.h> #include <ti/ndk/inc/netmain.h> /* BIOS6 include */ #include <ti/sysbios/BIOS.h> /* Platform utilities include */ #include "ti/platform/platform.h" #include "ti/platform/resource_mgr.h" /* Platform Information - we will read it form the Platform Library */ platform_info gPlatformInfo; //--------------------------------------------------------------------------- // Title String // char *VerStr = "\nTCP/IP Stack 'Hello World!' Application\n\n"; // Our NETCTRL callback functions static void NetworkOpen(); static void NetworkClose(); static void NetworkIPAddr( IPN IPAddr, uint IfIdx, uint fAdd ); // Fun reporting function static void ServiceReport( uint Item, uint Status, uint Report, HANDLE hCfgEntry ); // External references extern int dtask_udp_hello(); //--------------------------------------------------------------------------- // Configuration // char *HostName = "tidsp"; char *LocalIPAddr = "169.254.198.113"; char *LocalIPMask = "255.255.0.0"; // Not used when using DHCP char *GatewayIP = "169.254.1.1"; // Not used when using DHCP char *DomainName = "demo.net"; // Not used when using DHCP char *DNSServer = "0.0.0.0"; // Used when set to anything but zero // Simulator EMAC Switch does not handle ALE_LEARN mode, so please configure the // MAC address of the PC where you want to launch the webpages and initiate PING to NDK */ Uint8 clientMACAddress [6] = {0x5C, 0x26, 0x0A, 0x69, 0x44, 0x0B}; /* MAC address for my PC */ /************************************************************************* * @b EVM_init() * * @n * * Initializes the platform hardware. This routine is configured to start in * the evm.cfg configuration file. It is the first routine that BIOS * calls and is executed before Main is called. If you are debugging within * CCS the default option in your target configuration file may be to execute * all code up until Main as the image loads. To debug this you should disable * that option. * * @param[in] None * * @retval * None ************************************************************************/ void EVM_init() { platform_init_flags sFlags; platform_init_config sConfig; /* Status of the call to initialize the platform */ int32_t pform_status; /* * You can choose what to initialize on the platform by setting the following * flags. Things like the DDR, PLL, etc should have been set by the boot loader. */ memset( (void *) &sFlags, 0, sizeof(platform_init_flags)); memset( (void *) &sConfig, 0, sizeof(platform_init_config)); sFlags.pll = 0; /* PLLs for clocking */ sFlags.ddr = 0; /* External memory */ sFlags.tcsl = 1; /* Time stamp counter */ #ifdef _SCBP6618X_ sFlags.phy = 0; /* Ethernet */ #else sFlags.phy = 1; /* Ethernet */ #endif sFlags.ecc = 0; /* Memory ECC */ sConfig.pllm = 0; /* Use libraries default clock divisor */ pform_status = platform_init(&sFlags, &sConfig); /* If we initialized the platform okay */ if (pform_status != Platform_EOK) { /* Initialization of the platform failed... die */ while (1) { (void) platform_led(1, PLATFORM_LED_ON, PLATFORM_USER_LED_CLASS); (void) platform_delay(50000); (void) platform_led(1, PLATFORM_LED_OFF, PLATFORM_USER_LED_CLASS); (void) platform_delay(50000); } } } //--------------------------------------------------------------------- // Main Entry Point //--------------------------------------------------------------------- int main() { /* Start the BIOS 6 Scheduler */ BIOS_start (); } // // Main Thread // int StackTest() { int rc; int i; HANDLE hCfg; QMSS_CFG_T qmss_cfg; CPPI_CFG_T cppi_cfg; /* Get information about the platform so we can use it in various places */ memset( (void *) &gPlatformInfo, 0, sizeof(platform_info)); (void) platform_get_info(&gPlatformInfo); (void) platform_uart_init(); (void) platform_uart_set_baudrate(115200); (void) platform_write_configure(PLATFORM_WRITE_ALL); /* Clear the state of the User LEDs to OFF */ for (i=0; i < gPlatformInfo.led[PLATFORM_USER_LED_CLASS].count; i++) { (void) platform_led(i, PLATFORM_LED_OFF, PLATFORM_USER_LED_CLASS); } /* Initialize the components required to run this application: * (1) QMSS * (2) CPPI * (3) Packet Accelerator */ /* Initialize QMSS */ if (platform_get_coreid() == 0) { qmss_cfg.master_core = 1; } else { qmss_cfg.master_core = 0; } qmss_cfg.max_num_desc = MAX_NUM_DESC; qmss_cfg.desc_size = MAX_DESC_SIZE; qmss_cfg.mem_region = Qmss_MemRegion_MEMORY_REGION0; if (res_mgr_init_qmss (&qmss_cfg) != 0) { platform_write ("Failed to initialize the QMSS subsystem \n"); goto main_exit; } else { platform_write ("QMSS successfully initialized \n"); } /* Initialize CPPI */ if (platform_get_coreid() == 0) { cppi_cfg.master_core = 1; } else { cppi_cfg.master_core = 0; } cppi_cfg.dma_num = Cppi_CpDma_PASS_CPDMA; cppi_cfg.num_tx_queues = NUM_PA_TX_QUEUES; cppi_cfg.num_rx_channels = NUM_PA_RX_CHANNELS; if (res_mgr_init_cppi (&cppi_cfg) != 0) { platform_write ("Failed to initialize CPPI subsystem \n"); goto main_exit; } else { platform_write ("CPPI successfully initialized \n"); } if (res_mgr_init_pass()!= 0) { platform_write ("Failed to initialize the Packet Accelerator \n"); goto main_exit; } else { platform_write ("PA successfully initialized \n"); } // // THIS MUST BE THE ABSOLUTE FIRST THING DONE IN AN APPLICATION before // using the stack!! // rc = NC_SystemOpen( NC_PRIORITY_LOW, NC_OPMODE_INTERRUPT ); if( rc ) { platform_write("NC_SystemOpen Failed (%d)\n",rc); for(;;); } // Print out our banner platform_write(VerStr); // // Create and build the system configuration from scratch. // // Create a new configuration hCfg = CfgNew(); if( !hCfg ) { platform_write("Unable to create configuration\n"); goto main_exit; } // // THIS MUST BE THE ABSOLUTE FIRST THING DONE IN AN APPLICATION!! // rc = NC_SystemOpen( NC_PRIORITY_LOW, NC_OPMODE_INTERRUPT ); if( rc ) { printf("NC_SystemOpen Failed (%d)\n",rc); for(;;); } // Print out our banner printf(VerStr); // // Create and build the system configuration from scratch. // // Create a new configuration hCfg = CfgNew(); if( !hCfg ) { printf("Unable to create configuration\n"); goto main_exit; } // We better validate the length of the supplied names if( strlen( DomainName ) >= CFG_DOMAIN_MAX || strlen( HostName ) >= CFG_HOSTNAME_MAX ) { printf("Names too long\n"); goto main_exit; } // Add our global hostname to hCfg (to be claimed in all connected domains) CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_HOSTNAME, 0, strlen(HostName), (UINT8 *)HostName, 0 ); // If the IP address is specified, manually configure IP and Gateway //#if defined(_SCBP6618X_) || defined(_EVMTCI6614_) || defined(DEVICE_K2H) || defined(DEVICE_K2K) /* SCBP6618x, EVMTCI6614, EVMK2H, EVMK2K always uses DHCP */ // if (0) //#else // if (!platform_get_switch_state(1)) //#endif if(1) { CI_IPNET NA; CI_ROUTE RT; IPN IPTmp; // Setup manual IP address bzero( &NA, sizeof(NA) ); NA.IPAddr = inet_addr(LocalIPAddr); NA.IPMask = inet_addr(LocalIPMask); strcpy( NA.Domain, DomainName ); NA.NetType = 0; // Add the address to interface 1 CfgAddEntry( hCfg, CFGTAG_IPNET, 1, 0, sizeof(CI_IPNET), (UINT8 *)&NA, 0 ); // Add the default gateway. Since it is the default, the // destination address and mask are both zero (we go ahead // and show the assignment for clarity). bzero( &RT, sizeof(RT) ); RT.IPDestAddr = 0; RT.IPDestMask = 0; RT.IPGateAddr = inet_addr(GatewayIP); // Add the route CfgAddEntry( hCfg, CFGTAG_ROUTE, 0, 0, sizeof(CI_ROUTE), (UINT8 *)&RT, 0 ); // Manually add the DNS server when specified IPTmp = inet_addr(DNSServer); if( IPTmp ) CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER, 0, sizeof(IPTmp), (UINT8 *)&IPTmp, 0 ); } // Else we specify DHCP else { CI_SERVICE_DHCPC dhcpc; // Specify DHCP Service on IF-1 bzero( &dhcpc, sizeof(dhcpc) ); dhcpc.cisargs.Mode = CIS_FLG_IFIDXVALID; dhcpc.cisargs.IfIdx = 1; dhcpc.cisargs.pCbSrv = &ServiceReport; CfgAddEntry( hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_DHCPCLIENT, 0, sizeof(dhcpc), (UINT8 *)&dhcpc, 0 ); } // // Configure IPStack/OS Options // // We don't want to see debug messages less than WARNINGS rc = DBG_WARN; CfgAddEntry( hCfg, CFGTAG_OS, CFGITEM_OS_DBGPRINTLEVEL, CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 ); // // This code sets up the TCP and UDP buffer sizes // (Note 8192 is actually the default. This code is here to // illustrate how the buffer and limit sizes are configured.) // // UDP Receive limit rc = 8192; CfgAddEntry( hCfg, CFGTAG_IP, CFGITEM_IP_SOCKUDPRXLIMIT, CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 ); // // Boot the system using this configuration // // We keep booting until the function returns 0. This allows // us to have a "reboot" command. // do { rc = NC_NetStart( hCfg, NetworkOpen, NetworkClose, NetworkIPAddr ); } while( rc > 0 ); // Delete Configuration CfgFree( hCfg ); // Close the OS main_exit: NC_SystemClose(); return(0); } // // System Task Code [ Server Daemon Servers ] // static HANDLE hHello=0; // // NetworkOpen // // This function is called after the configuration has booted // static void NetworkOpen() { // Create our local server hHello = DaemonNew( SOCK_DGRAM, 0, 7, dtask_udp_hello, OS_TASKPRINORM, OS_TASKSTKNORM, 0, 1 ); } // // NetworkClose // // This function is called when the network is shutting down, // or when it no longer has any IP addresses assigned to it. // static void NetworkClose() { DaemonFree( hHello ); } // // NetworkIPAddr // // This function is called whenever an IP address binding is // added or removed from the system. // static void NetworkIPAddr( IPN IPAddr, uint IfIdx, uint fAdd ) { IPN IPTmp; if( fAdd ) printf("Network Added: "); else printf("Network Removed: "); // Print a message IPTmp = ntohl( IPAddr ); printf("If-%d:%d.%d.%d.%d\n", IfIdx, (UINT8)(IPTmp>>24)&0xFF, (UINT8)(IPTmp>>16)&0xFF, (UINT8)(IPTmp>>8)&0xFF, (UINT8)IPTmp&0xFF ); } // // Service Status Reports // // Here's a quick example of using service status updates // static char *TaskName[] = { "Telnet","HTTP","NAT","DHCPS","DHCPC","DNS" }; static char *ReportStr[] = { "","Running","Updated","Complete","Fault" }; static char *StatusStr[] = { "Disabled","Waiting","IPTerm","Failed","Enabled" }; static void ServiceReport( uint Item, uint Status, uint Report, HANDLE h ) { printf( "Service Status: %-9s: %-9s: %-9s: %03d\n", TaskName[Item-1], StatusStr[Status], ReportStr[Report/256], Report&0xFF ); // // Example of adding to the DHCP configuration space // // When using the DHCP client, the client has full control over access // to the first 256 entries in the CFGTAG_SYSINFO space. // // Note that the DHCP client will erase all CFGTAG_SYSINFO tags except // CFGITEM_DHCP_HOSTNAME. If the application needs to keep manual // entries in the DHCP tag range, then the code to maintain them should // be placed here. // // Here, we want to manually add a DNS server to the configuration, but // we can only do it once DHCP has finished its programming. // if( Item == CFGITEM_SERVICE_DHCPCLIENT && Status == CIS_SRV_STATUS_ENABLED && (Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPADD) || Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPRENEW)) ) { IPN IPTmp; // Manually add the DNS server when specified IPTmp = inet_addr(DNSServer); if( IPTmp ) CfgAddEntry( 0, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER, 0, sizeof(IPTmp), (UINT8 *)&IPTmp, 0 ); } }

2.udphello.c

 
 

/* * udpHello.c * * This program implements a UDP echo server, which echos back any * input it receives. * * Copyright (C) 2007 Texas Instruments Incorporated - http://www.ti.com/ * * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution. * * Neither the name of Texas Instruments Incorporated nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include <ti/ndk/inc/netmain.h> // // dtask_udp_hello() - UDP Echo Server Daemon Function // (SOCK_DGRAM, port 7) // // Returns "1" if socket 's' is still open, and "0" if its been closed // int dtask_udp_hello( SOCKET s, UINT32 unused ) { struct sockaddr_in sin1; struct timeval to; int i,tmp; char *pBuf; HANDLE hBuffer; (void)unused; // Configure our socket timeout to be 3 seconds to.tv_sec = 3; to.tv_usec = 0; setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) ); setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) ); for(;;) { tmp = sizeof( sin1 ); i = (int)recvncfrom( s, (void **)&pBuf, 0, (PSA)&sin1, &tmp, &hBuffer ); // Spit any data back out if( i >= 0 ) { sendto( s, pBuf, i, 0, (PSA)&sin1, sizeof(sin1) ); recvncfree( hBuffer ); } else break; } // Since the socket is still open, return "1" // (we need to leave UDP sockets open) return(1); }