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