2 FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 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 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! Why not get us to quote to get FreeRTOS.org *
\r
30 * running on your hardware - or even write all or part of your application*
\r
31 * for you? See http://www.OpenRTOS.com for details. *
\r
33 ***************************************************************************
\r
34 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 /* Standard includes. */
\r
53 /* Scheduler include files. */
\r
54 #include "FreeRTOS.h"
\r
59 /* Application includes. */
\r
62 /*-----------------------------------------------------------*/
\r
64 /* Bit definitions within the I2CONCLR register. */
\r
65 #define i2cSTA_BIT ( ( unsigned portCHAR ) 0x20 )
\r
66 #define i2cSI_BIT ( ( unsigned portCHAR ) 0x08 )
\r
67 #define i2cSTO_BIT ( ( unsigned portCHAR ) 0x10 )
\r
68 #define i2cAA_BIT ( ( unsigned portCHAR ) 0x04 )
\r
70 /* Status codes for the I2STAT register. */
\r
71 #define i2cSTATUS_START_TXED ( 0x08 )
\r
72 #define i2cSTATUS_REP_START_TXED ( 0x10 )
\r
73 #define i2cSTATUS_TX_ADDR_ACKED ( 0x18 )
\r
74 #define i2cSTATUS_DATA_TXED ( 0x28 )
\r
75 #define i2cSTATUS_RX_ADDR_ACKED ( 0x40 )
\r
76 #define i2cSTATUS_DATA_RXED ( 0x50 )
\r
77 #define i2cSTATUS_LAST_BYTE_RXED ( 0x58 )
\r
79 /* Constants for operation of the VIC. */
\r
80 #define i2cCLEAR_VIC_INTERRUPT ( 0 )
\r
82 /* Misc constants. */
\r
83 #define i2cJUST_ONE_BYTE_TO_RX ( 1 )
\r
84 #define i2cBUFFER_ADDRESS_BYTES ( 2 )
\r
86 /* End the current transmission and free the bus. */
\r
87 #define i2cEND_TRANSMISSION( lStatus ) \
\r
89 I2C_I2CONCLR = i2cAA_BIT; \
\r
90 I2C_I2CONSET = i2cSTO_BIT; \
\r
91 eCurrentState = eSentStart; \
\r
92 lTransactionCompleted = lStatus; \
\r
94 /*-----------------------------------------------------------*/
\r
96 /* Valid i2c communication states. */
\r
99 eSentStart, /*<< Last action was the transmission of a start bit. */
\r
100 eSentAddressForWrite, /*<< Last action was the transmission of the slave address we are to write to. */
\r
101 eSentAddressForRead, /*<< Last action was the transmission of the slave address we are to read from. */
\r
102 eSentData, /*<< Last action was the transmission of a data byte. */
\r
103 eReceiveData /*<< We expected data to be received. */
\r
105 /*-----------------------------------------------------------*/
\r
107 /* Points to the message currently being sent. */
\r
108 volatile xI2CMessage *pxCurrentMessage = NULL;
\r
110 /* The queue of messages waiting to be transmitted. */
\r
111 static xQueueHandle xMessagesForTx;
\r
113 /* Flag used to indicate whether or not the ISR is amid sending a message. */
\r
114 unsigned portLONG ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
116 /* Setting this to true will cause the TCP task to think a message is
\r
117 complete and thus restart. It can therefore be used under error states
\r
118 to force a restart. */
\r
119 volatile portLONG lTransactionCompleted = pdTRUE;
\r
121 /*-----------------------------------------------------------*/
\r
123 void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxTxMessages, unsigned portLONG **ppulBusFree )
\r
125 /* Create the queues used to hold Rx and Tx characters. */
\r
126 xMessagesForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( xI2CMessage * ) );
\r
128 /* Pass back a reference to the queue and bus free flag so the I2C API file
\r
129 can post messages. */
\r
130 *pxTxMessages = xMessagesForTx;
\r
131 *ppulBusFree = &ulBusFree;
\r
133 /*-----------------------------------------------------------*/
\r
135 /* The ISR entry point. */
\r
136 void vI2C_ISR_Wrapper( void ) __attribute__ (( naked ));
\r
138 /* The ISR function to perform the actual work. This must be a separate
\r
139 function from the wrapper to ensure the correct stack frame is set up. */
\r
140 void vI2C_ISR_Handler( void );
\r
142 /*-----------------------------------------------------------*/
\r
144 void vI2C_ISR_Wrapper( void )
\r
146 /* Save the context of the interrupted task. */
\r
147 portSAVE_CONTEXT();
\r
149 /* Call the handler to perform the actual work. This must be a
\r
150 separate function to ensure the correct stack frame is set up. */
\r
151 vI2C_ISR_Handler();
\r
153 /* Restore the context of whichever task is going to run next. */
\r
154 portRESTORE_CONTEXT();
\r
156 /*-----------------------------------------------------------*/
\r
158 void vI2C_ISR_Handler( void )
\r
160 /* Holds the current transmission state. */
\r
161 static I2C_STATE eCurrentState = eSentStart;
\r
162 static portLONG lMessageIndex = -i2cBUFFER_ADDRESS_BYTES; /* There are two address bytes to send prior to the data. */
\r
163 portBASE_TYPE xTaskWokenByTx = pdFALSE;
\r
164 portLONG lBytesLeft;
\r
166 /* The action taken for this interrupt depends on our current state. */
\r
167 switch( eCurrentState )
\r
171 /* We sent a start bit, if it was successful we can
\r
172 go on to send the slave address. */
\r
173 if( ( I2C_I2STAT == i2cSTATUS_START_TXED ) || ( I2C_I2STAT == i2cSTATUS_REP_START_TXED ) )
\r
175 /* Send the slave address. */
\r
176 I2C_I2DAT = pxCurrentMessage->ucSlaveAddress;
\r
178 if( pxCurrentMessage->ucSlaveAddress & i2cREAD )
\r
180 /* We are then going to read bytes back from the
\r
182 eCurrentState = eSentAddressForRead;
\r
184 /* Initialise the buffer index so the first byte goes
\r
185 into the first buffer position. */
\r
190 /* We are then going to write some data to the slave. */
\r
191 eCurrentState = eSentAddressForWrite;
\r
193 /* When writing bytes we first have to send the two
\r
194 byte buffer address so lMessageIndex is set negative,
\r
195 when it reaches 0 it is time to send the actual data. */
\r
196 lMessageIndex = -i2cBUFFER_ADDRESS_BYTES;
\r
201 /* Could not send the start bit so give up. */
\r
202 i2cEND_TRANSMISSION( pdFAIL );
\r
205 I2C_I2CONCLR = i2cSTA_BIT;
\r
209 case eSentAddressForWrite :
\r
211 /* We sent the address of the slave we are going to write to.
\r
212 If this was acknowledged we can go on to send the data. */
\r
213 if( I2C_I2STAT == i2cSTATUS_TX_ADDR_ACKED )
\r
215 /* Start the first byte transmitting which is the
\r
216 first byte of the buffer address to which the data will
\r
218 I2C_I2DAT = pxCurrentMessage->ucBufferAddressHighByte;
\r
219 eCurrentState = eSentData;
\r
223 /* Address was not acknowledged so give up. */
\r
224 i2cEND_TRANSMISSION( pdFAIL );
\r
228 case eSentAddressForRead :
\r
230 /* We sent the address of the slave we are going to read from.
\r
231 If this was acknowledged we can go on to read the data. */
\r
232 if( I2C_I2STAT == i2cSTATUS_RX_ADDR_ACKED )
\r
234 eCurrentState = eReceiveData;
\r
235 if( pxCurrentMessage->lMessageLength > i2cJUST_ONE_BYTE_TO_RX )
\r
237 /* Don't ack the last byte of the message. */
\r
238 I2C_I2CONSET = i2cAA_BIT;
\r
243 /* Something unexpected happened - give up. */
\r
244 i2cEND_TRANSMISSION( pdFAIL );
\r
248 case eReceiveData :
\r
250 /* We have just received a byte from the slave. */
\r
251 if( ( I2C_I2STAT == i2cSTATUS_DATA_RXED ) || ( I2C_I2STAT == i2cSTATUS_LAST_BYTE_RXED ) )
\r
253 /* Buffer the byte just received then increment the index
\r
254 so it points to the next free space. */
\r
255 pxCurrentMessage->pucBuffer[ lMessageIndex ] = I2C_I2DAT;
\r
258 /* How many more bytes are we expecting to receive? */
\r
259 lBytesLeft = pxCurrentMessage->lMessageLength - lMessageIndex;
\r
260 if( lBytesLeft == ( unsigned portLONG ) 0 )
\r
262 /* This was the last byte in the message. */
\r
263 i2cEND_TRANSMISSION( pdPASS );
\r
265 /* If xMessageCompleteSemaphore is not null then there
\r
266 is a task waiting for this message to complete and we
\r
267 must 'give' the semaphore so the task is woken.*/
\r
268 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
270 xTaskWokenByTx = xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, xTaskWokenByTx );
\r
273 /* Are there any other messages to transact? */
\r
274 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xTaskWokenByTx ) == pdTRUE )
\r
276 /* Start the next message - which was
\r
277 retrieved from the queue. */
\r
278 I2C_I2CONSET = i2cSTA_BIT;
\r
282 /* No more messages were found to be waiting for
\r
283 transaction so the bus is free. */
\r
284 ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
289 /* There are more bytes to receive but don't ack the
\r
291 if( lBytesLeft <= i2cJUST_ONE_BYTE_TO_RX )
\r
293 I2C_I2CONCLR = i2cAA_BIT;
\r
299 /* Something unexpected happened - give up. */
\r
300 i2cEND_TRANSMISSION( pdFAIL );
\r
307 /* We sent a data byte, if successful send the next byte in
\r
309 if( I2C_I2STAT == i2cSTATUS_DATA_TXED )
\r
311 /* Index to the next byte to send. */
\r
313 if( lMessageIndex < 0 )
\r
315 /* lMessage index is still negative so we have so far
\r
316 only sent the first byte of the buffer address. Send
\r
317 the second byte now, then initialise the buffer index
\r
318 to zero so the next byte sent comes from the actual
\r
320 I2C_I2DAT = pxCurrentMessage->ucBufferAddressLowByte;
\r
322 else if( lMessageIndex < pxCurrentMessage->lMessageLength )
\r
324 /* Simply send the next byte in the tx buffer. */
\r
325 I2C_I2DAT = pxCurrentMessage->pucBuffer[ lMessageIndex ];
\r
329 /* No more bytes in this message to be send. Finished
\r
330 sending message - send a stop bit. */
\r
331 i2cEND_TRANSMISSION( pdPASS );
\r
333 /* If xMessageCompleteSemaphore is not null then there
\r
334 is a task waiting for this message to be sent and the
\r
335 semaphore must be 'given' to wake the task. */
\r
336 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
338 xTaskWokenByTx = xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, xTaskWokenByTx );
\r
341 /* Are there any other messages to transact? */
\r
342 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xTaskWokenByTx ) == pdTRUE )
\r
344 /* Start the next message from the Tx queue. */
\r
345 I2C_I2CONSET = i2cSTA_BIT;
\r
349 /* No more message were queues for transaction so
\r
350 the bus is free. */
\r
351 ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
357 /* Something unexpected happened, give up. */
\r
358 i2cEND_TRANSMISSION( pdFAIL );
\r
364 /* Should never get here. */
\r
365 eCurrentState = eSentStart;
\r
369 /* Clear the interrupt. */
\r
370 I2C_I2CONCLR = i2cSI_BIT;
\r
371 VICVectAddr = i2cCLEAR_VIC_INTERRUPT;
\r
373 if( xTaskWokenByTx )
\r
375 portYIELD_FROM_ISR();
\r
378 /*-----------------------------------------------------------*/
\r