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