2 FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 /* Standard includes. */
\r
69 /* Scheduler include files. */
\r
70 #include "FreeRTOS.h"
\r
75 /* Application includes. */
\r
78 /*-----------------------------------------------------------*/
\r
80 /* Bit definitions within the I2CONCLR register. */
\r
81 #define i2cSTA_BIT ( ( unsigned char ) 0x20 )
\r
82 #define i2cSI_BIT ( ( unsigned char ) 0x08 )
\r
83 #define i2cSTO_BIT ( ( unsigned char ) 0x10 )
\r
84 #define i2cAA_BIT ( ( unsigned char ) 0x04 )
\r
86 /* Status codes for the I2STAT register. */
\r
87 #define i2cSTATUS_START_TXED ( 0x08 )
\r
88 #define i2cSTATUS_REP_START_TXED ( 0x10 )
\r
89 #define i2cSTATUS_TX_ADDR_ACKED ( 0x18 )
\r
90 #define i2cSTATUS_DATA_TXED ( 0x28 )
\r
91 #define i2cSTATUS_RX_ADDR_ACKED ( 0x40 )
\r
92 #define i2cSTATUS_DATA_RXED ( 0x50 )
\r
93 #define i2cSTATUS_LAST_BYTE_RXED ( 0x58 )
\r
95 /* Constants for operation of the VIC. */
\r
96 #define i2cCLEAR_VIC_INTERRUPT ( 0 )
\r
98 /* Misc constants. */
\r
99 #define i2cJUST_ONE_BYTE_TO_RX ( 1 )
\r
100 #define i2cBUFFER_ADDRESS_BYTES ( 2 )
\r
102 /* End the current transmission and free the bus. */
\r
103 #define i2cEND_TRANSMISSION( lStatus ) \
\r
105 I2C_I2CONCLR = i2cAA_BIT; \
\r
106 I2C_I2CONSET = i2cSTO_BIT; \
\r
107 eCurrentState = eSentStart; \
\r
108 lTransactionCompleted = lStatus; \
\r
110 /*-----------------------------------------------------------*/
\r
112 /* Valid i2c communication states. */
\r
115 eSentStart, /*<< Last action was the transmission of a start bit. */
\r
116 eSentAddressForWrite, /*<< Last action was the transmission of the slave address we are to write to. */
\r
117 eSentAddressForRead, /*<< Last action was the transmission of the slave address we are to read from. */
\r
118 eSentData, /*<< Last action was the transmission of a data byte. */
\r
119 eReceiveData /*<< We expected data to be received. */
\r
121 /*-----------------------------------------------------------*/
\r
123 /* Points to the message currently being sent. */
\r
124 volatile xI2CMessage *pxCurrentMessage = NULL;
\r
126 /* The queue of messages waiting to be transmitted. */
\r
127 static xQueueHandle xMessagesForTx;
\r
129 /* Flag used to indicate whether or not the ISR is amid sending a message. */
\r
130 unsigned long ulBusFree = ( unsigned long ) pdTRUE;
\r
132 /* Setting this to true will cause the TCP task to think a message is
\r
133 complete and thus restart. It can therefore be used under error states
\r
134 to force a restart. */
\r
135 volatile long lTransactionCompleted = pdTRUE;
\r
137 /*-----------------------------------------------------------*/
\r
139 void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxTxMessages, unsigned long **ppulBusFree )
\r
141 /* Create the queues used to hold Rx and Tx characters. */
\r
142 xMessagesForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( xI2CMessage * ) );
\r
144 /* Pass back a reference to the queue and bus free flag so the I2C API file
\r
145 can post messages. */
\r
146 *pxTxMessages = xMessagesForTx;
\r
147 *ppulBusFree = &ulBusFree;
\r
149 /*-----------------------------------------------------------*/
\r
151 /* The ISR entry point. */
\r
152 void vI2C_ISR_Wrapper( void ) __attribute__ (( naked ));
\r
154 /* The ISR function to perform the actual work. This must be a separate
\r
155 function from the wrapper to ensure the correct stack frame is set up. */
\r
156 void vI2C_ISR_Handler( void );
\r
158 /*-----------------------------------------------------------*/
\r
160 void vI2C_ISR_Wrapper( void )
\r
162 /* Save the context of the interrupted task. */
\r
163 portSAVE_CONTEXT();
\r
165 /* Call the handler to perform the actual work. This must be a
\r
166 separate function to ensure the correct stack frame is set up. */
\r
167 vI2C_ISR_Handler();
\r
169 /* Restore the context of whichever task is going to run next. */
\r
170 portRESTORE_CONTEXT();
\r
172 /*-----------------------------------------------------------*/
\r
174 void vI2C_ISR_Handler( void )
\r
176 /* Holds the current transmission state. */
\r
177 static I2C_STATE eCurrentState = eSentStart;
\r
178 static long lMessageIndex = -i2cBUFFER_ADDRESS_BYTES; /* There are two address bytes to send prior to the data. */
\r
179 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
182 /* The action taken for this interrupt depends on our current state. */
\r
183 switch( eCurrentState )
\r
187 /* We sent a start bit, if it was successful we can
\r
188 go on to send the slave address. */
\r
189 if( ( I2C_I2STAT == i2cSTATUS_START_TXED ) || ( I2C_I2STAT == i2cSTATUS_REP_START_TXED ) )
\r
191 /* Send the slave address. */
\r
192 I2C_I2DAT = pxCurrentMessage->ucSlaveAddress;
\r
194 if( pxCurrentMessage->ucSlaveAddress & i2cREAD )
\r
196 /* We are then going to read bytes back from the
\r
198 eCurrentState = eSentAddressForRead;
\r
200 /* Initialise the buffer index so the first byte goes
\r
201 into the first buffer position. */
\r
206 /* We are then going to write some data to the slave. */
\r
207 eCurrentState = eSentAddressForWrite;
\r
209 /* When writing bytes we first have to send the two
\r
210 byte buffer address so lMessageIndex is set negative,
\r
211 when it reaches 0 it is time to send the actual data. */
\r
212 lMessageIndex = -i2cBUFFER_ADDRESS_BYTES;
\r
217 /* Could not send the start bit so give up. */
\r
218 i2cEND_TRANSMISSION( pdFAIL );
\r
221 I2C_I2CONCLR = i2cSTA_BIT;
\r
225 case eSentAddressForWrite :
\r
227 /* We sent the address of the slave we are going to write to.
\r
228 If this was acknowledged we can go on to send the data. */
\r
229 if( I2C_I2STAT == i2cSTATUS_TX_ADDR_ACKED )
\r
231 /* Start the first byte transmitting which is the
\r
232 first byte of the buffer address to which the data will
\r
234 I2C_I2DAT = pxCurrentMessage->ucBufferAddressHighByte;
\r
235 eCurrentState = eSentData;
\r
239 /* Address was not acknowledged so give up. */
\r
240 i2cEND_TRANSMISSION( pdFAIL );
\r
244 case eSentAddressForRead :
\r
246 /* We sent the address of the slave we are going to read from.
\r
247 If this was acknowledged we can go on to read the data. */
\r
248 if( I2C_I2STAT == i2cSTATUS_RX_ADDR_ACKED )
\r
250 eCurrentState = eReceiveData;
\r
251 if( pxCurrentMessage->lMessageLength > i2cJUST_ONE_BYTE_TO_RX )
\r
253 /* Don't ack the last byte of the message. */
\r
254 I2C_I2CONSET = i2cAA_BIT;
\r
259 /* Something unexpected happened - give up. */
\r
260 i2cEND_TRANSMISSION( pdFAIL );
\r
264 case eReceiveData :
\r
266 /* We have just received a byte from the slave. */
\r
267 if( ( I2C_I2STAT == i2cSTATUS_DATA_RXED ) || ( I2C_I2STAT == i2cSTATUS_LAST_BYTE_RXED ) )
\r
269 /* Buffer the byte just received then increment the index
\r
270 so it points to the next free space. */
\r
271 pxCurrentMessage->pucBuffer[ lMessageIndex ] = I2C_I2DAT;
\r
274 /* How many more bytes are we expecting to receive? */
\r
275 lBytesLeft = pxCurrentMessage->lMessageLength - lMessageIndex;
\r
276 if( lBytesLeft == ( unsigned long ) 0 )
\r
278 /* This was the last byte in the message. */
\r
279 i2cEND_TRANSMISSION( pdPASS );
\r
281 /* If xMessageCompleteSemaphore is not null then there
\r
282 is a task waiting for this message to complete and we
\r
283 must 'give' the semaphore so the task is woken.*/
\r
284 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
286 xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
289 /* Are there any other messages to transact? */
\r
290 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
292 /* Start the next message - which was
\r
293 retrieved from the queue. */
\r
294 I2C_I2CONSET = i2cSTA_BIT;
\r
298 /* No more messages were found to be waiting for
\r
299 transaction so the bus is free. */
\r
300 ulBusFree = ( unsigned long ) pdTRUE;
\r
305 /* There are more bytes to receive but don't ack the
\r
307 if( lBytesLeft <= i2cJUST_ONE_BYTE_TO_RX )
\r
309 I2C_I2CONCLR = i2cAA_BIT;
\r
315 /* Something unexpected happened - give up. */
\r
316 i2cEND_TRANSMISSION( pdFAIL );
\r
323 /* We sent a data byte, if successful send the next byte in
\r
325 if( I2C_I2STAT == i2cSTATUS_DATA_TXED )
\r
327 /* Index to the next byte to send. */
\r
329 if( lMessageIndex < 0 )
\r
331 /* lMessage index is still negative so we have so far
\r
332 only sent the first byte of the buffer address. Send
\r
333 the second byte now, then initialise the buffer index
\r
334 to zero so the next byte sent comes from the actual
\r
336 I2C_I2DAT = pxCurrentMessage->ucBufferAddressLowByte;
\r
338 else if( lMessageIndex < pxCurrentMessage->lMessageLength )
\r
340 /* Simply send the next byte in the tx buffer. */
\r
341 I2C_I2DAT = pxCurrentMessage->pucBuffer[ lMessageIndex ];
\r
345 /* No more bytes in this message to be send. Finished
\r
346 sending message - send a stop bit. */
\r
347 i2cEND_TRANSMISSION( pdPASS );
\r
349 /* If xMessageCompleteSemaphore is not null then there
\r
350 is a task waiting for this message to be sent and the
\r
351 semaphore must be 'given' to wake the task. */
\r
352 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
354 xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
357 /* Are there any other messages to transact? */
\r
358 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
360 /* Start the next message from the Tx queue. */
\r
361 I2C_I2CONSET = i2cSTA_BIT;
\r
365 /* No more message were queues for transaction so
\r
366 the bus is free. */
\r
367 ulBusFree = ( unsigned long ) pdTRUE;
\r
373 /* Something unexpected happened, give up. */
\r
374 i2cEND_TRANSMISSION( pdFAIL );
\r
380 /* Should never get here. */
\r
381 eCurrentState = eSentStart;
\r
385 /* Clear the interrupt. */
\r
386 I2C_I2CONCLR = i2cSI_BIT;
\r
387 VICVectAddr = i2cCLEAR_VIC_INTERRUPT;
\r
389 if( xHigherPriorityTaskWoken )
\r
391 portYIELD_FROM_ISR();
\r
394 /*-----------------------------------------------------------*/
\r