]> git.sur5r.net Git - freertos/blob - Demo/WizNET_DEMO_GCC_ARM7/TCP.c
Ready for V5.2.0 release.
[freertos] / Demo / WizNET_DEMO_GCC_ARM7 / TCP.c
1 /*\r
2         FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it \r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9 \r
10         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
11         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
12         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for \r
13         more details.\r
14 \r
15         You should have received a copy of the GNU General Public License along \r
16         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 \r
17         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
18 \r
19         A special exception to the GPL is included to allow you to distribute a \r
20         combined work that includes FreeRTOS.org without being obliged to provide\r
21         the source code for any proprietary components.  See the licensing section\r
22         of http://www.FreeRTOS.org for full details.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /*\r
53         Changes from V3.2.3\r
54         \r
55         + Modified char* types to compile without warning when using GCC V4.0.1.\r
56         + Corrected the address to which the MAC address is written.  Thanks to\r
57           Bill Knight for this correction.\r
58 \r
59         Changes from V3.2.4\r
60 \r
61         + Changed the default MAC address to something more realistic.\r
62 \r
63 */\r
64 \r
65 /* Standard includes. */\r
66 #include <stdlib.h>\r
67 #include <string.h>\r
68 \r
69 /* Scheduler include files. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 #include "semphr.h"\r
73 #include "tcp.h"\r
74 #include "serial.h"\r
75 \r
76 /* Application includes. */\r
77 #include "i2c.h"\r
78 #include "html_pages.h"\r
79 \r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /* Hardwired i2c address of the WIZNet device. */\r
83 #define tcpDEVICE_ADDRESS                               ( ( unsigned portCHAR ) 0x00 )\r
84 \r
85 /* Constants used to configure the Tx and Rx buffer sizes within the WIZnet\r
86 device. */\r
87 #define tcp8K_RX                                                ( ( unsigned portCHAR ) 0x03 )\r
88 #define tcp8K_TX                                                ( ( unsigned portCHAR ) 0x03 )\r
89 \r
90 /* Constants used to generate the WIZnet internal buffer addresses. */\r
91 #define tcpSINGLE_SOCKET_ADDR_MASK              ( ( unsigned portLONG ) 0x1fff )\r
92 #define tcpSINGLE_SOCKET_ADDR_OFFSET    ( ( unsigned portLONG ) 0x4000 )\r
93 \r
94 /* Bit definitions of the commands that can be sent to the command register. */\r
95 #define tcpRESET_CMD                                    ( ( unsigned portCHAR ) 0x80 )\r
96 #define tcpSYS_INIT_CMD                                 ( ( unsigned portCHAR ) 0x01 )\r
97 #define tcpSOCK_STREAM                                  ( ( unsigned portCHAR ) 0x01 )\r
98 #define tcpSOCK_INIT                                    ( ( unsigned portCHAR ) 0x02 )\r
99 #define tcpLISTEN_CMD                                   ( ( unsigned portCHAR ) 0x08 )\r
100 #define tcpRECEIVE_CMD                                  ( ( unsigned portCHAR ) 0x40 )\r
101 #define tcpDISCONNECT_CMD                               ( ( unsigned portCHAR ) 0x10 )\r
102 #define tcpSEND_CMD                                             ( ( unsigned portCHAR ) 0x20 )\r
103 \r
104 /* Constants required to handle the interrupts. */\r
105 #define tcpCLEAR_EINT0                                  ( 1 )\r
106 #define i2cCLEAR_ALL_INTERRUPTS                 ( ( unsigned portCHAR ) 0xff )\r
107 #define i2cCHANNEL_0_ISR_ENABLE                 ( ( unsigned portCHAR ) 0x01 )\r
108 #define i2cCHANNEL_0_ISR_DISABLE                ( ( unsigned portCHAR ) 0x00 )\r
109 #define tcpWAKE_ON_EINT0                                ( 1 )\r
110 #define tcpENABLE_EINT0_FUNCTION                ( ( unsigned portLONG ) 0x01 )\r
111 #define tcpEINT0_VIC_CHANNEL_BIT                ( ( unsigned portLONG ) 0x4000 )\r
112 #define tcpEINT0_VIC_CHANNEL                    ( ( unsigned portLONG ) 14 )\r
113 #define tcpEINT0_VIC_ENABLE                             ( ( unsigned portLONG ) 0x0020 )\r
114 \r
115 /* Various delays used in the driver. */\r
116 #define tcpRESET_DELAY                                  ( ( portTickType ) 16 / portTICK_RATE_MS )\r
117 #define tcpINIT_DELAY                                   ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
118 #define tcpLONG_DELAY                                   ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
119 #define tcpSHORT_DELAY                                  ( ( portTickType ) 5 / portTICK_RATE_MS )\r
120 #define tcpCONNECTION_WAIT_DELAY                ( ( portTickType ) 100 / portTICK_RATE_MS )\r
121 #define tcpNO_DELAY                                             ( ( portTickType ) 0 )\r
122 \r
123 /* Length of the data to read for various register reads. */\r
124 #define tcpSTATUS_READ_LEN                              ( ( unsigned portLONG ) 1 )\r
125 #define tcpSHADOW_READ_LEN                              ( ( unsigned portLONG ) 1 )\r
126         \r
127 /* Register addresses within the WIZnet device. */\r
128 #define tcpCOMMAND_REG                                  ( ( unsigned portSHORT ) 0x0000 )\r
129 #define tcpGATEWAY_ADDR_REG                             ( ( unsigned portSHORT ) 0x0080 )\r
130 #define tcpSUBNET_MASK_REG                              ( ( unsigned portSHORT ) 0x0084 )\r
131 #define tcpSOURCE_HA_REG                                ( ( unsigned portSHORT ) 0x0088 )\r
132 #define tpcSOURCE_IP_REG                                ( ( unsigned portSHORT ) 0x008E )\r
133 #define tpcSOCKET_OPT_REG                               ( ( unsigned portSHORT ) 0x00A1 )\r
134 #define tcpSOURCE_PORT_REG                              ( ( unsigned portSHORT ) 0x00AE )\r
135 #define tcpTX_WRITE_POINTER_REG                 ( ( unsigned portSHORT ) 0x0040 )\r
136 #define tcpTX_READ_POINTER_REG                  ( ( unsigned portSHORT ) 0x0044 )\r
137 #define tcpTX_ACK_POINTER_REG                   ( ( unsigned portSHORT ) 0x0018 )\r
138 #define tcpTX_MEM_SIZE_REG                              ( ( unsigned portSHORT ) 0x0096 )\r
139 #define tcpRX_MEM_SIZE_REG                              ( ( unsigned portSHORT ) 0x0095 )\r
140 #define tcpINTERRUPT_STATUS_REG                 ( ( unsigned portSHORT ) 0x0004 )\r
141 #define tcpTX_WRITE_SHADOW_REG                  ( ( unsigned portSHORT ) 0x01F0 )\r
142 #define tcpTX_ACK_SHADOW_REG                    ( ( unsigned portSHORT ) 0x01E2 )\r
143 #define tcpISR_MASK_REG                                 ( ( unsigned portSHORT ) 0x0009 )\r
144 #define tcpINTERRUPT_REG                                ( ( unsigned portSHORT ) 0x0008 )\r
145 #define tcpSOCKET_STATE_REG                             ( ( unsigned portSHORT ) 0x00a0 )\r
146 \r
147 /* Constants required for hardware setup. */\r
148 #define tcpRESET_ACTIVE_LOW                     ( ( unsigned portLONG ) 0x20 )\r
149 #define tcpRESET_ACTIVE_HIGH                    ( ( unsigned portLONG ) 0x10 )\r
150 \r
151 /* Constants defining the source of the WIZnet ISR. */\r
152 #define tcpISR_SYS_INIT                                 ( ( unsigned portCHAR ) 0x01 )\r
153 #define tcpISR_SOCKET_INIT                              ( ( unsigned portCHAR ) 0x02 )\r
154 #define tcpISR_ESTABLISHED                              ( ( unsigned portCHAR ) 0x04 )\r
155 #define tcpISR_CLOSED                                   ( ( unsigned portCHAR ) 0x08 )\r
156 #define tcpISR_TIMEOUT                                  ( ( unsigned portCHAR ) 0x10 )\r
157 #define tcpISR_TX_COMPLETE                              ( ( unsigned portCHAR ) 0x20 )\r
158 #define tcpISR_RX_COMPLETE                              ( ( unsigned portCHAR ) 0x40 )\r
159 \r
160 /* Constants defining the socket status bits. */\r
161 #define tcpSTATUS_ESTABLISHED                   ( ( unsigned portCHAR ) 0x06 )\r
162 #define tcpSTATUS_LISTEN                                ( ( unsigned portCHAR ) 0x02 )\r
163 \r
164 /* Misc constants. */\r
165 #define tcpNO_STATUS_BITS                               ( ( unsigned portCHAR ) 0x00 )\r
166 #define i2cNO_ADDR_REQUIRED                             ( ( unsigned portSHORT ) 0x0000 )\r
167 #define i2cNO_DATA_REQUIRED                             ( 0x0000 )\r
168 #define tcpISR_QUEUE_LENGTH                             ( ( unsigned portBASE_TYPE ) 10 )\r
169 #define tcpISR_QUEUE_ITEM_SIZE                  ( ( unsigned portBASE_TYPE ) 0 )\r
170 #define tcpBUFFER_LEN                                   ( 4 * 1024 )\r
171 #define tcpMAX_REGISTER_LEN                             ( 4 )\r
172 #define tcpMAX_ATTEMPTS_TO_CHECK_BUFFER ( 6 )\r
173 #define tcpMAX_NON_LISTEN_STAUS_READS   ( 5 )\r
174 \r
175 /* Message definitions.  The IP address, MAC address, gateway address, etc.\r
176 is set here! */\r
177 const unsigned portCHAR const ucDataGAR[]                               = { 172, 25, 218, 3 };  /* Gateway address. */\r
178 const unsigned portCHAR const ucDataMSR[]                               = { 255, 255, 255, 0 }; /* Subnet mask.         */\r
179 const unsigned portCHAR const ucDataSIPR[]                              = { 172, 25, 218, 201 };/* IP address.          */\r
180 const unsigned portCHAR const ucDataSHAR[]                              = { 00, 23, 30, 41, 15, 26 }; /* MAC address - DO NOT USE THIS ON A PUBLIC NETWORK! */\r
181 \r
182 /* Other fixed messages. */\r
183 const unsigned portCHAR const ucDataReset[]                             = { tcpRESET_CMD }; \r
184 const unsigned portCHAR const ucDataInit[]                              = { tcpSYS_INIT_CMD }; \r
185 const unsigned portCHAR const ucDataProtocol[]                  = { tcpSOCK_STREAM };\r
186 const unsigned portCHAR const ucDataPort[]                              = { 0xBA, 0xCC };\r
187 const unsigned portCHAR const ucDataSockInit[]                  = { tcpSOCK_INIT };\r
188 const unsigned portCHAR const ucDataTxWritePointer[]    = { 0x11, 0x22, 0x00, 0x00 };\r
189 const unsigned portCHAR const ucDataTxAckPointer[]              = { 0x11, 0x22, 0x00, 0x00 };\r
190 const unsigned portCHAR const ucDataTxReadPointer[]             = { 0x11, 0x22, 0x00, 0x00 };\r
191 const unsigned portCHAR const ucDataListen[]                    = { tcpLISTEN_CMD };\r
192 const unsigned portCHAR const ucDataReceiveCmd[]                = { tcpRECEIVE_CMD };\r
193 const unsigned portCHAR const ucDataSetTxBufSize[]              = { tcp8K_TX };\r
194 const unsigned portCHAR const ucDataSetRxBufSize[]              = { tcp8K_RX };\r
195 const unsigned portCHAR const ucDataSend[]                              = { tcpSEND_CMD };\r
196 const unsigned portCHAR const ucDataDisconnect[]                = { tcpDISCONNECT_CMD };\r
197 const unsigned portCHAR const ucDataEnableISR[]                 = { i2cCHANNEL_0_ISR_ENABLE };\r
198 const unsigned portCHAR const ucDataDisableISR[]                = { i2cCHANNEL_0_ISR_DISABLE };\r
199 const unsigned portCHAR const ucDataClearInterrupt[]    = { i2cCLEAR_ALL_INTERRUPTS };\r
200 \r
201 static xSemaphoreHandle xMessageComplete = NULL;\r
202 xQueueHandle xTCPISRQueue = NULL;\r
203 \r
204 /* Dynamically generate and send an html page. */\r
205 static void prvSendSamplePage( void );\r
206 \r
207 /* Read a register from the WIZnet device via the i2c interface. */\r
208 static void prvReadRegister( unsigned portCHAR *pucDestination, unsigned portSHORT usAddress, unsigned portLONG ulLength );\r
209 \r
210 /* Send the entire Tx buffer (the Tx buffer within the WIZnet device). */\r
211 static void prvFlushBuffer( unsigned portLONG ulTxAddress );\r
212 \r
213 /* Write a string to the WIZnet Tx buffer. */\r
214 static void prvWriteString( const portCHAR * const pucTxBuffer, portLONG lTxLen, unsigned portLONG *pulTxAddress );\r
215 \r
216 /* Convert a number to a string. */\r
217 void ultoa( unsigned portLONG ulVal, portCHAR *pcBuffer, portLONG lIgnore );\r
218 \r
219 /*-----------------------------------------------------------*/\r
220 \r
221 void ultoa( unsigned portLONG ulVal, portCHAR *pcBuffer, portLONG lIgnore )\r
222 {\r
223 unsigned portLONG lNibble;\r
224 portLONG lIndex;\r
225 \r
226         /* Simple routine to convert an unsigned long value into a string in hex \r
227         format. */\r
228 \r
229         /* For each nibble in the number we are converting. */\r
230         for( lIndex = 0; lIndex < ( sizeof( ulVal ) * 2 ); lIndex++ )\r
231         {\r
232                 /* Take the top four bits of the number. */\r
233                 lNibble = ( ulVal >> 28 );\r
234 \r
235                 /* We are converting it to a hex string, so is the number in the range\r
236                 0-10 or A-F? */\r
237                 if( lNibble < 10 )\r
238                 {\r
239                         pcBuffer[ lIndex ] = '0' + lNibble;\r
240                 }\r
241                 else\r
242                 {\r
243                         lNibble -= 10;\r
244                         pcBuffer[ lIndex ] = 'A' + lNibble;\r
245                 }\r
246 \r
247                 /* Shift off the top nibble so we use the next nibble next time around. */\r
248                 ulVal <<= 4;\r
249         }       \r
250 \r
251         /* Mark the end of the string with a null terminator. */\r
252         pcBuffer[ lIndex ] = 0x00;\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 static void prvReadRegister( unsigned portCHAR *pucDestination, unsigned portSHORT usAddress, unsigned portLONG ulLength )\r
257 {\r
258 unsigned portCHAR ucRxBuffer[ tcpMAX_REGISTER_LEN ];\r
259 \r
260         /* Read a register value from the WIZnet device. */\r
261 \r
262         /* First write out the address of the register we want to read. */\r
263         i2cMessage( ucRxBuffer, i2cNO_DATA_REQUIRED, tcpDEVICE_ADDRESS, usAddress, i2cWRITE, NULL, portMAX_DELAY );\r
264         \r
265         /* Then read back from that address. */\r
266         i2cMessage( ( unsigned portCHAR * ) pucDestination, ulLength, tcpDEVICE_ADDRESS, i2cNO_ADDR_REQUIRED, i2cREAD, xMessageComplete, portMAX_DELAY );\r
267 \r
268         /* I2C messages are queued so use the semaphore to wait for the read to \r
269         complete - otherwise we will leave this function before the I2C \r
270         transactions have completed. */\r
271         xSemaphoreTake( xMessageComplete, tcpLONG_DELAY );\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 void vTCPHardReset( void )\r
276 {\r
277         /* Physical reset of the WIZnet device by using the GPIO lines to hold the \r
278         WIZnet reset lines active for a few milliseconds. */\r
279 \r
280         /* Make sure the interrupt from the WIZnet is disabled. */\r
281         VICIntEnClear |= tcpEINT0_VIC_CHANNEL_BIT;\r
282 \r
283         /* If xMessageComplete is NULL then this is the first time that this \r
284         function has been called and the queue and semaphore used in this file\r
285         have not yet been created. */\r
286         if( xMessageComplete == NULL )\r
287         {\r
288                 /* Create and obtain the semaphore used when we want to wait for an i2c\r
289                 message to be completed. */\r
290                 vSemaphoreCreateBinary( xMessageComplete );\r
291                 xSemaphoreTake( xMessageComplete, tcpNO_DELAY );\r
292 \r
293                 /* Create the queue used to communicate between the WIZnet and TCP tasks. */\r
294                 xTCPISRQueue = xQueueCreate( tcpISR_QUEUE_LENGTH, tcpISR_QUEUE_ITEM_SIZE );\r
295         }\r
296 \r
297         /* Use the GPIO to reset the network hardware. */\r
298         GPIO_IOCLR = tcpRESET_ACTIVE_LOW;\r
299         GPIO_IOSET = tcpRESET_ACTIVE_HIGH;\r
300 \r
301         /* Delay with the network hardware in reset for a short while. */\r
302         vTaskDelay( tcpRESET_DELAY );\r
303 \r
304         GPIO_IOCLR = tcpRESET_ACTIVE_HIGH;\r
305         GPIO_IOSET = tcpRESET_ACTIVE_LOW;\r
306 \r
307         vTaskDelay( tcpINIT_DELAY );\r
308 \r
309         /* Setup the EINT0 to interrupt on required events from the WIZnet device.\r
310         First enable the EINT0 function of the pin. */\r
311         PCB_PINSEL1 |= tcpENABLE_EINT0_FUNCTION;\r
312         \r
313         /* We want the TCP comms to wake us from power save. */\r
314         SCB_EXTWAKE = tcpWAKE_ON_EINT0;\r
315 \r
316         /* Install the ISR into the VIC - but don't enable it yet! */\r
317         portENTER_CRITICAL();\r
318         {\r
319                 extern void ( vEINT0_ISR_Wrapper )( void );\r
320 \r
321                 VICIntSelect &= ~( tcpEINT0_VIC_CHANNEL_BIT );\r
322                 VICVectAddr3 = ( portLONG ) vEINT0_ISR_Wrapper;\r
323 \r
324                 VICVectCntl3 = tcpEINT0_VIC_CHANNEL | tcpEINT0_VIC_ENABLE;\r
325         }\r
326         portEXIT_CRITICAL();\r
327 \r
328         /* Enable interrupts in the WIZnet itself. */\r
329         i2cMessage( ucDataEnableISR, sizeof( ucDataEnableISR ), tcpDEVICE_ADDRESS, tcpISR_MASK_REG, i2cWRITE, NULL, portMAX_DELAY );\r
330 \r
331         vTaskDelay( tcpLONG_DELAY );\r
332 }\r
333 /*-----------------------------------------------------------*/\r
334 \r
335 portLONG lTCPSoftReset( void )\r
336 {\r
337 unsigned portCHAR ucStatus;\r
338 extern volatile portLONG lTransactionCompleted;\r
339 \r
340         /* Send a message to the WIZnet device to tell it set all it's registers\r
341         back to their default states.  Then setup the WIZnet device as required. */\r
342 \r
343         /* Reset the internal WIZnet registers. */\r
344         i2cMessage( ucDataReset,        sizeof( ucDataReset ),  tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );\r
345 \r
346         /* Now we can configure the protocol.   Here the MAC address, gateway \r
347         address, subnet mask and IP address are configured. */\r
348         i2cMessage( ucDataSHAR,         sizeof( ucDataSHAR ),   tcpDEVICE_ADDRESS, tcpSOURCE_HA_REG, i2cWRITE, NULL, portMAX_DELAY );\r
349         i2cMessage( ucDataGAR,          sizeof( ucDataGAR ),    tcpDEVICE_ADDRESS, tcpGATEWAY_ADDR_REG, i2cWRITE, NULL, portMAX_DELAY );\r
350         i2cMessage( ucDataMSR,          sizeof( ucDataMSR ),    tcpDEVICE_ADDRESS, tcpSUBNET_MASK_REG,  i2cWRITE, NULL, portMAX_DELAY );\r
351         i2cMessage( ucDataSIPR,         sizeof( ucDataSIPR ),   tcpDEVICE_ADDRESS, tpcSOURCE_IP_REG,    i2cWRITE, NULL, portMAX_DELAY );\r
352         \r
353         /* Next the memory buffers are configured to give all the WIZnet internal\r
354         memory over to a single socket.  This gives the socket the maximum internal\r
355         Tx and Rx buffer space. */\r
356         i2cMessage( ucDataSetTxBufSize, sizeof( ucDataSetTxBufSize ), tcpDEVICE_ADDRESS, tcpTX_MEM_SIZE_REG, i2cWRITE, NULL, portMAX_DELAY );\r
357         i2cMessage( ucDataSetRxBufSize, sizeof( ucDataSetRxBufSize ), tcpDEVICE_ADDRESS, tcpRX_MEM_SIZE_REG, i2cWRITE, NULL, portMAX_DELAY );\r
358 \r
359         /* Send the sys init command so the above parameters take effect. */\r
360         i2cMessage( ucDataInit,         sizeof( ucDataInit ),   tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );\r
361 \r
362         /* Seems to like a little wait here. */\r
363         vTaskDelay( tcpINIT_DELAY );\r
364 \r
365         /* Read back the status to ensure the system initialised ok. */\r
366         prvReadRegister( &ucStatus, tcpINTERRUPT_STATUS_REG, tcpSTATUS_READ_LEN );\r
367 \r
368         /* We should find that the sys init was successful. */\r
369         if( ucStatus != tcpISR_SYS_INIT )\r
370         {\r
371                 return ( portLONG ) pdFAIL;\r
372         }\r
373 \r
374         /* No i2c errors yet. */\r
375         portENTER_CRITICAL();\r
376                 lTransactionCompleted = pdTRUE;\r
377         portEXIT_CRITICAL();\r
378 \r
379         return ( portLONG ) pdPASS;\r
380 }\r
381 /*-----------------------------------------------------------*/\r
382 \r
383 portLONG lTCPCreateSocket( void )\r
384 {\r
385 unsigned portCHAR ucStatus;\r
386 \r
387         /* Create and configure a socket. */\r
388 \r
389         /* Setup and init the socket.  Here the port number is set and the socket\r
390         is initialised. */\r
391         i2cMessage( ucDataProtocol, sizeof( ucDataProtocol),tcpDEVICE_ADDRESS, tpcSOCKET_OPT_REG, i2cWRITE, NULL, portMAX_DELAY );\r
392         i2cMessage( ucDataPort,         sizeof( ucDataPort),    tcpDEVICE_ADDRESS, tcpSOURCE_PORT_REG, i2cWRITE, NULL, portMAX_DELAY );\r
393         i2cMessage( ucDataSockInit, sizeof( ucDataSockInit),tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );\r
394 \r
395         /* Wait for the Init command to be sent. */\r
396         if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )\r
397         {\r
398                 /* For some reason the message was not transmitted within our block\r
399                 period. */\r
400                 return ( portLONG ) pdFAIL;\r
401         }\r
402 \r
403         /* Allow the socket to initialise. */\r
404         vTaskDelay( tcpINIT_DELAY );\r
405 \r
406         /* Read back the status to ensure the socket initialised ok. */\r
407         prvReadRegister( &ucStatus, tcpINTERRUPT_STATUS_REG, tcpSTATUS_READ_LEN );\r
408         \r
409         /* We should find that the socket init was successful. */\r
410         if( ucStatus != tcpISR_SOCKET_INIT )\r
411         {\r
412                 return ( portLONG ) pdFAIL;\r
413         }\r
414 \r
415 \r
416         /* Setup the Tx pointer registers to indicate that the Tx buffer is empty. */\r
417         i2cMessage( ucDataTxReadPointer, sizeof( ucDataTxReadPointer ), tcpDEVICE_ADDRESS, tcpTX_READ_POINTER_REG, i2cWRITE, NULL, portMAX_DELAY );\r
418         vTaskDelay( tcpSHORT_DELAY );\r
419         i2cMessage( ucDataTxWritePointer, sizeof( ucDataTxWritePointer ), tcpDEVICE_ADDRESS, tcpTX_WRITE_POINTER_REG, i2cWRITE, NULL, portMAX_DELAY );\r
420         vTaskDelay( tcpSHORT_DELAY );\r
421         i2cMessage( ucDataTxAckPointer,   sizeof( ucDataTxAckPointer ),   tcpDEVICE_ADDRESS, tcpTX_ACK_POINTER_REG, i2cWRITE, NULL, portMAX_DELAY );\r
422         vTaskDelay( tcpSHORT_DELAY );\r
423 \r
424         return ( portLONG ) pdPASS;\r
425 }\r
426 /*-----------------------------------------------------------*/\r
427 \r
428 void vTCPListen( void )\r
429 {\r
430 unsigned portCHAR ucISR;\r
431 \r
432         /* Start a passive listen on the socket. */\r
433 \r
434         /* Enable interrupts in the WizNet device after ensuring none are \r
435         currently pending. */\r
436         while( SCB_EXTINT & tcpCLEAR_EINT0 )\r
437         {\r
438                 /* The WIZnet device is still asserting and interrupt so tell it to \r
439                 clear. */\r
440                 i2cMessage( ucDataClearInterrupt, sizeof( ucDataClearInterrupt ), tcpDEVICE_ADDRESS, tcpINTERRUPT_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );\r
441                 xSemaphoreTake( xMessageComplete, tcpLONG_DELAY );\r
442 \r
443                 vTaskDelay( 1 );\r
444                 SCB_EXTINT = tcpCLEAR_EINT0;\r
445         }\r
446 \r
447         while( xQueueReceive( xTCPISRQueue, &ucISR, tcpNO_DELAY ) )\r
448         {\r
449                 /* Just clearing the queue used by the ISR routine to tell this task\r
450                 that the WIZnet device needs attention. */\r
451         }\r
452 \r
453         /* Now all the pending interrupts have been cleared we can enable the \r
454         processor interrupts. */\r
455         VICIntEnable |= tcpEINT0_VIC_CHANNEL_BIT;\r
456 \r
457         /* Then start listening for incoming connections. */\r
458         i2cMessage( ucDataListen, sizeof( ucDataListen ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );\r
459 }\r
460 /*-----------------------------------------------------------*/\r
461 \r
462 portLONG lProcessConnection( void )\r
463 {\r
464 unsigned portCHAR ucISR, ucState, ucLastState = 2, ucShadow;\r
465 extern volatile portLONG lTransactionCompleted;\r
466 portLONG lSameStateCount = 0, lDataSent = pdFALSE;\r
467 unsigned portLONG ulWritePointer, ulAckPointer;\r
468 \r
469         /* No I2C errors can yet have occurred. */\r
470         portENTER_CRITICAL();\r
471                 lTransactionCompleted = pdTRUE;\r
472         portEXIT_CRITICAL();\r
473 \r
474         /* Keep looping - processing interrupts, until we have completed a \r
475         transaction.   This uses the WIZnet in it's simplest form.  The socket\r
476         accepts a connection - we process the connection - then close the socket.\r
477         We then go back to reinitialise everything and start again. */\r
478         while( lTransactionCompleted == pdTRUE )\r
479         {\r
480                 /* Wait for a message on the queue from the WIZnet ISR.  When the \r
481                 WIZnet device asserts an interrupt the ISR simply posts a message\r
482                 onto this queue to wake this task. */\r
483                 if( xQueueReceive( xTCPISRQueue, &ucISR, tcpCONNECTION_WAIT_DELAY ) )\r
484                 {\r
485                         /* The ISR posted a message on this queue to tell us that the\r
486                         WIZnet device asserted an interrupt.  The ISR cannot process\r
487                         an I2C message so cannot tell us what caused the interrupt so\r
488                         we have to query the device here.  This task is the highest\r
489                         priority in the system so will run immediately following the ISR. */\r
490                         prvReadRegister( &ucISR, tcpINTERRUPT_STATUS_REG, tcpSTATUS_READ_LEN );\r
491 \r
492                         /* Once we have read what caused the ISR we can clear the interrupt\r
493                         in the WIZnet. */\r
494                         i2cMessage( ucDataClearInterrupt, sizeof( ucDataClearInterrupt ), tcpDEVICE_ADDRESS, tcpINTERRUPT_REG, i2cWRITE, NULL, portMAX_DELAY );\r
495 \r
496                         /* Now we can clear the processor interrupt and re-enable ready for\r
497                         the next. */\r
498                         SCB_EXTINT = tcpCLEAR_EINT0;\r
499                         VICIntEnable |= tcpEINT0_VIC_CHANNEL_BIT;\r
500         \r
501                         /* Process the interrupt ... */\r
502 \r
503                         if( ucISR & tcpISR_ESTABLISHED )\r
504                         {\r
505                                 /* A connection has been established - respond by sending\r
506                                 a receive command. */\r
507                                 i2cMessage( ucDataReceiveCmd, sizeof( ucDataReceiveCmd ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );\r
508                         }\r
509                 \r
510                         if( ucISR & tcpISR_RX_COMPLETE )\r
511                         {\r
512                                 /* We message has been received.  This will be an HTTP get \r
513                                 command.  We only have one page to send so just send it without\r
514                                 regard to what the actual requested page was. */\r
515                                 prvSendSamplePage();\r
516                         }\r
517                 \r
518                         if( ucISR & tcpISR_TX_COMPLETE )\r
519                         {\r
520                                 /* We have a TX complete interrupt - which oddly does not \r
521                                 indicate that the message being sent is complete so we cannot\r
522                                 yet close the socket.  Instead we read the position of the Tx\r
523                                 pointer within the WIZnet device so we know how much data it\r
524                                 has to send.  Later we will read the ack pointer and compare \r
525                                 this to the Tx pointer to ascertain whether the transmission \r
526                                 has completed. */\r
527 \r
528                                 /* First read the shadow register. */\r
529                                 prvReadRegister( &ucShadow, tcpTX_WRITE_SHADOW_REG, tcpSHADOW_READ_LEN );\r
530                         \r
531                                 /* Now a short delay is required. */\r
532                                 vTaskDelay( tcpSHORT_DELAY );\r
533 \r
534                                 /* Then we can read the real register. */\r
535                                 prvReadRegister( ( unsigned portCHAR * ) &ulWritePointer, tcpTX_WRITE_POINTER_REG, sizeof( ulWritePointer ) );\r
536 \r
537                                 /* We cannot do anything more here but need to remember that \r
538                                 this interrupt has occurred. */\r
539                                 lDataSent = pdTRUE;\r
540                         }\r
541                 \r
542                         if( ucISR & tcpISR_CLOSED )\r
543                         {\r
544                                 /* The socket has been closed so we can leave this function. */\r
545                                 lTransactionCompleted = pdFALSE;\r
546                         }\r
547                 }\r
548                 else\r
549                 {\r
550                         /* We have not received an interrupt from the WIZnet device for a \r
551                         while.  Read the socket status and check that everything is as\r
552                         expected. */\r
553                         prvReadRegister( &ucState, tcpSOCKET_STATE_REG, tcpSTATUS_READ_LEN );\r
554                         \r
555                         if( ( ucState == tcpSTATUS_ESTABLISHED ) && ( lDataSent > 0 ) ) \r
556                         {\r
557                                 /* The socket is established and we have already received a Tx\r
558                                 end interrupt.  We must therefore be waiting for the Tx buffer\r
559                                 inside the WIZnet device to be empty before we can close the\r
560                                 socket. \r
561 \r
562                                 Read the Ack pointer register to see if it has caught up with\r
563                                 the Tx pointer register.  First we have to read the shadow \r
564                                 register. */\r
565                                 prvReadRegister( &ucShadow, tcpTX_ACK_SHADOW_REG, tcpSHADOW_READ_LEN );\r
566                                 vTaskDelay( tcpSHORT_DELAY );\r
567                                 prvReadRegister( ( unsigned portCHAR * ) &ulAckPointer, tcpTX_ACK_POINTER_REG, sizeof( ulWritePointer ) );\r
568 \r
569                                 if( ulAckPointer == ulWritePointer )\r
570                                 {\r
571                                         /* The Ack and write pointer are now equal and we can \r
572                                         safely close the socket. */\r
573                                         i2cMessage( ucDataDisconnect, sizeof( ucDataDisconnect ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );\r
574                                 }\r
575                                 else\r
576                                 {\r
577                                         /* Keep a count of how many times we encounter the Tx\r
578                                         buffer still containing data. */\r
579                                         lDataSent++;\r
580                                         if( lDataSent > tcpMAX_ATTEMPTS_TO_CHECK_BUFFER )\r
581                                         {\r
582                                                 /* Assume we cannot complete sending the data and \r
583                                                 therefore cannot safely close the socket.  Start over. */\r
584                                                 vTCPHardReset();\r
585                                                 lTransactionCompleted = pdFALSE;\r
586                                         }\r
587                                 }\r
588                         }\r
589                         else if( ucState != tcpSTATUS_LISTEN )\r
590                         {\r
591                                 /* If we have not yet received a Tx end interrupt we would only \r
592                                 ever expect to find the socket still listening for any \r
593                                 sustained period. */\r
594                                 if( ucState == ucLastState )\r
595                                 {\r
596                                         lSameStateCount++;\r
597                                         if( lSameStateCount > tcpMAX_NON_LISTEN_STAUS_READS )\r
598                                         {                                               \r
599                                                 /* We are persistently in an unexpected state.  Assume\r
600                                                 we cannot safely close the socket and start over. */\r
601                                                 vTCPHardReset();\r
602                                                 lTransactionCompleted = pdFALSE;\r
603                                         }\r
604                                 }\r
605                         }\r
606                         else\r
607                         {\r
608                                 /* We are in the listen state so are happy that everything\r
609                                 is as expected. */\r
610                                 lSameStateCount = 0;\r
611                         }\r
612 \r
613                         /* Remember what state we are in this time around so we can check\r
614                         for a persistence on an unexpected state. */\r
615                         ucLastState = ucState;\r
616                 }\r
617         }\r
618 \r
619         /* We are going to reinitialise the WIZnet device so do not want our \r
620         interrupts from the WIZnet to be processed. */\r
621         VICIntEnClear |= tcpEINT0_VIC_CHANNEL_BIT;\r
622         return lTransactionCompleted;\r
623 }\r
624 /*-----------------------------------------------------------*/\r
625 \r
626 static void prvWriteString( const portCHAR * const pucTxBuffer, portLONG lTxLen, unsigned portLONG *pulTxAddress )\r
627 {\r
628 unsigned portLONG ulSendAddress;\r
629 \r
630         /* Send a string to the Tx buffer internal to the WIZnet device. */\r
631 \r
632         /* Calculate the address to which we are going to write in the buffer. */\r
633         ulSendAddress = ( *pulTxAddress & tcpSINGLE_SOCKET_ADDR_MASK ) + tcpSINGLE_SOCKET_ADDR_OFFSET;\r
634 \r
635         /* Send the buffer to the calculated address.  Use the semaphore so we\r
636         can wait until the entire message has been transferred. */\r
637         i2cMessage( ( unsigned portCHAR * ) pucTxBuffer, lTxLen, tcpDEVICE_ADDRESS, ( unsigned portSHORT ) ulSendAddress, i2cWRITE, xMessageComplete, portMAX_DELAY );\r
638 \r
639         /* Wait until the semaphore indicates that the message has been transferred. */\r
640         if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )\r
641         {\r
642                 return;\r
643         }\r
644 \r
645         /* Return the new address of the end of the buffer (within the WIZnet \r
646         device). */\r
647         *pulTxAddress += ( unsigned portLONG ) lTxLen;\r
648 }\r
649 /*-----------------------------------------------------------*/\r
650 \r
651 static void prvFlushBuffer( unsigned portLONG ulTxAddress )\r
652 {\r
653 unsigned portCHAR ucTxBuffer[ tcpMAX_REGISTER_LEN ];\r
654 \r
655         /* We have written some data to the Tx buffer internal to the WIZnet\r
656         device.  Now we update the Tx pointer inside the WIZnet then send a\r
657         Send command - which causes     the data up to the new Tx pointer to be \r
658         transmitted. */\r
659 \r
660         /* Make sure endieness is correct for transmission. */\r
661         ulTxAddress = htonl( ulTxAddress );\r
662 \r
663         /* Place the new Tx pointer in the string to be transmitted. */\r
664         ucTxBuffer[ 0 ] = ( unsigned portCHAR ) ( ulTxAddress & 0xff );\r
665         ulTxAddress >>= 8;\r
666         ucTxBuffer[ 1 ] = ( unsigned portCHAR ) ( ulTxAddress & 0xff );\r
667         ulTxAddress >>= 8;\r
668         ucTxBuffer[ 2 ] = ( unsigned portCHAR ) ( ulTxAddress & 0xff );\r
669         ulTxAddress >>= 8;\r
670         ucTxBuffer[ 3 ] = ( unsigned portCHAR ) ( ulTxAddress & 0xff );\r
671         ulTxAddress >>= 8;\r
672 \r
673         /* And send it to the WIZnet device. */\r
674         i2cMessage( ucTxBuffer, sizeof( ulTxAddress ), tcpDEVICE_ADDRESS, tcpTX_WRITE_POINTER_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );\r
675 \r
676         if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )\r
677         {\r
678                 return;\r
679         }\r
680 \r
681         vTaskDelay( tcpSHORT_DELAY );\r
682 \r
683         /* Transmit! */\r
684         i2cMessage( ucDataSend, sizeof( ucDataSend ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );\r
685 \r
686         if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )\r
687         {\r
688                 return;\r
689         }\r
690 }\r
691 /*-----------------------------------------------------------*/\r
692 \r
693 static void prvSendSamplePage( void )\r
694 {\r
695 extern portLONG lErrorInTask;\r
696 unsigned portLONG ulTxAddress;\r
697 unsigned portCHAR ucShadow;\r
698 portLONG lIndex;\r
699 static unsigned portLONG ulRefreshCount = 0x00;\r
700 static portCHAR cPageBuffer[ tcpBUFFER_LEN ];\r
701 \r
702 \r
703         /* This function just generates a sample page of HTML which gets\r
704         sent each time a client attaches to the socket.  The page is created\r
705         from two fixed strings (cSamplePageFirstPart and cSamplePageSecondPart)\r
706         with a bit of dynamically generated data in the middle. */\r
707 \r
708         /* We need to know the address to which the html string should be sent\r
709         in the WIZnet Tx buffer.  First read the shadow register. */\r
710         prvReadRegister( &ucShadow, tcpTX_WRITE_SHADOW_REG, tcpSHADOW_READ_LEN );\r
711 \r
712         /* Now a short delay is required. */\r
713         vTaskDelay( tcpSHORT_DELAY );\r
714 \r
715         /* Now we can read the real pointer value. */\r
716         prvReadRegister( ( unsigned portCHAR * ) &ulTxAddress, tcpTX_WRITE_POINTER_REG, sizeof( ulTxAddress ) );\r
717 \r
718         /* Make sure endieness is correct. */\r
719         ulTxAddress = htonl( ulTxAddress );\r
720 \r
721         /* Send the start of the page. */\r
722         prvWriteString( cSamplePageFirstPart, strlen( cSamplePageFirstPart ), &ulTxAddress );\r
723 \r
724         /* Generate a bit of dynamic data and place it in the buffer ready to be\r
725         transmitted. */\r
726         strcpy( cPageBuffer, "<BR>Number of ticks since boot = 0x" );\r
727         lIndex = strlen( cPageBuffer );\r
728         ultoa( xTaskGetTickCount(), &( cPageBuffer[ lIndex ] ), 0 );\r
729         strcat( cPageBuffer, "<br>Number of tasks executing = ");\r
730         lIndex = strlen( cPageBuffer );\r
731         ultoa( ( unsigned portLONG ) uxTaskGetNumberOfTasks(), &( cPageBuffer[ lIndex ] ), 0 );\r
732         strcat( cPageBuffer, "<br>IO port 0 state (used by flash tasks) = 0x" );\r
733         lIndex = strlen( cPageBuffer );\r
734         ultoa( ( unsigned portLONG ) GPIO0_IOPIN, &( cPageBuffer[ lIndex ] ), 0 );\r
735         strcat( cPageBuffer, "<br>Refresh = 0x" );\r
736         lIndex = strlen( cPageBuffer );\r
737         ultoa( ( unsigned portLONG ) ulRefreshCount, &( cPageBuffer[ lIndex ] ), 0 );\r
738         \r
739         if( lErrorInTask )\r
740         {\r
741                 strcat( cPageBuffer, "<p>An error has occurred in at least one task." );\r
742         }\r
743         else\r
744         {\r
745                 strcat( cPageBuffer, "<p>All tasks executing without error." );         \r
746         }\r
747 \r
748         ulRefreshCount++;\r
749 \r
750         /* Send the dynamically generated string. */\r
751         prvWriteString( cPageBuffer, strlen( cPageBuffer ), &ulTxAddress );\r
752 \r
753         /* Finish the page. */\r
754         prvWriteString( cSamplePageSecondPart, strlen( cSamplePageSecondPart ), &ulTxAddress );\r
755 \r
756         /* Tell the WIZnet to send the data we have just written to its Tx buffer. */\r
757         prvFlushBuffer( ulTxAddress );\r
758 }\r
759 \r