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