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