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