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