2 FreeRTOS.org V5.0.4 - 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! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
51 /* Standard includes. */
\r
54 /* Scheduler include files. */
\r
55 #include "FreeRTOS.h"
\r
60 /* Application includes. */
\r
63 /*-----------------------------------------------------------*/
\r
65 /* Bit definitions within the I2CONCLR register. */
\r
66 #define i2cSTA_BIT ( ( unsigned portCHAR ) 0x20 )
\r
67 #define i2cSI_BIT ( ( unsigned portCHAR ) 0x08 )
\r
68 #define i2cSTO_BIT ( ( unsigned portCHAR ) 0x10 )
\r
69 #define i2cAA_BIT ( ( unsigned portCHAR ) 0x04 )
\r
71 /* Status codes for the I2STAT register. */
\r
72 #define i2cSTATUS_START_TXED ( 0x08 )
\r
73 #define i2cSTATUS_REP_START_TXED ( 0x10 )
\r
74 #define i2cSTATUS_TX_ADDR_ACKED ( 0x18 )
\r
75 #define i2cSTATUS_DATA_TXED ( 0x28 )
\r
76 #define i2cSTATUS_RX_ADDR_ACKED ( 0x40 )
\r
77 #define i2cSTATUS_DATA_RXED ( 0x50 )
\r
78 #define i2cSTATUS_LAST_BYTE_RXED ( 0x58 )
\r
80 /* Constants for operation of the VIC. */
\r
81 #define i2cCLEAR_VIC_INTERRUPT ( 0 )
\r
83 /* Misc constants. */
\r
84 #define i2cJUST_ONE_BYTE_TO_RX ( 1 )
\r
85 #define i2cBUFFER_ADDRESS_BYTES ( 2 )
\r
87 /* End the current transmission and free the bus. */
\r
88 #define i2cEND_TRANSMISSION( lStatus ) \
\r
90 I2C_I2CONCLR = i2cAA_BIT; \
\r
91 I2C_I2CONSET = i2cSTO_BIT; \
\r
92 eCurrentState = eSentStart; \
\r
93 lTransactionCompleted = lStatus; \
\r
95 /*-----------------------------------------------------------*/
\r
97 /* Valid i2c communication states. */
\r
100 eSentStart, /*<< Last action was the transmission of a start bit. */
\r
101 eSentAddressForWrite, /*<< Last action was the transmission of the slave address we are to write to. */
\r
102 eSentAddressForRead, /*<< Last action was the transmission of the slave address we are to read from. */
\r
103 eSentData, /*<< Last action was the transmission of a data byte. */
\r
104 eReceiveData /*<< We expected data to be received. */
\r
106 /*-----------------------------------------------------------*/
\r
108 /* Points to the message currently being sent. */
\r
109 volatile xI2CMessage *pxCurrentMessage = NULL;
\r
111 /* The queue of messages waiting to be transmitted. */
\r
112 static xQueueHandle xMessagesForTx;
\r
114 /* Flag used to indicate whether or not the ISR is amid sending a message. */
\r
115 unsigned portLONG ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
117 /* Setting this to true will cause the TCP task to think a message is
\r
118 complete and thus restart. It can therefore be used under error states
\r
119 to force a restart. */
\r
120 volatile portLONG lTransactionCompleted = pdTRUE;
\r
122 /*-----------------------------------------------------------*/
\r
124 void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxTxMessages, unsigned portLONG **ppulBusFree )
\r
126 /* Create the queues used to hold Rx and Tx characters. */
\r
127 xMessagesForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( xI2CMessage * ) );
\r
129 /* Pass back a reference to the queue and bus free flag so the I2C API file
\r
130 can post messages. */
\r
131 *pxTxMessages = xMessagesForTx;
\r
132 *ppulBusFree = &ulBusFree;
\r
134 /*-----------------------------------------------------------*/
\r
136 /* The ISR entry point. */
\r
137 void vI2C_ISR_Wrapper( void ) __attribute__ (( naked ));
\r
139 /* The ISR function to perform the actual work. This must be a separate
\r
140 function from the wrapper to ensure the correct stack frame is set up. */
\r
141 void vI2C_ISR_Handler( void );
\r
143 /*-----------------------------------------------------------*/
\r
145 void vI2C_ISR_Wrapper( void )
\r
147 /* Save the context of the interrupted task. */
\r
148 portSAVE_CONTEXT();
\r
150 /* Call the handler to perform the actual work. This must be a
\r
151 separate function to ensure the correct stack frame is set up. */
\r
152 vI2C_ISR_Handler();
\r
154 /* Restore the context of whichever task is going to run next. */
\r
155 portRESTORE_CONTEXT();
\r
157 /*-----------------------------------------------------------*/
\r
159 void vI2C_ISR_Handler( void )
\r
161 /* Holds the current transmission state. */
\r
162 static I2C_STATE eCurrentState = eSentStart;
\r
163 static portLONG lMessageIndex = -i2cBUFFER_ADDRESS_BYTES; /* There are two address bytes to send prior to the data. */
\r
164 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
165 portLONG lBytesLeft;
\r
167 /* The action taken for this interrupt depends on our current state. */
\r
168 switch( eCurrentState )
\r
172 /* We sent a start bit, if it was successful we can
\r
173 go on to send the slave address. */
\r
174 if( ( I2C_I2STAT == i2cSTATUS_START_TXED ) || ( I2C_I2STAT == i2cSTATUS_REP_START_TXED ) )
\r
176 /* Send the slave address. */
\r
177 I2C_I2DAT = pxCurrentMessage->ucSlaveAddress;
\r
179 if( pxCurrentMessage->ucSlaveAddress & i2cREAD )
\r
181 /* We are then going to read bytes back from the
\r
183 eCurrentState = eSentAddressForRead;
\r
185 /* Initialise the buffer index so the first byte goes
\r
186 into the first buffer position. */
\r
191 /* We are then going to write some data to the slave. */
\r
192 eCurrentState = eSentAddressForWrite;
\r
194 /* When writing bytes we first have to send the two
\r
195 byte buffer address so lMessageIndex is set negative,
\r
196 when it reaches 0 it is time to send the actual data. */
\r
197 lMessageIndex = -i2cBUFFER_ADDRESS_BYTES;
\r
202 /* Could not send the start bit so give up. */
\r
203 i2cEND_TRANSMISSION( pdFAIL );
\r
206 I2C_I2CONCLR = i2cSTA_BIT;
\r
210 case eSentAddressForWrite :
\r
212 /* We sent the address of the slave we are going to write to.
\r
213 If this was acknowledged we can go on to send the data. */
\r
214 if( I2C_I2STAT == i2cSTATUS_TX_ADDR_ACKED )
\r
216 /* Start the first byte transmitting which is the
\r
217 first byte of the buffer address to which the data will
\r
219 I2C_I2DAT = pxCurrentMessage->ucBufferAddressHighByte;
\r
220 eCurrentState = eSentData;
\r
224 /* Address was not acknowledged so give up. */
\r
225 i2cEND_TRANSMISSION( pdFAIL );
\r
229 case eSentAddressForRead :
\r
231 /* We sent the address of the slave we are going to read from.
\r
232 If this was acknowledged we can go on to read the data. */
\r
233 if( I2C_I2STAT == i2cSTATUS_RX_ADDR_ACKED )
\r
235 eCurrentState = eReceiveData;
\r
236 if( pxCurrentMessage->lMessageLength > i2cJUST_ONE_BYTE_TO_RX )
\r
238 /* Don't ack the last byte of the message. */
\r
239 I2C_I2CONSET = i2cAA_BIT;
\r
244 /* Something unexpected happened - give up. */
\r
245 i2cEND_TRANSMISSION( pdFAIL );
\r
249 case eReceiveData :
\r
251 /* We have just received a byte from the slave. */
\r
252 if( ( I2C_I2STAT == i2cSTATUS_DATA_RXED ) || ( I2C_I2STAT == i2cSTATUS_LAST_BYTE_RXED ) )
\r
254 /* Buffer the byte just received then increment the index
\r
255 so it points to the next free space. */
\r
256 pxCurrentMessage->pucBuffer[ lMessageIndex ] = I2C_I2DAT;
\r
259 /* How many more bytes are we expecting to receive? */
\r
260 lBytesLeft = pxCurrentMessage->lMessageLength - lMessageIndex;
\r
261 if( lBytesLeft == ( unsigned portLONG ) 0 )
\r
263 /* This was the last byte in the message. */
\r
264 i2cEND_TRANSMISSION( pdPASS );
\r
266 /* If xMessageCompleteSemaphore is not null then there
\r
267 is a task waiting for this message to complete and we
\r
268 must 'give' the semaphore so the task is woken.*/
\r
269 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
271 xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
274 /* Are there any other messages to transact? */
\r
275 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
277 /* Start the next message - which was
\r
278 retrieved from the queue. */
\r
279 I2C_I2CONSET = i2cSTA_BIT;
\r
283 /* No more messages were found to be waiting for
\r
284 transaction so the bus is free. */
\r
285 ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
290 /* There are more bytes to receive but don't ack the
\r
292 if( lBytesLeft <= i2cJUST_ONE_BYTE_TO_RX )
\r
294 I2C_I2CONCLR = i2cAA_BIT;
\r
300 /* Something unexpected happened - give up. */
\r
301 i2cEND_TRANSMISSION( pdFAIL );
\r
308 /* We sent a data byte, if successful send the next byte in
\r
310 if( I2C_I2STAT == i2cSTATUS_DATA_TXED )
\r
312 /* Index to the next byte to send. */
\r
314 if( lMessageIndex < 0 )
\r
316 /* lMessage index is still negative so we have so far
\r
317 only sent the first byte of the buffer address. Send
\r
318 the second byte now, then initialise the buffer index
\r
319 to zero so the next byte sent comes from the actual
\r
321 I2C_I2DAT = pxCurrentMessage->ucBufferAddressLowByte;
\r
323 else if( lMessageIndex < pxCurrentMessage->lMessageLength )
\r
325 /* Simply send the next byte in the tx buffer. */
\r
326 I2C_I2DAT = pxCurrentMessage->pucBuffer[ lMessageIndex ];
\r
330 /* No more bytes in this message to be send. Finished
\r
331 sending message - send a stop bit. */
\r
332 i2cEND_TRANSMISSION( pdPASS );
\r
334 /* If xMessageCompleteSemaphore is not null then there
\r
335 is a task waiting for this message to be sent and the
\r
336 semaphore must be 'given' to wake the task. */
\r
337 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
339 xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
342 /* Are there any other messages to transact? */
\r
343 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
345 /* Start the next message from the Tx queue. */
\r
346 I2C_I2CONSET = i2cSTA_BIT;
\r
350 /* No more message were queues for transaction so
\r
351 the bus is free. */
\r
352 ulBusFree = ( unsigned portLONG ) pdTRUE;
\r
358 /* Something unexpected happened, give up. */
\r
359 i2cEND_TRANSMISSION( pdFAIL );
\r
365 /* Should never get here. */
\r
366 eCurrentState = eSentStart;
\r
370 /* Clear the interrupt. */
\r
371 I2C_I2CONCLR = i2cSI_BIT;
\r
372 VICVectAddr = i2cCLEAR_VIC_INTERRUPT;
\r
374 if( xHigherPriorityTaskWoken )
\r
376 portYIELD_FROM_ISR();
\r
379 /*-----------------------------------------------------------*/
\r