当前位置:   article > 正文

.net 网络调试助手源码_DSP:6678开发板NDK网口通信完整实现(附源码)

网络调试助手和dsp通讯

如果本文对你有所帮助,请点个赞啦!!!

下一篇会在这基础上加FFT 的功能!!!

//*************************************************************************************************//写在前面:

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时已经安装了,检查下安装目录)

e84fe558d0acccd404f89d5fdafc9ca7.png

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这个版本是有问题的,尽量不用这个,安装了也没关系,可以在设置里面修改成其他版本。)

a1c6b88e90a5f603f084e9f4a4936d1a.png
我ccs安装目录下文件状态

2.导入工程

在C:Timcsdk_2_01_02_06examplesndkhelloWorld路径下是ndk例程的官方例程,

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

(1)开始导入工程

97c632ff4f8f985d483a82143c2b95c2.png

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

3eeee713d7415f2d6722cecb0cdcb059.png

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

Description	

52519be07c2b2b8f2af8c881c4082179.png

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

0d75585d41cb7f0f504ee9463bcf78b0.png

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

79e3b528afe8a8a621677eebc080dc2d.png

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

3f01956e7d8c9693a49e447fe7786627.png

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

3fc135191c0bbf01c5764c3cc6d57dfc.png

24a72502736aae326745ff81bf19971d.png

3.配置仿真文件

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

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

5a1742c8d04d71bcda9f58283b879697.png

(2)命名为evmc6678l.ccxml

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

1044cbc404e2380bfaa1710abeedcdb3.png

(4)配置gel文件

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

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

ccsv5ccs_baseemulationboards

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

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

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

80f171055581bc6fb997b4100e5f5089.png

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

5ae1ba07597681cce85a580c5f4fb033.png

4.文件处理好的状态

d3cb57e8e3cb4ff44d606c84c10f084b.png

5.IP设置

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

fdc268767b2f8db8d2624e7f5d6d25cb.png

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

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

2f47aa3004775c17cd8410661823c018.png

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

475b52a11d1e21b2903623d945232674.png
原IP

修改后的IP段程序:

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

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

1220c5c268321eab50b0ea8ef4b5cdca.png

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

  1. // If the IP address is specified, manually configure IP and Gateway
  2. //#if defined(_SCBP6618X_) || defined(_EVMTCI6614_) || defined(DEVICE_K2H) || defined(DEVICE_K2K)
  3. /* SCBP6618x, EVMTCI6614, EVMK2H, EVMK2K always uses DHCP */
  4. // if (0)
  5. //#else
  6. // if (!platform_get_switch_state(1))
  7. //#endif
  8. if(1)
  9. {
  10. printf("IP address is specified.n");
  11. CI_IPNET NA;
  12. CI_ROUTE RT;
  13. IPN IPTmp;

6.硬件调试

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

(2)点击view里面的Target configurations

2f33861dd42270377be80ea63f8f36f5.png

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

5e1988189257631c18033742fde44744.png

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

d3cb74950164d780366205a626f78bb0.png

连接之后的打印信息:

  1. C66xx_0: GEL Output: Setup_Memory_Map...
  2. C66xx_0: GEL Output: Setup_Memory_Map... Done.
  3. C66xx_0: GEL Output:
  4. Connecting Target...
  5. C66xx_0: GEL Output: DSP core #0
  6. C66xx_0: GEL Output: C6678L GEL file Ver is 2.005
  7. C66xx_0: GEL Output: Global Default Setup...
  8. C66xx_0: GEL Output: Setup Cache...
  9. C66xx_0: GEL Output: L1P = 32K
  10. C66xx_0: GEL Output: L1D = 32K
  11. C66xx_0: GEL Output: L2 = ALL SRAM
  12. C66xx_0: GEL Output: Setup Cache... Done.
  13. C66xx_0: GEL Output: Main PLL (PLL1) Setup ...
  14. C66xx_0: GEL Output: PLL in Bypass ...
  15. C66xx_0: GEL Output: PLL1 Setup for DSP @ 1000.0 MHz.
  16. C66xx_0: GEL Output: SYSCLK2 = 333.3333 MHz, SYSCLK5 = 200.0 MHz.
  17. C66xx_0: GEL Output: SYSCLK8 = 15.625 MHz.
  18. C66xx_0: GEL Output: PLL1 Setup... Done.
  19. C66xx_0: GEL Output: Power on all PSC modules and DSP domains...
  20. C66xx_0: GEL Output: Security Accelerator disabled!
  21. C66xx_0: GEL Output: Power on all PSC modules and DSP domains... Done.
  22. C66xx_0: GEL Output: PA PLL (PLL3) Setup ...
  23. C66xx_0: GEL Output: PA PLL Setup... Done.
  24. C66xx_0: GEL Output: DDR3 PLL (PLL2) Setup ...
  25. C66xx_0: GEL Output: DDR3 PLL Setup... Done.
  26. C66xx_0: GEL Output: DDR begin (1333 auto)
  27. C66xx_0: GEL Output: XMC Setup ... Done
  28. C66xx_0: GEL Output:
  29. DDR3 initialization is complete.
  30. C66xx_0: GEL Output: DDR done
  31. C66xx_0: GEL Output: DDR3 memory test... Started
  32. C66xx_0: GEL Output: DDR3 memory test... Passed
  33. C66xx_0: GEL Output: PLL and DDR Initialization completed(0) ...
  34. C66xx_0: GEL Output: configSGMIISerdes Setup... Begin
  35. C66xx_0: GEL Output:
  36. SGMII SERDES has been configured.
  37. C66xx_0: GEL Output: Enabling EDC ...
  38. C66xx_0: GEL Output: L1P error detection logic is enabled.
  39. C66xx_0: GEL Output: L2 error detection/correction logic is enabled.
  40. C66xx_0: GEL Output: MSMC error detection/correction logic is enabled.
  41. C66xx_0: GEL Output: Enabling EDC ...Done
  42. C66xx_0: GEL Output: Configuring CPSW ...
  43. C66xx_0: GEL Output: Configuring CPSW ...Done
  44. C66xx_0: GEL Output: Global Default Setup... Done.

(5)load .out文件

d5018ff78e182fc761aa0ff37e1a746c.png

加载成功后的打印信息:

  1. C66xx_0: GEL Output: Invalidate All Cache...
  2. C66xx_0: GEL Output: Invalidate All Cache... Done.
  3. C66xx_0: GEL Output: GEL Reset...
  4. C66xx_0: GEL Output: GEL Reset... Done.
  5. C66xx_0: GEL Output: Disable all EDMA3 interrupts and events.

(6)运行DSP

95927bc4c3300773f41cf854ca50a966.png

运行的打印信息:

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

(7)打开助手软件

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

7bc9a605505dd25efa460a7f62dabc0e.png

(8)测试结果

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

ce9b12a8c64a79a19da2da5939558ad2.png

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


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

void EVM_init()

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

  1. int main()
  2. {
  3. /* Start the BIOS 6 Scheduler */
  4. BIOS_start ();
  5. }

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

int StackTest()

4.初始化platform

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

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

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

  1. /*
  2. * helloWorld_bios6.c
  3. *
  4. * TCP/IP Stack 'Hello World!' Example ported to use BIOS6 OS.
  5. *
  6. * Copyright (C) 2007, 2011 Texas Instruments Incorporated - http://www.ti.com/
  7. *
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * Neither the name of Texas Instruments Incorporated nor the names of
  22. * its contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. //--------------------------------------------------------------------------
  39. // IP Stack 'Hello World!' Example
  40. //
  41. // This is a skeleton application, intended to provide application
  42. // programmers with a basic Stack setup, to which they can start
  43. // adding their code.
  44. //
  45. // To test it as is, use with helloWorld.exe from winapps directory
  46. //
  47. #include <stdio.h>
  48. #include <ti/ndk/inc/netmain.h>
  49. /* BIOS6 include */
  50. #include <ti/sysbios/BIOS.h>
  51. /* Platform utilities include */
  52. #include "ti/platform/platform.h"
  53. #include "ti/platform/resource_mgr.h"
  54. /* Platform Information - we will read it form the Platform Library */
  55. platform_info gPlatformInfo;
  56. //---------------------------------------------------------------------------
  57. // Title String
  58. //
  59. char *VerStr = "nTCP/IP Stack 'Hello World!' Applicationnn";
  60. // Our NETCTRL callback functions
  61. static void NetworkOpen();
  62. static void NetworkClose();
  63. static void NetworkIPAddr( IPN IPAddr, uint IfIdx, uint fAdd );
  64. // Fun reporting function
  65. static void ServiceReport( uint Item, uint Status, uint Report, HANDLE hCfgEntry );
  66. // External references
  67. extern int dtask_udp_hello();
  68. //---------------------------------------------------------------------------
  69. // Configuration
  70. //
  71. char *HostName = "tidsp";
  72. char *LocalIPAddr = "169.254.198.113";
  73. char *LocalIPMask = "255.255.0.0"; // Not used when using DHCP
  74. char *GatewayIP = "169.254.1.1"; // Not used when using DHCP
  75. char *DomainName = "demo.net"; // Not used when using DHCP
  76. char *DNSServer = "0.0.0.0"; // Used when set to anything but zero
  77. // Simulator EMAC Switch does not handle ALE_LEARN mode, so please configure the
  78. // MAC address of the PC where you want to launch the webpages and initiate PING to NDK */
  79. Uint8 clientMACAddress [6] = {0x5C, 0x26, 0x0A, 0x69, 0x44, 0x0B}; /* MAC address for my PC */
  80. /*************************************************************************
  81. * @b EVM_init()
  82. *
  83. * @n
  84. *
  85. * Initializes the platform hardware. This routine is configured to start in
  86. * the evm.cfg configuration file. It is the first routine that BIOS
  87. * calls and is executed before Main is called. If you are debugging within
  88. * CCS the default option in your target configuration file may be to execute
  89. * all code up until Main as the image loads. To debug this you should disable
  90. * that option.
  91. *
  92. * @param[in] None
  93. *
  94. * @retval
  95. * None
  96. ************************************************************************/
  97. void EVM_init()
  98. {
  99. platform_init_flags sFlags;
  100. platform_init_config sConfig;
  101. /* Status of the call to initialize the platform */
  102. int32_t pform_status;
  103. /*
  104. * You can choose what to initialize on the platform by setting the following
  105. * flags. Things like the DDR, PLL, etc should have been set by the boot loader.
  106. */
  107. memset( (void *) &sFlags, 0, sizeof(platform_init_flags));
  108. memset( (void *) &sConfig, 0, sizeof(platform_init_config));
  109. sFlags.pll = 0; /* PLLs for clocking */
  110. sFlags.ddr = 0; /* External memory */
  111. sFlags.tcsl = 1; /* Time stamp counter */
  112. #ifdef _SCBP6618X_
  113. sFlags.phy = 0; /* Ethernet */
  114. #else
  115. sFlags.phy = 1; /* Ethernet */
  116. #endif
  117. sFlags.ecc = 0; /* Memory ECC */
  118. sConfig.pllm = 0; /* Use libraries default clock divisor */
  119. pform_status = platform_init(&sFlags, &sConfig);
  120. /* If we initialized the platform okay */
  121. if (pform_status != Platform_EOK) {
  122. /* Initialization of the platform failed... die */
  123. while (1) {
  124. (void) platform_led(1, PLATFORM_LED_ON, PLATFORM_USER_LED_CLASS);
  125. (void) platform_delay(50000);
  126. (void) platform_led(1, PLATFORM_LED_OFF, PLATFORM_USER_LED_CLASS);
  127. (void) platform_delay(50000);
  128. }
  129. }
  130. }
  131. //---------------------------------------------------------------------
  132. // Main Entry Point
  133. //---------------------------------------------------------------------
  134. int main()
  135. {
  136. /* Start the BIOS 6 Scheduler */
  137. BIOS_start ();
  138. }
  139. //
  140. // Main Thread
  141. //
  142. int StackTest()
  143. {
  144. int rc;
  145. int i;
  146. HANDLE hCfg;
  147. QMSS_CFG_T qmss_cfg;
  148. CPPI_CFG_T cppi_cfg;
  149. /* Get information about the platform so we can use it in various places */
  150. memset( (void *) &gPlatformInfo, 0, sizeof(platform_info));
  151. (void) platform_get_info(&gPlatformInfo);
  152. (void) platform_uart_init();
  153. (void) platform_uart_set_baudrate(115200);
  154. (void) platform_write_configure(PLATFORM_WRITE_ALL);
  155. /* Clear the state of the User LEDs to OFF */
  156. for (i=0; i < gPlatformInfo.led[PLATFORM_USER_LED_CLASS].count; i++) {
  157. (void) platform_led(i, PLATFORM_LED_OFF, PLATFORM_USER_LED_CLASS);
  158. }
  159. /* Initialize the components required to run this application:
  160. * (1) QMSS
  161. * (2) CPPI
  162. * (3) Packet Accelerator
  163. */
  164. /* Initialize QMSS */
  165. if (platform_get_coreid() == 0)
  166. {
  167. qmss_cfg.master_core = 1;
  168. }
  169. else
  170. {
  171. qmss_cfg.master_core = 0;
  172. }
  173. qmss_cfg.max_num_desc = MAX_NUM_DESC;
  174. qmss_cfg.desc_size = MAX_DESC_SIZE;
  175. qmss_cfg.mem_region = Qmss_MemRegion_MEMORY_REGION0;
  176. if (res_mgr_init_qmss (&qmss_cfg) != 0)
  177. {
  178. platform_write ("Failed to initialize the QMSS subsystem n");
  179. goto main_exit;
  180. }
  181. else
  182. {
  183. platform_write ("QMSS successfully initialized n");
  184. }
  185. /* Initialize CPPI */
  186. if (platform_get_coreid() == 0)
  187. {
  188. cppi_cfg.master_core = 1;
  189. }
  190. else
  191. {
  192. cppi_cfg.master_core = 0;
  193. }
  194. cppi_cfg.dma_num = Cppi_CpDma_PASS_CPDMA;
  195. cppi_cfg.num_tx_queues = NUM_PA_TX_QUEUES;
  196. cppi_cfg.num_rx_channels = NUM_PA_RX_CHANNELS;
  197. if (res_mgr_init_cppi (&cppi_cfg) != 0)
  198. {
  199. platform_write ("Failed to initialize CPPI subsystem n");
  200. goto main_exit;
  201. }
  202. else
  203. {
  204. platform_write ("CPPI successfully initialized n");
  205. }
  206. if (res_mgr_init_pass()!= 0) {
  207. platform_write ("Failed to initialize the Packet Accelerator n");
  208. goto main_exit;
  209. }
  210. else
  211. {
  212. platform_write ("PA successfully initialized n");
  213. }
  214. //
  215. // THIS MUST BE THE ABSOLUTE FIRST THING DONE IN AN APPLICATION before
  216. // using the stack!!
  217. //
  218. rc = NC_SystemOpen( NC_PRIORITY_LOW, NC_OPMODE_INTERRUPT );
  219. if( rc )
  220. {
  221. platform_write("NC_SystemOpen Failed (%d)n",rc);
  222. for(;;);
  223. }
  224. // Print out our banner
  225. platform_write(VerStr);
  226. //
  227. // Create and build the system configuration from scratch.
  228. //
  229. // Create a new configuration
  230. hCfg = CfgNew();
  231. if( !hCfg )
  232. {
  233. platform_write("Unable to create configurationn");
  234. goto main_exit;
  235. }
  236. //
  237. // THIS MUST BE THE ABSOLUTE FIRST THING DONE IN AN APPLICATION!!
  238. //
  239. rc = NC_SystemOpen( NC_PRIORITY_LOW, NC_OPMODE_INTERRUPT );
  240. if( rc )
  241. {
  242. printf("NC_SystemOpen Failed (%d)n",rc);
  243. for(;;);
  244. }
  245. // Print out our banner
  246. printf(VerStr);
  247. //
  248. // Create and build the system configuration from scratch.
  249. //
  250. // Create a new configuration
  251. hCfg = CfgNew();
  252. if( !hCfg )
  253. {
  254. printf("Unable to create configurationn");
  255. goto main_exit;
  256. }
  257. // We better validate the length of the supplied names
  258. if( strlen( DomainName ) >= CFG_DOMAIN_MAX ||
  259. strlen( HostName ) >= CFG_HOSTNAME_MAX )
  260. {
  261. printf("Names too longn");
  262. goto main_exit;
  263. }
  264. // Add our global hostname to hCfg (to be claimed in all connected domains)
  265. CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_HOSTNAME, 0,
  266. strlen(HostName), (UINT8 *)HostName, 0 );
  267. // If the IP address is specified, manually configure IP and Gateway
  268. //#if defined(_SCBP6618X_) || defined(_EVMTCI6614_) || defined(DEVICE_K2H) || defined(DEVICE_K2K)
  269. /* SCBP6618x, EVMTCI6614, EVMK2H, EVMK2K always uses DHCP */
  270. // if (0)
  271. //#else
  272. // if (!platform_get_switch_state(1))
  273. //#endif
  274. if(1)
  275. {
  276. CI_IPNET NA;
  277. CI_ROUTE RT;
  278. IPN IPTmp;
  279. // Setup manual IP address
  280. bzero( &NA, sizeof(NA) );
  281. NA.IPAddr = inet_addr(LocalIPAddr);
  282. NA.IPMask = inet_addr(LocalIPMask);
  283. strcpy( NA.Domain, DomainName );
  284. NA.NetType = 0;
  285. // Add the address to interface 1
  286. CfgAddEntry( hCfg, CFGTAG_IPNET, 1, 0,
  287. sizeof(CI_IPNET), (UINT8 *)&NA, 0 );
  288. // Add the default gateway. Since it is the default, the
  289. // destination address and mask are both zero (we go ahead
  290. // and show the assignment for clarity).
  291. bzero( &RT, sizeof(RT) );
  292. RT.IPDestAddr = 0;
  293. RT.IPDestMask = 0;
  294. RT.IPGateAddr = inet_addr(GatewayIP);
  295. // Add the route
  296. CfgAddEntry( hCfg, CFGTAG_ROUTE, 0, 0,
  297. sizeof(CI_ROUTE), (UINT8 *)&RT, 0 );
  298. // Manually add the DNS server when specified
  299. IPTmp = inet_addr(DNSServer);
  300. if( IPTmp )
  301. CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER,
  302. 0, sizeof(IPTmp), (UINT8 *)&IPTmp, 0 );
  303. }
  304. // Else we specify DHCP
  305. else
  306. {
  307. CI_SERVICE_DHCPC dhcpc;
  308. // Specify DHCP Service on IF-1
  309. bzero( &dhcpc, sizeof(dhcpc) );
  310. dhcpc.cisargs.Mode = CIS_FLG_IFIDXVALID;
  311. dhcpc.cisargs.IfIdx = 1;
  312. dhcpc.cisargs.pCbSrv = &ServiceReport;
  313. CfgAddEntry( hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_DHCPCLIENT, 0,
  314. sizeof(dhcpc), (UINT8 *)&dhcpc, 0 );
  315. }
  316. //
  317. // Configure IPStack/OS Options
  318. //
  319. // We don't want to see debug messages less than WARNINGS
  320. rc = DBG_WARN;
  321. CfgAddEntry( hCfg, CFGTAG_OS, CFGITEM_OS_DBGPRINTLEVEL,
  322. CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 );
  323. //
  324. // This code sets up the TCP and UDP buffer sizes
  325. // (Note 8192 is actually the default. This code is here to
  326. // illustrate how the buffer and limit sizes are configured.)
  327. //
  328. // UDP Receive limit
  329. rc = 8192;
  330. CfgAddEntry( hCfg, CFGTAG_IP, CFGITEM_IP_SOCKUDPRXLIMIT,
  331. CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 );
  332. //
  333. // Boot the system using this configuration
  334. //
  335. // We keep booting until the function returns 0. This allows
  336. // us to have a "reboot" command.
  337. //
  338. do
  339. {
  340. rc = NC_NetStart( hCfg, NetworkOpen, NetworkClose, NetworkIPAddr );
  341. } while( rc > 0 );
  342. // Delete Configuration
  343. CfgFree( hCfg );
  344. // Close the OS
  345. main_exit:
  346. NC_SystemClose();
  347. return(0);
  348. }
  349. //
  350. // System Task Code [ Server Daemon Servers ]
  351. //
  352. static HANDLE hHello=0;
  353. //
  354. // NetworkOpen
  355. //
  356. // This function is called after the configuration has booted
  357. //
  358. static void NetworkOpen()
  359. {
  360. // Create our local server
  361. hHello = DaemonNew( SOCK_DGRAM, 0, 7, dtask_udp_hello,
  362. OS_TASKPRINORM, OS_TASKSTKNORM, 0, 1 );
  363. }
  364. //
  365. // NetworkClose
  366. //
  367. // This function is called when the network is shutting down,
  368. // or when it no longer has any IP addresses assigned to it.
  369. //
  370. static void NetworkClose()
  371. {
  372. DaemonFree( hHello );
  373. }
  374. //
  375. // NetworkIPAddr
  376. //
  377. // This function is called whenever an IP address binding is
  378. // added or removed from the system.
  379. //
  380. static void NetworkIPAddr( IPN IPAddr, uint IfIdx, uint fAdd )
  381. {
  382. IPN IPTmp;
  383. if( fAdd )
  384. printf("Network Added: ");
  385. else
  386. printf("Network Removed: ");
  387. // Print a message
  388. IPTmp = ntohl( IPAddr );
  389. printf("If-%d:%d.%d.%d.%dn", IfIdx,
  390. (UINT8)(IPTmp>>24)&0xFF, (UINT8)(IPTmp>>16)&0xFF,
  391. (UINT8)(IPTmp>>8)&0xFF, (UINT8)IPTmp&0xFF );
  392. }
  393. //
  394. // Service Status Reports
  395. //
  396. // Here's a quick example of using service status updates
  397. //
  398. static char *TaskName[] = { "Telnet","HTTP","NAT","DHCPS","DHCPC","DNS" };
  399. static char *ReportStr[] = { "","Running","Updated","Complete","Fault" };
  400. static char *StatusStr[] = { "Disabled","Waiting","IPTerm","Failed","Enabled" };
  401. static void ServiceReport( uint Item, uint Status, uint Report, HANDLE h )
  402. {
  403. printf( "Service Status: %-9s: %-9s: %-9s: %03dn",
  404. TaskName[Item-1], StatusStr[Status],
  405. ReportStr[Report/256], Report&0xFF );
  406. //
  407. // Example of adding to the DHCP configuration space
  408. //
  409. // When using the DHCP client, the client has full control over access
  410. // to the first 256 entries in the CFGTAG_SYSINFO space.
  411. //
  412. // Note that the DHCP client will erase all CFGTAG_SYSINFO tags except
  413. // CFGITEM_DHCP_HOSTNAME. If the application needs to keep manual
  414. // entries in the DHCP tag range, then the code to maintain them should
  415. // be placed here.
  416. //
  417. // Here, we want to manually add a DNS server to the configuration, but
  418. // we can only do it once DHCP has finished its programming.
  419. //
  420. if( Item == CFGITEM_SERVICE_DHCPCLIENT &&
  421. Status == CIS_SRV_STATUS_ENABLED &&
  422. (Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPADD) ||
  423. Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPRENEW)) )
  424. {
  425. IPN IPTmp;
  426. // Manually add the DNS server when specified
  427. IPTmp = inet_addr(DNSServer);
  428. if( IPTmp )
  429. CfgAddEntry( 0, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER,
  430. 0, sizeof(IPTmp), (UINT8 *)&IPTmp, 0 );
  431. }
  432. }

2.udphello.c

  1. /*
  2. * udpHello.c
  3. *
  4. * This program implements a UDP echo server, which echos back any
  5. * input it receives.
  6. *
  7. * Copyright (C) 2007 Texas Instruments Incorporated - http://www.ti.com/
  8. *
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * Neither the name of Texas Instruments Incorporated nor the names of
  23. * its contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. */
  39. #include <ti/ndk/inc/netmain.h>
  40. //
  41. // dtask_udp_hello() - UDP Echo Server Daemon Function
  42. // (SOCK_DGRAM, port 7)
  43. //
  44. // Returns "1" if socket 's' is still open, and "0" if its been closed
  45. //
  46. int dtask_udp_hello( SOCKET s, UINT32 unused )
  47. {
  48. struct sockaddr_in sin1;
  49. struct timeval to;
  50. int i,tmp;
  51. char *pBuf;
  52. HANDLE hBuffer;
  53. (void)unused;
  54. // Configure our socket timeout to be 3 seconds
  55. to.tv_sec = 3;
  56. to.tv_usec = 0;
  57. setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) );
  58. setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) );
  59. for(;;)
  60. {
  61. tmp = sizeof( sin1 );
  62. i = (int)recvncfrom( s, (void **)&pBuf, 0, (PSA)&sin1, &tmp, &hBuffer );
  63. // Spit any data back out
  64. if( i >= 0 )
  65. {
  66. sendto( s, pBuf, i, 0, (PSA)&sin1, sizeof(sin1) );
  67. recvncfree( hBuffer );
  68. }
  69. else
  70. break;
  71. }
  72. // Since the socket is still open, return "1"
  73. // (we need to leave UDP sockets open)
  74. return(1);
  75. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/583688
推荐阅读
相关标签
  

闽ICP备14008679号