1 /*******************************************************************************
2 * Network Interface file
5 * Network Interface file for FreeRTOS-Plus-TCP stack
8 * - Interfaces PIC32 to the FreeRTOS TCP/IP stack
9 *******************************************************************************/
11 /*******************************************************************************
12 * File Name: pic32_NetworkInterface.c
13 * Copyright 2017 Microchip Technology Incorporated and its subsidiaries.
15 * Permission is hereby granted, free of charge, to any person obtaining a copy of
16 * this software and associated documentation files (the "Software"), to deal in
17 * the Software without restriction, including without limitation the rights to
18 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
19 * of the Software, and to permit persons to whom the Software is furnished to do
20 * so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in all
22 * copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 *******************************************************************************/
36 #include "event_groups.h"
37 #include "FreeRTOS_IP.h"
38 #include "FreeRTOS_IP_Private.h"
40 #include "NetworkInterface.h"
41 #include "NetworkBufferManagement.h"
44 #include "NetworkInterface.h"
45 #include "NetworkConfig.h"
47 #include "peripheral/eth/plib_eth.h"
49 #include "system_config.h"
50 #include "system/console/sys_console.h"
51 #include "system/debug/sys_debug.h"
52 #include "system/command/sys_command.h"
54 #include "driver/ethmac/drv_ethmac.h"
55 #include "driver/miim/drv_miim.h"
57 #include "tcpip/tcpip.h"
58 #include "tcpip/src/tcpip_private.h"
59 #include "tcpip/src/link_list.h"
61 #ifdef PIC32_USE_ETHERNET
63 /* local definitions and data */
66 #if ( PIC32_MAC_DEBUG_MESSAGES != 0 )
67 #define PIC32_MAC_DbgPrint( format, ... ) SYS_CONSOLE_PRINT( format, ## __VA_ARGS__ )
69 #define PIC32_MAC_DbgPrint( format, ... )
70 #endif /* (PIC32_MAC_DEBUG_MESSAGES != 0) */
74 PIC32_MAC_EVENT_INIT_NONE = 0x000, /* no event/invalid */
76 PIC32_MAC_EVENT_INIT_DONE = 0x001, /* initialization done event */
77 PIC32_MAC_EVENT_TIMEOUT = 0x002, /* periodic timeout event */
78 PIC32_MAC_EVENT_IF_PENDING = 0x004, /* an interface event signal: RX, TX, errors. etc. */
79 } PIC32_MAC_EVENT_TYPE;
83 eMACInit, /* Must initialise MAC. */
84 eMACPass, /* Initialisation was successful. */
85 eMACFailed, /* Initialisation failed. */
86 } eMAC_INIT_STATUS_TYPE;
88 static TCPIP_STACK_HEAP_HANDLE macHeapHandle;
90 static const TCPIP_MAC_OBJECT * macObject; /* the one and only MAC object; */
92 static SYS_MODULE_OBJ macObjHandle; /* the MAC object instance, obtained at initialization */
93 static TCPIP_MAC_HANDLE macCliHandle; /* client handle */
94 static volatile SYS_STATUS macObjStatus; /* current MAC status */
96 static TaskHandle_t macTaskHandle;
98 static TimerHandle_t macTmrHandle;
100 static bool macLinkStatus; /* true if link is ON */
102 static eMAC_INIT_STATUS_TYPE xMacInitStatus = eMACInit;
104 /* local prototypes */
105 static bool StartInitMac( void );
106 static void StartInitCleanup( void );
108 static void SetMacCtrl( TCPIP_MAC_MODULE_CTRL * pMacCtrl );
110 static bool MacSyncFunction( void * synchHandle,
111 TCPIP_MAC_SYNCH_REQUEST req );
113 /* the PIC32 MAC task function */
114 static void MacHandlerTask( void * params );
116 /* MAC interrupt event function */
117 static void MAC_EventFunction( TCPIP_MAC_EVENT event,
118 const void * eventParam );
120 /* timer callback for link maintenance, etc; */
121 static void MacTmrCallback( TimerHandle_t xTimer );
123 /* MAC RX packets functions */
124 static void MacRxPackets( void );
125 static void MacProcessRxPacket( TCPIP_MAC_PACKET * pRxPkt );
128 /* memory allocation mapping to FreeRTOS */
129 static void * _malloc( size_t nBytes )
131 return pvPortMalloc( nBytes );
134 /*-----------------------------------------------------------*/
136 static void * _calloc( size_t nElems,
139 size_t nBytes = nElems * elemSize;
141 void * ptr = pvPortMalloc( nBytes );
145 memset( ptr, 0, nBytes );
151 /*-----------------------------------------------------------*/
153 static void _free( void * pBuff )
158 /* extern references */
160 /* use the configuration data from the system_init.c */
161 extern const TCPIP_NETWORK_CONFIG TCPIP_HOSTS_CONFIGURATION[];
163 /* BufferAllocation_2.c:: packet allocation function */
164 extern TCPIP_MAC_PACKET * PIC32_MacPacketAllocate( uint16_t pktLen,
166 TCPIP_MAC_PACKET_FLAGS flags );
168 extern void PIC32_MacAssociate( TCPIP_MAC_PACKET * pRxPkt,
169 NetworkBufferDescriptor_t * pxBufferDescriptor,
171 extern void PIC32_MacPacketOrphan( TCPIP_MAC_PACKET * pPkt );
173 /* cannot use the system_init.c::tcpipHeapConfig because FreeRTOS does not have a calloc function! */
174 /* we build it here! */
176 /* make sure we're running with external heap! Redirect to FreeRTOS. */
177 #if !defined( TCPIP_STACK_USE_EXTERNAL_HEAP ) || defined( TCPIP_STACK_USE_INTERNAL_HEAP ) || defined( TCPIP_STACK_USE_INTERNAL_HEAP_POOL )
178 #error "TCPIP_STACK_USE_EXTERNAL_HEAP should be defined for this project!"
181 static const TCPIP_STACK_HEAP_EXTERNAL_CONFIG tcpipHeapConfig =
183 .heapType = TCPIP_STACK_HEAP_TYPE_EXTERNAL_HEAP,
184 .heapFlags = TCPIP_STACK_HEAP_FLAG_ALLOC_UNCACHED | TCPIP_STACK_HEAP_FLAG_NO_MTHREAD_SYNC,
185 .heapUsage = TCPIP_STACK_HEAP_USE_DEFAULT,
186 .malloc_fnc = _malloc,
187 .calloc_fnc = _calloc,
191 #if ( PIC32_MAC_DEBUG_COMMANDS != 0 )
192 static int _Command_MacInfo( SYS_CMD_DEVICE_NODE * pCmdIO,
195 static int _Command_NetInfo( SYS_CMD_DEVICE_NODE * pCmdIO,
198 static int _Command_Version( SYS_CMD_DEVICE_NODE * pCmdIO,
202 static const SYS_CMD_DESCRIPTOR macCmdTbl[] =
204 { "macinfo", _Command_MacInfo, ": Check MAC statistics" },
205 { "netinfo", _Command_NetInfo, ": Net info" },
206 { "version", _Command_Version, ": Version info" },
208 #endif /* (PIC32_MAC_DEBUG_COMMANDS != 0) */
211 /* FreeRTOS implementation functions */
212 BaseType_t xNetworkInterfaceInitialise( void )
216 if( xMacInitStatus == eMACInit )
218 /* This is the first time this function is called. */
219 if( StartInitMac() != false )
221 /* Indicate that the MAC initialisation succeeded. */
222 xMacInitStatus = eMACPass;
226 xMacInitStatus = eMACFailed;
230 if( xMacInitStatus == eMACPass )
232 xResult = xGetPhyLinkStatus();
239 PIC32_MAC_DbgPrint( "xNetworkInterfaceInitialise: %d %d\r\n", ( int ) xMacInitStatus, ( int ) xResult );
245 /*-----------------------------------------------------------*/
247 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxDescriptor,
248 BaseType_t xReleaseAfterSend )
250 TCPIP_MAC_RES macRes;
251 TCPIP_MAC_PACKET * pTxPkt;
253 BaseType_t retRes = pdFALSE;
256 if( ( pxDescriptor != 0 ) && ( pxDescriptor->pucEthernetBuffer != 0 ) && ( pxDescriptor->xDataLength != 0 ) )
258 TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pxDescriptor->pucEthernetBuffer - PIC32_BUFFER_PKT_PTR_OSSET );
259 configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );
261 configASSERT( pTxPkt != 0 );
263 /* prepare the packet for transmission */
264 /* set the correct data length: */
265 configASSERT( pTxPkt->pDSeg->segSize >= pTxPkt->pDSeg->segLen );
266 pTxPkt->pDSeg->segLen = pxDescriptor->xDataLength;
267 pTxPkt->next = 0; /* unlink it */
268 macRes = ( macObject->TCPIP_MAC_PacketTx )( macCliHandle, pTxPkt );
273 pxDescriptor->pucEthernetBuffer = 0; /* it will be released by the MAC driver once it's transmitted */
274 iptraceNETWORK_INTERFACE_TRANSMIT();
277 /* else same error occurred; this normally should not happen! But the buffer is left in there so it shold be freed! */
279 /* The buffer has been sent so can be released. */
280 if( xReleaseAfterSend != pdFALSE )
282 vReleaseNetworkBufferAndDescriptor( pxDescriptor );
290 /************************************* Section: helper functions ************************************************** */
293 void PIC32_GetMACAddress( uint8_t macAdd[ 6 ] )
295 #if defined( __PIC32MZ__ ) || defined( __PIC32MX__ )
296 PLIB_ETH_MACGetAddress( ETH_ID_0, macAdd );
298 #error "MAC Address: not supported architecture!"
303 /*-----------------------------------------------------------*/
305 const void * const PIC32_GetMacConfigData( void )
307 #if defined( __PIC32MZ__ ) || defined( __PIC32MX__ )
308 extern const TCPIP_MODULE_MAC_PIC32INT_CONFIG tcpipMACPIC32INTInitData;
310 return &tcpipMACPIC32INTInitData;
312 #error "MAC Address: not supported architecture!"
316 /************************************* Section: worker code ************************************************** */
320 static bool StartInitMac( void )
322 TCPIP_MAC_MODULE_CTRL macCtrl;
323 SYS_MODULE_INIT moduleInit;
327 /* perform some initialization of all variables so that we can cleanup what failed */
328 /* if something failed, the routine will be called again and again by FreeRTOS! */
334 macObject = TCPIP_HOSTS_CONFIGURATION[ 0 ].pMacObject; /* the MAC object we use */
335 macObjStatus = SYS_STATUS_UNINITIALIZED;
336 macLinkStatus = false;
342 /* start the allocator */
343 macHeapHandle = TCPIP_HEAP_Create( ( const TCPIP_STACK_HEAP_CONFIG * ) &tcpipHeapConfig, 0 );
345 if( macHeapHandle == 0 )
351 if( TCPIP_PKT_Initialize( macHeapHandle, 0, 0 ) == false )
357 moduleInit.sys.powerState = SYS_MODULE_POWER_RUN_FULL;
359 /* Initialize the MAC. MAC address is defined to 0x000000000000 in
360 * FreeRTOSConfig.h and therefore it will be initialized to the
361 * factory programmed MAC address. */
362 SetMacCtrl( &macCtrl );
363 /* Set the mac address in the FreeRTOS+TCP stack. */
364 FreeRTOS_UpdateMACAddress( macCtrl.ifPhyAddress.v );
366 TCPIP_MAC_INIT macInit =
368 .moduleInit = { moduleInit.value },
369 .macControl = &macCtrl,
370 .moduleData = PIC32_GetMacConfigData(),
373 macObjHandle = ( macObject->TCPIP_MAC_Initialize )( TCPIP_MODULE_MAC_PIC32INT, &macInit.moduleInit );
375 if( macObjHandle == SYS_MODULE_OBJ_INVALID )
383 macCliHandle = ( macObject->TCPIP_MAC_Open )( TCPIP_MODULE_MAC_PIC32INT, DRV_IO_INTENT_READWRITE );
385 if( macCliHandle == DRV_HANDLE_INVALID )
392 if( !( macObject->TCPIP_MAC_EventMaskSet )( macCliHandle, ( TCPIP_MAC_EV_RX_DONE | TCPIP_MAC_EV_TX_DONE | TCPIP_MAC_EV_RXTX_ERRORS ), true ) )
398 /* completed the MAC initialization */
399 /* continue the initialization */
400 macTmrHandle = xTimerCreate( PIC32_MAC_TIMER_NAME, PIC32_MAC_TIMER_PERIOD, pdTRUE, 0, MacTmrCallback );
402 if( ( macTmrHandle == 0 ) || ( xTimerStart( macTmrHandle, 0 ) != pdPASS ) )
408 /* spawn the PIC32 MAC task function */
409 /* and wait for its event signal */
410 macObjStatus = SYS_STATUS_BUSY;
412 if( xTaskCreate( MacHandlerTask, PIC32_MAC_TASK_NAME, PIC32_MAC_TASK_STACK_SIZE, xTaskGetCurrentTaskHandle(), PIC32_MAC_TASK_PRI, &macTaskHandle ) != pdPASS )
418 xTaskNotifyWait( PIC32_MAC_EVENT_INIT_DONE, PIC32_MAC_EVENT_INIT_DONE, &evBits, PIC32_MAC_INIT_TIMEOUT );
420 if( ( evBits & PIC32_MAC_EVENT_INIT_DONE ) == 0 )
426 if( macObjStatus != SYS_STATUS_READY )
427 { /* failed somehow ??? */
438 PIC32_MAC_DbgPrint( " MAC Init success!\r\n" );
440 #if ( PIC32_MAC_DEBUG_COMMANDS != 0 )
441 /* create command group */
442 if( !SYS_CMD_ADDGRP( macCmdTbl, sizeof( macCmdTbl ) / sizeof( *macCmdTbl ), "mac", ": mac commands" ) )
444 PIC32_MAC_DbgPrint( "Failed to create MAC Commands\r\n" );
446 #endif /* (PIC32_MAC_DEBUG_COMMANDS != 0) */
453 PIC32_MAC_DbgPrint( "MAC Init failed: %d!\r\n", netUpFail );
459 /*-----------------------------------------------------------*/
461 static void StartInitCleanup( void )
463 if( macHeapHandle != 0 )
465 TCPIP_HEAP_Delete( macHeapHandle );
469 if( macObjHandle != 0 )
471 ( macObject->TCPIP_MAC_Deinitialize )( macObjHandle );
475 if( macTmrHandle != 0 )
477 xTimerDelete( macTmrHandle, portMAX_DELAY );
481 if( macTaskHandle != 0 )
483 vTaskDelete( macTaskHandle );
488 /*-----------------------------------------------------------*/
490 static void SetMacCtrl( TCPIP_MAC_MODULE_CTRL * pMacCtrl )
492 TCPIP_MAC_ADDR macAdd;
493 uint8_t unsetMACAddr[ 6 ] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; /* not set MAC address */
497 pMacCtrl->mallocF = TCPIP_HEAP_MallocOutline;
498 pMacCtrl->callocF = TCPIP_HEAP_CallocOutline;
499 pMacCtrl->freeF = TCPIP_HEAP_FreeOutline;
500 pMacCtrl->memH = macHeapHandle;
503 pMacCtrl->pktAllocF = PIC32_MacPacketAllocate;
504 pMacCtrl->pktFreeF = ( TCPIP_MAC_PKT_FreeF ) _TCPIP_PKT_FREE_FNC;
505 pMacCtrl->pktAckF = ( TCPIP_MAC_PKT_AckF ) _TCPIP_PKT_ACK_FNC;
507 pMacCtrl->synchF = MacSyncFunction;
509 pMacCtrl->eventF = MAC_EventFunction;
510 pMacCtrl->eventParam = 0;
512 pMacCtrl->moduleId = TCPIP_MODULE_MAC_PIC32INT;
514 pMacCtrl->macAction = TCPIP_MAC_ACTION_INIT;
515 pMacCtrl->powerMode = TCPIP_MAC_POWER_FULL;
517 macAdd.v[ 0 ] = configMAC_ADDR0;
518 macAdd.v[ 1 ] = configMAC_ADDR1;
519 macAdd.v[ 2 ] = configMAC_ADDR2;
520 macAdd.v[ 3 ] = configMAC_ADDR3;
521 macAdd.v[ 4 ] = configMAC_ADDR4;
522 macAdd.v[ 5 ] = configMAC_ADDR5;
524 if( memcmp( macAdd.v, unsetMACAddr, sizeof( unsetMACAddr ) ) == 0 )
525 { /* if unspecified we use the factory pre-programmed address */
526 PIC32_GetMACAddress( pMacCtrl->ifPhyAddress.v );
529 { /* use the config suggested one */
530 memcpy( pMacCtrl->ifPhyAddress.v, macAdd.v, sizeof( macAdd ) );
534 /*-----------------------------------------------------------*/
536 static bool MacSyncFunction( void * synchHandle,
537 TCPIP_MAC_SYNCH_REQUEST req )
541 case TCPIP_MAC_SYNCH_REQUEST_OBJ_CREATE:
542 vSemaphoreCreateBinary( *( SemaphoreHandle_t * ) synchHandle );
544 return ( *( SemaphoreHandle_t * ) synchHandle == NULL ) ? false : true;
546 case TCPIP_MAC_SYNCH_REQUEST_OBJ_DELETE:
547 vSemaphoreDelete( *( SemaphoreHandle_t * ) synchHandle );
548 *( SemaphoreHandle_t * ) synchHandle = NULL;
552 case TCPIP_MAC_SYNCH_REQUEST_OBJ_LOCK:
554 return ( xSemaphoreTake( *( SemaphoreHandle_t * ) synchHandle, portMAX_DELAY ) == pdTRUE ) ? true : false;
556 case TCPIP_MAC_SYNCH_REQUEST_OBJ_UNLOCK:
558 return ( xSemaphoreGive( *( SemaphoreHandle_t * ) synchHandle ) == pdTRUE ) ? true : false;
560 case TCPIP_MAC_SYNCH_REQUEST_CRIT_ENTER:
565 case TCPIP_MAC_SYNCH_REQUEST_CRIT_LEAVE:
577 /*-----------------------------------------------------------*/
579 static void MacHandlerTask( void * params )
583 /* perform the MAC initialization */
584 while( macObjStatus == SYS_STATUS_BUSY )
586 /* process the underlying MAC module tasks */
587 ( macObject->TCPIP_MAC_Tasks )( macObjHandle );
589 SYS_STATUS macStatus = ( macObject->TCPIP_MAC_Status )( macObjHandle );
591 if( macStatus == SYS_STATUS_BUSY )
592 { /* still pending */
593 vTaskDelay( PIC32_MAC_TASK_INIT_PENDING_DELAY );
596 { /* completed ...somehow */
597 macObjStatus = macStatus;
599 xTaskNotify( ( TaskHandle_t ) params, PIC32_MAC_EVENT_INIT_DONE, eSetBits );
601 if( macStatus != SYS_STATUS_READY )
602 { /* failed miserably */
606 /* done, up and running */
612 xTaskNotifyWait( PIC32_MAC_EVENT_TIMEOUT | PIC32_MAC_EVENT_IF_PENDING, PIC32_MAC_EVENT_TIMEOUT | PIC32_MAC_EVENT_IF_PENDING, &evBits, portMAX_DELAY );
614 if( ( evBits & PIC32_MAC_EVENT_TIMEOUT ) != 0 )
615 { /* timeout occurred... */
616 ( macObject->TCPIP_MAC_Tasks )( macObjHandle );
617 bool linkCurr = ( macObject->TCPIP_MAC_LinkCheck )( macCliHandle ); /* check link status */
619 if( macLinkStatus != linkCurr )
620 { /* link status changed; some event could ve fired here if needed */
621 PIC32_MAC_DbgPrint( " MAC link: %s!\r\n", linkCurr ? "ON" : "OFF" );
622 macLinkStatus = linkCurr;
626 if( ( evBits & PIC32_MAC_EVENT_IF_PENDING ) != 0 )
627 { /* IF events signal */
628 TCPIP_MAC_EVENT activeEvents = ( macObject->TCPIP_MAC_EventPendingGet )( macCliHandle );
630 if( activeEvents != TCPIP_MAC_EV_NONE )
632 /* acknowledge the events */
633 ( macObject->TCPIP_MAC_EventAcknowledge )( macCliHandle, activeEvents );
636 if( ( activeEvents & ( TCPIP_MAC_EV_RX_DONE | TCPIP_MAC_EV_RX_OVFLOW | TCPIP_MAC_EV_RX_BUFNA ) ) != 0 )
637 { /* RX packets available */
641 /* call the driver process function; */
642 /* PIC32 driver requests it through TCPIP_MAC_ParametersGet() which is bypassed here! */
643 ( macObject->TCPIP_MAC_Process )( macCliHandle );
647 /* do what you have to do and then wait for another event... */
651 /*-----------------------------------------------------------*/
653 static void MacTmrCallback( TimerHandle_t xTimer )
655 xTaskNotify( macTaskHandle, PIC32_MAC_EVENT_TIMEOUT, eSetBits );
658 /* MAC interrupt event function */
659 /* MAC signals an event, probably from within ISR */
660 /* we care just for RX related events */
661 static void MAC_EventFunction( TCPIP_MAC_EVENT event,
662 const void * eventParam )
664 BaseType_t xHigherPriorityTaskWoken;
666 if( ( event & ( TCPIP_MAC_EV_RX_DONE | TCPIP_MAC_EV_TX_DONE | TCPIP_MAC_EV_RXTX_ERRORS ) ) != 0 )
668 xHigherPriorityTaskWoken = pdFALSE;
669 xTaskNotifyFromISR( macTaskHandle, PIC32_MAC_EVENT_IF_PENDING, eSetBits, &xHigherPriorityTaskWoken );
671 if( xHigherPriorityTaskWoken )
673 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
678 /*-----------------------------------------------------------*/
680 BaseType_t xGetPhyLinkStatus( void )
682 return macLinkStatus == true ? pdPASS : pdFAIL;
686 /* receive packets from the MAC driver */
687 static void MacRxPackets( void )
689 TCPIP_MAC_PACKET * pRxPkt;
691 /* get all the new MAC packets */
692 while( ( pRxPkt = ( macObject->TCPIP_MAC_PacketRx )( macCliHandle, 0, 0 ) ) != 0 )
694 MacProcessRxPacket( pRxPkt );
698 /*-----------------------------------------------------------*/
700 static void MacProcessRxPacket( TCPIP_MAC_PACKET * pRxPkt )
702 bool pktSuccess, pktLost;
704 TCPIP_MAC_DATA_SEGMENT * pSeg;
706 NetworkBufferDescriptor_t * pxBufferDescriptor;
707 IPStackEvent_t xRxEvent;
709 pxBufferDescriptor = 0;
710 pktSuccess = pktLost = false;
716 pSeg = pRxPkt->pDSeg;
717 pPktBuff = pSeg->segLoad;
719 /* calculate the packet size */
722 pktLength += pSeg->segLen;
725 } while( pSeg != 0 );
728 { /* no support in FreeRTOS for multi segment packets! */
732 /* sizeof(TCPIP_MAC_ETHERNET_HEADER) is subtracted by the driver */
733 /* but FreeRTOS needs the whole frame! */
734 pktLength += sizeof( TCPIP_MAC_ETHERNET_HEADER );
736 if( eConsiderFrameForProcessing( pPktBuff ) != eProcessBuffer )
741 /* get the network descriptor (no data buffer) to hold this packet */
742 pxBufferDescriptor = pxGetNetworkBufferWithDescriptor( 0, 0 );
744 if( pxBufferDescriptor == 0 )
750 PIC32_MacAssociate( pRxPkt, pxBufferDescriptor, pktLength );
752 xRxEvent.eEventType = eNetworkRxEvent;
753 xRxEvent.pvData = ( void * ) pxBufferDescriptor;
755 /* Send the data to the TCP/IP stack */
756 if( xSendEventStructToIPTask( &xRxEvent, 0 ) == pdFALSE )
763 iptraceNETWORK_INTERFACE_RECEIVE();
770 { /* smth went wrong; nothing sent to the */
771 if( pxBufferDescriptor != 0 )
773 pxBufferDescriptor->pucEthernetBuffer = 0;
774 vReleaseNetworkBufferAndDescriptor( pxBufferDescriptor );
779 iptraceETHERNET_RX_EVENT_LOST();
782 /* acknowledge the packet to the MAC driver */
783 if( pRxPkt->ackFunc )
785 ( *pRxPkt->ackFunc )( pRxPkt, pRxPkt->ackParam );
789 PIC32_MacPacketOrphan( pRxPkt );
794 #if ( PIC32_MAC_DEBUG_COMMANDS != 0 )
796 static int _Command_MacInfo( SYS_CMD_DEVICE_NODE * pCmdIO,
800 TCPIP_MAC_RES macRes;
801 TCPIP_MAC_RX_STATISTICS rxStatistics;
802 TCPIP_MAC_TX_STATISTICS txStatistics;
803 TCPIP_MAC_STATISTICS_REG_ENTRY regEntries[ 8 ];
804 TCPIP_MAC_STATISTICS_REG_ENTRY * pRegEntry;
806 char entryName[ sizeof( pRegEntry->registerName ) + 1 ];
808 const void * cmdIoParam = pCmdIO->cmdIoParam;
812 ( *pCmdIO->pCmdApi->msg )( cmdIoParam, "Usage: macinfo \r\n" );
813 ( *pCmdIO->pCmdApi->msg )( cmdIoParam, "Ex: macinfo \r\n" );
818 ( *pCmdIO->pCmdApi->print )( cmdIoParam, "Interface: %s driver statistics\r\n", macObject->macName );
819 macRes = ( macObject->TCPIP_MAC_StatisticsGet )( macCliHandle, &rxStatistics, &txStatistics );
821 if( macRes == TCPIP_MAC_RES_OK )
823 ( *pCmdIO->pCmdApi->print )( cmdIoParam, "\tnRxOkPackets: %d, nRxPendBuffers: %d, nRxSchedBuffers: %d, ",
824 rxStatistics.nRxOkPackets, rxStatistics.nRxPendBuffers, rxStatistics.nRxSchedBuffers );
825 ( *pCmdIO->pCmdApi->print )( cmdIoParam, "nRxErrorPackets: %d, nRxFragmentErrors: %d\r\n", rxStatistics.nRxErrorPackets, rxStatistics.nRxFragmentErrors );
826 ( *pCmdIO->pCmdApi->print )( cmdIoParam, "\tnTxOkPackets: %d, nTxPendBuffers: %d, nTxErrorPackets: %d, nTxQueueFull: %d\r\n",
827 txStatistics.nTxOkPackets, txStatistics.nTxPendBuffers, txStatistics.nTxErrorPackets, txStatistics.nTxQueueFull );
831 ( *pCmdIO->pCmdApi->msg )( cmdIoParam, "\tnot supported\r\n" );
834 ( *pCmdIO->pCmdApi->print )( cmdIoParam, "Interface: %s hardware statistics\r\n", macObject->macName );
835 macRes = ( macObject->TCPIP_MAC_RegisterStatisticsGet )( macCliHandle, regEntries, sizeof( regEntries ) / sizeof( *regEntries ), &hwEntries );
837 if( macRes == TCPIP_MAC_RES_OK )
839 entryName[ sizeof( entryName ) - 1 ] = 0;
841 for( jx = 0, pRegEntry = regEntries; jx < hwEntries && jx < sizeof( regEntries ) / sizeof( *regEntries ); jx++, pRegEntry++ )
843 strncpy( entryName, pRegEntry->registerName, sizeof( entryName ) - 1 );
844 ( *pCmdIO->pCmdApi->print )( cmdIoParam, "\t%s: 0x%8x\r\n", entryName, pRegEntry->registerValue );
849 ( *pCmdIO->pCmdApi->msg )( cmdIoParam, "\tnot supported\r\n" );
855 /*-----------------------------------------------------------*/
857 static int _Command_NetInfo( SYS_CMD_DEVICE_NODE * pCmdIO,
861 const void * cmdIoParam = pCmdIO->cmdIoParam;
870 sUl.ul = FreeRTOS_GetIPAddress();
872 bool linkUp = FreeRTOS_IsNetworkUp() == pdTRUE;
874 ( *pCmdIO->pCmdApi->print )( cmdIoParam, "IP address: %d.%d.%d.%d\r\n", sUl.b[ 0 ], sUl.b[ 1 ], sUl.b[ 2 ], sUl.b[ 3 ] );
875 ( *pCmdIO->pCmdApi->print )( cmdIoParam, "Link is: %s\r\n", linkUp ? "Up" : "Down" );
880 #include "aws_application_version.h"
882 static int _Command_Version(SYS_CMD_DEVICE_NODE* pCmdIO, int argc, char** argv)
884 configPRINTF( ( "App version - maj: %d, min: %d, build: %d\r\n", xAppFirmwareVersion.u.x.ucMajor, xAppFirmwareVersion.u.x.ucMinor, xAppFirmwareVersion.u.x.usBuild) );
888 #endif /* (PIC32_MAC_DEBUG_COMMANDS != 0) */
889 #endif /* #ifdef PIC32_USE_ETHERNET */