]> git.sur5r.net Git - freertos/blob - Demo/MB96350_Softune_Dice_Kit/main.c
Ready for V5.2.0 release.
[freertos] / Demo / MB96350_Softune_Dice_Kit / main.c
1 /*\r
2         FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it \r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9 \r
10         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
11         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
12         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for \r
13         more details.\r
14 \r
15         You should have received a copy of the GNU General Public License along \r
16         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 \r
17         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
18 \r
19         A special exception to the GPL is included to allow you to distribute a \r
20         combined work that includes FreeRTOS.org without being obliged to provide\r
21         the source code for any proprietary components.  See the licensing section\r
22         of http://www.FreeRTOS.org for full details.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 \r
53 /*****\r
54  *\r
55  * See http://www.freertos.org/Documentation/FreeRTOS-documentation-and-book.html\r
56  * for an introductory guide to using real time kernels, and FreeRTOS in \r
57  * particular. \r
58  *\r
59  *****\r
60  *  \r
61  * The DICE-KIT-16FX has two 7 segment displays and two buttons that can\r
62  * generate interrupts.  This example uses this IO as follows:\r
63  *\r
64  *\r
65  * - Left 7 segment display - \r
66  *\r
67  * 7 'flash' tasks are created, each of which toggles a single segment of the \r
68  * left display.  Each task executes at a fixed frequency, with a different \r
69  * frequency being used by each task.\r
70  *\r
71  * When button SW2 is pressed an interrupt is generated that wakes up a 'dice'\r
72  * task.  The dice task suspends the 7 tasks that are accessing the left display\r
73  * before simulating a dice being thrown by generating a random number between\r
74  * 1 and 6.  After the number has been generated the task sleeps for 5 seconds,\r
75  * if SW2 is pressed again within the 5 seconds another random number is \r
76  * generated, if SW2 is not pressed within the 5 seconds then the 7 tasks are\r
77  * un-suspended and will once again toggle the segments of the left hand display.\r
78  *\r
79  *\r
80  * - Right 7 segment display -\r
81  *\r
82  * Control of the right side 7 segment display is very similar to that of the\r
83  * left, except co-routines are used to toggle the segments instead of tasks,\r
84  * and button SW3 is used instead of SW2.\r
85  *\r
86  *\r
87  * - Notes -\r
88  *\r
89  * Only one dice task is actually defined.  Two instances of this single\r
90  * definition are created, the first to simulate a dice being thrown on the left\r
91  * display, and the other to simulate a dice being thrown on the right display.\r
92  * The task parameter is used to let the dice tasks know which display to \r
93  * control.\r
94  *\r
95  * Both dice tasks and the flash tasks operate completely independently under\r
96  * the control of FreeRTOS.  11 tasks and 7 co-routines are created in total,\r
97  * including the idle task. \r
98  *\r
99  * The co-routines all execute within a single low priority task.\r
100  *\r
101  *\r
102  *\r
103  * When this demo is executing as expected:\r
104  *\r
105  * + Every segment of both displays will toggle at a fixed frequency - with each\r
106  *   segment using a different frequency.\r
107  * + When a button is pushed the segment toggling will temporarily stop and the\r
108  *   dice 'throw' will start whereby the display will show a fast changing random\r
109  *   number for a few seconds before the dice value is chosen and displayed.\r
110  * + If the button is not pushed again within five seconds of the dice value being\r
111  *   displayed the segment toggling will commence again.\r
112  *\r
113  *****/\r
114 \r
115 /* Kernel includes. */\r
116 #include "FreeRTOS.h"\r
117 #include "Task.h"\r
118 \r
119 /* Demo includes. */\r
120 #include "DiceTask.h"\r
121 #include "ParTest.h"\r
122 #include "Flash.h"\r
123 \r
124 /* The priority at which the dice task execute. */\r
125 #define mainDICE_PRIORITY                       ( tskIDLE_PRIORITY + 2 )\r
126 \r
127 /*\r
128  * Sets up the MCU IO for the 7 segment displays and the button inputs.\r
129  */\r
130 static void prvSetupHardware( void );\r
131 \r
132 /*\r
133  * The function that creates the flash tasks and co-routines (the tasks and\r
134  * co-routines that toggle the 7 segment display segments.\r
135  */\r
136 extern vCreateFlashTasksAndCoRoutines( void );\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 void main( void )\r
141 {\r
142         /* Setup the MCU IO. */\r
143         prvSetupHardware();\r
144 \r
145         /* Create the tasks and co-routines that toggle the display segments. */\r
146         vCreateFlashTasksAndCoRoutines();\r
147 \r
148         /* Create a 'dice' task to control the left hand display. */\r
149         xTaskCreate( vDiceTask, ( signed char * ) "Dice1", configMINIMAL_STACK_SIZE, ( void * ) configLEFT_DISPLAY, mainDICE_PRIORITY, NULL );\r
150 \r
151         /* Create a 'dice' task to control the right hand display. */\r
152         xTaskCreate( vDiceTask, ( signed char * ) "Dice2", configMINIMAL_STACK_SIZE, ( void * ) configRIGHT_DISPLAY, mainDICE_PRIORITY, NULL );\r
153 \r
154         /* Start the scheduler running. */\r
155         vTaskStartScheduler();\r
156 \r
157         /* If this loop is executed then there was insufficient heap memory for the\r
158         idle task to be created - causing vTaskStartScheduler() to return. */\r
159         while( 1 );\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 static void prvSetupHardware( void )\r
164 {\r
165         /* Setup interrupt hardware - interrupts are kept disabled for now to\r
166         prevent any interrupts attempting to cause a context switch before the\r
167         scheduler has been started. */\r
168         InitIrqLevels();\r
169         portDISABLE_INTERRUPTS();\r
170         __set_il( 7 );  \r
171 \r
172         /* Set Port3 as output (7Segment Display). */\r
173         DDR03  = 0xff;\r
174 \r
175         /* Use Port 5 as I/O-Port. */\r
176         ADER1  = 0;\r
177         PDR05  = 0x7f;\r
178 \r
179         /* Set Port5 as output (7Segment Display). */\r
180         DDR05  = 0xff;\r
181 \r
182         /* Disable inputs on the following ports. */\r
183         PIER02 = 0x00;\r
184         PDR02  = 0x00;\r
185         DDR02  = 0xff;\r
186         PIER03 = 0x00;\r
187         PDR03  = 0xff;\r
188         PIER05 = 0x00;\r
189         PDR05  = 0x00;\r
190         PIER06 = 0x00;\r
191         PDR06  = 0x00;\r
192         DDR06  = 0xff;\r
193 \r
194         /* Enable P00_0/INT8 and P00_1/INT9 as input. */\r
195         PIER00 = 0x03;\r
196 \r
197         /* Enable external interrupt 8. */\r
198         PIER00_IE0 = 1;\r
199         \r
200         /* LB0, LA0 = 11 -> falling edge. */\r
201         ELVRL1_LB8 = 1;\r
202         ELVRL1_LA8 = 1;\r
203 \r
204         /* Reset and enable the interrupt request. */\r
205         EIRR1_ER8 = 0;\r
206         ENIR1_EN8 = 1;\r
207 \r
208         /* Enable external interrupt 9. */\r
209         PIER00_IE1 = 1;\r
210         \r
211         /* LB1, LA1 = 11 -> falling edge. */\r
212         ELVRL1_LB9 = 1;\r
213         ELVRL1_LA9 = 1;\r
214 \r
215         /* Reset and enable the interrupt request. */\r
216         EIRR1_ER9 = 0;\r
217         ENIR1_EN9 = 1;  \r
218 }\r
219 \r