⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp.c

📁 最新的FreeRTOS源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
		
			if( ucISR & tcpISR_RX_COMPLETE )
			{
				/* We message has been received.  This will be an HTTP get 
				command.  We only have one page to send so just send it without
				regard to what the actual requested page was. */
				prvSendSamplePage();
			}
		
		 	if( ucISR & tcpISR_TX_COMPLETE )
			{
				/* We have a TX complete interrupt - which oddly does not 
				indicate that the message being sent is complete so we cannot
				yet close the socket.  Instead we read the position of the Tx
				pointer within the WIZnet device so we know how much data it
				has to send.  Later we will read the ack pointer and compare 
				this to the Tx pointer to ascertain whether the transmission 
				has completed. */

				/* First read the shadow register. */
				prvReadRegister( &ucShadow, tcpTX_WRITE_SHADOW_REG, tcpSHADOW_READ_LEN );
			
				/* Now a short delay is required. */
				vTaskDelay( tcpSHORT_DELAY );

				/* Then we can read the real register. */
				prvReadRegister( ( unsigned portCHAR * ) &ulWritePointer, tcpTX_WRITE_POINTER_REG, sizeof( ulWritePointer ) );

				/* We cannot do anything more here but need to remember that 
				this interrupt has occurred. */
				lDataSent = pdTRUE;
			}
		
			if( ucISR & tcpISR_CLOSED )
			{
				/* The socket has been closed so we can leave this function. */
				lTransactionCompleted = pdFALSE;
			}
		}
		else
		{
			/* We have not received an interrupt from the WIZnet device for a 
			while.  Read the socket status and check that everything is as
			expected. */
			prvReadRegister( &ucState, tcpSOCKET_STATE_REG, tcpSTATUS_READ_LEN );
			
			if( ( ucState == tcpSTATUS_ESTABLISHED ) && ( lDataSent > 0 ) ) 
			{
				/* The socket is established and we have already received a Tx
				end interrupt.  We must therefore be waiting for the Tx buffer
				inside the WIZnet device to be empty before we can close the
				socket. 

				Read the Ack pointer register to see if it has caught up with
				the Tx pointer register.  First we have to read the shadow 
				register. */
				prvReadRegister( &ucShadow, tcpTX_ACK_SHADOW_REG, tcpSHADOW_READ_LEN );
				vTaskDelay( tcpSHORT_DELAY );
				prvReadRegister( ( unsigned portCHAR * ) &ulAckPointer, tcpTX_ACK_POINTER_REG, sizeof( ulWritePointer ) );

				if( ulAckPointer == ulWritePointer )
				{
					/* The Ack and write pointer are now equal and we can 
					safely close the socket. */
					i2cMessage( ucDataDisconnect, sizeof( ucDataDisconnect ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );
				}
				else
				{
					/* Keep a count of how many times we encounter the Tx
					buffer still containing data. */
					lDataSent++;
					if( lDataSent > tcpMAX_ATTEMPTS_TO_CHECK_BUFFER )
					{
						/* Assume we cannot complete sending the data and 
						therefore cannot safely close the socket.  Start over. */
						vTCPHardReset();
						lTransactionCompleted = pdFALSE;
					}
				}
			}
			else if( ucState != tcpSTATUS_LISTEN )
			{
				/* If we have not yet received a Tx end interrupt we would only 
				ever expect to find the socket still listening for any 
				sustained period. */
				if( ucState == ucLastState )
				{
					lSameStateCount++;
					if( lSameStateCount > tcpMAX_NON_LISTEN_STAUS_READS )
					{						
						/* We are persistently in an unexpected state.  Assume
						we cannot safely close the socket and start over. */
						vTCPHardReset();
						lTransactionCompleted = pdFALSE;
					}
				}
			}
			else
			{
				/* We are in the listen state so are happy that everything
				is as expected. */
				lSameStateCount = 0;
			}

			/* Remember what state we are in this time around so we can check
			for a persistence on an unexpected state. */
			ucLastState = ucState;
		}
	}

	/* We are going to reinitialise the WIZnet device so do not want our 
	interrupts from the WIZnet to be processed. */
	VICIntEnClear |= tcpEINT0_VIC_CHANNEL_BIT;
	return lTransactionCompleted;
}
/*-----------------------------------------------------------*/

static void prvWriteString( const portCHAR * const pucTxBuffer, portLONG lTxLen, unsigned portLONG *pulTxAddress )
{
unsigned portLONG ulSendAddress;

	/* Send a string to the Tx buffer internal to the WIZnet device. */

	/* Calculate the address to which we are going to write in the buffer. */
	ulSendAddress = ( *pulTxAddress & tcpSINGLE_SOCKET_ADDR_MASK ) + tcpSINGLE_SOCKET_ADDR_OFFSET;

	/* Send the buffer to the calculated address.  Use the semaphore so we
	can wait until the entire message has been transferred. */
	i2cMessage( ( unsigned portCHAR * ) pucTxBuffer, lTxLen, tcpDEVICE_ADDRESS, ( unsigned portSHORT ) ulSendAddress, i2cWRITE, xMessageComplete, portMAX_DELAY );

	/* Wait until the semaphore indicates that the message has been transferred. */
	if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
	{
		return;
	}

	/* Return the new address of the end of the buffer (within the WIZnet 
	device). */
	*pulTxAddress += ( unsigned portLONG ) lTxLen;
}
/*-----------------------------------------------------------*/

static void prvFlushBuffer( unsigned portLONG ulTxAddress )
{
unsigned portCHAR ucTxBuffer[ tcpMAX_REGISTER_LEN ];

	/* We have written some data to the Tx buffer internal to the WIZnet
	device.  Now we update the Tx pointer inside the WIZnet then send a
	Send command - which causes	the data up to the new Tx pointer to be 
	transmitted. */

	/* Make sure endieness is correct for transmission. */
	ulTxAddress = htonl( ulTxAddress );

	/* Place the new Tx pointer in the string to be transmitted. */
	ucTxBuffer[ 0 ] = ( unsigned portCHAR ) ( ulTxAddress & 0xff );
	ulTxAddress >>= 8;
	ucTxBuffer[ 1 ] = ( unsigned portCHAR ) ( ulTxAddress & 0xff );
	ulTxAddress >>= 8;
	ucTxBuffer[ 2 ] = ( unsigned portCHAR ) ( ulTxAddress & 0xff );
	ulTxAddress >>= 8;
	ucTxBuffer[ 3 ] = ( unsigned portCHAR ) ( ulTxAddress & 0xff );
	ulTxAddress >>= 8;

	/* And send it to the WIZnet device. */
	i2cMessage( ucTxBuffer, sizeof( ulTxAddress ), tcpDEVICE_ADDRESS, tcpTX_WRITE_POINTER_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );

	if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
	{
		return;
	}

	vTaskDelay( tcpSHORT_DELAY );

	/* Transmit! */
	i2cMessage( ucDataSend, sizeof( ucDataSend ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );

	if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
	{
		return;
	}
}
/*-----------------------------------------------------------*/

static void prvSendSamplePage( void )
{
extern portLONG lErrorInTask;
unsigned portLONG ulTxAddress;
unsigned portCHAR ucShadow;
portLONG lIndex;
static unsigned portLONG ulRefreshCount = 0x00;
static portCHAR cPageBuffer[ tcpBUFFER_LEN ];


	/* This function just generates a sample page of HTML which gets
	sent each time a client attaches to the socket.  The page is created
	from two fixed strings (cSamplePageFirstPart and cSamplePageSecondPart)
	with a bit of dynamically generated data in the middle. */

	/* We need to know the address to which the html string should be sent
	in the WIZnet Tx buffer.  First read the shadow register. */
	prvReadRegister( &ucShadow, tcpTX_WRITE_SHADOW_REG, tcpSHADOW_READ_LEN );

	/* Now a short delay is required. */
	vTaskDelay( tcpSHORT_DELAY );

	/* Now we can read the real pointer value. */
	prvReadRegister( ( unsigned portCHAR * ) &ulTxAddress, tcpTX_WRITE_POINTER_REG, sizeof( ulTxAddress ) );

	/* Make sure endieness is correct. */
	ulTxAddress = htonl( ulTxAddress );

	/* Send the start of the page. */
	prvWriteString( cSamplePageFirstPart, strlen( cSamplePageFirstPart ), &ulTxAddress );

	/* Generate a bit of dynamic data and place it in the buffer ready to be
	transmitted. */
	strcpy( cPageBuffer, "<BR>Number of ticks since boot = 0x" );
	lIndex = strlen( cPageBuffer );
	ultoa( xTaskGetTickCount(), &( cPageBuffer[ lIndex ] ), 0 );
	strcat( cPageBuffer, "<br>Number of tasks executing = ");
	lIndex = strlen( cPageBuffer );
	ultoa( ( unsigned portLONG ) uxTaskGetNumberOfTasks(), &( cPageBuffer[ lIndex ] ), 0 );
	strcat( cPageBuffer, "<br>IO port 0 state (used by flash tasks) = 0x" );
	lIndex = strlen( cPageBuffer );
	ultoa( ( unsigned portLONG ) GPIO0_IOPIN, &( cPageBuffer[ lIndex ] ), 0 );
	strcat( cPageBuffer, "<br>Refresh = 0x" );
	lIndex = strlen( cPageBuffer );
	ultoa( ( unsigned portLONG ) ulRefreshCount, &( cPageBuffer[ lIndex ] ), 0 );
	
	if( lErrorInTask )
	{
		strcat( cPageBuffer, "<p>An error has occurred in at least one task." );
	}
	else
	{
		strcat( cPageBuffer, "<p>All tasks executing without error." );		
	}

	ulRefreshCount++;

	/* Send the dynamically generated string. */
	prvWriteString( cPageBuffer, strlen( cPageBuffer ), &ulTxAddress );

	/* Finish the page. */
	prvWriteString( cSamplePageSecondPart, strlen( cSamplePageSecondPart ), &ulTxAddress );

	/* Tell the WIZnet to send the data we have just written to its Tx buffer. */
	prvFlushBuffer( ulTxAddress );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -