UARTInit( 0, 19200 );
NVIC_SetPriority( UART0_IRQn,29 ); // Display output text/message on COM3 port
sprintf(pBuff,"%sn","WELCOME TO RTOS PROJECT WORLD:n"); kgprint(pBuff);
/* Create the tasks that use the TCP/IP stack if they have not already been created. */
uint8_t ucMACAddress[ 6 ] = { 0x00,0x07,0x27,0x07,0x19,0x54 };static const uint8_t ucIPAddress[4] = { 192,168,1,211 };
static const uint8_t ucNetMask[ 4 ] = { 255, 255, 255, 0 }; static const uint8_t ucGatewayAddress[ 4 ] = { 192, 168, 1, 210 };
static const uint8_t ucDNSServerAddress[ 4 ] = { 192, 168, 1, 210 };
/* Initialise the RTOS's TCP/IP stack. The tasks(IP-TASK) that use the
network are created in the vApplicationIPNetworkEventHook() hook function. The hook function is called when the network connects. */
FreeRTOS_IPInit(ucIPAddress,ucNetMask,ucGatewayAddress,ucDNSServerAddress,ucMACAddress );
/* Display the IP address, then create the uIP task. The WEB server runs in this task.*/
xTaskCreate( vuKGTCPTask, “uIP WEBSERVER”, mainBASICUIPWEBSTACKSIZE, NULL, mainUIPTASK_PRIORITY, &xUIPTaskHandle );
/* Start the scheduler so our tasks start executing. */
vTaskStartScheduler();
for( ;; )
{
}
return 0;
}
void vApplicationIPNetworkEventHook( eIPCallbackEventt eNetworkEvent )
{
static BaseTypet xTasksAlreadyCreated = pdFALSE;
/* Both eNetworkUp and eNetworkDown events can be processed here. */
if( eNetworkEvent == eNetworkUp )
{
if( xTasksAlreadyCreated == pdFALSE )
{
sprintf(pBuff,"%sn","Network UPn"); kgprint(pBuff);
/** For convenience, tasks that use FreeRTOS+TCP can be created here to ensure they are not created before the network is usable.*/
xTaskCreate( vuKGTCP_Send_Receive_Task, "uIP SendReceive", mainBASIC_SndRCv_WEB_STACK_SIZE, NULL, mainUIPSendReceive_TASK_PRIORITY, &xUIPSndRcvTaskHandle );
xTasksAlreadyCreated = pdTRUE;
}
/* The network is up and configured. Print out the configuration */
FreeRTOS_GetAddressConfiguration(&ulIPAddress,&ulNetMask,&ulGatewayAddress,&ulDNSServerAddress );
/* Convert the IP address to a string then print it out. */
FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );
sprintf(pBuff, "%s %sn", "SERVER IP.......:", cBuffer); kgprint(pBuff);
}
}
void vuKGTCPSendReceiveTask(void)
static struct freertossockaddr xClient, xBindAdress;
Sockett xListeningSocket, xConnectedSocket;
socklent xSize = sizeof(xClient);
xWinProperties_t xWinProps;
const BaseType_t xBacklog = 20;
//static const TickTypet xReceiveTimeOut = portMAXDELAY;
//static const TickTypet xSendTimeOut = portMAXDELAY;
static const TickTypet xReceiveTimeOut = pdMSTOTICKS( 20 );
static const TickTypet xSendTimeOut = pdMSTOTICKS(100);
BaseType_t xBytesRecceived = 0;
define Buffer_Size 30
static char RcvBuffer[BufferSize]; xListeningSocket = FreeRTOSsocket(FREERTOSAFINET, FREERTOSSOCKSTREAM, FREERTOSIPPROTOTCP); configASSERT(xListeningSocket != FREERTOSINVALIDSOCKET); if (xListeningSocket != FREERTOSINVALIDSOCKET) { sprintf(pBuff, “%sn”, “Server Socket Created Successfully…n”); kgprint(pBuff); } else { sprintf(pBuff, “%sn”, “Failed to Create Server Socket …n”); kgprint(pBuff); //goto ReviveUIPWEBSERVER; } FreeRTOSsetsockopt(xListeningSocket, 0, FREERTOSSORCVTIMEO, &xReceiveTimeOut, sizeof(xReceiveTimeOut)); FreeRTOSsetsockopt(xListeningSocket, 0, FREERTOSSOSNDTIMEO, &xSendTimeOut, sizeof(xSendTimeOut));if( ipconfigUSETCPWIN == 1 )
/* Fill in the buffer and window sizes that will be used by the socket. */
xWinProps.lTxBufSize = 4 * ipconfigTCP_MSS;
xWinProps.lTxWinSize = 3;
xWinProps.lRxBufSize = 4 * ipconfigTCP_MSS;
xWinProps.lRxWinSize = 3;
/* Set the window and buffer sizes. */
FreeRTOSsetsockopt( xListeningSocket,0,FREERTOSSOWINPROPERTIES,&xWinProps, sizeof( xWinProps ) );
endif /* ipconfigUSETCPWIN */
xBindAdress.sinaddr = FreeRTOSinetaddrquick( 192,168,1,211); xBindAdress.sinport = FreeRTOShtons( 27754); if (FreeRTOS_bind( xListeningSocket, &xBindAdress, sizeof(xBindAdress) ) == 0) { sprintf(pBuff,”%sn”,”Server Bind AT PORT 27754 Successful….n”);kgprint(pBuff); } else { sprintf(pBuff,”%sn”,”Server Bind Failed….n”);kgprint(pBuff); } if (FreeRTOS_listen(xListeningSocket, xBacklog) == 0) { sprintf(pBuff,”%sn”,”Listening for Client…n”); kgprint(pBuff} sprintf(pBuff, “%sn”, “Waiting For Accepting From 192.168.1.210 n”); kgprint(pBuff); for (;;) {
xConnectedSocket = FreeRTOS_accept(xListeningSocket, &xClient, &xSize);
configASSERT(xConnectedSocket != FREERTOS_INVALID_SOCKET);
if (FreeRTOS_issocketconnected(xConnectedSocket) == pdTRUE)
{
sprintf(pBuff, "%sn", "Connected with 192.168.1.210.....n"); kgprint(pBuff);
sprintf(pBuff, "%sn", "Receiving Data From Client...n"); kgprint(pBuff);
}
xBytesRecceived = FreeRTOS_recv(xConnectedSocket, &RcvBuffer, Buffer_Size, 0);
if (xBytesRecceived > 0)
{
strcpy(pBuff, RcvBuffer);
sprintf(pBuff, "%sn", "Received Data From PC Client:"); kgprint(pBuff);
}
vTaskDelay(100); //Delay for next receive send cycle
}
SocShutdown:
/* Initiate graceful shutdown. */
FreeRTOSshutdown( xListeningSocket, FREERTOSSHUTRDWR );
FreeRTOSshutdown( xConnectedSocket, FREERTOSSHUT_RDWR );
vTaskDelay(500 ); //wait for graceful disconnect
/* The socket has shut down and is safe to close. */
sprintf(pBuff,”%sn”,”Closing Sockets…..n”); kgprint(pBuff);
FreeRTOS_closesocket( xListeningSocket );
FreeRTOS_closesocket( xConnectedSocket );
}
void kgprint( const char *str )
{
unsigned int i;
for( i = 0 ; str[i] !=’