NetworkInterface.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /***********************************************************************************************************************
  2. * Copyright (C) 2021 Arkmicro Corporation. All rights reserved.
  3. ***********************************************************************************************************************/
  4. /***********************************************************************************************************************
  5. * File Name : NetworkInterface.c
  6. * Device(s) : RTL8189FTV
  7. * Description : Interfaces FreeRTOS TCP/IP stack to RX Ethernet driver.
  8. ***********************************************************************************************************************/
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. /* FreeRTOS includes. */
  14. #include "FreeRTOS.h"
  15. #include "task.h"
  16. #include "FreeRTOS_IP.h"
  17. #include "FreeRTOS_IP_Private.h"
  18. /*#include "FreeRTOS_DNS.h" */
  19. #include "NetworkBufferManagement.h"
  20. #include "NetworkInterface.h"
  21. #include "wifi_constants.h"
  22. #include "net_stack_intf.h"
  23. #include "FreeRTOS_ARP.h"
  24. #include "wifi_conf.h"
  25. #include "board.h"
  26. //#define USE_AP 0
  27. //#define DUMP_NETWORK_DATA
  28. //#undef DUMP_NETWORK_DATA
  29. #ifdef WIFI_SUPPORT
  30. #if CONFIG_USB_DEVICE_CDC_NCM
  31. #error "Do not choose USE_USB_DEVICE when use WIFI_SUPPORT"
  32. #endif
  33. #endif
  34. typedef enum
  35. {
  36. eMACInit, /* Must initialise MAC. */
  37. eMACPass, /* Initialisation was successful. */
  38. eMACFailed, /* Initialisation failed. */
  39. } eMAC_INIT_STATUS_TYPE;
  40. static eMAC_INIT_STATUS_TYPE xMacInitStatus = eMACInit;
  41. extern void cmd_test(const char* temp_uart_buf);
  42. static int InitializeNetwork( void );
  43. int g_ncm_register(const char *name);
  44. /***********************************************************************************************************************
  45. * Function Name: xNetworkInterfaceInitialise ()
  46. * Description : Initialization of Ethernet driver.
  47. * Arguments : none
  48. * Return Value : pdPASS, pdFAIL
  49. **********************************************************************************************************************/
  50. BaseType_t xNetworkInterfaceInitialise( void )
  51. {
  52. BaseType_t xReturn;
  53. if( xMacInitStatus == eMACInit )
  54. {
  55. //rltk_wlan_set_netif_info(0, NULL, "00:0c:29:5d:2e:05");
  56. /*
  57. * Perform the hardware specific network initialization here using the Ethernet driver library to initialize the
  58. * Ethernet hardware, initialize DMA descriptors, and perform a PHY auto-negotiation to obtain a network link.
  59. *
  60. * InitialiseNetwork() uses Ethernet peripheral driver library function, and returns 0 if the initialization fails.
  61. */
  62. if( InitializeNetwork() == pdFALSE )
  63. {
  64. xMacInitStatus = eMACFailed;
  65. }
  66. else
  67. {
  68. /* Indicate that the MAC initialisation succeeded. */
  69. xMacInitStatus = eMACPass;
  70. }
  71. FreeRTOS_printf( ( "InitializeNetwork returns %s\n", ( xMacInitStatus == eMACPass ) ? "OK" : " Fail" ) );
  72. }
  73. if( xMacInitStatus == eMACPass )
  74. {
  75. xReturn = pdPASS;
  76. }
  77. else
  78. {
  79. xReturn = pdFAIL;
  80. }
  81. FreeRTOS_printf( ( "xNetworkInterfaceInitialise returns %d\n", xReturn ) );
  82. return xReturn;
  83. } /* End of function xNetworkInterfaceInitialise() */
  84. /***********************************************************************************************************************
  85. * Function Name: xNetworkInterfaceOutput ()
  86. * Description : Simple network output interface.
  87. * Arguments : pxDescriptor, xReleaseAfterSend
  88. * Return Value : pdTRUE, pdFALSE
  89. **********************************************************************************************************************/
  90. #if CONFIG_USB_DEVICE_CDC_NCM
  91. void gether_send(NetworkBufferDescriptor_t * const pxDescriptor);
  92. #endif
  93. BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxDescriptor,
  94. BaseType_t xReleaseAfterSend )
  95. {
  96. BaseType_t xReturn = pdFALSE;
  97. #ifdef WIFI_SUPPORT
  98. struct eth_drv_sg sg_list = {0};
  99. #endif
  100. #ifdef DUMP_NETWORK_DATA
  101. if (1) {
  102. int i;
  103. char* tmp = (char *)pxDescriptor->pucEthernetBuffer;
  104. printf("\r\nsend len:%d--> ", pxDescriptor->xDataLength);
  105. for (i = 0; i < pxDescriptor->xDataLength; i++) {
  106. printf("%02x ", tmp[i]);
  107. }printf("send end\r\n");
  108. }
  109. #endif
  110. #ifdef WIFI_SUPPORT
  111. if (!rltk_wlan_running(0)) {
  112. goto exit;
  113. }
  114. sg_list.buf = (unsigned int)pxDescriptor->pucEthernetBuffer;
  115. sg_list.len = (unsigned int)pxDescriptor->xDataLength;
  116. xReturn = (BaseType_t)rltk_wlan_send(0, &sg_list, 1, pxDescriptor->xDataLength);
  117. #endif
  118. xReturn = pdTRUE;
  119. #if CONFIG_USB_DEVICE_CDC_NCM
  120. NetworkBufferDescriptor_t *pxDescriptor_eth = pxDescriptor;
  121. if (!xReleaseAfterSend) {
  122. pxDescriptor_eth = pxDuplicateNetworkBufferWithDescriptor(pxDescriptor, pxDescriptor->xDataLength);
  123. xReleaseAfterSend = pdFALSE;
  124. } else {
  125. xReleaseAfterSend = pdFALSE;//release at usb net driver
  126. }
  127. //print_hex(pxDescriptor->pucEthernetBuffer, pxDescriptor->xDataLength, NULL);
  128. gether_send(pxDescriptor_eth);
  129. #else
  130. #ifdef WIFI_SUPPORT
  131. exit:
  132. #endif
  133. #endif
  134. if( xReleaseAfterSend != pdFALSE )
  135. {
  136. /* It is assumed SendData() copies the data out of the FreeRTOS+TCP Ethernet
  137. * buffer. The Ethernet buffer is therefore no longer needed, and must be
  138. * freed for re-use. */
  139. vReleaseNetworkBufferAndDescriptor( pxDescriptor );
  140. }
  141. return xReturn;
  142. } /* End of function xNetworkInterfaceOutput() */
  143. /***********************************************************************************************************************
  144. * Function Name: vNetworkInterfaceAllocateRAMToBuffers ()
  145. * Description : .
  146. * Arguments : pxNetworkBuffers
  147. * Return Value : none
  148. **********************************************************************************************************************/
  149. void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )
  150. {
  151. } /* End of function vNetworkInterfaceAllocateRAMToBuffers() */
  152. /***********************************************************************************************************************
  153. * Function Name: InitializeNetwork ()
  154. * Description :
  155. * Arguments : none
  156. * Return Value : pdTRUE, pdFALSE
  157. **********************************************************************************************************************/
  158. #include "wifi_structures.h"
  159. #if 0
  160. static rtw_result_t ark_rtw_scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result )
  161. {
  162. char bssid[32] = {0};
  163. char *ptr = (char *)malloced_scan_result->ap_details.BSSID.octet;
  164. sprintf(bssid, "%02x:%02x:%02x:%02x:%02x:%02x", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
  165. printf("\r\nSSID:%s Bssid:%s Signal strength:%d DB\r\n", malloced_scan_result->ap_details.SSID.val, bssid,
  166. malloced_scan_result->ap_details.signal_strength);
  167. if (malloced_scan_result->scan_complete != 0) {
  168. printf("scan complete!\r\n");
  169. scan_comp_flag = 1;
  170. }
  171. return 0;
  172. }
  173. #endif
  174. static int InitializeNetwork( void )
  175. {
  176. BaseType_t return_code = pdTRUE;
  177. #if CONFIG_USB_DEVICE_CDC_NCM
  178. g_ncm_register("ncm");
  179. #endif
  180. return return_code;
  181. } /* End of function InitializeNetwork() */
  182. /***********************************************************************************************************************
  183. * End of file "NetworkInterface.c"
  184. **********************************************************************************************************************/
  185. static UBaseType_t ulNextRand;
  186. BaseType_t xTotalSuccess = 0;
  187. UBaseType_t uxRand( void )
  188. {
  189. const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
  190. ulNextRand = portGET_RUN_TIME_COUNTER_VALUE();
  191. ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
  192. return( ( int ) ( ulNextRand >> 16UL ) & 0x7fffUL );
  193. }
  194. extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,
  195. uint16_t usSourcePort,
  196. uint32_t ulDestinationAddress,
  197. uint16_t usDestinationPort )
  198. {
  199. ( void ) ulSourceAddress;
  200. ( void ) usSourcePort;
  201. ( void ) ulDestinationAddress;
  202. ( void ) usDestinationPort;
  203. return uxRand();
  204. }
  205. BaseType_t xApplicationGetRandomNumber( uint32_t * pulNumber )
  206. {
  207. *( pulNumber ) = uxRand();
  208. return pdTRUE;
  209. }
  210. #if 0
  211. void vApplicationPingReplyHook( ePingReplyStatus_t eStatus,
  212. uint16_t usIdentifier )
  213. {
  214. //if( eStatus == eSuccess )
  215. {
  216. FreeRTOS_printf( ( "Ping response received. ID: %d\r\n", usIdentifier ) );
  217. printf("Ping response received. ID: %d\r\n", usIdentifier);
  218. /* Increment successful ping replies. */
  219. xTotalSuccess++;
  220. }
  221. }
  222. #endif