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

📄 tcp.c

📁 开源实时操作系统
💻 C
📖 第 1 页 / 共 2 页
字号:
	i2cMessage( ucDataProtocol, sizeof( ucDataProtocol),tcpDEVICE_ADDRESS, tpcSOCKET_OPT_REG, i2cWRITE, NULL, portMAX_DELAY );
	i2cMessage( ucDataPort,		sizeof( ucDataPort),	tcpDEVICE_ADDRESS, tcpSOURCE_PORT_REG, i2cWRITE, NULL, portMAX_DELAY );
	i2cMessage( ucDataSockInit, sizeof( ucDataSockInit),tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );

	/* Wait for the Init command to be sent. */
	if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
	{
		/* For some reason the message was not transmitted within our block
		period. */
		return ( portLONG ) pdFAIL;
	}

	/* Allow the socket to initialise. */
	vTaskDelay( tcpINIT_DELAY );

	/* Read back the status to ensure the socket initialised ok. */
	prvReadRegister( &ucStatus, tcpINTERRUPT_STATUS_REG, tcpSTATUS_READ_LEN );
	
	/* We should find that the socket init was successful. */
	if( ucStatus != tcpISR_SOCKET_INIT )
	{
		return ( portLONG ) pdFAIL;
	}


	/* Setup the Tx pointer registers to indicate that the Tx buffer is empty. */
	i2cMessage( ucDataTxReadPointer, sizeof( ucDataTxReadPointer ), tcpDEVICE_ADDRESS, tcpTX_READ_POINTER_REG, i2cWRITE, NULL, portMAX_DELAY );
	vTaskDelay( tcpSHORT_DELAY );
	i2cMessage( ucDataTxWritePointer, sizeof( ucDataTxWritePointer ), tcpDEVICE_ADDRESS, tcpTX_WRITE_POINTER_REG, i2cWRITE, NULL, portMAX_DELAY );
	vTaskDelay( tcpSHORT_DELAY );
	i2cMessage( ucDataTxAckPointer,	  sizeof( ucDataTxAckPointer ),	  tcpDEVICE_ADDRESS, tcpTX_ACK_POINTER_REG, i2cWRITE, NULL, portMAX_DELAY );
	vTaskDelay( tcpSHORT_DELAY );

	return ( portLONG ) pdPASS;
}
/*-----------------------------------------------------------*/

void vTCPListen( void )
{
unsigned portCHAR ucISR;

	/* Start a passive listen on the socket. */

	/* Enable interrupts in the WizNet device after ensuring none are 
	currently pending. */
	while( SCB_EXTINT & tcpCLEAR_EINT0 )
	{
		/* The WIZnet device is still asserting and interrupt so tell it to 
		clear. */
		i2cMessage( ucDataClearInterrupt, sizeof( ucDataClearInterrupt ), tcpDEVICE_ADDRESS, tcpINTERRUPT_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );
		xSemaphoreTake( xMessageComplete, tcpLONG_DELAY );

		vTaskDelay( 1 );
		SCB_EXTINT = tcpCLEAR_EINT0;
	}

	while( xQueueReceive( xTCPISRQueue, &ucISR, tcpNO_DELAY ) )
	{
		/* Just clearing the queue used by the ISR routine to tell this task
		that the WIZnet device needs attention. */
	}

	/* Now all the pending interrupts have been cleared we can enable the 
	processor interrupts. */
	VICIntEnable |= tcpEINT0_VIC_CHANNEL_BIT;

	/* Then start listening for incoming connections. */
	i2cMessage( ucDataListen, sizeof( ucDataListen ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );
}
/*-----------------------------------------------------------*/

portLONG lProcessConnection( void )
{
unsigned portCHAR ucISR, ucState, ucLastState = 2, ucShadow;
extern volatile portLONG lTransactionCompleted;
portLONG lSameStateCount = 0, lDataSent = pdFALSE;
unsigned portLONG ulWritePointer, ulAckPointer;

	/* No I2C errors can yet have occurred. */
	portENTER_CRITICAL();
		lTransactionCompleted = pdTRUE;
	portEXIT_CRITICAL();

	/* Keep looping - processing interrupts, until we have completed a 
	transaction.   This uses the WIZnet in it's simplest form.  The socket
	accepts a connection - we process the connection - then close the socket.
	We then go back to reinitialise everything and start again. */
	while( lTransactionCompleted == pdTRUE )
	{
		/* Wait for a message on the queue from the WIZnet ISR.  When the 
		WIZnet device asserts an interrupt the ISR simply posts a message
		onto this queue to wake this task. */
		if( xQueueReceive( xTCPISRQueue, &ucISR, tcpCONNECTION_WAIT_DELAY ) )
		{
			/* The ISR posted a message on this queue to tell us that the
			WIZnet device asserted an interrupt.  The ISR cannot process
			an I2C message so cannot tell us what caused the interrupt so
			we have to query the device here.  This task is the highest
			priority in the system so will run immediately following the ISR. */
			prvReadRegister( &ucISR, tcpINTERRUPT_STATUS_REG, tcpSTATUS_READ_LEN );

			/* Once we have read what caused the ISR we can clear the interrupt
			in the WIZnet. */
			i2cMessage( ucDataClearInterrupt, sizeof( ucDataClearInterrupt ), tcpDEVICE_ADDRESS, tcpINTERRUPT_REG, i2cWRITE, NULL, portMAX_DELAY );

			/* Now we can clear the processor interrupt and re-enable ready for
			the next. */
			SCB_EXTINT = tcpCLEAR_EINT0;
			VICIntEnable |= tcpEINT0_VIC_CHANNEL_BIT;
	
			/* Process the interrupt ... */

			if( ucISR & tcpISR_ESTABLISHED )
			{
				/* A connection has been established - respond by sending
				a receive command. */
				i2cMessage( ucDataReceiveCmd, sizeof( ucDataReceiveCmd ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );
			}
		
			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 + -