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