/* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. Copyright (C) 2012 - 2018 Xilinx, Inc. All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. If you wish to use our Amazon FreeRTOS name, please do so in a fair use way that does not cause confusion. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. http://www.FreeRTOS.org http://aws.amazon.com/freertos 1 tab == 4 spaces! */ /* Standard includes. */ #include #include /* FreeRTOS includes. */ #include "FreeRTOS.h" #include "task.h" #include "queue.h" #include "timers.h" /* FreeRTOS+TCP includes. */ #include "FreeRTOS-Plus/include/FreeRTOS_IP.h" #include "FreeRTOS-Plus/include/FreeRTOS_Sockets.h" #include "FreeRTOS-Plus/x_emacpsif.h" /* Xilinx includes. */ #include "xil_printf.h" #include "xparameters.h" /* Define names that will be used for SDN, LLMNR and NBNS searches. */ #define mainHOST_NAME "zynqMP" static const uint8_t ucIPAddress[ 4 ] = { 192, 168, 1, 12 }; static const uint8_t ucNetMask[ 4 ] = { 255, 255, 255, 0 }; static const uint8_t ucGatewayAddress[ 4 ] = { 192, 168, 1, 1 }; ///* The following is the address of an OpenDNS server. */ static const uint8_t ucDNSServerAddress[ 4 ] = { 208, 67, 222, 222 }; /* * Just seeds the simple pseudo random number generator. */ static void prvSRand(UBaseType_t ulSeed); /* * Miscellaneous initialisation including preparing the logging and seeding the * random number generator. */ static void prvMiscInitialisation(void); /* Set the following constant to pdTRUE to log using the method indicated by the name of the constant, or pdFALSE to not log using the method indicated by the name of the constant. Options include to standard out (xLogToStdout), to a disk file (xLogToFile), and to a UDP port (xLogToUDP). If xLogToUDP is set to pdTRUE then UDP messages are sent to the IP address configured as the echo server address (see the configECHO_SERVER_ADDR0 definitions in FreeRTOSConfig.h) and the port number set by configPRINT_PORT in FreeRTOSConfig.h. */ const BaseType_t xLogToStdout = pdTRUE, xLogToFile = pdFALSE, xLogToUDP = pdFALSE; /* Use by the pseudo random number generator. */ static UBaseType_t ulNextRand; const uint8_t ucMACAddress[ 6 ] = { 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 }; int main(void) { /* ***NOTE*** Tasks that use the network are created in the network event hook when the network is connected and ready for use (see the definition of vApplicationIPNetworkEventHook() below). The address values passed in here are used if ipconfigUSE_DHCP is set to 0, or if ipconfigUSE_DHCP is set to 1 but a DHCP server cannot be contacted. */ xil_printf("---------------------------------\r\n"); FreeRTOS_debug_printf( ( "\rFreeRTOS_IPInit\r\n" ) ); FreeRTOS_IPInit (ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress); /* Start the RTOS scheduler. */ FreeRTOS_debug_printf(("\rvTaskStartScheduler\r\n")); xil_printf("---------------------------------\r\n"); /* Start the tasks and timer running. */ vTaskStartScheduler(); /* If all is well, the scheduler will now be running, and the following line will never be reached. If the following line does execute, then there was insufficient FreeRTOS heap memory available for the idle and/or timer tasks to be created. See the memory management section on the FreeRTOS web site for more details. */ for (;;){ } } /*-----------------------------------------------------------*/ void vAssertCalled(const char *pcFile, uint32_t ulLine) { volatile uint32_t ulBlockVariable = 0UL; volatile char *pcFileName = (volatile char *) pcFile; volatile uint32_t ulLineNumber = ulLine; (void) pcFileName; (void) ulLineNumber; FreeRTOS_debug_printf(( "vAssertCalled( %s, %ld\n", pcFile, ulLine )); /* Setting ulBlockVariable to a non-zero value in the debugger will allow this function to be exited. */ taskDISABLE_INTERRUPTS(); { while (ulBlockVariable == 0UL) { __asm volatile( "NOP" ); } } taskENABLE_INTERRUPTS(); } /*-----------------------------------------------------------*/ /* Called by FreeRTOS+TCP when the network connects or disconnects. Disconnect events are only received if implemented in the MAC driver. */ void vApplicationIPNetworkEventHook(eIPCallbackEvent_t eNetworkEvent) { uint32_t ulIPAddress, ulNetMask, ulGatewayAddress, ulDNSServerAddress; char cBuffer[16]; static BaseType_t xTasksAlreadyCreated = pdFALSE; /* If the network has just come up...*/ if (eNetworkEvent == eNetworkUp) { /* Create the tasks that use the IP stack if they have not already been created. */ xil_printf ("Network is UP\r\n"); /* Print out the network configuration, which may have come from a DHCP server. */ FreeRTOS_GetAddressConfiguration( &ulIPAddress, &ulNetMask, &ulGatewayAddress, &ulDNSServerAddress ); FreeRTOS_inet_ntoa( ulIPAddress, cBuffer ); xil_printf( "IP Address: %s\r\n", cBuffer ); FreeRTOS_inet_ntoa( ulNetMask, cBuffer ); xil_printf( "Subnet Mask: %s\r\n", cBuffer ); FreeRTOS_inet_ntoa( ulGatewayAddress, cBuffer ); xil_printf( "Gateway Address: %s\r\n", cBuffer ); FreeRTOS_inet_ntoa( ulDNSServerAddress, cBuffer ); xil_printf( "DNS Server Address: %s\r\n", cBuffer ); xil_printf("Static MAC Address: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n", ucMACAddress[0],ucMACAddress[1],ucMACAddress[2],ucMACAddress[3],ucMACAddress[4],ucMACAddress[5]); if (xTasksAlreadyCreated == pdFALSE) { xTasksAlreadyCreated = pdTRUE; } } } /*-----------------------------------------------------------*/ UBaseType_t uxRand(void) { const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL; /* Utility function to generate a pseudo random number. */ ulNextRand = (ulMultiplier * ulNextRand) + ulIncrement; return ((int) (ulNextRand >> 16UL) & 0x7fffUL); } /*-----------------------------------------------------------*/ static void prvSRand(UBaseType_t ulSeed) { /* Utility function to seed the pseudo random number generator. */ ulNextRand = ulSeed; } /*-----------------------------------------------------------*/ static void prvMiscInitialisation(void) { time_t xTimeNow; /* Seed the random number generator. */ time(&xTimeNow); FreeRTOS_debug_printf(( "Seed for randomiser: %lu\n", xTimeNow )); prvSRand((uint32_t) xTimeNow); // FreeRTOS_debug_printf( // ( "Random numbers: %08X %08X %08X %08X\n", ipconfigRAND32(), ipconfigRAND32(), ipconfigRAND32(), ipconfigRAND32() )); } /*-----------------------------------------------------------*/ #if( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) || ( ipconfigDHCP_REGISTER_HOSTNAME == 1 ) const char *pcApplicationHostnameHook(void) { /* Assign the name "FreeRTOS" to this network node. This function will be called during the DHCP: the machine will be registered with an IP address plus this name. */ return mainHOST_NAME; } #endif /*-----------------------------------------------------------*/ #if( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) BaseType_t xApplicationDNSQueryHook( const char *pcName ) { BaseType_t xReturn; /* Determine if a name lookup is for this node. Two names are given to this node: that returned by pcApplicationHostnameHook() and that set by mainDEVICE_NICK_NAME. */ if( _stricmp( pcName, pcApplicationHostnameHook() ) == 0 ) { xReturn = pdPASS; } else if( _stricmp( pcName, mainDEVICE_NICK_NAME ) == 0 ) { xReturn = pdPASS; } else { xReturn = pdFAIL; } return xReturn; } #endif /* * Callback that provides the inputs necessary to generate a randomized TCP * Initial Sequence Number per RFC 6528. THIS IS ONLY A DUMMY IMPLEMENTATION * THAT RETURNS A PSEUDO RANDOM NUMBER SO IS NOT INTENDED FOR USE IN PRODUCTION * SYSTEMS. */ extern uint32_t ulApplicationGetNextSequenceNumber(uint32_t ulSourceAddress, uint16_t usSourcePort, uint32_t ulDestinationAddress, uint16_t usDestinationPort) { (void) ulSourceAddress; (void) usSourcePort; (void) ulDestinationAddress; (void) usDestinationPort; return uxRand(); } /* * Supply a random number to FreeRTOS+TCP stack. * THIS IS ONLY A DUMMY IMPLEMENTATION THAT RETURNS A PSEUDO RANDOM NUMBER * SO IS NOT INTENDED FOR USE IN PRODUCTION SYSTEMS. */ BaseType_t xApplicationGetRandomNumber(uint32_t* pulNumber) { *(pulNumber) = uxRand(); return pdTRUE; } /* Called automatically when a reply to an outgoing ping is received. */ void vApplicationPingReplyHook(ePingReplyStatus_t eStatus, uint16_t usIdentifier) { char *pcSuccess = "Ping reply received - "; char *pcInvalidChecksum = "Ping reply received with invalid checksum - "; char *pcInvalidData = "Ping reply received with invalid data - "; switch (eStatus) { case eSuccess: FreeRTOS_debug_printf(( pcSuccess )); break; case eInvalidChecksum: FreeRTOS_debug_printf(( pcInvalidChecksum )); break; case eInvalidData: FreeRTOS_debug_printf(( pcInvalidData )); break; default: /* It is not possible to get here as all enums have their own case. */ break; } // FreeRTOS_debug_printf( // ( cMessage, "identifier %d\r\n", ( int ) usIdentifier )); /* Prevent compiler warnings in case FreeRTOS_debug_printf() is not defined. */ (void) usIdentifier; } /*-----------------------------------------------------------*/ #warning _gettimeofday_r is just stubbed out here. struct timezone; struct timeval; int _gettimeofday_r(struct _reent * x, struct timeval *y, struct timezone * ptimezone) { (void) x; (void) y; (void) ptimezone; return 0; } /* configSUPPORT_STATIC_ALLOCATION is set to 1, so the application must provide an implementation of vApplicationGetIdleTaskMemory() to provide the memory that is used by the Idle task. */ void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize ) { /* If the buffers to be provided to the Idle task are declared inside this function then they must be declared static – otherwise they will be allocated on the stack and so not exists after this function exits. */ static StaticTask_t xIdleTaskTCB; static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ]; /* Pass out a pointer to the StaticTask_t structure in which the Idle task’s state will be stored. */ *ppxIdleTaskTCBBuffer = &xIdleTaskTCB; /* Pass out the array that will be used as the Idle task’s stack. */ *ppxIdleTaskStackBuffer = uxIdleTaskStack; /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer. Note that, as the array is necessarily of type StackType_t, configMINIMAL_STACK_SIZE is specified in words, not bytes. */ *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; } /*———————————————————–*/ /* configSUPPORT_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the application must provide an implementation of vApplicationGetTimerTaskMemory() to provide the memory that is used by the Timer service task. */ void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize ) { /* If the buffers to be provided to the Timer task are declared inside this function then they must be declared static – otherwise they will be allocated on the stack and so not exists after this function exits. */ static StaticTask_t xTimerTaskTCB; static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ]; /* Pass out a pointer to the StaticTask_t structure in which the Timer task’s state will be stored. */ *ppxTimerTaskTCBBuffer = &xTimerTaskTCB; /* Pass out the array that will be used as the Timer task’s stack. */ *ppxTimerTaskStackBuffer = uxTimerTaskStack; /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer. Note that, as the array is necessarily of type StackType_t, configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */ *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; }