2 FreeRTOS.org V4.5.0 - 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 for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
37 /* Standard includes. */
\r
40 /* Scheduler include files. */
\r
41 #include "FreeRTOS.h"
\r
46 /* Application includes. */
\r
49 /*-----------------------------------------------------------*/
\r
51 /* Bit definitions within the I2CONCLR register. */
\r
52 #define i2cSTA_BIT ( ( unsigned portCHAR ) 0x20 )
\r
53 #define i2cSI_BIT ( ( unsigned portCHAR ) 0x08 )
\r
54 #define i2cSTO_BIT ( ( unsigned portCHAR ) 0x10 )
\r
55 #define i2cAA_BIT ( ( unsigned portCHAR ) 0x04 )
\r
57 /* Status codes for the I2STAT register. */
\r
58 #define i2cSTATUS_START_TXED ( 0x08 )
\r
59 #define i2cSTATUS_REP_START_TXED ( 0x10 )
\r
60 #define i2cSTATUS_TX_ADDR_ACKED ( 0x18 )
\r
61 #define i2cSTATUS_DATA_TXED ( 0x28 )
\r
62 #define i2cSTATUS_RX_ADDR_ACKED ( 0x40 )
\r
63 #define i2cSTATUS_DATA_RXED ( 0x50 )
\r
64 #define i2cSTATUS_LAST_BYTE_RXED ( 0x58 )
\r
66 /* Constants for operation of the VIC. */
\r
67 #define i2cCLEAR_VIC_INTERRUPT ( 0 )
\r
69 /* Misc constants. */
\r
70 #define i2cJUST_ONE_BYTE_TO_RX ( 1 )
\r
71 #define i2cBUFFER_ADDRESS_BYTES ( 2 )
\r
73 /* End the current transmission and free the bus. */
\r
74 #define i2cEND_TRANSMISSION( lStatus ) \
\r
76 I2C_I2CONCLR = i2cAA_BIT; \
\r
77 I2C_I2CONSET = i2cSTO_BIT; \
\r
78 eCurrentState = eSentStart; \
\r
79 lTransactionCompleted = lStatus; \
\r
81 /*-----------------------------------------------------------*/
\r
83 /* Valid i2c communication states. */
\r
86 eSentStart, /*<< Last action was the transmission of a start bit. */
\r
87 eSentAddressForWrite, /*<< Last action was the transmission of the slave address we are to write to. */
\r
88 eSentAddressForRead, /*<< Last action was the transmission of the slave address we are to read from. */
\r
89 eSentData, /*<< Last action was the transmission of a data byte. */
\r
90 eReceiveData /*<< We expected data to be received. */
\r
92 /*-----------------------------------------------------------*/
\r
94 /* Points to the message currently being sent. */
\r
95 volatile xI2CMessage *pxCurrentMessage = NULL;
\r
97 /* The queue of messages waiting to be transmitted. */
\r
98 static xQueueHandle xMessagesForTx;
\r
100 /* Flag used to indicate whether or not the ISR is amid sending a message. */
\r
101 unsigned portLONG ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
103 /* Setting this to true will cause the TCP task to think a message is
\r
104 complete and thus restart. It can therefore be used under error states
\r
105 to force a restart. */
\r
106 volatile portLONG lTransactionCompleted = pdTRUE;
\r
108 /*-----------------------------------------------------------*/
\r
110 void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxTxMessages, unsigned portLONG **ppulBusFree )
\r
112 /* Create the queues used to hold Rx and Tx characters. */
\r
113 xMessagesForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( xI2CMessage * ) );
\r
115 /* Pass back a reference to the queue and bus free flag so the I2C API file
\r
116 can post messages. */
\r
117 *pxTxMessages = xMessagesForTx;
\r
118 *ppulBusFree = &ulBusFree;
\r
120 /*-----------------------------------------------------------*/
\r
122 void vI2C_ISR( void ) __attribute__ (( naked ));
\r
123 void vI2C_ISR( void )
\r
125 portENTER_SWITCHING_ISR();
\r
127 /* Variables must be static. */
\r
129 /* Holds the current transmission state. */
\r
130 static I2C_STATE eCurrentState = eSentStart;
\r
131 static portLONG lMessageIndex = -i2cBUFFER_ADDRESS_BYTES; /* There are two address bytes to send prior to the data. */
\r
132 static portBASE_TYPE xTaskWokenByTx;
\r
133 static portLONG lBytesLeft;
\r
135 xTaskWokenByTx = pdFALSE;
\r
138 /* The action taken for this interrupt depends on our current state. */
\r
139 switch( eCurrentState )
\r
143 /* We sent a start bit, if it was successful we can
\r
144 go on to send the slave address. */
\r
145 if( ( I2C_I2STAT == i2cSTATUS_START_TXED ) || ( I2C_I2STAT == i2cSTATUS_REP_START_TXED ) )
\r
147 /* Send the slave address. */
\r
148 I2C_I2DAT = pxCurrentMessage->ucSlaveAddress;
\r
150 if( pxCurrentMessage->ucSlaveAddress & i2cREAD )
\r
152 /* We are then going to read bytes back from the
\r
154 eCurrentState = eSentAddressForRead;
\r
156 /* Initialise the buffer index so the first byte goes
\r
157 into the first buffer position. */
\r
162 /* We are then going to write some data to the slave. */
\r
163 eCurrentState = eSentAddressForWrite;
\r
165 /* When writing bytes we first have to send the two
\r
166 byte buffer address so lMessageIndex is set negative,
\r
167 when it reaches 0 it is time to send the actual data. */
\r
168 lMessageIndex = -i2cBUFFER_ADDRESS_BYTES;
\r
173 /* Could not send the start bit so give up. */
\r
174 i2cEND_TRANSMISSION( pdFAIL );
\r
177 I2C_I2CONCLR = i2cSTA_BIT;
\r
181 case eSentAddressForWrite :
\r
183 /* We sent the address of the slave we are going to write to.
\r
184 If this was acknowledged we can go on to send the data. */
\r
185 if( I2C_I2STAT == i2cSTATUS_TX_ADDR_ACKED )
\r
187 /* Start the first byte transmitting which is the
\r
188 first byte of the buffer address to which the data will
\r
190 I2C_I2DAT = pxCurrentMessage->ucBufferAddressHighByte;
\r
191 eCurrentState = eSentData;
\r
195 /* Address was not acknowledged so give up. */
\r
196 i2cEND_TRANSMISSION( pdFAIL );
\r
200 case eSentAddressForRead :
\r
202 /* We sent the address of the slave we are going to read from.
\r
203 If this was acknowledged we can go on to read the data. */
\r
204 if( I2C_I2STAT == i2cSTATUS_RX_ADDR_ACKED )
\r
206 eCurrentState = eReceiveData;
\r
207 if( pxCurrentMessage->lMessageLength > i2cJUST_ONE_BYTE_TO_RX )
\r
209 /* Don't ack the last byte of the message. */
\r
210 I2C_I2CONSET = i2cAA_BIT;
\r
215 /* Something unexpected happened - give up. */
\r
216 i2cEND_TRANSMISSION( pdFAIL );
\r
220 case eReceiveData :
\r
222 /* We have just received a byte from the slave. */
\r
223 if( ( I2C_I2STAT == i2cSTATUS_DATA_RXED ) || ( I2C_I2STAT == i2cSTATUS_LAST_BYTE_RXED ) )
\r
225 /* Buffer the byte just received then increment the index
\r
226 so it points to the next free space. */
\r
227 pxCurrentMessage->pucBuffer[ lMessageIndex ] = I2C_I2DAT;
\r
230 /* How many more bytes are we expecting to receive? */
\r
231 lBytesLeft = pxCurrentMessage->lMessageLength - lMessageIndex;
\r
232 if( lBytesLeft == ( unsigned portLONG ) 0 )
\r
234 /* This was the last byte in the message. */
\r
235 i2cEND_TRANSMISSION( pdPASS );
\r
237 /* If xMessageCompleteSemaphore is not null then there
\r
238 is a task waiting for this message to complete and we
\r
239 must 'give' the semaphore so the task is woken.*/
\r
240 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
242 xTaskWokenByTx = xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, xTaskWokenByTx );
\r
245 /* Are there any other messages to transact? */
\r
246 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xTaskWokenByTx ) == pdTRUE )
\r
248 /* Start the next message - which was
\r
249 retrieved from the queue. */
\r
250 I2C_I2CONSET = i2cSTA_BIT;
\r
254 /* No more messages were found to be waiting for
\r
255 transaction so the bus is free. */
\r
256 ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
261 /* There are more bytes to receive but don't ack the
\r
263 if( lBytesLeft <= i2cJUST_ONE_BYTE_TO_RX )
\r
265 I2C_I2CONCLR = i2cAA_BIT;
\r
271 /* Something unexpected happened - give up. */
\r
272 i2cEND_TRANSMISSION( pdFAIL );
\r
279 /* We sent a data byte, if successful send the next byte in
\r
281 if( I2C_I2STAT == i2cSTATUS_DATA_TXED )
\r
283 /* Index to the next byte to send. */
\r
285 if( lMessageIndex < 0 )
\r
287 /* lMessage index is still negative so we have so far
\r
288 only sent the first byte of the buffer address. Send
\r
289 the second byte now, then initialise the buffer index
\r
290 to zero so the next byte sent comes from the actual
\r
292 I2C_I2DAT = pxCurrentMessage->ucBufferAddressLowByte;
\r
294 else if( lMessageIndex < pxCurrentMessage->lMessageLength )
\r
296 /* Simply send the next byte in the tx buffer. */
\r
297 I2C_I2DAT = pxCurrentMessage->pucBuffer[ lMessageIndex ];
\r
301 /* No more bytes in this message to be send. Finished
\r
302 sending message - send a stop bit. */
\r
303 i2cEND_TRANSMISSION( pdPASS );
\r
305 /* If xMessageCompleteSemaphore is not null then there
\r
306 is a task waiting for this message to be sent and the
\r
307 semaphore must be 'given' to wake the task. */
\r
308 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
310 xTaskWokenByTx = xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, xTaskWokenByTx );
\r
313 /* Are there any other messages to transact? */
\r
314 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xTaskWokenByTx ) == pdTRUE )
\r
316 /* Start the next message from the Tx queue. */
\r
317 I2C_I2CONSET = i2cSTA_BIT;
\r
321 /* No more message were queues for transaction so
\r
322 the bus is free. */
\r
323 ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
329 /* Something unexpected happened, give up. */
\r
330 i2cEND_TRANSMISSION( pdFAIL );
\r
336 /* Should never get here. */
\r
337 eCurrentState = eSentStart;
\r
341 /* Clear the interrupt. */
\r
342 I2C_I2CONCLR = i2cSI_BIT;
\r
343 VICVectAddr = i2cCLEAR_VIC_INTERRUPT;
\r
345 portEXIT_SWITCHING_ISR( ( xTaskWokenByTx ) );
\r
347 /*-----------------------------------------------------------*/
\r