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