]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB96350_Softune_Dice_Kit/main.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / MB96350_Softune_Dice_Kit / main.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 /*****\r
69  *\r
70  * See http://www.freertos.org/Documentation/FreeRTOS-documentation-and-book.html\r
71  * for an introductory guide to using real time kernels, and FreeRTOS in \r
72  * particular. \r
73  *\r
74  *****\r
75  *  \r
76  * The DICE-KIT-16FX has two 7 segment displays and two buttons that can\r
77  * generate interrupts.  This example uses this IO as follows:\r
78  *\r
79  *\r
80  * - Left 7 segment display - \r
81  *\r
82  * 7 'flash' tasks are created, each of which toggles a single segment of the \r
83  * left display.  Each task executes at a fixed frequency, with a different \r
84  * frequency being used by each task.\r
85  *\r
86  * When button SW2 is pressed an interrupt is generated that wakes up a 'dice'\r
87  * task.  The dice task suspends the 7 tasks that are accessing the left display\r
88  * before simulating a dice being thrown by generating a random number between\r
89  * 1 and 6.  After the number has been generated the task sleeps for 5 seconds,\r
90  * if SW2 is pressed again within the 5 seconds another random number is \r
91  * generated, if SW2 is not pressed within the 5 seconds then the 7 tasks are\r
92  * un-suspended and will once again toggle the segments of the left hand display.\r
93  *\r
94  *\r
95  * - Right 7 segment display -\r
96  *\r
97  * Control of the right side 7 segment display is very similar to that of the\r
98  * left, except co-routines are used to toggle the segments instead of tasks,\r
99  * and button SW3 is used instead of SW2.\r
100  *\r
101  *\r
102  * - Notes -\r
103  *\r
104  * Only one dice task is actually defined.  Two instances of this single\r
105  * definition are created, the first to simulate a dice being thrown on the left\r
106  * display, and the other to simulate a dice being thrown on the right display.\r
107  * The task parameter is used to let the dice tasks know which display to \r
108  * control.\r
109  *\r
110  * Both dice tasks and the flash tasks operate completely independently under\r
111  * the control of FreeRTOS.  11 tasks and 7 co-routines are created in total,\r
112  * including the idle task. \r
113  *\r
114  * The co-routines all execute within a single low priority task.\r
115  *\r
116  *\r
117  *\r
118  * When this demo is executing as expected:\r
119  *\r
120  * + Every segment of both displays will toggle at a fixed frequency - with each\r
121  *   segment using a different frequency.\r
122  * + When a button is pushed the segment toggling will temporarily stop and the\r
123  *   dice 'throw' will start whereby the display will show a fast changing random\r
124  *   number for a few seconds before the dice value is chosen and displayed.\r
125  * + If the button is not pushed again within five seconds of the dice value being\r
126  *   displayed the segment toggling will commence again.\r
127  *\r
128  *****/\r
129 \r
130 /* Kernel includes. */\r
131 #include "FreeRTOS.h"\r
132 #include "Task.h"\r
133 \r
134 /* Demo includes. */\r
135 #include "DiceTask.h"\r
136 #include "ParTest.h"\r
137 #include "Flash.h"\r
138 \r
139 /* The priority at which the dice task execute. */\r
140 #define mainDICE_PRIORITY                       ( tskIDLE_PRIORITY + 2 )\r
141 \r
142 /*\r
143  * Sets up the MCU IO for the 7 segment displays and the button inputs.\r
144  */\r
145 static void prvSetupHardware( void );\r
146 \r
147 /*\r
148  * The function that creates the flash tasks and co-routines (the tasks and\r
149  * co-routines that toggle the 7 segment display segments.\r
150  */\r
151 extern vCreateFlashTasksAndCoRoutines( void );\r
152 \r
153 /*-----------------------------------------------------------*/\r
154 \r
155 void main( void )\r
156 {\r
157         /* Setup the MCU IO. */\r
158         prvSetupHardware();\r
159 \r
160         /* Create the tasks and co-routines that toggle the display segments. */\r
161         vCreateFlashTasksAndCoRoutines();\r
162 \r
163         /* Create a 'dice' task to control the left hand display. */\r
164         xTaskCreate( vDiceTask, ( signed char * ) "Dice1", configMINIMAL_STACK_SIZE, ( void * ) configLEFT_DISPLAY, mainDICE_PRIORITY, NULL );\r
165 \r
166         /* Create a 'dice' task to control the right hand display. */\r
167         xTaskCreate( vDiceTask, ( signed char * ) "Dice2", configMINIMAL_STACK_SIZE, ( void * ) configRIGHT_DISPLAY, mainDICE_PRIORITY, NULL );\r
168 \r
169         /* Start the scheduler running. */\r
170         vTaskStartScheduler();\r
171 \r
172         /* If this loop is executed then there was insufficient heap memory for the\r
173         idle task to be created - causing vTaskStartScheduler() to return. */\r
174         while( 1 );\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 static void prvSetupHardware( void )\r
179 {\r
180         /* Setup interrupt hardware - interrupts are kept disabled for now to\r
181         prevent any interrupts attempting to cause a context switch before the\r
182         scheduler has been started. */\r
183         InitIrqLevels();\r
184         portDISABLE_INTERRUPTS();\r
185         __set_il( 7 );  \r
186 \r
187         /* Set Port3 as output (7Segment Display). */\r
188         DDR03  = 0xff;\r
189 \r
190         /* Use Port 5 as I/O-Port. */\r
191         ADER1  = 0;\r
192         PDR05  = 0x7f;\r
193 \r
194         /* Set Port5 as output (7Segment Display). */\r
195         DDR05  = 0xff;\r
196 \r
197         /* Disable inputs on the following ports. */\r
198         PIER02 = 0x00;\r
199         PDR02  = 0x00;\r
200         DDR02  = 0xff;\r
201         PIER03 = 0x00;\r
202         PDR03  = 0xff;\r
203         PIER05 = 0x00;\r
204         PDR05  = 0x00;\r
205         PIER06 = 0x00;\r
206         PDR06  = 0x00;\r
207         DDR06  = 0xff;\r
208 \r
209         /* Enable P00_0/INT8 and P00_1/INT9 as input. */\r
210         PIER00 = 0x03;\r
211 \r
212         /* Enable external interrupt 8. */\r
213         PIER00_IE0 = 1;\r
214         \r
215         /* LB0, LA0 = 11 -> falling edge. */\r
216         ELVRL1_LB8 = 1;\r
217         ELVRL1_LA8 = 1;\r
218 \r
219         /* Reset and enable the interrupt request. */\r
220         EIRR1_ER8 = 0;\r
221         ENIR1_EN8 = 1;\r
222 \r
223         /* Enable external interrupt 9. */\r
224         PIER00_IE1 = 1;\r
225         \r
226         /* LB1, LA1 = 11 -> falling edge. */\r
227         ELVRL1_LB9 = 1;\r
228         ELVRL1_LA9 = 1;\r
229 \r
230         /* Reset and enable the interrupt request. */\r
231         EIRR1_ER9 = 0;\r
232         ENIR1_EN9 = 1;  \r
233 }\r
234 \r