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