PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/Zynq/NetworkInterface.c

https://gitlab.com/21mece13/FreeRTOS
C | 464 lines | 292 code | 78 blank | 94 comment | 59 complexity | 2ff77862b1c0a2283ba6919c88189f2c MD5 | raw file
  1. /*
  2. * FreeRTOS V202002.00
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://aws.amazon.com/freertos
  23. * http://www.FreeRTOS.org
  24. */
  25. /* Standard includes. */
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. /* FreeRTOS includes. */
  30. #include "FreeRTOS.h"
  31. #include "task.h"
  32. #include "queue.h"
  33. #include "semphr.h"
  34. /* FreeRTOS+TCP includes. */
  35. #include "FreeRTOS_IP.h"
  36. #include "FreeRTOS_Sockets.h"
  37. #include "FreeRTOS_IP_Private.h"
  38. #include "FreeRTOS_ARP.h"
  39. #include "NetworkBufferManagement.h"
  40. #include "NetworkInterface.h"
  41. /* Xilinx library files. */
  42. #include <xemacps.h>
  43. #include "Zynq/x_topology.h"
  44. #include "Zynq/x_emacpsif.h"
  45. #include "Zynq/x_emacpsif_hw.h"
  46. /* Provided memory configured as uncached. */
  47. #include "uncached_memory.h"
  48. #ifndef niEMAC_HANDLER_TASK_PRIORITY
  49. /* Define the priority of the task prvEMACHandlerTask(). */
  50. #define niEMAC_HANDLER_TASK_PRIORITY configMAX_PRIORITIES - 1
  51. #endif
  52. #define niBMSR_LINK_STATUS 0x0004uL
  53. #ifndef PHY_LS_HIGH_CHECK_TIME_MS
  54. /* Check if the LinkSStatus in the PHY is still high after 15 seconds of not
  55. receiving packets. */
  56. #define PHY_LS_HIGH_CHECK_TIME_MS 15000
  57. #endif
  58. #ifndef PHY_LS_LOW_CHECK_TIME_MS
  59. /* Check if the LinkSStatus in the PHY is still low every second. */
  60. #define PHY_LS_LOW_CHECK_TIME_MS 1000
  61. #endif
  62. /* The size of each buffer when BufferAllocation_1 is used:
  63. http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html */
  64. #define niBUFFER_1_PACKET_SIZE 1536
  65. /* Naming and numbering of PHY registers. */
  66. #define PHY_REG_01_BMSR 0x01 /* Basic mode status register */
  67. #ifndef iptraceEMAC_TASK_STARTING
  68. #define iptraceEMAC_TASK_STARTING() do { } while( 0 )
  69. #endif
  70. /* Default the size of the stack used by the EMAC deferred handler task to twice
  71. the size of the stack used by the idle task - but allow this to be overridden in
  72. FreeRTOSConfig.h as configMINIMAL_STACK_SIZE is a user definable constant. */
  73. #ifndef configEMAC_TASK_STACK_SIZE
  74. #define configEMAC_TASK_STACK_SIZE ( 2 * configMINIMAL_STACK_SIZE )
  75. #endif
  76. #if( ipconfigZERO_COPY_RX_DRIVER == 0 || ipconfigZERO_COPY_TX_DRIVER == 0 )
  77. #error Please define both 'ipconfigZERO_COPY_RX_DRIVER' and 'ipconfigZERO_COPY_TX_DRIVER' as 1
  78. #endif
  79. #if( ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM == 0 || ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
  80. #warning Please define both 'ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM' and 'ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM' as 1
  81. #endif
  82. /*-----------------------------------------------------------*/
  83. /*
  84. * Look for the link to be up every few milliseconds until either xMaxTime time
  85. * has passed or a link is found.
  86. */
  87. static BaseType_t prvGMACWaitLS( TickType_t xMaxTime );
  88. /*
  89. * A deferred interrupt handler for all MAC/DMA interrupt sources.
  90. */
  91. static void prvEMACHandlerTask( void *pvParameters );
  92. #if ( ipconfigHAS_PRINTF != 0 )
  93. static void prvMonitorResources( void );
  94. #endif
  95. /*-----------------------------------------------------------*/
  96. /* EMAC data/descriptions. */
  97. static xemacpsif_s xEMACpsif;
  98. struct xtopology_t xXTopology =
  99. {
  100. .emac_baseaddr = XPAR_PS7_ETHERNET_0_BASEADDR,
  101. .emac_type = xemac_type_emacps,
  102. .intc_baseaddr = 0x0,
  103. .intc_emac_intr = 0x0,
  104. .scugic_baseaddr = XPAR_PS7_SCUGIC_0_BASEADDR,
  105. .scugic_emac_intr = 0x36,
  106. };
  107. XEmacPs_Config mac_config =
  108. {
  109. .DeviceId = XPAR_PS7_ETHERNET_0_DEVICE_ID, /**< Unique ID of device */
  110. .BaseAddress = XPAR_PS7_ETHERNET_0_BASEADDR /**< Physical base address of IPIF registers */
  111. };
  112. extern int phy_detected;
  113. /* A copy of PHY register 1: 'PHY_REG_01_BMSR' */
  114. static uint32_t ulPHYLinkStatus = 0uL;
  115. #if( ipconfigUSE_LLMNR == 1 )
  116. static const uint8_t xLLMNR_MACAddress[] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0xFC };
  117. #endif
  118. /* ucMACAddress as it appears in main.c */
  119. extern const uint8_t ucMACAddress[ 6 ];
  120. /* Holds the handle of the task used as a deferred interrupt processor. The
  121. handle is used so direct notifications can be sent to the task for all EMAC/DMA
  122. related interrupts. */
  123. TaskHandle_t xEMACTaskHandle = NULL;
  124. /*-----------------------------------------------------------*/
  125. BaseType_t xNetworkInterfaceInitialise( void )
  126. {
  127. uint32_t ulLinkSpeed, ulDMAReg;
  128. BaseType_t xStatus, xLinkStatus;
  129. XEmacPs *pxEMAC_PS;
  130. const TickType_t xWaitLinkDelay = pdMS_TO_TICKS( 7000UL ), xWaitRelinkDelay = pdMS_TO_TICKS( 1000UL );
  131. /* Guard against the init function being called more than once. */
  132. if( xEMACTaskHandle == NULL )
  133. {
  134. pxEMAC_PS = &( xEMACpsif.emacps );
  135. memset( &xEMACpsif, '\0', sizeof( xEMACpsif ) );
  136. xStatus = XEmacPs_CfgInitialize( pxEMAC_PS, &mac_config, mac_config.BaseAddress);
  137. if( xStatus != XST_SUCCESS )
  138. {
  139. FreeRTOS_printf( ( "xEMACInit: EmacPs Configuration Failed....\n" ) );
  140. }
  141. /* Initialize the mac and set the MAC address. */
  142. XEmacPs_SetMacAddress( pxEMAC_PS, ( void * ) ucMACAddress, 1 );
  143. #if( ipconfigUSE_LLMNR == 1 )
  144. {
  145. /* Also add LLMNR multicast MAC address. */
  146. XEmacPs_SetMacAddress( pxEMAC_PS, ( void * )xLLMNR_MACAddress, 2 );
  147. }
  148. #endif /* ipconfigUSE_LLMNR == 1 */
  149. XEmacPs_SetMdioDivisor( pxEMAC_PS, MDC_DIV_224 );
  150. ulLinkSpeed = Phy_Setup( pxEMAC_PS );
  151. XEmacPs_SetOperatingSpeed( pxEMAC_PS, ulLinkSpeed);
  152. /* Setting the operating speed of the MAC needs a delay. */
  153. vTaskDelay( pdMS_TO_TICKS( 25UL ) );
  154. ulDMAReg = XEmacPs_ReadReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET);
  155. /* DISC_WHEN_NO_AHB: when set, the GEM DMA will automatically discard receive
  156. packets from the receiver packet buffer memory when no AHB resource is available. */
  157. XEmacPs_WriteReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET,
  158. ulDMAReg | XEMACPS_DMACR_DISC_WHEN_NO_AHB_MASK);
  159. setup_isr( &xEMACpsif );
  160. init_dma( &xEMACpsif );
  161. start_emacps( &xEMACpsif );
  162. prvGMACWaitLS( xWaitLinkDelay );
  163. /* The deferred interrupt handler task is created at the highest
  164. possible priority to ensure the interrupt handler can return directly
  165. to it. The task's handle is stored in xEMACTaskHandle so interrupts can
  166. notify the task when there is something to process. */
  167. xTaskCreate( prvEMACHandlerTask, "EMAC", configEMAC_TASK_STACK_SIZE, NULL, niEMAC_HANDLER_TASK_PRIORITY, &xEMACTaskHandle );
  168. }
  169. else
  170. {
  171. /* Initialisation was already performed, just wait for the link. */
  172. prvGMACWaitLS( xWaitRelinkDelay );
  173. }
  174. /* Only return pdTRUE when the Link Status of the PHY is high, otherwise the
  175. DHCP process and all other communication will fail. */
  176. xLinkStatus = xGetPhyLinkStatus();
  177. return ( xLinkStatus != pdFALSE );
  178. }
  179. /*-----------------------------------------------------------*/
  180. BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxBuffer, BaseType_t bReleaseAfterSend )
  181. {
  182. #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM != 0 )
  183. {
  184. ProtocolPacket_t *pxPacket;
  185. /* If the peripheral must calculate the checksum, it wants
  186. the protocol checksum to have a value of zero. */
  187. pxPacket = ( ProtocolPacket_t * ) ( pxBuffer->pucEthernetBuffer );
  188. if( ( pxPacket->xICMPPacket.xIPHeader.ucProtocol != ipPROTOCOL_UDP ) &&
  189. ( pxPacket->xICMPPacket.xIPHeader.ucProtocol != ipPROTOCOL_TCP ) )
  190. {
  191. /* The EMAC will calculate the checksum of the IP-header.
  192. It can only calculate protocol checksums of UDP and TCP,
  193. so for ICMP and other protocols it must be done manually. */
  194. usGenerateProtocolChecksum( (uint8_t*)&( pxPacket->xUDPPacket ), pxBuffer->xDataLength, pdTRUE );
  195. }
  196. }
  197. #endif /* ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM */
  198. if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0UL )
  199. {
  200. iptraceNETWORK_INTERFACE_TRANSMIT();
  201. emacps_send_message( &xEMACpsif, pxBuffer, bReleaseAfterSend );
  202. }
  203. else if( bReleaseAfterSend != pdFALSE )
  204. {
  205. /* No link. */
  206. vReleaseNetworkBufferAndDescriptor( pxBuffer );
  207. }
  208. return pdTRUE;
  209. }
  210. /*-----------------------------------------------------------*/
  211. static inline unsigned long ulReadMDIO( unsigned ulRegister )
  212. {
  213. uint16_t usValue;
  214. XEmacPs_PhyRead( &( xEMACpsif.emacps ), phy_detected, ulRegister, &usValue );
  215. return usValue;
  216. }
  217. /*-----------------------------------------------------------*/
  218. static BaseType_t prvGMACWaitLS( TickType_t xMaxTime )
  219. {
  220. TickType_t xStartTime, xEndTime;
  221. const TickType_t xShortDelay = pdMS_TO_TICKS( 20UL );
  222. BaseType_t xReturn;
  223. xStartTime = xTaskGetTickCount();
  224. for( ;; )
  225. {
  226. xEndTime = xTaskGetTickCount();
  227. if( xEndTime - xStartTime > xMaxTime )
  228. {
  229. xReturn = pdFALSE;
  230. break;
  231. }
  232. ulPHYLinkStatus = ulReadMDIO( PHY_REG_01_BMSR );
  233. if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0uL )
  234. {
  235. xReturn = pdTRUE;
  236. break;
  237. }
  238. vTaskDelay( xShortDelay );
  239. }
  240. return xReturn;
  241. }
  242. /*-----------------------------------------------------------*/
  243. void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )
  244. {
  245. static uint8_t ucNetworkPackets[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS * niBUFFER_1_PACKET_SIZE ] __attribute__ ( ( aligned( 32 ) ) );
  246. uint8_t *ucRAMBuffer = ucNetworkPackets;
  247. uint32_t ul;
  248. for( ul = 0; ul < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; ul++ )
  249. {
  250. pxNetworkBuffers[ ul ].pucEthernetBuffer = ucRAMBuffer + ipBUFFER_PADDING;
  251. *( ( unsigned * ) ucRAMBuffer ) = ( unsigned ) ( &( pxNetworkBuffers[ ul ] ) );
  252. ucRAMBuffer += niBUFFER_1_PACKET_SIZE;
  253. }
  254. }
  255. /*-----------------------------------------------------------*/
  256. BaseType_t xGetPhyLinkStatus( void )
  257. {
  258. BaseType_t xReturn;
  259. if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) == 0uL )
  260. {
  261. xReturn = pdFALSE;
  262. }
  263. else
  264. {
  265. xReturn = pdTRUE;
  266. }
  267. return xReturn;
  268. }
  269. /*-----------------------------------------------------------*/
  270. #if ( ipconfigHAS_PRINTF != 0 )
  271. static void prvMonitorResources()
  272. {
  273. static UBaseType_t uxLastMinBufferCount = 0u;
  274. static size_t uxMinLastSize = 0uL;
  275. UBaseType_t uxCurrentBufferCount;
  276. size_t uxMinSize;
  277. uxCurrentBufferCount = uxGetMinimumFreeNetworkBuffers();
  278. if( uxLastMinBufferCount != uxCurrentBufferCount )
  279. {
  280. /* The logging produced below may be helpful
  281. * while tuning +TCP: see how many buffers are in use. */
  282. uxLastMinBufferCount = uxCurrentBufferCount;
  283. FreeRTOS_printf( ( "Network buffers: %lu lowest %lu\n",
  284. uxGetNumberOfFreeNetworkBuffers(),
  285. uxCurrentBufferCount ) );
  286. }
  287. uxMinSize = xPortGetMinimumEverFreeHeapSize();
  288. if( uxMinLastSize != uxMinSize )
  289. {
  290. uxMinLastSize = uxMinSize;
  291. FreeRTOS_printf( ( "Heap: current %lu lowest %lu\n", xPortGetFreeHeapSize(), uxMinSize ) );
  292. }
  293. #if ( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
  294. {
  295. static UBaseType_t uxLastMinQueueSpace = 0;
  296. UBaseType_t uxCurrentCount = 0u;
  297. uxCurrentCount = uxGetMinimumIPQueueSpace();
  298. if( uxLastMinQueueSpace != uxCurrentCount )
  299. {
  300. /* The logging produced below may be helpful
  301. * while tuning +TCP: see how many buffers are in use. */
  302. uxLastMinQueueSpace = uxCurrentCount;
  303. FreeRTOS_printf( ( "Queue space: lowest %lu\n", uxCurrentCount ) );
  304. }
  305. }
  306. #endif /* ipconfigCHECK_IP_QUEUE_SPACE */
  307. }
  308. #endif /* ( ipconfigHAS_PRINTF != 0 ) */
  309. /*-----------------------------------------------------------*/
  310. static void prvEMACHandlerTask( void *pvParameters )
  311. {
  312. TimeOut_t xPhyTime;
  313. TickType_t xPhyRemTime;
  314. BaseType_t xResult = 0;
  315. uint32_t xStatus;
  316. const TickType_t ulMaxBlockTime = pdMS_TO_TICKS( 100UL );
  317. /* Remove compiler warnings about unused parameters. */
  318. ( void ) pvParameters;
  319. /* A possibility to set some additional task properties like calling
  320. portTASK_USES_FLOATING_POINT() */
  321. iptraceEMAC_TASK_STARTING();
  322. vTaskSetTimeOutState( &xPhyTime );
  323. xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
  324. for( ;; )
  325. {
  326. #if ( ipconfigHAS_PRINTF != 0 )
  327. {
  328. prvMonitorResources();
  329. }
  330. #endif /* ipconfigHAS_PRINTF != 0 ) */
  331. if( ( xEMACpsif.isr_events & EMAC_IF_ALL_EVENT ) == 0 )
  332. {
  333. /* No events to process now, wait for the next. */
  334. ulTaskNotifyTake( pdFALSE, ulMaxBlockTime );
  335. }
  336. if( ( xEMACpsif.isr_events & EMAC_IF_RX_EVENT ) != 0 )
  337. {
  338. xEMACpsif.isr_events &= ~EMAC_IF_RX_EVENT;
  339. xResult = emacps_check_rx( &xEMACpsif );
  340. }
  341. if( ( xEMACpsif.isr_events & EMAC_IF_TX_EVENT ) != 0 )
  342. {
  343. xEMACpsif.isr_events &= ~EMAC_IF_TX_EVENT;
  344. emacps_check_tx( &xEMACpsif );
  345. }
  346. if( ( xEMACpsif.isr_events & EMAC_IF_ERR_EVENT ) != 0 )
  347. {
  348. xEMACpsif.isr_events &= ~EMAC_IF_ERR_EVENT;
  349. emacps_check_errors( &xEMACpsif );
  350. }
  351. if( xResult > 0 )
  352. {
  353. /* A packet was received. No need to check for the PHY status now,
  354. but set a timer to check it later on. */
  355. vTaskSetTimeOutState( &xPhyTime );
  356. xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
  357. xResult = 0;
  358. if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) == 0uL )
  359. {
  360. /* Indicate that the Link Status is high, so that
  361. xNetworkInterfaceOutput() can send packets. */
  362. ulPHYLinkStatus |= niBMSR_LINK_STATUS;
  363. FreeRTOS_printf( ( "prvEMACHandlerTask: PHY LS assume 1\n" ) );
  364. }
  365. }
  366. else if( xTaskCheckForTimeOut( &xPhyTime, &xPhyRemTime ) != pdFALSE )
  367. {
  368. xStatus = ulReadMDIO( PHY_REG_01_BMSR );
  369. if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != ( xStatus & niBMSR_LINK_STATUS ) )
  370. {
  371. ulPHYLinkStatus = xStatus;
  372. FreeRTOS_printf( ( "prvEMACHandlerTask: PHY LS now %d\n", ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0uL ) );
  373. }
  374. vTaskSetTimeOutState( &xPhyTime );
  375. if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0uL )
  376. {
  377. xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
  378. }
  379. else
  380. {
  381. xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
  382. }
  383. }
  384. }
  385. }
  386. /*-----------------------------------------------------------*/