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