2 FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 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 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
31 * This is a concise, step by step, 'hands on' guide that describes both *
\r
32 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
33 * explains numerous examples that are written using the FreeRTOS API. *
\r
34 * Full source code for all the examples is provided in an accompanying *
\r
37 ***************************************************************************
\r
38 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
54 /* Standard includes. */
\r
57 /* Scheduler include files. */
\r
58 #include "FreeRTOS.h"
\r
63 /* Application includes. */
\r
66 /*-----------------------------------------------------------*/
\r
68 /* Bit definitions within the I2CONCLR register. */
\r
69 #define i2cSTA_BIT ( ( unsigned portCHAR ) 0x20 )
\r
70 #define i2cSI_BIT ( ( unsigned portCHAR ) 0x08 )
\r
71 #define i2cSTO_BIT ( ( unsigned portCHAR ) 0x10 )
\r
72 #define i2cAA_BIT ( ( unsigned portCHAR ) 0x04 )
\r
74 /* Status codes for the I2STAT register. */
\r
75 #define i2cSTATUS_START_TXED ( 0x08 )
\r
76 #define i2cSTATUS_REP_START_TXED ( 0x10 )
\r
77 #define i2cSTATUS_TX_ADDR_ACKED ( 0x18 )
\r
78 #define i2cSTATUS_DATA_TXED ( 0x28 )
\r
79 #define i2cSTATUS_RX_ADDR_ACKED ( 0x40 )
\r
80 #define i2cSTATUS_DATA_RXED ( 0x50 )
\r
81 #define i2cSTATUS_LAST_BYTE_RXED ( 0x58 )
\r
83 /* Constants for operation of the VIC. */
\r
84 #define i2cCLEAR_VIC_INTERRUPT ( 0 )
\r
86 /* Misc constants. */
\r
87 #define i2cJUST_ONE_BYTE_TO_RX ( 1 )
\r
88 #define i2cBUFFER_ADDRESS_BYTES ( 2 )
\r
90 /* End the current transmission and free the bus. */
\r
91 #define i2cEND_TRANSMISSION( lStatus ) \
\r
93 I2C_I2CONCLR = i2cAA_BIT; \
\r
94 I2C_I2CONSET = i2cSTO_BIT; \
\r
95 eCurrentState = eSentStart; \
\r
96 lTransactionCompleted = lStatus; \
\r
98 /*-----------------------------------------------------------*/
\r
100 /* Valid i2c communication states. */
\r
103 eSentStart, /*<< Last action was the transmission of a start bit. */
\r
104 eSentAddressForWrite, /*<< Last action was the transmission of the slave address we are to write to. */
\r
105 eSentAddressForRead, /*<< Last action was the transmission of the slave address we are to read from. */
\r
106 eSentData, /*<< Last action was the transmission of a data byte. */
\r
107 eReceiveData /*<< We expected data to be received. */
\r
109 /*-----------------------------------------------------------*/
\r
111 /* Points to the message currently being sent. */
\r
112 volatile xI2CMessage *pxCurrentMessage = NULL;
\r
114 /* The queue of messages waiting to be transmitted. */
\r
115 static xQueueHandle xMessagesForTx;
\r
117 /* Flag used to indicate whether or not the ISR is amid sending a message. */
\r
118 unsigned portLONG ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
120 /* Setting this to true will cause the TCP task to think a message is
\r
121 complete and thus restart. It can therefore be used under error states
\r
122 to force a restart. */
\r
123 volatile portLONG lTransactionCompleted = pdTRUE;
\r
125 /*-----------------------------------------------------------*/
\r
127 void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxTxMessages, unsigned portLONG **ppulBusFree )
\r
129 /* Create the queues used to hold Rx and Tx characters. */
\r
130 xMessagesForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( xI2CMessage * ) );
\r
132 /* Pass back a reference to the queue and bus free flag so the I2C API file
\r
133 can post messages. */
\r
134 *pxTxMessages = xMessagesForTx;
\r
135 *ppulBusFree = &ulBusFree;
\r
137 /*-----------------------------------------------------------*/
\r
139 /* The ISR entry point. */
\r
140 void vI2C_ISR_Wrapper( void ) __attribute__ (( naked ));
\r
142 /* The ISR function to perform the actual work. This must be a separate
\r
143 function from the wrapper to ensure the correct stack frame is set up. */
\r
144 void vI2C_ISR_Handler( void );
\r
146 /*-----------------------------------------------------------*/
\r
148 void vI2C_ISR_Wrapper( void )
\r
150 /* Save the context of the interrupted task. */
\r
151 portSAVE_CONTEXT();
\r
153 /* Call the handler to perform the actual work. This must be a
\r
154 separate function to ensure the correct stack frame is set up. */
\r
155 vI2C_ISR_Handler();
\r
157 /* Restore the context of whichever task is going to run next. */
\r
158 portRESTORE_CONTEXT();
\r
160 /*-----------------------------------------------------------*/
\r
162 void vI2C_ISR_Handler( void )
\r
164 /* Holds the current transmission state. */
\r
165 static I2C_STATE eCurrentState = eSentStart;
\r
166 static portLONG lMessageIndex = -i2cBUFFER_ADDRESS_BYTES; /* There are two address bytes to send prior to the data. */
\r
167 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
168 portLONG lBytesLeft;
\r
170 /* The action taken for this interrupt depends on our current state. */
\r
171 switch( eCurrentState )
\r
175 /* We sent a start bit, if it was successful we can
\r
176 go on to send the slave address. */
\r
177 if( ( I2C_I2STAT == i2cSTATUS_START_TXED ) || ( I2C_I2STAT == i2cSTATUS_REP_START_TXED ) )
\r
179 /* Send the slave address. */
\r
180 I2C_I2DAT = pxCurrentMessage->ucSlaveAddress;
\r
182 if( pxCurrentMessage->ucSlaveAddress & i2cREAD )
\r
184 /* We are then going to read bytes back from the
\r
186 eCurrentState = eSentAddressForRead;
\r
188 /* Initialise the buffer index so the first byte goes
\r
189 into the first buffer position. */
\r
194 /* We are then going to write some data to the slave. */
\r
195 eCurrentState = eSentAddressForWrite;
\r
197 /* When writing bytes we first have to send the two
\r
198 byte buffer address so lMessageIndex is set negative,
\r
199 when it reaches 0 it is time to send the actual data. */
\r
200 lMessageIndex = -i2cBUFFER_ADDRESS_BYTES;
\r
205 /* Could not send the start bit so give up. */
\r
206 i2cEND_TRANSMISSION( pdFAIL );
\r
209 I2C_I2CONCLR = i2cSTA_BIT;
\r
213 case eSentAddressForWrite :
\r
215 /* We sent the address of the slave we are going to write to.
\r
216 If this was acknowledged we can go on to send the data. */
\r
217 if( I2C_I2STAT == i2cSTATUS_TX_ADDR_ACKED )
\r
219 /* Start the first byte transmitting which is the
\r
220 first byte of the buffer address to which the data will
\r
222 I2C_I2DAT = pxCurrentMessage->ucBufferAddressHighByte;
\r
223 eCurrentState = eSentData;
\r
227 /* Address was not acknowledged so give up. */
\r
228 i2cEND_TRANSMISSION( pdFAIL );
\r
232 case eSentAddressForRead :
\r
234 /* We sent the address of the slave we are going to read from.
\r
235 If this was acknowledged we can go on to read the data. */
\r
236 if( I2C_I2STAT == i2cSTATUS_RX_ADDR_ACKED )
\r
238 eCurrentState = eReceiveData;
\r
239 if( pxCurrentMessage->lMessageLength > i2cJUST_ONE_BYTE_TO_RX )
\r
241 /* Don't ack the last byte of the message. */
\r
242 I2C_I2CONSET = i2cAA_BIT;
\r
247 /* Something unexpected happened - give up. */
\r
248 i2cEND_TRANSMISSION( pdFAIL );
\r
252 case eReceiveData :
\r
254 /* We have just received a byte from the slave. */
\r
255 if( ( I2C_I2STAT == i2cSTATUS_DATA_RXED ) || ( I2C_I2STAT == i2cSTATUS_LAST_BYTE_RXED ) )
\r
257 /* Buffer the byte just received then increment the index
\r
258 so it points to the next free space. */
\r
259 pxCurrentMessage->pucBuffer[ lMessageIndex ] = I2C_I2DAT;
\r
262 /* How many more bytes are we expecting to receive? */
\r
263 lBytesLeft = pxCurrentMessage->lMessageLength - lMessageIndex;
\r
264 if( lBytesLeft == ( unsigned portLONG ) 0 )
\r
266 /* This was the last byte in the message. */
\r
267 i2cEND_TRANSMISSION( pdPASS );
\r
269 /* If xMessageCompleteSemaphore is not null then there
\r
270 is a task waiting for this message to complete and we
\r
271 must 'give' the semaphore so the task is woken.*/
\r
272 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
274 xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
277 /* Are there any other messages to transact? */
\r
278 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
280 /* Start the next message - which was
\r
281 retrieved from the queue. */
\r
282 I2C_I2CONSET = i2cSTA_BIT;
\r
286 /* No more messages were found to be waiting for
\r
287 transaction so the bus is free. */
\r
288 ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
293 /* There are more bytes to receive but don't ack the
\r
295 if( lBytesLeft <= i2cJUST_ONE_BYTE_TO_RX )
\r
297 I2C_I2CONCLR = i2cAA_BIT;
\r
303 /* Something unexpected happened - give up. */
\r
304 i2cEND_TRANSMISSION( pdFAIL );
\r
311 /* We sent a data byte, if successful send the next byte in
\r
313 if( I2C_I2STAT == i2cSTATUS_DATA_TXED )
\r
315 /* Index to the next byte to send. */
\r
317 if( lMessageIndex < 0 )
\r
319 /* lMessage index is still negative so we have so far
\r
320 only sent the first byte of the buffer address. Send
\r
321 the second byte now, then initialise the buffer index
\r
322 to zero so the next byte sent comes from the actual
\r
324 I2C_I2DAT = pxCurrentMessage->ucBufferAddressLowByte;
\r
326 else if( lMessageIndex < pxCurrentMessage->lMessageLength )
\r
328 /* Simply send the next byte in the tx buffer. */
\r
329 I2C_I2DAT = pxCurrentMessage->pucBuffer[ lMessageIndex ];
\r
333 /* No more bytes in this message to be send. Finished
\r
334 sending message - send a stop bit. */
\r
335 i2cEND_TRANSMISSION( pdPASS );
\r
337 /* If xMessageCompleteSemaphore is not null then there
\r
338 is a task waiting for this message to be sent and the
\r
339 semaphore must be 'given' to wake the task. */
\r
340 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
342 xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
345 /* Are there any other messages to transact? */
\r
346 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
348 /* Start the next message from the Tx queue. */
\r
349 I2C_I2CONSET = i2cSTA_BIT;
\r
353 /* No more message were queues for transaction so
\r
354 the bus is free. */
\r
355 ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
361 /* Something unexpected happened, give up. */
\r
362 i2cEND_TRANSMISSION( pdFAIL );
\r
368 /* Should never get here. */
\r
369 eCurrentState = eSentStart;
\r
373 /* Clear the interrupt. */
\r
374 I2C_I2CONCLR = i2cSI_BIT;
\r
375 VICVectAddr = i2cCLEAR_VIC_INTERRUPT;
\r
377 if( xHigherPriorityTaskWoken )
\r
379 portYIELD_FROM_ISR();
\r
382 /*-----------------------------------------------------------*/
\r