]> git.sur5r.net Git - freertos/blob - Demo/WizNET_DEMO_GCC_ARM7/i2cISR.c
Update to V5.1.2.
[freertos] / Demo / WizNET_DEMO_GCC_ARM7 / i2cISR.c
1 /*\r
2         FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
30         *                                                                         *\r
31         * This is a concise, step by step, 'hands on' guide that describes both   *\r
32         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
33         * explains numerous examples that are written using the FreeRTOS API.     *\r
34         * Full source code for all the examples is provided in an accompanying    *\r
35         * .zip file.                                                              *\r
36     *                                                                         *\r
37     ***************************************************************************\r
38     ***************************************************************************\r
39 \r
40         Please ensure to read the configuration and relevant port sections of the\r
41         online documentation.\r
42 \r
43         http://www.FreeRTOS.org - Documentation, latest information, license and \r
44         contact details.\r
45 \r
46         http://www.SafeRTOS.com - A version that is certified for use in safety \r
47         critical systems.\r
48 \r
49         http://www.OpenRTOS.com - Commercial support, development, porting, \r
50         licensing and training services.\r
51 */\r
52 \r
53 \r
54 /* Standard includes. */\r
55 #include <stdlib.h>\r
56 \r
57 /* Scheduler include files. */\r
58 #include "FreeRTOS.h"\r
59 #include "task.h"\r
60 #include "queue.h"\r
61 #include "semphr.h"\r
62 \r
63 /* Application includes. */\r
64 #include "i2c.h"\r
65 \r
66 /*-----------------------------------------------------------*/\r
67 \r
68 /* Bit definitions within the I2CONCLR register. */\r
69 #define i2cSTA_BIT              ( ( unsigned portCHAR ) 0x20 )\r
70 #define i2cSI_BIT               ( ( unsigned portCHAR ) 0x08 )\r
71 #define i2cSTO_BIT              ( ( unsigned portCHAR ) 0x10 )\r
72 #define i2cAA_BIT               ( ( unsigned portCHAR ) 0x04 )\r
73 \r
74 /* Status codes for the I2STAT register. */\r
75 #define i2cSTATUS_START_TXED                    ( 0x08 )\r
76 #define i2cSTATUS_REP_START_TXED                ( 0x10 )\r
77 #define i2cSTATUS_TX_ADDR_ACKED                 ( 0x18 )\r
78 #define i2cSTATUS_DATA_TXED                             ( 0x28 )\r
79 #define i2cSTATUS_RX_ADDR_ACKED                 ( 0x40 )\r
80 #define i2cSTATUS_DATA_RXED                             ( 0x50 )\r
81 #define i2cSTATUS_LAST_BYTE_RXED                ( 0x58 )\r
82 \r
83 /* Constants for operation of the VIC. */\r
84 #define i2cCLEAR_VIC_INTERRUPT  ( 0 )\r
85 \r
86 /* Misc constants. */\r
87 #define i2cJUST_ONE_BYTE_TO_RX  ( 1 )\r
88 #define i2cBUFFER_ADDRESS_BYTES ( 2 )\r
89 \r
90 /* End the current transmission and free the bus. */\r
91 #define i2cEND_TRANSMISSION( lStatus )                                  \\r
92 {                                                                                                               \\r
93         I2C_I2CONCLR = i2cAA_BIT;                                                       \\r
94         I2C_I2CONSET = i2cSTO_BIT;                                                      \\r
95         eCurrentState = eSentStart;                                                     \\r
96         lTransactionCompleted = lStatus;                                        \\r
97 }\r
98 /*-----------------------------------------------------------*/\r
99 \r
100 /* Valid i2c communication states. */\r
101 typedef enum\r
102 {\r
103         eSentStart,                             /*<< Last action was the transmission of a start bit. */\r
104         eSentAddressForWrite,   /*<< Last action was the transmission of the slave address we are to write to. */\r
105         eSentAddressForRead,    /*<< Last action was the transmission of the slave address we are to read from. */\r
106         eSentData,                              /*<< Last action was the transmission of a data byte. */\r
107         eReceiveData                    /*<< We expected data to be received. */\r
108 } I2C_STATE;\r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /* Points to the message currently being sent. */\r
112 volatile xI2CMessage *pxCurrentMessage = NULL;  \r
113 \r
114 /* The queue of messages waiting to be transmitted. */\r
115 static xQueueHandle xMessagesForTx;\r
116 \r
117 /* Flag used to indicate whether or not the ISR is amid sending a message. */\r
118 unsigned portLONG ulBusFree = ( unsigned portLONG ) pdTRUE;\r
119 \r
120 /* Setting this to true will cause the TCP task to think a message is \r
121 complete and thus restart.  It can therefore be used under error states\r
122 to force a restart. */\r
123 volatile portLONG lTransactionCompleted = pdTRUE;\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxTxMessages, unsigned portLONG **ppulBusFree )\r
128 {\r
129         /* Create the queues used to hold Rx and Tx characters. */\r
130         xMessagesForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( xI2CMessage * ) );\r
131 \r
132         /* Pass back a reference to the queue and bus free flag so the I2C API file \r
133         can post messages. */\r
134         *pxTxMessages = xMessagesForTx;\r
135         *ppulBusFree = &ulBusFree;\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 /* The ISR entry point. */\r
140 void vI2C_ISR_Wrapper( void ) __attribute__ (( naked ));\r
141 \r
142 /* The ISR function to perform the actual work.  This must be a separate\r
143 function from the wrapper to ensure the correct stack frame is set up. */\r
144 void vI2C_ISR_Handler( void );\r
145 \r
146 /*-----------------------------------------------------------*/\r
147 \r
148 void vI2C_ISR_Wrapper( void )\r
149 {\r
150         /* Save the context of the interrupted task. */\r
151         portSAVE_CONTEXT();\r
152 \r
153         /* Call the handler to perform the actual work.  This must be a\r
154         separate function to ensure the correct stack frame is set up. */\r
155         vI2C_ISR_Handler();\r
156 \r
157         /* Restore the context of whichever task is going to run next. */\r
158         portRESTORE_CONTEXT();\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 void vI2C_ISR_Handler( void )\r
163 {\r
164 /* Holds the current transmission state. */                                                     \r
165 static I2C_STATE eCurrentState = eSentStart;\r
166 static portLONG lMessageIndex = -i2cBUFFER_ADDRESS_BYTES; /* There are two address bytes to send prior to the data. */\r
167 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
168 portLONG lBytesLeft;\r
169 \r
170         /* The action taken for this interrupt depends on our current state. */\r
171         switch( eCurrentState )\r
172         {\r
173                 case eSentStart :       \r
174 \r
175                                 /* We sent a start bit, if it was successful we can\r
176                                 go on to send the slave address. */\r
177                                 if( ( I2C_I2STAT == i2cSTATUS_START_TXED ) || ( I2C_I2STAT == i2cSTATUS_REP_START_TXED ) )\r
178                                 {\r
179                                         /* Send the slave address. */\r
180                                         I2C_I2DAT = pxCurrentMessage->ucSlaveAddress;\r
181 \r
182                                         if( pxCurrentMessage->ucSlaveAddress & i2cREAD )\r
183                                         {\r
184                                                 /* We are then going to read bytes back from the \r
185                                                 slave. */\r
186                                                 eCurrentState = eSentAddressForRead;\r
187                                                 \r
188                                                 /* Initialise the buffer index so the first byte goes\r
189                                                 into the first buffer position. */\r
190                                                 lMessageIndex = 0;\r
191                                         }\r
192                                         else\r
193                                         {\r
194                                                 /* We are then going to write some data to the slave. */\r
195                                                 eCurrentState = eSentAddressForWrite;\r
196 \r
197                                                 /* When writing bytes we first have to send the two\r
198                                                 byte buffer address so lMessageIndex is set negative,\r
199                                                 when it reaches 0 it is time to send the actual data. */\r
200                                                 lMessageIndex = -i2cBUFFER_ADDRESS_BYTES;\r
201                                         }\r
202                                 }\r
203                                 else\r
204                                 {\r
205                                         /* Could not send the start bit so give up. */\r
206                                         i2cEND_TRANSMISSION( pdFAIL );                                  \r
207                                 }\r
208 \r
209                                 I2C_I2CONCLR = i2cSTA_BIT;                              \r
210 \r
211                                 break;\r
212 \r
213                 case eSentAddressForWrite :\r
214 \r
215                                 /* We sent the address of the slave we are going to write to.\r
216                                 If this was acknowledged we     can go on to send the data. */\r
217                                 if( I2C_I2STAT == i2cSTATUS_TX_ADDR_ACKED )\r
218                                 {\r
219                                         /* Start the first byte transmitting which is the \r
220                                         first byte of the buffer address to which the data will \r
221                                         be sent. */\r
222                                         I2C_I2DAT = pxCurrentMessage->ucBufferAddressHighByte;\r
223                                         eCurrentState = eSentData;\r
224                                 }\r
225                                 else\r
226                                 {\r
227                                         /* Address was not acknowledged so give up. */\r
228                                         i2cEND_TRANSMISSION( pdFAIL );                                  \r
229                                 }                                       \r
230                                 break;\r
231 \r
232                 case eSentAddressForRead :\r
233 \r
234                                 /* We sent the address of the slave we are going to read from.\r
235                                 If this was acknowledged we can go on to read the data. */\r
236                                 if( I2C_I2STAT == i2cSTATUS_RX_ADDR_ACKED )\r
237                                 {\r
238                                         eCurrentState = eReceiveData;\r
239                                         if( pxCurrentMessage->lMessageLength > i2cJUST_ONE_BYTE_TO_RX )\r
240                                         {\r
241                                                 /* Don't ack the last byte of the message. */\r
242                                                 I2C_I2CONSET = i2cAA_BIT;\r
243                                         }                                       \r
244                                 }\r
245                                 else\r
246                                 {\r
247                                         /* Something unexpected happened - give up. */\r
248                                         i2cEND_TRANSMISSION( pdFAIL );                                  \r
249                                 }\r
250                                 break;\r
251 \r
252                 case eReceiveData :\r
253                                 \r
254                                 /* We have just received a byte from the slave. */\r
255                                 if( ( I2C_I2STAT == i2cSTATUS_DATA_RXED ) || ( I2C_I2STAT == i2cSTATUS_LAST_BYTE_RXED ) )\r
256                                 {\r
257                                         /* Buffer the byte just received then increment the index \r
258                                         so it points to the next free space. */\r
259                                         pxCurrentMessage->pucBuffer[ lMessageIndex ] = I2C_I2DAT;\r
260                                         lMessageIndex++;\r
261 \r
262                                         /* How many more bytes are we expecting to receive? */\r
263                                         lBytesLeft = pxCurrentMessage->lMessageLength - lMessageIndex;\r
264                                         if( lBytesLeft == ( unsigned portLONG ) 0 )\r
265                                         {\r
266                                                 /* This was the last byte in the message. */\r
267                                                 i2cEND_TRANSMISSION( pdPASS );\r
268 \r
269                                                 /* If xMessageCompleteSemaphore is not null then there\r
270                                                 is a task waiting for this message to complete and we\r
271                                                 must 'give' the semaphore so the task is woken.*/\r
272                                                 if( pxCurrentMessage->xMessageCompleteSemaphore )\r
273                                                 {\r
274                                                         xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, &xHigherPriorityTaskWoken );\r
275                                                 }\r
276 \r
277                                                 /* Are there any other messages to transact? */\r
278                                                 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xHigherPriorityTaskWoken ) == pdTRUE )\r
279                                                 {\r
280                                                         /* Start the next message - which was\r
281                                                         retrieved from the queue. */\r
282                                                         I2C_I2CONSET = i2cSTA_BIT;\r
283                                                 }\r
284                                                 else\r
285                                                 {\r
286                                                         /* No more messages were found to be waiting for\r
287                                                         transaction so the bus is free. */\r
288                                                         ulBusFree = ( unsigned portLONG ) pdTRUE;                       \r
289                                                 }                                               \r
290                                         }\r
291                                         else\r
292                                         {\r
293                                                 /* There are more bytes to receive but don't ack the \r
294                                                 last byte. */\r
295                                                 if( lBytesLeft <= i2cJUST_ONE_BYTE_TO_RX )\r
296                                                 {\r
297                                                         I2C_I2CONCLR = i2cAA_BIT;\r
298                                                 }                                                        \r
299                                         }\r
300                                 }\r
301                                 else\r
302                                 {\r
303                                         /* Something unexpected happened - give up. */\r
304                                         i2cEND_TRANSMISSION( pdFAIL );                                  \r
305                                 }\r
306 \r
307                                 break;\r
308                                 \r
309                 case eSentData  :       \r
310 \r
311                                 /* We sent a data byte, if successful send the  next byte in \r
312                                 the message. */\r
313                                 if( I2C_I2STAT == i2cSTATUS_DATA_TXED )\r
314                                 {\r
315                                         /* Index to the next byte to send. */\r
316                                         lMessageIndex++;\r
317                                         if( lMessageIndex < 0 )\r
318                                         {\r
319                                                 /* lMessage index is still negative so we have so far \r
320                                                 only sent the first byte of the buffer address.  Send \r
321                                                 the second byte now, then initialise the buffer index\r
322                                                 to zero so the next byte sent comes from the actual \r
323                                                 data buffer. */\r
324                                                 I2C_I2DAT = pxCurrentMessage->ucBufferAddressLowByte;\r
325                                         }\r
326                                         else if( lMessageIndex < pxCurrentMessage->lMessageLength )\r
327                                         {\r
328                                                 /* Simply send the next byte in the tx buffer. */\r
329                                                 I2C_I2DAT = pxCurrentMessage->pucBuffer[ lMessageIndex ];                                                                               \r
330                                         }\r
331                                         else\r
332                                         {\r
333                                                 /* No more bytes in this message to be send.  Finished \r
334                                                 sending message - send a stop bit. */\r
335                                                 i2cEND_TRANSMISSION( pdPASS );\r
336 \r
337                                                 /* If xMessageCompleteSemaphore is not null then there\r
338                                                 is a task waiting for this message to be sent and the\r
339                                                 semaphore must be 'given' to wake the task. */\r
340                                                 if( pxCurrentMessage->xMessageCompleteSemaphore )\r
341                                                 {\r
342                                                         xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, &xHigherPriorityTaskWoken );\r
343                                                 }\r
344 \r
345                                                 /* Are there any other messages to transact? */\r
346                                                 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xHigherPriorityTaskWoken ) == pdTRUE )\r
347                                                 {\r
348                                                         /* Start the next message from the Tx queue. */\r
349                                                         I2C_I2CONSET = i2cSTA_BIT;\r
350                                                 }\r
351                                                 else\r
352                                                 {\r
353                                                         /* No more message were queues for transaction so \r
354                                                         the bus is free. */\r
355                                                         ulBusFree = ( unsigned portLONG ) pdTRUE;                       \r
356                                                 }\r
357                                         }\r
358                                 }\r
359                                 else\r
360                                 {\r
361                                         /* Something unexpected happened, give up. */\r
362                                         i2cEND_TRANSMISSION( pdFAIL );                                  \r
363                                 }\r
364                                 break;\r
365 \r
366                 default :       \r
367                 \r
368                                 /* Should never get here. */\r
369                                 eCurrentState = eSentStart;\r
370                                 break;\r
371         }       \r
372 \r
373         /* Clear the interrupt. */\r
374         I2C_I2CONCLR = i2cSI_BIT;\r
375         VICVectAddr = i2cCLEAR_VIC_INTERRUPT;\r
376 \r
377         if( xHigherPriorityTaskWoken )\r
378         {\r
379                 portYIELD_FROM_ISR();\r
380         }\r
381 }\r
382 /*-----------------------------------------------------------*/\r
383 \r