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