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