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