2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 /* Standard includes. */
\r
79 /* Scheduler include files. */
\r
80 #include "FreeRTOS.h"
\r
85 /* Application includes. */
\r
88 /*-----------------------------------------------------------*/
\r
90 /* Bit definitions within the I2CONCLR register. */
\r
91 #define i2cSTA_BIT ( ( unsigned char ) 0x20 )
\r
92 #define i2cSI_BIT ( ( unsigned char ) 0x08 )
\r
93 #define i2cSTO_BIT ( ( unsigned char ) 0x10 )
\r
94 #define i2cAA_BIT ( ( unsigned char ) 0x04 )
\r
96 /* Status codes for the I2STAT register. */
\r
97 #define i2cSTATUS_START_TXED ( 0x08 )
\r
98 #define i2cSTATUS_REP_START_TXED ( 0x10 )
\r
99 #define i2cSTATUS_TX_ADDR_ACKED ( 0x18 )
\r
100 #define i2cSTATUS_DATA_TXED ( 0x28 )
\r
101 #define i2cSTATUS_RX_ADDR_ACKED ( 0x40 )
\r
102 #define i2cSTATUS_DATA_RXED ( 0x50 )
\r
103 #define i2cSTATUS_LAST_BYTE_RXED ( 0x58 )
\r
105 /* Constants for operation of the VIC. */
\r
106 #define i2cCLEAR_VIC_INTERRUPT ( 0 )
\r
108 /* Misc constants. */
\r
109 #define i2cJUST_ONE_BYTE_TO_RX ( 1 )
\r
110 #define i2cBUFFER_ADDRESS_BYTES ( 2 )
\r
112 /* End the current transmission and free the bus. */
\r
113 #define i2cEND_TRANSMISSION( lStatus ) \
\r
115 I2C_I2CONCLR = i2cAA_BIT; \
\r
116 I2C_I2CONSET = i2cSTO_BIT; \
\r
117 eCurrentState = eSentStart; \
\r
118 lTransactionCompleted = lStatus; \
\r
120 /*-----------------------------------------------------------*/
\r
122 /* Valid i2c communication states. */
\r
125 eSentStart, /*<< Last action was the transmission of a start bit. */
\r
126 eSentAddressForWrite, /*<< Last action was the transmission of the slave address we are to write to. */
\r
127 eSentAddressForRead, /*<< Last action was the transmission of the slave address we are to read from. */
\r
128 eSentData, /*<< Last action was the transmission of a data byte. */
\r
129 eReceiveData /*<< We expected data to be received. */
\r
131 /*-----------------------------------------------------------*/
\r
133 /* Points to the message currently being sent. */
\r
134 volatile xI2CMessage *pxCurrentMessage = NULL;
\r
136 /* The queue of messages waiting to be transmitted. */
\r
137 static xQueueHandle xMessagesForTx;
\r
139 /* Flag used to indicate whether or not the ISR is amid sending a message. */
\r
140 unsigned long ulBusFree = ( unsigned long ) pdTRUE;
\r
142 /* Setting this to true will cause the TCP task to think a message is
\r
143 complete and thus restart. It can therefore be used under error states
\r
144 to force a restart. */
\r
145 volatile long lTransactionCompleted = pdTRUE;
\r
147 /*-----------------------------------------------------------*/
\r
149 void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxTxMessages, unsigned long **ppulBusFree )
\r
151 /* Create the queues used to hold Rx and Tx characters. */
\r
152 xMessagesForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( xI2CMessage * ) );
\r
154 /* Pass back a reference to the queue and bus free flag so the I2C API file
\r
155 can post messages. */
\r
156 *pxTxMessages = xMessagesForTx;
\r
157 *ppulBusFree = &ulBusFree;
\r
159 /*-----------------------------------------------------------*/
\r
161 /* The ISR entry point. */
\r
162 void vI2C_ISR_Wrapper( void ) __attribute__ (( naked ));
\r
164 /* The ISR function to perform the actual work. This must be a separate
\r
165 function from the wrapper to ensure the correct stack frame is set up. */
\r
166 void vI2C_ISR_Handler( void );
\r
168 /*-----------------------------------------------------------*/
\r
170 void vI2C_ISR_Wrapper( void )
\r
172 /* Save the context of the interrupted task. */
\r
173 portSAVE_CONTEXT();
\r
175 /* Call the handler to perform the actual work. This must be a
\r
176 separate function to ensure the correct stack frame is set up. */
\r
177 vI2C_ISR_Handler();
\r
179 /* Restore the context of whichever task is going to run next. */
\r
180 portRESTORE_CONTEXT();
\r
182 /*-----------------------------------------------------------*/
\r
184 void vI2C_ISR_Handler( void )
\r
186 /* Holds the current transmission state. */
\r
187 static I2C_STATE eCurrentState = eSentStart;
\r
188 static long lMessageIndex = -i2cBUFFER_ADDRESS_BYTES; /* There are two address bytes to send prior to the data. */
\r
189 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
192 /* The action taken for this interrupt depends on our current state. */
\r
193 switch( eCurrentState )
\r
197 /* We sent a start bit, if it was successful we can
\r
198 go on to send the slave address. */
\r
199 if( ( I2C_I2STAT == i2cSTATUS_START_TXED ) || ( I2C_I2STAT == i2cSTATUS_REP_START_TXED ) )
\r
201 /* Send the slave address. */
\r
202 I2C_I2DAT = pxCurrentMessage->ucSlaveAddress;
\r
204 if( pxCurrentMessage->ucSlaveAddress & i2cREAD )
\r
206 /* We are then going to read bytes back from the
\r
208 eCurrentState = eSentAddressForRead;
\r
210 /* Initialise the buffer index so the first byte goes
\r
211 into the first buffer position. */
\r
216 /* We are then going to write some data to the slave. */
\r
217 eCurrentState = eSentAddressForWrite;
\r
219 /* When writing bytes we first have to send the two
\r
220 byte buffer address so lMessageIndex is set negative,
\r
221 when it reaches 0 it is time to send the actual data. */
\r
222 lMessageIndex = -i2cBUFFER_ADDRESS_BYTES;
\r
227 /* Could not send the start bit so give up. */
\r
228 i2cEND_TRANSMISSION( pdFAIL );
\r
231 I2C_I2CONCLR = i2cSTA_BIT;
\r
235 case eSentAddressForWrite :
\r
237 /* We sent the address of the slave we are going to write to.
\r
238 If this was acknowledged we can go on to send the data. */
\r
239 if( I2C_I2STAT == i2cSTATUS_TX_ADDR_ACKED )
\r
241 /* Start the first byte transmitting which is the
\r
242 first byte of the buffer address to which the data will
\r
244 I2C_I2DAT = pxCurrentMessage->ucBufferAddressHighByte;
\r
245 eCurrentState = eSentData;
\r
249 /* Address was not acknowledged so give up. */
\r
250 i2cEND_TRANSMISSION( pdFAIL );
\r
254 case eSentAddressForRead :
\r
256 /* We sent the address of the slave we are going to read from.
\r
257 If this was acknowledged we can go on to read the data. */
\r
258 if( I2C_I2STAT == i2cSTATUS_RX_ADDR_ACKED )
\r
260 eCurrentState = eReceiveData;
\r
261 if( pxCurrentMessage->lMessageLength > i2cJUST_ONE_BYTE_TO_RX )
\r
263 /* Don't ack the last byte of the message. */
\r
264 I2C_I2CONSET = i2cAA_BIT;
\r
269 /* Something unexpected happened - give up. */
\r
270 i2cEND_TRANSMISSION( pdFAIL );
\r
274 case eReceiveData :
\r
276 /* We have just received a byte from the slave. */
\r
277 if( ( I2C_I2STAT == i2cSTATUS_DATA_RXED ) || ( I2C_I2STAT == i2cSTATUS_LAST_BYTE_RXED ) )
\r
279 /* Buffer the byte just received then increment the index
\r
280 so it points to the next free space. */
\r
281 pxCurrentMessage->pucBuffer[ lMessageIndex ] = I2C_I2DAT;
\r
284 /* How many more bytes are we expecting to receive? */
\r
285 lBytesLeft = pxCurrentMessage->lMessageLength - lMessageIndex;
\r
286 if( lBytesLeft == ( unsigned long ) 0 )
\r
288 /* This was the last byte in the message. */
\r
289 i2cEND_TRANSMISSION( pdPASS );
\r
291 /* If xMessageCompleteSemaphore is not null then there
\r
292 is a task waiting for this message to complete and we
\r
293 must 'give' the semaphore so the task is woken.*/
\r
294 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
296 xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
299 /* Are there any other messages to transact? */
\r
300 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
302 /* Start the next message - which was
\r
303 retrieved from the queue. */
\r
304 I2C_I2CONSET = i2cSTA_BIT;
\r
308 /* No more messages were found to be waiting for
\r
309 transaction so the bus is free. */
\r
310 ulBusFree = ( unsigned long ) pdTRUE;
\r
315 /* There are more bytes to receive but don't ack the
\r
317 if( lBytesLeft <= i2cJUST_ONE_BYTE_TO_RX )
\r
319 I2C_I2CONCLR = i2cAA_BIT;
\r
325 /* Something unexpected happened - give up. */
\r
326 i2cEND_TRANSMISSION( pdFAIL );
\r
333 /* We sent a data byte, if successful send the next byte in
\r
335 if( I2C_I2STAT == i2cSTATUS_DATA_TXED )
\r
337 /* Index to the next byte to send. */
\r
339 if( lMessageIndex < 0 )
\r
341 /* lMessage index is still negative so we have so far
\r
342 only sent the first byte of the buffer address. Send
\r
343 the second byte now, then initialise the buffer index
\r
344 to zero so the next byte sent comes from the actual
\r
346 I2C_I2DAT = pxCurrentMessage->ucBufferAddressLowByte;
\r
348 else if( lMessageIndex < pxCurrentMessage->lMessageLength )
\r
350 /* Simply send the next byte in the tx buffer. */
\r
351 I2C_I2DAT = pxCurrentMessage->pucBuffer[ lMessageIndex ];
\r
355 /* No more bytes in this message to be send. Finished
\r
356 sending message - send a stop bit. */
\r
357 i2cEND_TRANSMISSION( pdPASS );
\r
359 /* If xMessageCompleteSemaphore is not null then there
\r
360 is a task waiting for this message to be sent and the
\r
361 semaphore must be 'given' to wake the task. */
\r
362 if( pxCurrentMessage->xMessageCompleteSemaphore )
\r
364 xSemaphoreGiveFromISR( pxCurrentMessage->xMessageCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
367 /* Are there any other messages to transact? */
\r
368 if( xQueueReceiveFromISR( xMessagesForTx, &pxCurrentMessage, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
370 /* Start the next message from the Tx queue. */
\r
371 I2C_I2CONSET = i2cSTA_BIT;
\r
375 /* No more message were queues for transaction so
\r
376 the bus is free. */
\r
377 ulBusFree = ( unsigned long ) pdTRUE;
\r
383 /* Something unexpected happened, give up. */
\r
384 i2cEND_TRANSMISSION( pdFAIL );
\r
390 /* Should never get here. */
\r
391 eCurrentState = eSentStart;
\r
395 /* Clear the interrupt. */
\r
396 I2C_I2CONCLR = i2cSI_BIT;
\r
397 VICVectAddr = i2cCLEAR_VIC_INTERRUPT;
\r
399 if( xHigherPriorityTaskWoken )
\r
401 portYIELD_FROM_ISR();
\r
404 /*-----------------------------------------------------------*/
\r