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