eth_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. #include "chip.h"
  2. #include "board.h"
  3. #include "FreeRTOS.h"
  4. #include "FreeRTOS_IP.h"
  5. #include "FreeRTOS_Sockets.h"
  6. #include "FreeRTOS_DHCP.h"
  7. #include "FreeRTOS_CLI.h"
  8. typedef struct
  9. {
  10. void (*ItemFunc)(void);
  11. char *ItemName;
  12. }TTestItem;
  13. typedef void * xComPortHandle;
  14. static int test_index = -1;
  15. #if !USE_LWIP
  16. #define mainHOST_NAME "HV160"
  17. static uint8_t ucMACAddress[ 6 ] =
  18. {
  19. 0x11,
  20. 0x22,
  21. 0x33,
  22. 0x44,
  23. 0x55,
  24. 0x99,
  25. };
  26. static uint8_t ucIPAddress[ 4 ] =
  27. {
  28. 192,
  29. 168,
  30. 137,
  31. 4,
  32. };
  33. static uint8_t ucNetMask[ 4 ] =
  34. {
  35. 255,
  36. 255,
  37. 255,
  38. 0
  39. };
  40. static uint8_t ucGatewayAddress[ 4 ] =
  41. {
  42. 192,
  43. 168,
  44. 137,
  45. 1,
  46. };
  47. static uint8_t ucDNSServerAddress[ 4 ] =
  48. {
  49. 0,
  50. 0,
  51. 0,
  52. 0
  53. };
  54. extern void eth_tcp_server_test(void);
  55. extern void eth_tcp_client_test(void);
  56. extern void eth_udp_test(void);
  57. extern void https_client_test(void);
  58. extern void http_client_test(void);
  59. static void ETH_IRQHandler(void *param)
  60. {
  61. ETH_HandleTypeDef *eth = (ETH_HandleTypeDef *)param;
  62. printf("ETH_IRQHandler DMASR:0x%x \n",eth->Instance->DMASR);
  63. }
  64. ETH_DMADescTypeDef tx_des_tab[5] = {0};
  65. static uint8_t Tx_Buff[ 5 ][ ETH_TX_BUF_SIZE ];
  66. static int dhcp_state = 0;
  67. #if FREERTOS_TCP_ETH_EN
  68. void eth_test_task(void *param)
  69. {
  70. INT32 i;
  71. char chr;
  72. INT8 string[30];
  73. INT32 nItemCount;
  74. int dhcp_tout = 10;
  75. dhcp_state = 0;
  76. #if 1
  77. TTestItem stItems[] =
  78. {
  79. {eth_tcp_server_test,"tcp server test"},
  80. {eth_tcp_client_test,"tcp client test"},
  81. {eth_udp_test,"udp test"},
  82. {https_client_test, "https client test"},
  83. {http_client_test, "http client teset"}
  84. };
  85. setDhcpClientState(1); // dhcp enable
  86. if(FreeRTOS_IPInit(
  87. ucIPAddress,
  88. ucNetMask,
  89. ucGatewayAddress,
  90. ucDNSServerAddress,
  91. ucMACAddress ) != pdPASS){
  92. printf("FREERTOS IP INIT FAIL!\n");
  93. return ;
  94. }else{
  95. printf("FREERTOS IP INIT OK!\n");
  96. }
  97. if (getDhcpClientState()) {
  98. while (dhcp_state == 0) {
  99. vTaskDelay(pdMS_TO_TICKS(1000));
  100. dhcp_tout--;
  101. if (dhcp_tout == 0) {
  102. printf("dhcp time out!\n");
  103. while (1) {
  104. vTaskDelay(portMAX_DELAY);
  105. }
  106. }
  107. }
  108. }
  109. nItemCount = 0;
  110. printf("TEST ETH \r\n");
  111. nItemCount = sizeof(stItems)/sizeof(stItems[0]);
  112. while(1){
  113. #if 0
  114. for(i=0;i<nItemCount;i++){
  115. printf("%d: %s\r\n", i, stItems[i].ItemName);
  116. }
  117. printf("%d: Exit\r\n", i);
  118. printf("Please select one Item to test...\r\n");
  119. extern signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime );
  120. extern xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength );
  121. do {
  122. xSerialGetChar((void *)xSerialPortInitMinimal(NULL, NULL), (signed char *)&chr, portMAX_DELAY);
  123. } while((chr < '0') || (chr > '9'));
  124. i = chr - '0';
  125. #endif
  126. i = test_index;
  127. test_index = -1;
  128. if (i >= 0) {
  129. if(i<nItemCount){
  130. if(stItems[i].ItemFunc)
  131. stItems[i].ItemFunc();
  132. }
  133. else if(i == nItemCount) {
  134. printf("End of Test routing and exit.\r\n");
  135. break;
  136. }
  137. else {
  138. printf("You input a uncorrect item.\r\n");
  139. return;
  140. }
  141. }
  142. vTaskDelay(pdMS_TO_TICKS(100));
  143. }
  144. #else
  145. NetworkBufferDescriptor_t tx_buf;
  146. xNetworkInterfaceInitialise();
  147. tx_buf.pucEthernetBuffer = pvPortMalloc(256);
  148. tx_buf.xDataLength = 256;
  149. for(i=0;i<256;i++){
  150. tx_buf.pucEthernetBuffer[i] = i;
  151. }
  152. while(1){
  153. //xNetworkInterfaceOutput(&tx_buf,0);
  154. vTaskDelay(pdMS_TO_TICKS(4000));
  155. }
  156. #endif
  157. }
  158. #if ipconfigUSE_NETWORK_EVENT_HOOK
  159. void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )
  160. {
  161. uint32_t pulIPAddress;
  162. uint32_t pulNetMask;
  163. uint32_t pulGatewayAddress;
  164. uint32_t pulDNSServerAddress;
  165. static BaseType_t xTasksAlreadyCreated = pdFALSE;
  166. char cBuffer[16];
  167. /* If the network has just come up...*/
  168. if( ( eNetworkEvent == eNetworkUp ) && ( xTasksAlreadyCreated == pdFALSE ) )
  169. {
  170. /* Do nothing. Just a stub. */
  171. xTasksAlreadyCreated = pdTRUE;
  172. }
  173. if(eNetworkEvent == eNetworkUp){
  174. printf("NetworkUp \n");
  175. FreeRTOS_GetAddressConfiguration(&pulIPAddress, &pulNetMask, &pulGatewayAddress, &pulDNSServerAddress);
  176. FreeRTOS_inet_ntoa(pulIPAddress, cBuffer);
  177. printf("IP:%s \n",cBuffer);
  178. FreeRTOS_inet_ntoa(pulNetMask, cBuffer);
  179. printf("Mask:%s \n",cBuffer);
  180. FreeRTOS_inet_ntoa(pulGatewayAddress, cBuffer);
  181. printf("Gateway:%s \n",cBuffer);
  182. FreeRTOS_inet_ntoa(pulDNSServerAddress, cBuffer);
  183. printf("DNS:%s \n",cBuffer);
  184. }
  185. }
  186. #endif
  187. #if ( ( ipconfigUSE_LLMNR != 0 ) || \
  188. ( ipconfigUSE_NBNS != 0 ) || \
  189. ( ipconfigDHCP_REGISTER_HOSTNAME == 1 ) )
  190. const char * pcApplicationHostnameHook( void )
  191. {
  192. /* This function will be called during the DHCP: the machine will be registered
  193. * with an IP address plus this name. */
  194. //printf("pcApplicationHostnameHook \n");
  195. return mainHOST_NAME;
  196. }
  197. #endif
  198. #if ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 )
  199. BaseType_t xApplicationDNSQueryHook( const char * pcName )
  200. {
  201. BaseType_t xReturn;
  202. /* Determine if a name lookup is for this node. Two names are given
  203. * to this node: that returned by pcApplicationHostnameHook() and that set
  204. * by mainDEVICE_NICK_NAME. */
  205. if( _stricmp( pcName, pcApplicationHostnameHook() ) == 0 )
  206. {
  207. xReturn = pdPASS;
  208. }
  209. else if( _stricmp( pcName, mainDEVICE_NICK_NAME ) == 0 )
  210. {
  211. xReturn = pdPASS;
  212. }
  213. else
  214. {
  215. xReturn = pdFAIL;
  216. }
  217. return xReturn;
  218. }
  219. #endif
  220. static UBaseType_t ulNextRand;
  221. UBaseType_t uxRand( void )
  222. {
  223. const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
  224. /* Utility function to generate a pseudo random number. */
  225. ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
  226. return( ( int ) ( ulNextRand ) & 0x7fffUL );
  227. }
  228. BaseType_t xApplicationGetRandomNumber( uint32_t * pulNumber )
  229. {
  230. *pulNumber = uxRand();
  231. return pdTRUE;
  232. }
  233. /*
  234. * Callback that provides the inputs necessary to generate a randomized TCP
  235. * Initial Sequence Number per RFC 6528. THIS IS ONLY A DUMMY IMPLEMENTATION
  236. * THAT RETURNS A PSEUDO RANDOM NUMBER SO IS NOT INTENDED FOR USE IN PRODUCTION
  237. * SYSTEMS.
  238. */
  239. extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,
  240. uint16_t usSourcePort,
  241. uint32_t ulDestinationAddress,
  242. uint16_t usDestinationPort )
  243. {
  244. ( void ) ulSourceAddress;
  245. ( void ) usSourcePort;
  246. ( void ) ulDestinationAddress;
  247. ( void ) usDestinationPort;
  248. return uxRand();
  249. }
  250. #if ( ( ipconfigUSE_TCP == 1 ) && ( ipconfigUSE_DHCP_HOOK != 0 ) )
  251. __weak eDHCPCallbackAnswer_t xApplicationDHCPHook( eDHCPCallbackPhase_t eDHCPPhase,
  252. uint32_t ulIPAddress )
  253. {
  254. /* Provide a stub for this function. */
  255. char ip_str[20] = {0};
  256. if (getDhcpClientState() == 0) {
  257. sprintf(ip_str, "%d.%d.%d.%d", (ulIPAddress >> 0) & 0xFF,
  258. (ulIPAddress >> 8) & 0xFF, (ulIPAddress >> 16) & 0xFF, (ulIPAddress >> 24) & 0xFF);
  259. printf("eDHCPPhase:%d ulIPAddress:%s state:%d \r\n", eDHCPPhase, ip_str, getDhcpClientState());
  260. return eDHCPStopNoChanges;
  261. } else if (eDHCPPhase == eDHCPPhaseFinished) {
  262. sprintf(ip_str, "%d.%d.%d.%d", (ulIPAddress >> 0) & 0xFF,
  263. (ulIPAddress >> 8) & 0xFF, (ulIPAddress >> 16) & 0xFF, (ulIPAddress >> 24) & 0xFF);
  264. printf("eDHCPPhase:%d ulIPAddress:%s state:%d \r\n", eDHCPPhase, ip_str, getDhcpClientState());
  265. dhcp_state = 1;
  266. }
  267. return eDHCPContinue;
  268. }
  269. #endif
  270. void vApplicationPingReplyHook( ePingReplyStatus_t eStatus,
  271. uint16_t usIdentifier )
  272. {
  273. /* Provide a stub for this function. */
  274. }
  275. #else
  276. void eth_test_task(void *param)
  277. {
  278. printf("------------ FREERTOS_TCP_ETH_EN == 0 ------------\n");
  279. while(1) {
  280. vTaskDelay(portMAX_DELAY);
  281. }
  282. }
  283. #endif
  284. #else
  285. #include "tcpip.h"
  286. #include "api.h"
  287. #include "dhcp.h"
  288. static void tcpip_init_done(void *arg)
  289. {
  290. printf("%s\n", __func__);
  291. }
  292. extern void eth_tcp_server_test(void);
  293. extern void eth_tcp_client_test(void);
  294. extern void eth_udp_test(void);
  295. extern void https_client_test(void);
  296. extern void ping_test(void);
  297. extern void http_client_test(void);
  298. static struct netif g_lwip_netif; /* 定义一个全局的网络接口 */
  299. static int dhcp_state;
  300. static void dhcp_client_status_callback(struct netif *netif, int status, const ip_addr_t *server_addr)
  301. {
  302. char addr_str[40];
  303. if (&g_lwip_netif == netif) {
  304. printf("dhcp server ip : %s\n", ipaddr_ntoa_r(server_addr, addr_str, 40));
  305. printf("dhcp client ip : %s\n", ipaddr_ntoa_r(&netif->ip_addr, addr_str, 40));
  306. printf("dhcp client netmask: %s\n", ipaddr_ntoa_r(&netif->netmask, addr_str, 40));
  307. printf("dhcp client gw : %s\n", ipaddr_ntoa_r(&netif->gw, addr_str, 40));
  308. dhcp_state = 1;
  309. }
  310. }
  311. void eth_test_task(void *param)
  312. {
  313. INT32 i;
  314. INT8 string[30];
  315. INT32 nItemCount;
  316. ip_addr_t ip;
  317. ip_addr_t mask;
  318. ip_addr_t gw;
  319. char chr;
  320. int dhcp_tout = 10;
  321. TTestItem stItems[] =
  322. {
  323. {eth_tcp_server_test,"tcp server test"},
  324. {eth_tcp_client_test,"tcp client test"},
  325. {eth_udp_test,"udp test"},
  326. {https_client_test, "https client test"},
  327. {http_client_test, "http client test"},
  328. {ping_test, "ping test"}
  329. };
  330. IP4_ADDR(&ip, 192, 168, 137, 2);
  331. IP4_ADDR(&mask, 255, 255, 255, 0);
  332. IP4_ADDR(&gw, 192, 168, 137, 1);
  333. #if LWIP_DHCP
  334. dhcp_state = 0;
  335. dhcp_regisger_status_callback(dhcp_client_status_callback);
  336. #endif
  337. extern err_t ethernetif_init(struct netif *netif);
  338. tcpip_init(tcpip_init_done, NULL);
  339. if (netif_add(&g_lwip_netif, (const ip_addr_t *)&ip, (const ip_addr_t *)&mask, \
  340. (const ip_addr_t *)&gw, NULL, &ethernetif_init, &tcpip_input) == NULL){
  341. printf("add netif fail!\n");
  342. return;
  343. }
  344. netif_set_default(&g_lwip_netif);
  345. if (netif_is_link_up(&g_lwip_netif)) {
  346. netif_set_up(&g_lwip_netif); /* 打开netif网口 */
  347. } else {
  348. netif_set_down(&g_lwip_netif);
  349. }
  350. // 设置主机名称
  351. netif_set_hostname(&g_lwip_netif, "hv160");
  352. #if LWIP_DHCP
  353. // 启动 DHCP Client
  354. dhcp_start(&g_lwip_netif);
  355. while (dhcp_state == 0) {
  356. vTaskDelay(pdMS_TO_TICKS(1000));
  357. dhcp_tout--;
  358. if (dhcp_tout == 0) {
  359. printf("dhcp time out!\n");
  360. while (1) {
  361. vTaskDelay(portMAX_DELAY);
  362. }
  363. }
  364. }
  365. #endif
  366. nItemCount = 0;
  367. printf("TEST ETH \r\n");
  368. nItemCount = sizeof(stItems)/sizeof(stItems[0]);
  369. while(1){
  370. #if 0
  371. for(i=0;i<nItemCount;i++){
  372. printf("%d: %s\r\n", i, stItems[i].ItemName);
  373. }
  374. printf("%d: Exit\r\n", i);
  375. printf("Please select one Item to test...\r\n");
  376. extern signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime );
  377. extern xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength );
  378. do {
  379. xSerialGetChar((void *)xSerialPortInitMinimal(NULL, NULL), (signed char *)&chr, portMAX_DELAY);
  380. } while((chr < '0') || (chr > '9'));
  381. i = chr - '0';
  382. #endif
  383. i = test_index;
  384. test_index = -1;
  385. if (i >= 0) {
  386. if(i<nItemCount){
  387. if(stItems[i].ItemFunc)
  388. stItems[i].ItemFunc();
  389. }
  390. else if(i == nItemCount) {
  391. printf("End of Test routing and exit.\r\n");
  392. break;
  393. }
  394. else {
  395. printf("You input a uncorrect item.\r\n");
  396. return;
  397. }
  398. }
  399. vTaskDelay(pdMS_TO_TICKS(100));
  400. }
  401. }
  402. #endif
  403. static BaseType_t prvEthTestCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
  404. {
  405. const char *pcParameter;
  406. BaseType_t lParameterStringLength;
  407. /* Remove compile time warnings about unused parameters, and check the
  408. write buffer is not NULL. NOTE - for simplicity, this example assumes the
  409. write buffer length is adequate, so does not check for buffer overflows. */
  410. ( void ) pcCommandString;
  411. ( void ) xWriteBufferLen;
  412. configASSERT( pcWriteBuffer );
  413. /* Obtain the parameter string. */
  414. pcParameter = FreeRTOS_CLIGetParameter
  415. (
  416. pcCommandString, /* The command string itself. */
  417. 1, /* Return the first parameter. */
  418. &lParameterStringLength /* Store the parameter string length. */
  419. );
  420. /* Sanity check something was returned. */
  421. configASSERT( pcParameter );
  422. sscanf(pcParameter, "%d", &test_index);
  423. /* There is no more data to return after this single string, so return
  424. pdFALSE. */
  425. return pdFALSE;
  426. }
  427. static const CLI_Command_Definition_t xStartStopTrace =
  428. {
  429. "eth-test",
  430. "\r\neth-test:\r\n0:tcp server test\r\n1:tcp client test\r\n2:udp test\r\n3:https client test\r\n4:http client teset\r\n",
  431. prvEthTestCommand, /* The function to run. */
  432. 1 /* One parameter is expected. Valid values are "start" and "stop". */
  433. };
  434. void eth_test_demo(void)
  435. {
  436. xTaskCreate(eth_test_task, "eth_test_task", configMINIMAL_STACK_SIZE * 10, NULL,
  437. configMAX_PRIORITIES - 3, NULL);
  438. FreeRTOS_CLIRegisterCommand(&xStartStopTrace);
  439. }