2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
52 + Modified char* types to compile without warning when using GCC V4.0.1.
\r
53 + Corrected the address to which the MAC address is written. Thanks to
\r
54 Bill Knight for this correction.
\r
58 + Changed the default MAC address to something more realistic.
\r
62 /* Standard includes. */
\r
66 /* Scheduler include files. */
\r
67 #include "FreeRTOS.h"
\r
73 /* Application includes. */
\r
75 #include "html_pages.h"
\r
77 /*-----------------------------------------------------------*/
\r
79 /* Hardwired i2c address of the WIZNet device. */
\r
80 #define tcpDEVICE_ADDRESS ( ( unsigned char ) 0x00 )
\r
82 /* Constants used to configure the Tx and Rx buffer sizes within the WIZnet
\r
84 #define tcp8K_RX ( ( unsigned char ) 0x03 )
\r
85 #define tcp8K_TX ( ( unsigned char ) 0x03 )
\r
87 /* Constants used to generate the WIZnet internal buffer addresses. */
\r
88 #define tcpSINGLE_SOCKET_ADDR_MASK ( ( unsigned long ) 0x1fff )
\r
89 #define tcpSINGLE_SOCKET_ADDR_OFFSET ( ( unsigned long ) 0x4000 )
\r
91 /* Bit definitions of the commands that can be sent to the command register. */
\r
92 #define tcpRESET_CMD ( ( unsigned char ) 0x80 )
\r
93 #define tcpSYS_INIT_CMD ( ( unsigned char ) 0x01 )
\r
94 #define tcpSOCK_STREAM ( ( unsigned char ) 0x01 )
\r
95 #define tcpSOCK_INIT ( ( unsigned char ) 0x02 )
\r
96 #define tcpLISTEN_CMD ( ( unsigned char ) 0x08 )
\r
97 #define tcpRECEIVE_CMD ( ( unsigned char ) 0x40 )
\r
98 #define tcpDISCONNECT_CMD ( ( unsigned char ) 0x10 )
\r
99 #define tcpSEND_CMD ( ( unsigned char ) 0x20 )
\r
101 /* Constants required to handle the interrupts. */
\r
102 #define tcpCLEAR_EINT0 ( 1 )
\r
103 #define i2cCLEAR_ALL_INTERRUPTS ( ( unsigned char ) 0xff )
\r
104 #define i2cCHANNEL_0_ISR_ENABLE ( ( unsigned char ) 0x01 )
\r
105 #define i2cCHANNEL_0_ISR_DISABLE ( ( unsigned char ) 0x00 )
\r
106 #define tcpWAKE_ON_EINT0 ( 1 )
\r
107 #define tcpENABLE_EINT0_FUNCTION ( ( unsigned long ) 0x01 )
\r
108 #define tcpEINT0_VIC_CHANNEL_BIT ( ( unsigned long ) 0x4000 )
\r
109 #define tcpEINT0_VIC_CHANNEL ( ( unsigned long ) 14 )
\r
110 #define tcpEINT0_VIC_ENABLE ( ( unsigned long ) 0x0020 )
\r
112 /* Various delays used in the driver. */
\r
113 #define tcpRESET_DELAY ( ( portTickType ) 16 / portTICK_RATE_MS )
\r
114 #define tcpINIT_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
115 #define tcpLONG_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
116 #define tcpSHORT_DELAY ( ( portTickType ) 5 / portTICK_RATE_MS )
\r
117 #define tcpCONNECTION_WAIT_DELAY ( ( portTickType ) 100 / portTICK_RATE_MS )
\r
118 #define tcpNO_DELAY ( ( portTickType ) 0 )
\r
120 /* Length of the data to read for various register reads. */
\r
121 #define tcpSTATUS_READ_LEN ( ( unsigned long ) 1 )
\r
122 #define tcpSHADOW_READ_LEN ( ( unsigned long ) 1 )
\r
124 /* Register addresses within the WIZnet device. */
\r
125 #define tcpCOMMAND_REG ( ( unsigned short ) 0x0000 )
\r
126 #define tcpGATEWAY_ADDR_REG ( ( unsigned short ) 0x0080 )
\r
127 #define tcpSUBNET_MASK_REG ( ( unsigned short ) 0x0084 )
\r
128 #define tcpSOURCE_HA_REG ( ( unsigned short ) 0x0088 )
\r
129 #define tpcSOURCE_IP_REG ( ( unsigned short ) 0x008E )
\r
130 #define tpcSOCKET_OPT_REG ( ( unsigned short ) 0x00A1 )
\r
131 #define tcpSOURCE_PORT_REG ( ( unsigned short ) 0x00AE )
\r
132 #define tcpTX_WRITE_POINTER_REG ( ( unsigned short ) 0x0040 )
\r
133 #define tcpTX_READ_POINTER_REG ( ( unsigned short ) 0x0044 )
\r
134 #define tcpTX_ACK_POINTER_REG ( ( unsigned short ) 0x0018 )
\r
135 #define tcpTX_MEM_SIZE_REG ( ( unsigned short ) 0x0096 )
\r
136 #define tcpRX_MEM_SIZE_REG ( ( unsigned short ) 0x0095 )
\r
137 #define tcpINTERRUPT_STATUS_REG ( ( unsigned short ) 0x0004 )
\r
138 #define tcpTX_WRITE_SHADOW_REG ( ( unsigned short ) 0x01F0 )
\r
139 #define tcpTX_ACK_SHADOW_REG ( ( unsigned short ) 0x01E2 )
\r
140 #define tcpISR_MASK_REG ( ( unsigned short ) 0x0009 )
\r
141 #define tcpINTERRUPT_REG ( ( unsigned short ) 0x0008 )
\r
142 #define tcpSOCKET_STATE_REG ( ( unsigned short ) 0x00a0 )
\r
144 /* Constants required for hardware setup. */
\r
145 #define tcpRESET_ACTIVE_LOW ( ( unsigned long ) 0x20 )
\r
146 #define tcpRESET_ACTIVE_HIGH ( ( unsigned long ) 0x10 )
\r
148 /* Constants defining the source of the WIZnet ISR. */
\r
149 #define tcpISR_SYS_INIT ( ( unsigned char ) 0x01 )
\r
150 #define tcpISR_SOCKET_INIT ( ( unsigned char ) 0x02 )
\r
151 #define tcpISR_ESTABLISHED ( ( unsigned char ) 0x04 )
\r
152 #define tcpISR_CLOSED ( ( unsigned char ) 0x08 )
\r
153 #define tcpISR_TIMEOUT ( ( unsigned char ) 0x10 )
\r
154 #define tcpISR_TX_COMPLETE ( ( unsigned char ) 0x20 )
\r
155 #define tcpISR_RX_COMPLETE ( ( unsigned char ) 0x40 )
\r
157 /* Constants defining the socket status bits. */
\r
158 #define tcpSTATUS_ESTABLISHED ( ( unsigned char ) 0x06 )
\r
159 #define tcpSTATUS_LISTEN ( ( unsigned char ) 0x02 )
\r
161 /* Misc constants. */
\r
162 #define tcpNO_STATUS_BITS ( ( unsigned char ) 0x00 )
\r
163 #define i2cNO_ADDR_REQUIRED ( ( unsigned short ) 0x0000 )
\r
164 #define i2cNO_DATA_REQUIRED ( 0x0000 )
\r
165 #define tcpISR_QUEUE_LENGTH ( ( unsigned portBASE_TYPE ) 10 )
\r
166 #define tcpISR_QUEUE_ITEM_SIZE ( ( unsigned portBASE_TYPE ) 0 )
\r
167 #define tcpBUFFER_LEN ( 4 * 1024 )
\r
168 #define tcpMAX_REGISTER_LEN ( 4 )
\r
169 #define tcpMAX_ATTEMPTS_TO_CHECK_BUFFER ( 6 )
\r
170 #define tcpMAX_NON_LISTEN_STAUS_READS ( 5 )
\r
172 /* Message definitions. The IP address, MAC address, gateway address, etc.
\r
174 const unsigned char const ucDataGAR[] = { 172, 25, 218, 3 }; /* Gateway address. */
\r
175 const unsigned char const ucDataMSR[] = { 255, 255, 255, 0 }; /* Subnet mask. */
\r
176 const unsigned char const ucDataSIPR[] = { 172, 25, 218, 201 };/* IP address. */
\r
177 const unsigned char const ucDataSHAR[] = { 00, 23, 30, 41, 15, 26 }; /* MAC address - DO NOT USE THIS ON A PUBLIC NETWORK! */
\r
179 /* Other fixed messages. */
\r
180 const unsigned char const ucDataReset[] = { tcpRESET_CMD };
\r
181 const unsigned char const ucDataInit[] = { tcpSYS_INIT_CMD };
\r
182 const unsigned char const ucDataProtocol[] = { tcpSOCK_STREAM };
\r
183 const unsigned char const ucDataPort[] = { 0xBA, 0xCC };
\r
184 const unsigned char const ucDataSockInit[] = { tcpSOCK_INIT };
\r
185 const unsigned char const ucDataTxWritePointer[] = { 0x11, 0x22, 0x00, 0x00 };
\r
186 const unsigned char const ucDataTxAckPointer[] = { 0x11, 0x22, 0x00, 0x00 };
\r
187 const unsigned char const ucDataTxReadPointer[] = { 0x11, 0x22, 0x00, 0x00 };
\r
188 const unsigned char const ucDataListen[] = { tcpLISTEN_CMD };
\r
189 const unsigned char const ucDataReceiveCmd[] = { tcpRECEIVE_CMD };
\r
190 const unsigned char const ucDataSetTxBufSize[] = { tcp8K_TX };
\r
191 const unsigned char const ucDataSetRxBufSize[] = { tcp8K_RX };
\r
192 const unsigned char const ucDataSend[] = { tcpSEND_CMD };
\r
193 const unsigned char const ucDataDisconnect[] = { tcpDISCONNECT_CMD };
\r
194 const unsigned char const ucDataEnableISR[] = { i2cCHANNEL_0_ISR_ENABLE };
\r
195 const unsigned char const ucDataDisableISR[] = { i2cCHANNEL_0_ISR_DISABLE };
\r
196 const unsigned char const ucDataClearInterrupt[] = { i2cCLEAR_ALL_INTERRUPTS };
\r
198 static xSemaphoreHandle xMessageComplete = NULL;
\r
199 xQueueHandle xTCPISRQueue = NULL;
\r
201 /* Dynamically generate and send an html page. */
\r
202 static void prvSendSamplePage( void );
\r
204 /* Read a register from the WIZnet device via the i2c interface. */
\r
205 static void prvReadRegister( unsigned char *pucDestination, unsigned short usAddress, unsigned long ulLength );
\r
207 /* Send the entire Tx buffer (the Tx buffer within the WIZnet device). */
\r
208 static void prvFlushBuffer( unsigned long ulTxAddress );
\r
210 /* Write a string to the WIZnet Tx buffer. */
\r
211 static void prvWriteString( const char * const pucTxBuffer, long lTxLen, unsigned long *pulTxAddress );
\r
213 /* Convert a number to a string. */
\r
214 void ultoa( unsigned long ulVal, char *pcBuffer, long lIgnore );
\r
216 /*-----------------------------------------------------------*/
\r
218 void ultoa( unsigned long ulVal, char *pcBuffer, long lIgnore )
\r
220 unsigned long lNibble;
\r
223 /* Simple routine to convert an unsigned long value into a string in hex
\r
226 /* For each nibble in the number we are converting. */
\r
227 for( lIndex = 0; lIndex < ( sizeof( ulVal ) * 2 ); lIndex++ )
\r
229 /* Take the top four bits of the number. */
\r
230 lNibble = ( ulVal >> 28 );
\r
232 /* We are converting it to a hex string, so is the number in the range
\r
236 pcBuffer[ lIndex ] = '0' + lNibble;
\r
241 pcBuffer[ lIndex ] = 'A' + lNibble;
\r
244 /* Shift off the top nibble so we use the next nibble next time around. */
\r
248 /* Mark the end of the string with a null terminator. */
\r
249 pcBuffer[ lIndex ] = 0x00;
\r
251 /*-----------------------------------------------------------*/
\r
253 static void prvReadRegister( unsigned char *pucDestination, unsigned short usAddress, unsigned long ulLength )
\r
255 unsigned char ucRxBuffer[ tcpMAX_REGISTER_LEN ];
\r
257 /* Read a register value from the WIZnet device. */
\r
259 /* First write out the address of the register we want to read. */
\r
260 i2cMessage( ucRxBuffer, i2cNO_DATA_REQUIRED, tcpDEVICE_ADDRESS, usAddress, i2cWRITE, NULL, portMAX_DELAY );
\r
262 /* Then read back from that address. */
\r
263 i2cMessage( ( unsigned char * ) pucDestination, ulLength, tcpDEVICE_ADDRESS, i2cNO_ADDR_REQUIRED, i2cREAD, xMessageComplete, portMAX_DELAY );
\r
265 /* I2C messages are queued so use the semaphore to wait for the read to
\r
266 complete - otherwise we will leave this function before the I2C
\r
267 transactions have completed. */
\r
268 xSemaphoreTake( xMessageComplete, tcpLONG_DELAY );
\r
270 /*-----------------------------------------------------------*/
\r
272 void vTCPHardReset( void )
\r
274 /* Physical reset of the WIZnet device by using the GPIO lines to hold the
\r
275 WIZnet reset lines active for a few milliseconds. */
\r
277 /* Make sure the interrupt from the WIZnet is disabled. */
\r
278 VICIntEnClear |= tcpEINT0_VIC_CHANNEL_BIT;
\r
280 /* If xMessageComplete is NULL then this is the first time that this
\r
281 function has been called and the queue and semaphore used in this file
\r
282 have not yet been created. */
\r
283 if( xMessageComplete == NULL )
\r
285 /* Create and obtain the semaphore used when we want to wait for an i2c
\r
286 message to be completed. */
\r
287 vSemaphoreCreateBinary( xMessageComplete );
\r
288 xSemaphoreTake( xMessageComplete, tcpNO_DELAY );
\r
290 /* Create the queue used to communicate between the WIZnet and TCP tasks. */
\r
291 xTCPISRQueue = xQueueCreate( tcpISR_QUEUE_LENGTH, tcpISR_QUEUE_ITEM_SIZE );
\r
294 /* Use the GPIO to reset the network hardware. */
\r
295 GPIO_IOCLR = tcpRESET_ACTIVE_LOW;
\r
296 GPIO_IOSET = tcpRESET_ACTIVE_HIGH;
\r
298 /* Delay with the network hardware in reset for a short while. */
\r
299 vTaskDelay( tcpRESET_DELAY );
\r
301 GPIO_IOCLR = tcpRESET_ACTIVE_HIGH;
\r
302 GPIO_IOSET = tcpRESET_ACTIVE_LOW;
\r
304 vTaskDelay( tcpINIT_DELAY );
\r
306 /* Setup the EINT0 to interrupt on required events from the WIZnet device.
\r
307 First enable the EINT0 function of the pin. */
\r
308 PCB_PINSEL1 |= tcpENABLE_EINT0_FUNCTION;
\r
310 /* We want the TCP comms to wake us from power save. */
\r
311 SCB_EXTWAKE = tcpWAKE_ON_EINT0;
\r
313 /* Install the ISR into the VIC - but don't enable it yet! */
\r
314 portENTER_CRITICAL();
\r
316 extern void ( vEINT0_ISR_Wrapper )( void );
\r
318 VICIntSelect &= ~( tcpEINT0_VIC_CHANNEL_BIT );
\r
319 VICVectAddr3 = ( long ) vEINT0_ISR_Wrapper;
\r
321 VICVectCntl3 = tcpEINT0_VIC_CHANNEL | tcpEINT0_VIC_ENABLE;
\r
323 portEXIT_CRITICAL();
\r
325 /* Enable interrupts in the WIZnet itself. */
\r
326 i2cMessage( ucDataEnableISR, sizeof( ucDataEnableISR ), tcpDEVICE_ADDRESS, tcpISR_MASK_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
328 vTaskDelay( tcpLONG_DELAY );
\r
330 /*-----------------------------------------------------------*/
\r
332 long lTCPSoftReset( void )
\r
334 unsigned char ucStatus;
\r
335 extern volatile long lTransactionCompleted;
\r
337 /* Send a message to the WIZnet device to tell it set all it's registers
\r
338 back to their default states. Then setup the WIZnet device as required. */
\r
340 /* Reset the internal WIZnet registers. */
\r
341 i2cMessage( ucDataReset, sizeof( ucDataReset ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
343 /* Now we can configure the protocol. Here the MAC address, gateway
\r
344 address, subnet mask and IP address are configured. */
\r
345 i2cMessage( ucDataSHAR, sizeof( ucDataSHAR ), tcpDEVICE_ADDRESS, tcpSOURCE_HA_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
346 i2cMessage( ucDataGAR, sizeof( ucDataGAR ), tcpDEVICE_ADDRESS, tcpGATEWAY_ADDR_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
347 i2cMessage( ucDataMSR, sizeof( ucDataMSR ), tcpDEVICE_ADDRESS, tcpSUBNET_MASK_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
348 i2cMessage( ucDataSIPR, sizeof( ucDataSIPR ), tcpDEVICE_ADDRESS, tpcSOURCE_IP_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
350 /* Next the memory buffers are configured to give all the WIZnet internal
\r
351 memory over to a single socket. This gives the socket the maximum internal
\r
352 Tx and Rx buffer space. */
\r
353 i2cMessage( ucDataSetTxBufSize, sizeof( ucDataSetTxBufSize ), tcpDEVICE_ADDRESS, tcpTX_MEM_SIZE_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
354 i2cMessage( ucDataSetRxBufSize, sizeof( ucDataSetRxBufSize ), tcpDEVICE_ADDRESS, tcpRX_MEM_SIZE_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
356 /* Send the sys init command so the above parameters take effect. */
\r
357 i2cMessage( ucDataInit, sizeof( ucDataInit ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
359 /* Seems to like a little wait here. */
\r
360 vTaskDelay( tcpINIT_DELAY );
\r
362 /* Read back the status to ensure the system initialised ok. */
\r
363 prvReadRegister( &ucStatus, tcpINTERRUPT_STATUS_REG, tcpSTATUS_READ_LEN );
\r
365 /* We should find that the sys init was successful. */
\r
366 if( ucStatus != tcpISR_SYS_INIT )
\r
368 return ( long ) pdFAIL;
\r
371 /* No i2c errors yet. */
\r
372 portENTER_CRITICAL();
\r
373 lTransactionCompleted = pdTRUE;
\r
374 portEXIT_CRITICAL();
\r
376 return ( long ) pdPASS;
\r
378 /*-----------------------------------------------------------*/
\r
380 long lTCPCreateSocket( void )
\r
382 unsigned char ucStatus;
\r
384 /* Create and configure a socket. */
\r
386 /* Setup and init the socket. Here the port number is set and the socket
\r
388 i2cMessage( ucDataProtocol, sizeof( ucDataProtocol),tcpDEVICE_ADDRESS, tpcSOCKET_OPT_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
389 i2cMessage( ucDataPort, sizeof( ucDataPort), tcpDEVICE_ADDRESS, tcpSOURCE_PORT_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
390 i2cMessage( ucDataSockInit, sizeof( ucDataSockInit),tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );
\r
392 /* Wait for the Init command to be sent. */
\r
393 if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
\r
395 /* For some reason the message was not transmitted within our block
\r
397 return ( long ) pdFAIL;
\r
400 /* Allow the socket to initialise. */
\r
401 vTaskDelay( tcpINIT_DELAY );
\r
403 /* Read back the status to ensure the socket initialised ok. */
\r
404 prvReadRegister( &ucStatus, tcpINTERRUPT_STATUS_REG, tcpSTATUS_READ_LEN );
\r
406 /* We should find that the socket init was successful. */
\r
407 if( ucStatus != tcpISR_SOCKET_INIT )
\r
409 return ( long ) pdFAIL;
\r
413 /* Setup the Tx pointer registers to indicate that the Tx buffer is empty. */
\r
414 i2cMessage( ucDataTxReadPointer, sizeof( ucDataTxReadPointer ), tcpDEVICE_ADDRESS, tcpTX_READ_POINTER_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
415 vTaskDelay( tcpSHORT_DELAY );
\r
416 i2cMessage( ucDataTxWritePointer, sizeof( ucDataTxWritePointer ), tcpDEVICE_ADDRESS, tcpTX_WRITE_POINTER_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
417 vTaskDelay( tcpSHORT_DELAY );
\r
418 i2cMessage( ucDataTxAckPointer, sizeof( ucDataTxAckPointer ), tcpDEVICE_ADDRESS, tcpTX_ACK_POINTER_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
419 vTaskDelay( tcpSHORT_DELAY );
\r
421 return ( long ) pdPASS;
\r
423 /*-----------------------------------------------------------*/
\r
425 void vTCPListen( void )
\r
427 unsigned char ucISR;
\r
429 /* Start a passive listen on the socket. */
\r
431 /* Enable interrupts in the WizNet device after ensuring none are
\r
432 currently pending. */
\r
433 while( SCB_EXTINT & tcpCLEAR_EINT0 )
\r
435 /* The WIZnet device is still asserting and interrupt so tell it to
\r
437 i2cMessage( ucDataClearInterrupt, sizeof( ucDataClearInterrupt ), tcpDEVICE_ADDRESS, tcpINTERRUPT_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );
\r
438 xSemaphoreTake( xMessageComplete, tcpLONG_DELAY );
\r
441 SCB_EXTINT = tcpCLEAR_EINT0;
\r
444 while( xQueueReceive( xTCPISRQueue, &ucISR, tcpNO_DELAY ) )
\r
446 /* Just clearing the queue used by the ISR routine to tell this task
\r
447 that the WIZnet device needs attention. */
\r
450 /* Now all the pending interrupts have been cleared we can enable the
\r
451 processor interrupts. */
\r
452 VICIntEnable |= tcpEINT0_VIC_CHANNEL_BIT;
\r
454 /* Then start listening for incoming connections. */
\r
455 i2cMessage( ucDataListen, sizeof( ucDataListen ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
457 /*-----------------------------------------------------------*/
\r
459 long lProcessConnection( void )
\r
461 unsigned char ucISR, ucState, ucLastState = 2, ucShadow;
\r
462 extern volatile long lTransactionCompleted;
\r
463 long lSameStateCount = 0, lDataSent = pdFALSE;
\r
464 unsigned long ulWritePointer, ulAckPointer;
\r
466 /* No I2C errors can yet have occurred. */
\r
467 portENTER_CRITICAL();
\r
468 lTransactionCompleted = pdTRUE;
\r
469 portEXIT_CRITICAL();
\r
471 /* Keep looping - processing interrupts, until we have completed a
\r
472 transaction. This uses the WIZnet in it's simplest form. The socket
\r
473 accepts a connection - we process the connection - then close the socket.
\r
474 We then go back to reinitialise everything and start again. */
\r
475 while( lTransactionCompleted == pdTRUE )
\r
477 /* Wait for a message on the queue from the WIZnet ISR. When the
\r
478 WIZnet device asserts an interrupt the ISR simply posts a message
\r
479 onto this queue to wake this task. */
\r
480 if( xQueueReceive( xTCPISRQueue, &ucISR, tcpCONNECTION_WAIT_DELAY ) )
\r
482 /* The ISR posted a message on this queue to tell us that the
\r
483 WIZnet device asserted an interrupt. The ISR cannot process
\r
484 an I2C message so cannot tell us what caused the interrupt so
\r
485 we have to query the device here. This task is the highest
\r
486 priority in the system so will run immediately following the ISR. */
\r
487 prvReadRegister( &ucISR, tcpINTERRUPT_STATUS_REG, tcpSTATUS_READ_LEN );
\r
489 /* Once we have read what caused the ISR we can clear the interrupt
\r
491 i2cMessage( ucDataClearInterrupt, sizeof( ucDataClearInterrupt ), tcpDEVICE_ADDRESS, tcpINTERRUPT_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
493 /* Now we can clear the processor interrupt and re-enable ready for
\r
495 SCB_EXTINT = tcpCLEAR_EINT0;
\r
496 VICIntEnable |= tcpEINT0_VIC_CHANNEL_BIT;
\r
498 /* Process the interrupt ... */
\r
500 if( ucISR & tcpISR_ESTABLISHED )
\r
502 /* A connection has been established - respond by sending
\r
503 a receive command. */
\r
504 i2cMessage( ucDataReceiveCmd, sizeof( ucDataReceiveCmd ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
507 if( ucISR & tcpISR_RX_COMPLETE )
\r
509 /* We message has been received. This will be an HTTP get
\r
510 command. We only have one page to send so just send it without
\r
511 regard to what the actual requested page was. */
\r
512 prvSendSamplePage();
\r
515 if( ucISR & tcpISR_TX_COMPLETE )
\r
517 /* We have a TX complete interrupt - which oddly does not
\r
518 indicate that the message being sent is complete so we cannot
\r
519 yet close the socket. Instead we read the position of the Tx
\r
520 pointer within the WIZnet device so we know how much data it
\r
521 has to send. Later we will read the ack pointer and compare
\r
522 this to the Tx pointer to ascertain whether the transmission
\r
525 /* First read the shadow register. */
\r
526 prvReadRegister( &ucShadow, tcpTX_WRITE_SHADOW_REG, tcpSHADOW_READ_LEN );
\r
528 /* Now a short delay is required. */
\r
529 vTaskDelay( tcpSHORT_DELAY );
\r
531 /* Then we can read the real register. */
\r
532 prvReadRegister( ( unsigned char * ) &ulWritePointer, tcpTX_WRITE_POINTER_REG, sizeof( ulWritePointer ) );
\r
534 /* We cannot do anything more here but need to remember that
\r
535 this interrupt has occurred. */
\r
536 lDataSent = pdTRUE;
\r
539 if( ucISR & tcpISR_CLOSED )
\r
541 /* The socket has been closed so we can leave this function. */
\r
542 lTransactionCompleted = pdFALSE;
\r
547 /* We have not received an interrupt from the WIZnet device for a
\r
548 while. Read the socket status and check that everything is as
\r
550 prvReadRegister( &ucState, tcpSOCKET_STATE_REG, tcpSTATUS_READ_LEN );
\r
552 if( ( ucState == tcpSTATUS_ESTABLISHED ) && ( lDataSent > 0 ) )
\r
554 /* The socket is established and we have already received a Tx
\r
555 end interrupt. We must therefore be waiting for the Tx buffer
\r
556 inside the WIZnet device to be empty before we can close the
\r
559 Read the Ack pointer register to see if it has caught up with
\r
560 the Tx pointer register. First we have to read the shadow
\r
562 prvReadRegister( &ucShadow, tcpTX_ACK_SHADOW_REG, tcpSHADOW_READ_LEN );
\r
563 vTaskDelay( tcpSHORT_DELAY );
\r
564 prvReadRegister( ( unsigned char * ) &ulAckPointer, tcpTX_ACK_POINTER_REG, sizeof( ulWritePointer ) );
\r
566 if( ulAckPointer == ulWritePointer )
\r
568 /* The Ack and write pointer are now equal and we can
\r
569 safely close the socket. */
\r
570 i2cMessage( ucDataDisconnect, sizeof( ucDataDisconnect ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
574 /* Keep a count of how many times we encounter the Tx
\r
575 buffer still containing data. */
\r
577 if( lDataSent > tcpMAX_ATTEMPTS_TO_CHECK_BUFFER )
\r
579 /* Assume we cannot complete sending the data and
\r
580 therefore cannot safely close the socket. Start over. */
\r
582 lTransactionCompleted = pdFALSE;
\r
586 else if( ucState != tcpSTATUS_LISTEN )
\r
588 /* If we have not yet received a Tx end interrupt we would only
\r
589 ever expect to find the socket still listening for any
\r
590 sustained period. */
\r
591 if( ucState == ucLastState )
\r
594 if( lSameStateCount > tcpMAX_NON_LISTEN_STAUS_READS )
\r
596 /* We are persistently in an unexpected state. Assume
\r
597 we cannot safely close the socket and start over. */
\r
599 lTransactionCompleted = pdFALSE;
\r
605 /* We are in the listen state so are happy that everything
\r
607 lSameStateCount = 0;
\r
610 /* Remember what state we are in this time around so we can check
\r
611 for a persistence on an unexpected state. */
\r
612 ucLastState = ucState;
\r
616 /* We are going to reinitialise the WIZnet device so do not want our
\r
617 interrupts from the WIZnet to be processed. */
\r
618 VICIntEnClear |= tcpEINT0_VIC_CHANNEL_BIT;
\r
619 return lTransactionCompleted;
\r
621 /*-----------------------------------------------------------*/
\r
623 static void prvWriteString( const char * const pucTxBuffer, long lTxLen, unsigned long *pulTxAddress )
\r
625 unsigned long ulSendAddress;
\r
627 /* Send a string to the Tx buffer internal to the WIZnet device. */
\r
629 /* Calculate the address to which we are going to write in the buffer. */
\r
630 ulSendAddress = ( *pulTxAddress & tcpSINGLE_SOCKET_ADDR_MASK ) + tcpSINGLE_SOCKET_ADDR_OFFSET;
\r
632 /* Send the buffer to the calculated address. Use the semaphore so we
\r
633 can wait until the entire message has been transferred. */
\r
634 i2cMessage( ( unsigned char * ) pucTxBuffer, lTxLen, tcpDEVICE_ADDRESS, ( unsigned short ) ulSendAddress, i2cWRITE, xMessageComplete, portMAX_DELAY );
\r
636 /* Wait until the semaphore indicates that the message has been transferred. */
\r
637 if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
\r
642 /* Return the new address of the end of the buffer (within the WIZnet
\r
644 *pulTxAddress += ( unsigned long ) lTxLen;
\r
646 /*-----------------------------------------------------------*/
\r
648 static void prvFlushBuffer( unsigned long ulTxAddress )
\r
650 unsigned char ucTxBuffer[ tcpMAX_REGISTER_LEN ];
\r
652 /* We have written some data to the Tx buffer internal to the WIZnet
\r
653 device. Now we update the Tx pointer inside the WIZnet then send a
\r
654 Send command - which causes the data up to the new Tx pointer to be
\r
657 /* Make sure endieness is correct for transmission. */
\r
658 ulTxAddress = htonl( ulTxAddress );
\r
660 /* Place the new Tx pointer in the string to be transmitted. */
\r
661 ucTxBuffer[ 0 ] = ( unsigned char ) ( ulTxAddress & 0xff );
\r
663 ucTxBuffer[ 1 ] = ( unsigned char ) ( ulTxAddress & 0xff );
\r
665 ucTxBuffer[ 2 ] = ( unsigned char ) ( ulTxAddress & 0xff );
\r
667 ucTxBuffer[ 3 ] = ( unsigned char ) ( ulTxAddress & 0xff );
\r
670 /* And send it to the WIZnet device. */
\r
671 i2cMessage( ucTxBuffer, sizeof( ulTxAddress ), tcpDEVICE_ADDRESS, tcpTX_WRITE_POINTER_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );
\r
673 if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
\r
678 vTaskDelay( tcpSHORT_DELAY );
\r
681 i2cMessage( ucDataSend, sizeof( ucDataSend ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );
\r
683 if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
\r
688 /*-----------------------------------------------------------*/
\r
690 static void prvSendSamplePage( void )
\r
692 extern long lErrorInTask;
\r
693 unsigned long ulTxAddress;
\r
694 unsigned char ucShadow;
\r
696 static unsigned long ulRefreshCount = 0x00;
\r
697 static char cPageBuffer[ tcpBUFFER_LEN ];
\r
700 /* This function just generates a sample page of HTML which gets
\r
701 sent each time a client attaches to the socket. The page is created
\r
702 from two fixed strings (cSamplePageFirstPart and cSamplePageSecondPart)
\r
703 with a bit of dynamically generated data in the middle. */
\r
705 /* We need to know the address to which the html string should be sent
\r
706 in the WIZnet Tx buffer. First read the shadow register. */
\r
707 prvReadRegister( &ucShadow, tcpTX_WRITE_SHADOW_REG, tcpSHADOW_READ_LEN );
\r
709 /* Now a short delay is required. */
\r
710 vTaskDelay( tcpSHORT_DELAY );
\r
712 /* Now we can read the real pointer value. */
\r
713 prvReadRegister( ( unsigned char * ) &ulTxAddress, tcpTX_WRITE_POINTER_REG, sizeof( ulTxAddress ) );
\r
715 /* Make sure endieness is correct. */
\r
716 ulTxAddress = htonl( ulTxAddress );
\r
718 /* Send the start of the page. */
\r
719 prvWriteString( cSamplePageFirstPart, strlen( cSamplePageFirstPart ), &ulTxAddress );
\r
721 /* Generate a bit of dynamic data and place it in the buffer ready to be
\r
723 strcpy( cPageBuffer, "<BR>Number of ticks since boot = 0x" );
\r
724 lIndex = strlen( cPageBuffer );
\r
725 ultoa( xTaskGetTickCount(), &( cPageBuffer[ lIndex ] ), 0 );
\r
726 strcat( cPageBuffer, "<br>Number of tasks executing = ");
\r
727 lIndex = strlen( cPageBuffer );
\r
728 ultoa( ( unsigned long ) uxTaskGetNumberOfTasks(), &( cPageBuffer[ lIndex ] ), 0 );
\r
729 strcat( cPageBuffer, "<br>IO port 0 state (used by flash tasks) = 0x" );
\r
730 lIndex = strlen( cPageBuffer );
\r
731 ultoa( ( unsigned long ) GPIO0_IOPIN, &( cPageBuffer[ lIndex ] ), 0 );
\r
732 strcat( cPageBuffer, "<br>Refresh = 0x" );
\r
733 lIndex = strlen( cPageBuffer );
\r
734 ultoa( ( unsigned long ) ulRefreshCount, &( cPageBuffer[ lIndex ] ), 0 );
\r
738 strcat( cPageBuffer, "<p>An error has occurred in at least one task." );
\r
742 strcat( cPageBuffer, "<p>All tasks executing without error." );
\r
747 /* Send the dynamically generated string. */
\r
748 prvWriteString( cPageBuffer, strlen( cPageBuffer ), &ulTxAddress );
\r
750 /* Finish the page. */
\r
751 prvWriteString( cSamplePageSecondPart, strlen( cSamplePageSecondPart ), &ulTxAddress );
\r
753 /* Tell the WIZnet to send the data we have just written to its Tx buffer. */
\r
754 prvFlushBuffer( ulTxAddress );
\r