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