]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_M4F_M0_LPC43xx_Keil/M4/ParTest.c
Added fast book files to project - although fast boot is not integrated yet.
[freertos] / Demo / CORTEX_M4F_M0_LPC43xx_Keil / M4 / ParTest.c
1 /*\r
2     FreeRTOS V7.0.2 - Copyright (C) 2011 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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*-----------------------------------------------------------\r
55  * Normally, a demo application would define ParTest (parallel port test) \r
56  * functions to write to an LED.  In this case, four '*' symbols that are\r
57  * output to the debug printf() port are used to simulate LED outputs.\r
58  *-----------------------------------------------------------*/\r
59 \r
60 /* Standard includes. */\r
61 #include <stdio.h>\r
62 #include <string.h>\r
63 \r
64 /* Library includes. */\r
65 #include "lpc43xx_i2c.h"\r
66 \r
67 /* Kernel includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 #include "queue.h"\r
71 \r
72 /* Standard demo include. */\r
73 #include "partest.h"\r
74 \r
75 /* The number of LED outputs. */\r
76 #define partstMAX_LEDS 4\r
77 \r
78 /* Commands written to the PCA9502. */\r
79 #define partstIO_WRITE_COMMAND  ( ( unsigned char ) ( 0x0BU << 3U ) )\r
80 #define partstIO_DIR_COMMAND    ( ( unsigned char ) ( 0x0AU << 3U ) )\r
81 #define partstSLAVE_ADDRESS             ( ( unsigned char ) ( 0x9AU >> 1U ) )\r
82 \r
83 /* Just defines the length of the queue used to pass toggle commands to the I2C\r
84 gatekeeper task. */\r
85 #define partstLED_COMMAND_QUEUE_LENGTH  ( 6 )\r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /*\r
89  * The LEDs are connected to an I2C port expander.  Therefore, writing to an\r
90  * LED takes longer than might be expected if the LED was connected directly\r
91  * to a GPIO pin.  As several tasks, and a timer, toggle LEDs, it is convenient\r
92  * to use a gatekeeper task to ensure access is both mutually exclusive and\r
93  * serialised.  Tasks other than this gatekeeper task must not access the I2C\r
94  * port directly.\r
95  */\r
96 static void prvI2CGateKeeperTask( void *pvParameters );\r
97 \r
98 /* The queue used to communicate toggle commands with the I2C gatekeeper \r
99 task. */\r
100 static xQueueHandle xI2CCommandQueue = NULL;\r
101 /*-----------------------------------------------------------*/\r
102 \r
103 void vParTestInitialise( void )\r
104 {\r
105 unsigned char ucBuffer[ 2 ];\r
106 I2C_M_SETUP_Type xI2CMessage;\r
107 \r
108         /* The LEDs are on an I2C IO expander.  Initialise the I2C interface. */\r
109         I2C_Init( LPC_I2C0, 300000 );\r
110         I2C_Cmd( LPC_I2C0, ENABLE );\r
111 \r
112         /* GPIO0-GPIO2 to output. */\r
113         ucBuffer[ 0 ] = partstIO_DIR_COMMAND;\r
114         ucBuffer[ 1 ] = 0x0f;\r
115         xI2CMessage.sl_addr7bit = partstSLAVE_ADDRESS;\r
116         xI2CMessage.tx_data = ucBuffer ;\r
117         xI2CMessage.tx_length = sizeof( ucBuffer );\r
118         xI2CMessage.rx_data = NULL;\r
119         xI2CMessage.rx_length = 0;\r
120         xI2CMessage.retransmissions_max = 3;\r
121         I2C_MasterTransferData( LPC_I2C0, &xI2CMessage, I2C_TRANSFER_POLLING );\r
122 \r
123         /* Create the mutex used to guard access to the I2C bus. */\r
124         xI2CCommandQueue = xQueueCreate( partstLED_COMMAND_QUEUE_LENGTH, sizeof( unsigned char ) );\r
125         configASSERT( xI2CCommandQueue );\r
126 \r
127         /* Create the I2C gatekeeper task itself. */\r
128         xTaskCreate( prvI2CGateKeeperTask, ( signed char * ) "I2C", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
129 }\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 void vParTestToggleLED( unsigned long ulLED )\r
133 {\r
134 unsigned char ucLED = ( unsigned char ) ulLED;\r
135 \r
136         /* Only the gatekeeper task will actually access the I2C port, so send the\r
137         toggle request to the gatekeeper task.  A block time of zero is used as\r
138         this function is called by a software timer callback. */\r
139         xQueueSend( xI2CCommandQueue, &ucLED, 0UL );\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 static void prvI2CGateKeeperTask( void *pvParameters )\r
144 {\r
145 unsigned char ucBuffer[ 2 ], ucLED;\r
146 static unsigned char ucLEDState = 0xffU;\r
147 static I2C_M_SETUP_Type xI2CMessage; /* Static so it is not on the stack as this is called from task code. */\r
148 \r
149         /* Just to remove compiler warnings. */\r
150         ( void ) pvParameters;\r
151 \r
152         for( ;; )\r
153         {\r
154                 /* Wait for the next command. */\r
155                 xQueueReceive( xI2CCommandQueue, &ucLED, portMAX_DELAY );\r
156 \r
157                 /* Only this task is allowed to touch the I2C port, so there is no need\r
158                 for additional mutual exclusion. */\r
159                 if( ucLED < partstMAX_LEDS )\r
160                 {\r
161                         /* Which bit is being manipulated? */\r
162                         ucLED = 0x01 << ucLED;\r
163         \r
164                         /* Is the bit currently set or clear? */\r
165                         if( ( ucLEDState & ucLED ) == 0U )\r
166                         {\r
167                                 ucLEDState |= ucLED;\r
168                         }\r
169                         else\r
170                         {\r
171                                 ucLEDState &= ~ucLED;\r
172                         }\r
173         \r
174                         ucBuffer[ 0 ] = partstIO_WRITE_COMMAND;\r
175                         ucBuffer[ 1 ] = ucLEDState;\r
176         \r
177                         xI2CMessage.sl_addr7bit = partstSLAVE_ADDRESS;\r
178                         xI2CMessage.tx_data = ucBuffer ;\r
179                         xI2CMessage.tx_length = sizeof( ucBuffer );\r
180                         xI2CMessage.rx_data = NULL;\r
181                         xI2CMessage.rx_length = 0;\r
182                         xI2CMessage.retransmissions_max = 3;\r
183                         I2C_MasterTransferData( LPC_I2C0, &xI2CMessage, I2C_TRANSFER_POLLING );\r
184                 }\r
185         }\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r