]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_M0_LPC43xx_Keil/M4/ParTest.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / CORTEX_M4F_M0_LPC43xx_Keil / M4 / ParTest.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \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
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\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
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \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
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*-----------------------------------------------------------\r
70  * Normally, a demo application would define ParTest (parallel port test) \r
71  * functions to write to an LED.  In this case, four '*' symbols that are\r
72  * output to the debug printf() port are used to simulate LED outputs.\r
73  *-----------------------------------------------------------*/\r
74 \r
75 /* Standard includes. */\r
76 #include <stdio.h>\r
77 #include <string.h>\r
78 \r
79 /* Library includes. */\r
80 #include "lpc43xx_i2c.h"\r
81 \r
82 /* Kernel includes. */\r
83 #include "FreeRTOS.h"\r
84 #include "task.h"\r
85 #include "queue.h"\r
86 \r
87 /* Standard demo include. */\r
88 #include "partest.h"\r
89 \r
90 /* The number of LED outputs. */\r
91 #define partstMAX_LEDS 4\r
92 \r
93 /* Commands written to the PCA9502. */\r
94 #define partstIO_WRITE_COMMAND  ( ( unsigned char ) ( 0x0BU << 3U ) )\r
95 #define partstIO_DIR_COMMAND    ( ( unsigned char ) ( 0x0AU << 3U ) )\r
96 #define partstSLAVE_ADDRESS             ( ( unsigned char ) ( 0x9AU >> 1U ) )\r
97 \r
98 /* Just defines the length of the queue used to pass toggle commands to the I2C\r
99 gatekeeper task. */\r
100 #define partstLED_COMMAND_QUEUE_LENGTH  ( 6 )\r
101 /*-----------------------------------------------------------*/\r
102 \r
103 /*\r
104  * The LEDs are connected to an I2C port expander.  Therefore, writing to an\r
105  * LED takes longer than might be expected if the LED was connected directly\r
106  * to a GPIO pin.  As several tasks, and a timer, toggle LEDs, it is convenient\r
107  * to use a gatekeeper task to ensure access is both mutually exclusive and\r
108  * serialised.  Tasks other than this gatekeeper task must not access the I2C\r
109  * port directly.\r
110  */\r
111 static void prvI2CGateKeeperTask( void *pvParameters );\r
112 \r
113 /* The queue used to communicate toggle commands with the I2C gatekeeper \r
114 task. */\r
115 static xQueueHandle xI2CCommandQueue = NULL;\r
116 /*-----------------------------------------------------------*/\r
117 \r
118 void vParTestInitialise( void )\r
119 {\r
120 unsigned char ucBuffer[ 2 ];\r
121 I2C_M_SETUP_Type xI2CMessage;\r
122 \r
123         /* The LEDs are on an I2C IO expander.  Initialise the I2C interface. */\r
124         I2C_Init( LPC_I2C0, 300000 );\r
125         I2C_Cmd( LPC_I2C0, ENABLE );\r
126 \r
127         /* GPIO0-GPIO2 to output. */\r
128         ucBuffer[ 0 ] = partstIO_DIR_COMMAND;\r
129         ucBuffer[ 1 ] = 0x0f;\r
130         xI2CMessage.sl_addr7bit = partstSLAVE_ADDRESS;\r
131         xI2CMessage.tx_data = ucBuffer ;\r
132         xI2CMessage.tx_length = sizeof( ucBuffer );\r
133         xI2CMessage.rx_data = NULL;\r
134         xI2CMessage.rx_length = 0;\r
135         xI2CMessage.retransmissions_max = 3;\r
136         I2C_MasterTransferData( LPC_I2C0, &xI2CMessage, I2C_TRANSFER_POLLING );\r
137 \r
138         /* Create the mutex used to guard access to the I2C bus. */\r
139         xI2CCommandQueue = xQueueCreate( partstLED_COMMAND_QUEUE_LENGTH, sizeof( unsigned char ) );\r
140         configASSERT( xI2CCommandQueue );\r
141 \r
142         /* Create the I2C gatekeeper task itself. */\r
143         xTaskCreate( prvI2CGateKeeperTask, ( signed char * ) "I2C", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 void vParTestToggleLED( unsigned long ulLED )\r
148 {\r
149 unsigned char ucLED = ( unsigned char ) ulLED;\r
150 \r
151         /* Only the gatekeeper task will actually access the I2C port, so send the\r
152         toggle request to the gatekeeper task.  A block time of zero is used as\r
153         this function is called by a software timer callback. */\r
154         xQueueSend( xI2CCommandQueue, &ucLED, 0UL );\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 static void prvI2CGateKeeperTask( void *pvParameters )\r
159 {\r
160 unsigned char ucBuffer[ 2 ], ucLED;\r
161 static unsigned char ucLEDState = 0xffU;\r
162 static I2C_M_SETUP_Type xI2CMessage; /* Static so it is not on the stack as this is called from task code. */\r
163 \r
164         /* Just to remove compiler warnings. */\r
165         ( void ) pvParameters;\r
166 \r
167         for( ;; )\r
168         {\r
169                 /* Wait for the next command. */\r
170                 xQueueReceive( xI2CCommandQueue, &ucLED, portMAX_DELAY );\r
171 \r
172                 /* Only this task is allowed to touch the I2C port, so there is no need\r
173                 for additional mutual exclusion. */\r
174                 if( ucLED < partstMAX_LEDS )\r
175                 {\r
176                         /* Which bit is being manipulated? */\r
177                         ucLED = 0x01 << ucLED;\r
178         \r
179                         /* Is the bit currently set or clear? */\r
180                         if( ( ucLEDState & ucLED ) == 0U )\r
181                         {\r
182                                 ucLEDState |= ucLED;\r
183                         }\r
184                         else\r
185                         {\r
186                                 ucLEDState &= ~ucLED;\r
187                         }\r
188         \r
189                         ucBuffer[ 0 ] = partstIO_WRITE_COMMAND;\r
190                         ucBuffer[ 1 ] = ucLEDState;\r
191         \r
192                         xI2CMessage.sl_addr7bit = partstSLAVE_ADDRESS;\r
193                         xI2CMessage.tx_data = ucBuffer ;\r
194                         xI2CMessage.tx_length = sizeof( ucBuffer );\r
195                         xI2CMessage.rx_data = NULL;\r
196                         xI2CMessage.rx_length = 0;\r
197                         xI2CMessage.retransmissions_max = 3;\r
198                         I2C_MasterTransferData( LPC_I2C0, &xI2CMessage, I2C_TRANSFER_POLLING );\r
199                 }\r
200         }\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r