2 FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\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
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
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
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
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
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
34 ***************************************************************************
\r
38 /* Standard includes. */
\r
41 /* Scheduler include files. */
\r
42 #include "FreeRTOS.h"
\r
47 /* Application includes. */
\r
50 /*-----------------------------------------------------------*/
\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
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
67 /* Constants for operation of the VIC. */
\r
68 #define i2cCLEAR_VIC_INTERRUPT ( 0 )
\r
70 /* Misc constants. */
\r
71 #define i2cJUST_ONE_BYTE_TO_RX ( 1 )
\r
72 #define i2cBUFFER_ADDRESS_BYTES ( 2 )
\r
74 /* End the current transmission and free the bus. */
\r
75 #define i2cEND_TRANSMISSION( lStatus ) \
\r
77 I2C_I2CONCLR = i2cAA_BIT; \
\r
78 I2C_I2CONSET = i2cSTO_BIT; \
\r
79 eCurrentState = eSentStart; \
\r
80 lTransactionCompleted = lStatus; \
\r
82 /*-----------------------------------------------------------*/
\r
84 /* Valid i2c communication states. */
\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
93 /*-----------------------------------------------------------*/
\r
95 /* Points to the message currently being sent. */
\r
96 volatile xI2CMessage *pxCurrentMessage = NULL;
\r
98 /* The queue of messages waiting to be transmitted. */
\r
99 static xQueueHandle xMessagesForTx;
\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
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
109 /*-----------------------------------------------------------*/
\r
111 void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxTxMessages, unsigned portLONG **ppulBusFree )
\r
113 /* Create the queues used to hold Rx and Tx characters. */
\r
114 xMessagesForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( xI2CMessage * ) );
\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
121 /*-----------------------------------------------------------*/
\r
123 /* The ISR entry point. */
\r
124 void vI2C_ISR_Wrapper( void ) __attribute__ (( naked ));
\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
130 /*-----------------------------------------------------------*/
\r
132 void vI2C_ISR_Wrapper( void )
\r
134 /* Save the context of the interrupted task. */
\r
135 portSAVE_CONTEXT();
\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
141 /* Restore the context of whichever task is going to run next. */
\r
142 portRESTORE_CONTEXT();
\r
144 /*-----------------------------------------------------------*/
\r
146 void vI2C_ISR_Handler( void )
\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
154 /* The action taken for this interrupt depends on our current state. */
\r
155 switch( eCurrentState )
\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
163 /* Send the slave address. */
\r
164 I2C_I2DAT = pxCurrentMessage->ucSlaveAddress;
\r
166 if( pxCurrentMessage->ucSlaveAddress & i2cREAD )
\r
168 /* We are then going to read bytes back from the
\r
170 eCurrentState = eSentAddressForRead;
\r
172 /* Initialise the buffer index so the first byte goes
\r
173 into the first buffer position. */
\r
178 /* We are then going to write some data to the slave. */
\r
179 eCurrentState = eSentAddressForWrite;
\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
189 /* Could not send the start bit so give up. */
\r
190 i2cEND_TRANSMISSION( pdFAIL );
\r
193 I2C_I2CONCLR = i2cSTA_BIT;
\r
197 case eSentAddressForWrite :
\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
203 /* Start the first byte transmitting which is the
\r
204 first byte of the buffer address to which the data will
\r
206 I2C_I2DAT = pxCurrentMessage->ucBufferAddressHighByte;
\r
207 eCurrentState = eSentData;
\r
211 /* Address was not acknowledged so give up. */
\r
212 i2cEND_TRANSMISSION( pdFAIL );
\r
216 case eSentAddressForRead :
\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
222 eCurrentState = eReceiveData;
\r
223 if( pxCurrentMessage->lMessageLength > i2cJUST_ONE_BYTE_TO_RX )
\r
225 /* Don't ack the last byte of the message. */
\r
226 I2C_I2CONSET = i2cAA_BIT;
\r
231 /* Something unexpected happened - give up. */
\r
232 i2cEND_TRANSMISSION( pdFAIL );
\r
236 case eReceiveData :
\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
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
246 /* How many more bytes are we expecting to receive? */
\r
247 lBytesLeft = pxCurrentMessage->lMessageLength - lMessageIndex;
\r
248 if( lBytesLeft == ( unsigned portLONG ) 0 )
\r
250 /* This was the last byte in the message. */
\r
251 i2cEND_TRANSMISSION( pdPASS );
\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
258 xTaskWokenByTx = xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, xTaskWokenByTx );
\r
261 /* Are there any other messages to transact? */
\r
262 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xTaskWokenByTx ) == pdTRUE )
\r
264 /* Start the next message - which was
\r
265 retrieved from the queue. */
\r
266 I2C_I2CONSET = i2cSTA_BIT;
\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
277 /* There are more bytes to receive but don't ack the
\r
279 if( lBytesLeft <= i2cJUST_ONE_BYTE_TO_RX )
\r
281 I2C_I2CONCLR = i2cAA_BIT;
\r
287 /* Something unexpected happened - give up. */
\r
288 i2cEND_TRANSMISSION( pdFAIL );
\r
295 /* We sent a data byte, if successful send the next byte in
\r
297 if( I2C_I2STAT == i2cSTATUS_DATA_TXED )
\r
299 /* Index to the next byte to send. */
\r
301 if( lMessageIndex < 0 )
\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
308 I2C_I2DAT = pxCurrentMessage->ucBufferAddressLowByte;
\r
310 else if( lMessageIndex < pxCurrentMessage->lMessageLength )
\r
312 /* Simply send the next byte in the tx buffer. */
\r
313 I2C_I2DAT = pxCurrentMessage->pucBuffer[ lMessageIndex ];
\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
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
326 xTaskWokenByTx = xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, xTaskWokenByTx );
\r
329 /* Are there any other messages to transact? */
\r
330 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xTaskWokenByTx ) == pdTRUE )
\r
332 /* Start the next message from the Tx queue. */
\r
333 I2C_I2CONSET = i2cSTA_BIT;
\r
337 /* No more message were queues for transaction so
\r
338 the bus is free. */
\r
339 ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
345 /* Something unexpected happened, give up. */
\r
346 i2cEND_TRANSMISSION( pdFAIL );
\r
352 /* Should never get here. */
\r
353 eCurrentState = eSentStart;
\r
357 /* Clear the interrupt. */
\r
358 I2C_I2CONCLR = i2cSI_BIT;
\r
359 VICVectAddr = i2cCLEAR_VIC_INTERRUPT;
\r
361 if( xTaskWokenByTx )
\r
363 portYIELD_FROM_ISR();
\r
366 /*-----------------------------------------------------------*/
\r