2 FreeRTOS V7.5.3 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /*-----------------------------------------------------------
\r
67 * Normally, a demo application would define ParTest (parallel port test)
\r
68 * functions to write to an LED. In this case, four '*' symbols that are
\r
69 * output to the debug printf() port are used to simulate LED outputs.
\r
70 *-----------------------------------------------------------*/
\r
72 /* Standard includes. */
\r
76 /* Library includes. */
\r
77 #include "lpc43xx_i2c.h"
\r
79 /* Kernel includes. */
\r
80 #include "FreeRTOS.h"
\r
84 /* Standard demo include. */
\r
85 #include "partest.h"
\r
87 /* The number of LED outputs. */
\r
88 #define partstMAX_LEDS 4
\r
90 /* Commands written to the PCA9502. */
\r
91 #define partstIO_WRITE_COMMAND ( ( unsigned char ) ( 0x0BU << 3U ) )
\r
92 #define partstIO_DIR_COMMAND ( ( unsigned char ) ( 0x0AU << 3U ) )
\r
93 #define partstSLAVE_ADDRESS ( ( unsigned char ) ( 0x9AU >> 1U ) )
\r
95 /* Just defines the length of the queue used to pass toggle commands to the I2C
\r
97 #define partstLED_COMMAND_QUEUE_LENGTH ( 6 )
\r
98 /*-----------------------------------------------------------*/
\r
101 * The LEDs are connected to an I2C port expander. Therefore, writing to an
\r
102 * LED takes longer than might be expected if the LED was connected directly
\r
103 * to a GPIO pin. As several tasks, and a timer, toggle LEDs, it is convenient
\r
104 * to use a gatekeeper task to ensure access is both mutually exclusive and
\r
105 * serialised. Tasks other than this gatekeeper task must not access the I2C
\r
108 static void prvI2CGateKeeperTask( void *pvParameters );
\r
110 /* The queue used to communicate toggle commands with the I2C gatekeeper
\r
112 static xQueueHandle xI2CCommandQueue = NULL;
\r
113 /*-----------------------------------------------------------*/
\r
115 void vParTestInitialise( void )
\r
117 unsigned char ucBuffer[ 2 ];
\r
118 I2C_M_SETUP_Type xI2CMessage;
\r
120 /* The LEDs are on an I2C IO expander. Initialise the I2C interface. */
\r
121 I2C_Init( LPC_I2C0, 300000 );
\r
122 I2C_Cmd( LPC_I2C0, ENABLE );
\r
124 /* GPIO0-GPIO2 to output. */
\r
125 ucBuffer[ 0 ] = partstIO_DIR_COMMAND;
\r
126 ucBuffer[ 1 ] = 0x0f;
\r
127 xI2CMessage.sl_addr7bit = partstSLAVE_ADDRESS;
\r
128 xI2CMessage.tx_data = ucBuffer ;
\r
129 xI2CMessage.tx_length = sizeof( ucBuffer );
\r
130 xI2CMessage.rx_data = NULL;
\r
131 xI2CMessage.rx_length = 0;
\r
132 xI2CMessage.retransmissions_max = 3;
\r
133 I2C_MasterTransferData( LPC_I2C0, &xI2CMessage, I2C_TRANSFER_POLLING );
\r
135 /* Create the mutex used to guard access to the I2C bus. */
\r
136 xI2CCommandQueue = xQueueCreate( partstLED_COMMAND_QUEUE_LENGTH, sizeof( unsigned char ) );
\r
137 configASSERT( xI2CCommandQueue );
\r
139 /* Create the I2C gatekeeper task itself. */
\r
140 xTaskCreate( prvI2CGateKeeperTask, ( signed char * ) "I2C", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
\r
142 /*-----------------------------------------------------------*/
\r
144 void vParTestToggleLED( unsigned long ulLED )
\r
146 unsigned char ucLED = ( unsigned char ) ulLED;
\r
148 /* Only the gatekeeper task will actually access the I2C port, so send the
\r
149 toggle request to the gatekeeper task. A block time of zero is used as
\r
150 this function is called by a software timer callback. */
\r
151 xQueueSend( xI2CCommandQueue, &ucLED, 0UL );
\r
153 /*-----------------------------------------------------------*/
\r
155 static void prvI2CGateKeeperTask( void *pvParameters )
\r
157 unsigned char ucBuffer[ 2 ], ucLED;
\r
158 static unsigned char ucLEDState = 0xffU;
\r
159 static I2C_M_SETUP_Type xI2CMessage; /* Static so it is not on the stack as this is called from task code. */
\r
161 /* Just to remove compiler warnings. */
\r
162 ( void ) pvParameters;
\r
166 /* Wait for the next command. */
\r
167 xQueueReceive( xI2CCommandQueue, &ucLED, portMAX_DELAY );
\r
169 /* Only this task is allowed to touch the I2C port, so there is no need
\r
170 for additional mutual exclusion. */
\r
171 if( ucLED < partstMAX_LEDS )
\r
173 /* Which bit is being manipulated? */
\r
174 ucLED = 0x01 << ucLED;
\r
176 /* Is the bit currently set or clear? */
\r
177 if( ( ucLEDState & ucLED ) == 0U )
\r
179 ucLEDState |= ucLED;
\r
183 ucLEDState &= ~ucLED;
\r
186 ucBuffer[ 0 ] = partstIO_WRITE_COMMAND;
\r
187 ucBuffer[ 1 ] = ucLEDState;
\r
189 xI2CMessage.sl_addr7bit = partstSLAVE_ADDRESS;
\r
190 xI2CMessage.tx_data = ucBuffer ;
\r
191 xI2CMessage.tx_length = sizeof( ucBuffer );
\r
192 xI2CMessage.rx_data = NULL;
\r
193 xI2CMessage.rx_length = 0;
\r
194 xI2CMessage.retransmissions_max = 3;
\r
195 I2C_MasterTransferData( LPC_I2C0, &xI2CMessage, I2C_TRANSFER_POLLING );
\r
199 /*-----------------------------------------------------------*/
\r