2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\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
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
46 ***************************************************************************
\r
48 ***************************************************************************
\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
62 ***************************************************************************
\r
64 ***************************************************************************
\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
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\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
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
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
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
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
103 Changes from V3.2.4
\r
105 + Changed the default MAC address to something more realistic.
\r
109 /* Standard includes. */
\r
110 #include <stdlib.h>
\r
111 #include <string.h>
\r
113 /* Scheduler include files. */
\r
114 #include "FreeRTOS.h"
\r
116 #include "semphr.h"
\r
118 #include "serial.h"
\r
120 /* Application includes. */
\r
122 #include "html_pages.h"
\r
124 /*-----------------------------------------------------------*/
\r
126 /* Hardwired i2c address of the WIZNet device. */
\r
127 #define tcpDEVICE_ADDRESS ( ( unsigned char ) 0x00 )
\r
129 /* Constants used to configure the Tx and Rx buffer sizes within the WIZnet
\r
131 #define tcp8K_RX ( ( unsigned char ) 0x03 )
\r
132 #define tcp8K_TX ( ( unsigned char ) 0x03 )
\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
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
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
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
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
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
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
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
204 /* Constants defining the socket status bits. */
\r
205 #define tcpSTATUS_ESTABLISHED ( ( unsigned char ) 0x06 )
\r
206 #define tcpSTATUS_LISTEN ( ( unsigned char ) 0x02 )
\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
219 /* Message definitions. The IP address, MAC address, gateway address, etc.
\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
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
245 static SemaphoreHandle_t xMessageComplete = NULL;
\r
246 QueueHandle_t xTCPISRQueue = NULL;
\r
248 /* Dynamically generate and send an html page. */
\r
249 static void prvSendSamplePage( void );
\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
254 /* Send the entire Tx buffer (the Tx buffer within the WIZnet device). */
\r
255 static void prvFlushBuffer( unsigned long ulTxAddress );
\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
260 /* Convert a number to a string. */
\r
261 void ultoa( unsigned long ulVal, char *pcBuffer, long lIgnore );
\r
263 /*-----------------------------------------------------------*/
\r
265 void ultoa( unsigned long ulVal, char *pcBuffer, long lIgnore )
\r
267 unsigned long lNibble;
\r
270 /* Simple routine to convert an unsigned long value into a string in hex
\r
273 /* For each nibble in the number we are converting. */
\r
274 for( lIndex = 0; lIndex < ( sizeof( ulVal ) * 2 ); lIndex++ )
\r
276 /* Take the top four bits of the number. */
\r
277 lNibble = ( ulVal >> 28 );
\r
279 /* We are converting it to a hex string, so is the number in the range
\r
283 pcBuffer[ lIndex ] = '0' + lNibble;
\r
288 pcBuffer[ lIndex ] = 'A' + lNibble;
\r
291 /* Shift off the top nibble so we use the next nibble next time around. */
\r
295 /* Mark the end of the string with a null terminator. */
\r
296 pcBuffer[ lIndex ] = 0x00;
\r
298 /*-----------------------------------------------------------*/
\r
300 static void prvReadRegister( unsigned char *pucDestination, unsigned short usAddress, unsigned long ulLength )
\r
302 unsigned char ucRxBuffer[ tcpMAX_REGISTER_LEN ];
\r
304 /* Read a register value from the WIZnet device. */
\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
309 /* Then read back from that address. */
\r
310 i2cMessage( ( unsigned char * ) pucDestination, ulLength, tcpDEVICE_ADDRESS, i2cNO_ADDR_REQUIRED, i2cREAD, xMessageComplete, portMAX_DELAY );
\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
317 /*-----------------------------------------------------------*/
\r
319 void vTCPHardReset( void )
\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
324 /* Make sure the interrupt from the WIZnet is disabled. */
\r
325 VICIntEnClear |= tcpEINT0_VIC_CHANNEL_BIT;
\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
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
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
341 /* Use the GPIO to reset the network hardware. */
\r
342 GPIO_IOCLR = tcpRESET_ACTIVE_LOW;
\r
343 GPIO_IOSET = tcpRESET_ACTIVE_HIGH;
\r
345 /* Delay with the network hardware in reset for a short while. */
\r
346 vTaskDelay( tcpRESET_DELAY );
\r
348 GPIO_IOCLR = tcpRESET_ACTIVE_HIGH;
\r
349 GPIO_IOSET = tcpRESET_ACTIVE_LOW;
\r
351 vTaskDelay( tcpINIT_DELAY );
\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
357 /* We want the TCP comms to wake us from power save. */
\r
358 SCB_EXTWAKE = tcpWAKE_ON_EINT0;
\r
360 /* Install the ISR into the VIC - but don't enable it yet! */
\r
361 portENTER_CRITICAL();
\r
363 extern void ( vEINT0_ISR_Wrapper )( void );
\r
365 VICIntSelect &= ~( tcpEINT0_VIC_CHANNEL_BIT );
\r
366 VICVectAddr3 = ( long ) vEINT0_ISR_Wrapper;
\r
368 VICVectCntl3 = tcpEINT0_VIC_CHANNEL | tcpEINT0_VIC_ENABLE;
\r
370 portEXIT_CRITICAL();
\r
372 /* Enable interrupts in the WIZnet itself. */
\r
373 i2cMessage( ucDataEnableISR, sizeof( ucDataEnableISR ), tcpDEVICE_ADDRESS, tcpISR_MASK_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
375 vTaskDelay( tcpLONG_DELAY );
\r
377 /*-----------------------------------------------------------*/
\r
379 long lTCPSoftReset( void )
\r
381 unsigned char ucStatus;
\r
382 extern volatile long lTransactionCompleted;
\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
387 /* Reset the internal WIZnet registers. */
\r
388 i2cMessage( ucDataReset, sizeof( ucDataReset ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );
\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
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
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
406 /* Seems to like a little wait here. */
\r
407 vTaskDelay( tcpINIT_DELAY );
\r
409 /* Read back the status to ensure the system initialised ok. */
\r
410 prvReadRegister( &ucStatus, tcpINTERRUPT_STATUS_REG, tcpSTATUS_READ_LEN );
\r
412 /* We should find that the sys init was successful. */
\r
413 if( ucStatus != tcpISR_SYS_INIT )
\r
415 return ( long ) pdFAIL;
\r
418 /* No i2c errors yet. */
\r
419 portENTER_CRITICAL();
\r
420 lTransactionCompleted = pdTRUE;
\r
421 portEXIT_CRITICAL();
\r
423 return ( long ) pdPASS;
\r
425 /*-----------------------------------------------------------*/
\r
427 long lTCPCreateSocket( void )
\r
429 unsigned char ucStatus;
\r
431 /* Create and configure a socket. */
\r
433 /* Setup and init the socket. Here the port number is set and the socket
\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
439 /* Wait for the Init command to be sent. */
\r
440 if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
\r
442 /* For some reason the message was not transmitted within our block
\r
444 return ( long ) pdFAIL;
\r
447 /* Allow the socket to initialise. */
\r
448 vTaskDelay( tcpINIT_DELAY );
\r
450 /* Read back the status to ensure the socket initialised ok. */
\r
451 prvReadRegister( &ucStatus, tcpINTERRUPT_STATUS_REG, tcpSTATUS_READ_LEN );
\r
453 /* We should find that the socket init was successful. */
\r
454 if( ucStatus != tcpISR_SOCKET_INIT )
\r
456 return ( long ) pdFAIL;
\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
468 return ( long ) pdPASS;
\r
470 /*-----------------------------------------------------------*/
\r
472 void vTCPListen( void )
\r
474 unsigned char ucISR;
\r
476 /* Start a passive listen on the socket. */
\r
478 /* Enable interrupts in the WizNet device after ensuring none are
\r
479 currently pending. */
\r
480 while( SCB_EXTINT & tcpCLEAR_EINT0 )
\r
482 /* The WIZnet device is still asserting and interrupt so tell it to
\r
484 i2cMessage( ucDataClearInterrupt, sizeof( ucDataClearInterrupt ), tcpDEVICE_ADDRESS, tcpINTERRUPT_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );
\r
485 xSemaphoreTake( xMessageComplete, tcpLONG_DELAY );
\r
488 SCB_EXTINT = tcpCLEAR_EINT0;
\r
491 while( xQueueReceive( xTCPISRQueue, &ucISR, tcpNO_DELAY ) )
\r
493 /* Just clearing the queue used by the ISR routine to tell this task
\r
494 that the WIZnet device needs attention. */
\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
501 /* Then start listening for incoming connections. */
\r
502 i2cMessage( ucDataListen, sizeof( ucDataListen ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
504 /*-----------------------------------------------------------*/
\r
506 long lProcessConnection( void )
\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
513 /* No I2C errors can yet have occurred. */
\r
514 portENTER_CRITICAL();
\r
515 lTransactionCompleted = pdTRUE;
\r
516 portEXIT_CRITICAL();
\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
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
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
536 /* Once we have read what caused the ISR we can clear the interrupt
\r
538 i2cMessage( ucDataClearInterrupt, sizeof( ucDataClearInterrupt ), tcpDEVICE_ADDRESS, tcpINTERRUPT_REG, i2cWRITE, NULL, portMAX_DELAY );
\r
540 /* Now we can clear the processor interrupt and re-enable ready for
\r
542 SCB_EXTINT = tcpCLEAR_EINT0;
\r
543 VICIntEnable |= tcpEINT0_VIC_CHANNEL_BIT;
\r
545 /* Process the interrupt ... */
\r
547 if( ucISR & tcpISR_ESTABLISHED )
\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
554 if( ucISR & tcpISR_RX_COMPLETE )
\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
562 if( ucISR & tcpISR_TX_COMPLETE )
\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
572 /* First read the shadow register. */
\r
573 prvReadRegister( &ucShadow, tcpTX_WRITE_SHADOW_REG, tcpSHADOW_READ_LEN );
\r
575 /* Now a short delay is required. */
\r
576 vTaskDelay( tcpSHORT_DELAY );
\r
578 /* Then we can read the real register. */
\r
579 prvReadRegister( ( unsigned char * ) &ulWritePointer, tcpTX_WRITE_POINTER_REG, sizeof( ulWritePointer ) );
\r
581 /* We cannot do anything more here but need to remember that
\r
582 this interrupt has occurred. */
\r
583 lDataSent = pdTRUE;
\r
586 if( ucISR & tcpISR_CLOSED )
\r
588 /* The socket has been closed so we can leave this function. */
\r
589 lTransactionCompleted = pdFALSE;
\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
597 prvReadRegister( &ucState, tcpSOCKET_STATE_REG, tcpSTATUS_READ_LEN );
\r
599 if( ( ucState == tcpSTATUS_ESTABLISHED ) && ( lDataSent > 0 ) )
\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
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
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
613 if( ulAckPointer == ulWritePointer )
\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
621 /* Keep a count of how many times we encounter the Tx
\r
622 buffer still containing data. */
\r
624 if( lDataSent > tcpMAX_ATTEMPTS_TO_CHECK_BUFFER )
\r
626 /* Assume we cannot complete sending the data and
\r
627 therefore cannot safely close the socket. Start over. */
\r
629 lTransactionCompleted = pdFALSE;
\r
633 else if( ucState != tcpSTATUS_LISTEN )
\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
641 if( lSameStateCount > tcpMAX_NON_LISTEN_STAUS_READS )
\r
643 /* We are persistently in an unexpected state. Assume
\r
644 we cannot safely close the socket and start over. */
\r
646 lTransactionCompleted = pdFALSE;
\r
652 /* We are in the listen state so are happy that everything
\r
654 lSameStateCount = 0;
\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
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
668 /*-----------------------------------------------------------*/
\r
670 static void prvWriteString( const char * const pucTxBuffer, long lTxLen, unsigned long *pulTxAddress )
\r
672 unsigned long ulSendAddress;
\r
674 /* Send a string to the Tx buffer internal to the WIZnet device. */
\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
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
683 /* Wait until the semaphore indicates that the message has been transferred. */
\r
684 if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
\r
689 /* Return the new address of the end of the buffer (within the WIZnet
\r
691 *pulTxAddress += ( unsigned long ) lTxLen;
\r
693 /*-----------------------------------------------------------*/
\r
695 static void prvFlushBuffer( unsigned long ulTxAddress )
\r
697 unsigned char ucTxBuffer[ tcpMAX_REGISTER_LEN ];
\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
704 /* Make sure endieness is correct for transmission. */
\r
705 ulTxAddress = htonl( ulTxAddress );
\r
707 /* Place the new Tx pointer in the string to be transmitted. */
\r
708 ucTxBuffer[ 0 ] = ( unsigned char ) ( ulTxAddress & 0xff );
\r
710 ucTxBuffer[ 1 ] = ( unsigned char ) ( ulTxAddress & 0xff );
\r
712 ucTxBuffer[ 2 ] = ( unsigned char ) ( ulTxAddress & 0xff );
\r
714 ucTxBuffer[ 3 ] = ( unsigned char ) ( ulTxAddress & 0xff );
\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
720 if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
\r
725 vTaskDelay( tcpSHORT_DELAY );
\r
728 i2cMessage( ucDataSend, sizeof( ucDataSend ), tcpDEVICE_ADDRESS, tcpCOMMAND_REG, i2cWRITE, xMessageComplete, portMAX_DELAY );
\r
730 if( !xSemaphoreTake( xMessageComplete, tcpLONG_DELAY ) )
\r
735 /*-----------------------------------------------------------*/
\r
737 static void prvSendSamplePage( void )
\r
739 extern long lErrorInTask;
\r
740 unsigned long ulTxAddress;
\r
741 unsigned char ucShadow;
\r
743 static unsigned long ulRefreshCount = 0x00;
\r
744 static char cPageBuffer[ tcpBUFFER_LEN ];
\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
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
756 /* Now a short delay is required. */
\r
757 vTaskDelay( tcpSHORT_DELAY );
\r
759 /* Now we can read the real pointer value. */
\r
760 prvReadRegister( ( unsigned char * ) &ulTxAddress, tcpTX_WRITE_POINTER_REG, sizeof( ulTxAddress ) );
\r
762 /* Make sure endieness is correct. */
\r
763 ulTxAddress = htonl( ulTxAddress );
\r
765 /* Send the start of the page. */
\r
766 prvWriteString( cSamplePageFirstPart, strlen( cSamplePageFirstPart ), &ulTxAddress );
\r
768 /* Generate a bit of dynamic data and place it in the buffer ready to be
\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
785 strcat( cPageBuffer, "<p>An error has occurred in at least one task." );
\r
789 strcat( cPageBuffer, "<p>All tasks executing without error." );
\r
794 /* Send the dynamically generated string. */
\r
795 prvWriteString( cPageBuffer, strlen( cPageBuffer ), &ulTxAddress );
\r
797 /* Finish the page. */
\r
798 prvWriteString( cSamplePageSecondPart, strlen( cSamplePageSecondPart ), &ulTxAddress );
\r
800 /* Tell the WIZnet to send the data we have just written to its Tx buffer. */
\r
801 prvFlushBuffer( ulTxAddress );
\r