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