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