2 FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /*-----------------------------------------------------------
\r
55 * Normally, a demo application would define ParTest (parallel port test)
\r
56 * functions to write to an LED. In this case, four '*' symbols that are
\r
57 * output to the debug printf() port are used to simulate LED outputs.
\r
58 *-----------------------------------------------------------*/
\r
60 /* Standard includes. */
\r
64 /* Library includes. */
\r
65 #include "lpc43xx_i2c.h"
\r
67 /* Kernel includes. */
\r
68 #include "FreeRTOS.h"
\r
72 /* Standard demo include. */
\r
73 #include "partest.h"
\r
75 /* The number of LED outputs. */
\r
76 #define partstMAX_LEDS 4
\r
78 /* Commands written to the PCA9502. */
\r
79 #define partstIO_WRITE_COMMAND ( ( unsigned char ) ( 0x0BU << 3U ) )
\r
80 #define partstIO_DIR_COMMAND ( ( unsigned char ) ( 0x0AU << 3U ) )
\r
81 #define partstSLAVE_ADDRESS ( ( unsigned char ) ( 0x9AU >> 1U ) )
\r
83 /* Just defines the length of the queue used to pass toggle commands to the I2C
\r
85 #define partstLED_COMMAND_QUEUE_LENGTH ( 6 )
\r
86 /*-----------------------------------------------------------*/
\r
89 * The LEDs are connected to an I2C port expander. Therefore, writing to an
\r
90 * LED takes longer than might be expected if the LED was connected directly
\r
91 * to a GPIO pin. As several tasks, and a timer, toggle LEDs, it is convenient
\r
92 * to use a gatekeeper task to ensure access is both mutually exclusive and
\r
93 * serialised. Tasks other than this gatekeeper task must not access the I2C
\r
96 static void prvI2CGateKeeperTask( void *pvParameters );
\r
98 /* The queue used to communicate toggle commands with the I2C gatekeeper
\r
100 static xQueueHandle xI2CCommandQueue = NULL;
\r
101 /*-----------------------------------------------------------*/
\r
103 void vParTestInitialise( void )
\r
105 unsigned char ucBuffer[ 2 ];
\r
106 I2C_M_SETUP_Type xI2CMessage;
\r
108 /* The LEDs are on an I2C IO expander. Initialise the I2C interface. */
\r
109 I2C_Init( LPC_I2C0, 300000 );
\r
110 I2C_Cmd( LPC_I2C0, ENABLE );
\r
112 /* GPIO0-GPIO2 to output. */
\r
113 ucBuffer[ 0 ] = partstIO_DIR_COMMAND;
\r
114 ucBuffer[ 1 ] = 0x0f;
\r
115 xI2CMessage.sl_addr7bit = partstSLAVE_ADDRESS;
\r
116 xI2CMessage.tx_data = ucBuffer ;
\r
117 xI2CMessage.tx_length = sizeof( ucBuffer );
\r
118 xI2CMessage.rx_data = NULL;
\r
119 xI2CMessage.rx_length = 0;
\r
120 xI2CMessage.retransmissions_max = 3;
\r
121 I2C_MasterTransferData( LPC_I2C0, &xI2CMessage, I2C_TRANSFER_POLLING );
\r
123 /* Create the mutex used to guard access to the I2C bus. */
\r
124 xI2CCommandQueue = xQueueCreate( partstLED_COMMAND_QUEUE_LENGTH, sizeof( unsigned char ) );
\r
125 configASSERT( xI2CCommandQueue );
\r
127 /* Create the I2C gatekeeper task itself. */
\r
128 xTaskCreate( prvI2CGateKeeperTask, ( signed char * ) "I2C", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
\r
130 /*-----------------------------------------------------------*/
\r
132 void vParTestToggleLED( unsigned long ulLED )
\r
134 unsigned char ucLED = ( unsigned char ) ulLED;
\r
136 /* Only the gatekeeper task will actually access the I2C port, so send the
\r
137 toggle request to the gatekeeper task. A block time of zero is used as
\r
138 this function is called by a software timer callback. */
\r
139 xQueueSend( xI2CCommandQueue, &ucLED, 0UL );
\r
141 /*-----------------------------------------------------------*/
\r
143 static void prvI2CGateKeeperTask( void *pvParameters )
\r
145 unsigned char ucBuffer[ 2 ], ucLED;
\r
146 static unsigned char ucLEDState = 0xffU;
\r
147 static I2C_M_SETUP_Type xI2CMessage; /* Static so it is not on the stack as this is called from task code. */
\r
149 /* Just to remove compiler warnings. */
\r
150 ( void ) pvParameters;
\r
154 /* Wait for the next command. */
\r
155 xQueueReceive( xI2CCommandQueue, &ucLED, portMAX_DELAY );
\r
157 /* Only this task is allowed to touch the I2C port, so there is no need
\r
158 for additional mutual exclusion. */
\r
159 if( ucLED < partstMAX_LEDS )
\r
161 /* Which bit is being manipulated? */
\r
162 ucLED = 0x01 << ucLED;
\r
164 /* Is the bit currently set or clear? */
\r
165 if( ( ucLEDState & ucLED ) == 0U )
\r
167 ucLEDState |= ucLED;
\r
171 ucLEDState &= ~ucLED;
\r
174 ucBuffer[ 0 ] = partstIO_WRITE_COMMAND;
\r
175 ucBuffer[ 1 ] = ucLEDState;
\r
177 xI2CMessage.sl_addr7bit = partstSLAVE_ADDRESS;
\r
178 xI2CMessage.tx_data = ucBuffer ;
\r
179 xI2CMessage.tx_length = sizeof( ucBuffer );
\r
180 xI2CMessage.rx_data = NULL;
\r
181 xI2CMessage.rx_length = 0;
\r
182 xI2CMessage.retransmissions_max = 3;
\r
183 I2C_MasterTransferData( LPC_I2C0, &xI2CMessage, I2C_TRANSFER_POLLING );
\r
187 /*-----------------------------------------------------------*/
\r