2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /*-----------------------------------------------------------
\r
30 * Normally, a demo application would define ParTest (parallel port test)
\r
31 * functions to write to an LED. In this case, four '*' symbols that are
\r
32 * output to the debug printf() port are used to simulate LED outputs.
\r
33 *-----------------------------------------------------------*/
\r
35 /* Standard includes. */
\r
39 /* Library includes. */
\r
40 #include "lpc43xx_i2c.h"
\r
42 /* Kernel includes. */
\r
43 #include "FreeRTOS.h"
\r
47 /* Standard demo include. */
\r
48 #include "partest.h"
\r
50 /* The number of LED outputs. */
\r
51 #define partstMAX_LEDS 4
\r
53 /* Commands written to the PCA9502. */
\r
54 #define partstIO_WRITE_COMMAND ( ( unsigned char ) ( 0x0BU << 3U ) )
\r
55 #define partstIO_DIR_COMMAND ( ( unsigned char ) ( 0x0AU << 3U ) )
\r
56 #define partstSLAVE_ADDRESS ( ( unsigned char ) ( 0x9AU >> 1U ) )
\r
58 /* Just defines the length of the queue used to pass toggle commands to the I2C
\r
60 #define partstLED_COMMAND_QUEUE_LENGTH ( 6 )
\r
61 /*-----------------------------------------------------------*/
\r
64 * The LEDs are connected to an I2C port expander. Therefore, writing to an
\r
65 * LED takes longer than might be expected if the LED was connected directly
\r
66 * to a GPIO pin. As several tasks, and a timer, toggle LEDs, it is convenient
\r
67 * to use a gatekeeper task to ensure access is both mutually exclusive and
\r
68 * serialised. Tasks other than this gatekeeper task must not access the I2C
\r
71 static void prvI2CGateKeeperTask( void *pvParameters );
\r
73 /* The queue used to communicate toggle commands with the I2C gatekeeper
\r
75 static QueueHandle_t xI2CCommandQueue = NULL;
\r
76 /*-----------------------------------------------------------*/
\r
78 void vParTestInitialise( void )
\r
80 unsigned char ucBuffer[ 2 ];
\r
81 I2C_M_SETUP_Type xI2CMessage;
\r
83 /* The LEDs are on an I2C IO expander. Initialise the I2C interface. */
\r
84 I2C_Init( LPC_I2C0, 300000 );
\r
85 I2C_Cmd( LPC_I2C0, ENABLE );
\r
87 /* GPIO0-GPIO2 to output. */
\r
88 ucBuffer[ 0 ] = partstIO_DIR_COMMAND;
\r
89 ucBuffer[ 1 ] = 0x0f;
\r
90 xI2CMessage.sl_addr7bit = partstSLAVE_ADDRESS;
\r
91 xI2CMessage.tx_data = ucBuffer ;
\r
92 xI2CMessage.tx_length = sizeof( ucBuffer );
\r
93 xI2CMessage.rx_data = NULL;
\r
94 xI2CMessage.rx_length = 0;
\r
95 xI2CMessage.retransmissions_max = 3;
\r
96 I2C_MasterTransferData( LPC_I2C0, &xI2CMessage, I2C_TRANSFER_POLLING );
\r
98 /* Create the mutex used to guard access to the I2C bus. */
\r
99 xI2CCommandQueue = xQueueCreate( partstLED_COMMAND_QUEUE_LENGTH, sizeof( unsigned char ) );
\r
100 configASSERT( xI2CCommandQueue );
\r
102 /* Create the I2C gatekeeper task itself. */
\r
103 xTaskCreate( prvI2CGateKeeperTask, "I2C", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
\r
105 /*-----------------------------------------------------------*/
\r
107 void vParTestToggleLED( unsigned long ulLED )
\r
109 unsigned char ucLED = ( unsigned char ) ulLED;
\r
111 /* Only the gatekeeper task will actually access the I2C port, so send the
\r
112 toggle request to the gatekeeper task. A block time of zero is used as
\r
113 this function is called by a software timer callback. */
\r
114 xQueueSend( xI2CCommandQueue, &ucLED, 0UL );
\r
116 /*-----------------------------------------------------------*/
\r
118 static void prvI2CGateKeeperTask( void *pvParameters )
\r
120 unsigned char ucBuffer[ 2 ], ucLED;
\r
121 static unsigned char ucLEDState = 0xffU;
\r
122 static I2C_M_SETUP_Type xI2CMessage; /* Static so it is not on the stack as this is called from task code. */
\r
124 /* Just to remove compiler warnings. */
\r
125 ( void ) pvParameters;
\r
129 /* Wait for the next command. */
\r
130 xQueueReceive( xI2CCommandQueue, &ucLED, portMAX_DELAY );
\r
132 /* Only this task is allowed to touch the I2C port, so there is no need
\r
133 for additional mutual exclusion. */
\r
134 if( ucLED < partstMAX_LEDS )
\r
136 /* Which bit is being manipulated? */
\r
137 ucLED = 0x01 << ucLED;
\r
139 /* Is the bit currently set or clear? */
\r
140 if( ( ucLEDState & ucLED ) == 0U )
\r
142 ucLEDState |= ucLED;
\r
146 ucLEDState &= ~ucLED;
\r
149 ucBuffer[ 0 ] = partstIO_WRITE_COMMAND;
\r
150 ucBuffer[ 1 ] = ucLEDState;
\r
152 xI2CMessage.sl_addr7bit = partstSLAVE_ADDRESS;
\r
153 xI2CMessage.tx_data = ucBuffer ;
\r
154 xI2CMessage.tx_length = sizeof( ucBuffer );
\r
155 xI2CMessage.rx_data = NULL;
\r
156 xI2CMessage.rx_length = 0;
\r
157 xI2CMessage.retransmissions_max = 3;
\r
158 I2C_MasterTransferData( LPC_I2C0, &xI2CMessage, I2C_TRANSFER_POLLING );
\r
162 /*-----------------------------------------------------------*/
\r