]> git.sur5r.net Git - freertos/blob - Demo/MB96350_Softune_Dice_Kit/main.c
027b62166012b699542c3e2a5400432a1f76d5fc
[freertos] / Demo / MB96350_Softune_Dice_Kit / main.c
1 /*\r
2     FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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 /*****\r
56  *\r
57  * See http://www.freertos.org/Documentation/FreeRTOS-documentation-and-book.html\r
58  * for an introductory guide to using real time kernels, and FreeRTOS in \r
59  * particular. \r
60  *\r
61  *****\r
62  *  \r
63  * The DICE-KIT-16FX has two 7 segment displays and two buttons that can\r
64  * generate interrupts.  This example uses this IO as follows:\r
65  *\r
66  *\r
67  * - Left 7 segment display - \r
68  *\r
69  * 7 'flash' tasks are created, each of which toggles a single segment of the \r
70  * left display.  Each task executes at a fixed frequency, with a different \r
71  * frequency being used by each task.\r
72  *\r
73  * When button SW2 is pressed an interrupt is generated that wakes up a 'dice'\r
74  * task.  The dice task suspends the 7 tasks that are accessing the left display\r
75  * before simulating a dice being thrown by generating a random number between\r
76  * 1 and 6.  After the number has been generated the task sleeps for 5 seconds,\r
77  * if SW2 is pressed again within the 5 seconds another random number is \r
78  * generated, if SW2 is not pressed within the 5 seconds then the 7 tasks are\r
79  * un-suspended and will once again toggle the segments of the left hand display.\r
80  *\r
81  *\r
82  * - Right 7 segment display -\r
83  *\r
84  * Control of the right side 7 segment display is very similar to that of the\r
85  * left, except co-routines are used to toggle the segments instead of tasks,\r
86  * and button SW3 is used instead of SW2.\r
87  *\r
88  *\r
89  * - Notes -\r
90  *\r
91  * Only one dice task is actually defined.  Two instances of this single\r
92  * definition are created, the first to simulate a dice being thrown on the left\r
93  * display, and the other to simulate a dice being thrown on the right display.\r
94  * The task parameter is used to let the dice tasks know which display to \r
95  * control.\r
96  *\r
97  * Both dice tasks and the flash tasks operate completely independently under\r
98  * the control of FreeRTOS.  11 tasks and 7 co-routines are created in total,\r
99  * including the idle task. \r
100  *\r
101  * The co-routines all execute within a single low priority task.\r
102  *\r
103  *\r
104  *\r
105  * When this demo is executing as expected:\r
106  *\r
107  * + Every segment of both displays will toggle at a fixed frequency - with each\r
108  *   segment using a different frequency.\r
109  * + When a button is pushed the segment toggling will temporarily stop and the\r
110  *   dice 'throw' will start whereby the display will show a fast changing random\r
111  *   number for a few seconds before the dice value is chosen and displayed.\r
112  * + If the button is not pushed again within five seconds of the dice value being\r
113  *   displayed the segment toggling will commence again.\r
114  *\r
115  *****/\r
116 \r
117 /* Kernel includes. */\r
118 #include "FreeRTOS.h"\r
119 #include "Task.h"\r
120 \r
121 /* Demo includes. */\r
122 #include "DiceTask.h"\r
123 #include "ParTest.h"\r
124 #include "Flash.h"\r
125 \r
126 /* The priority at which the dice task execute. */\r
127 #define mainDICE_PRIORITY                       ( tskIDLE_PRIORITY + 2 )\r
128 \r
129 /*\r
130  * Sets up the MCU IO for the 7 segment displays and the button inputs.\r
131  */\r
132 static void prvSetupHardware( void );\r
133 \r
134 /*\r
135  * The function that creates the flash tasks and co-routines (the tasks and\r
136  * co-routines that toggle the 7 segment display segments.\r
137  */\r
138 extern vCreateFlashTasksAndCoRoutines( void );\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void main( void )\r
143 {\r
144         /* Setup the MCU IO. */\r
145         prvSetupHardware();\r
146 \r
147         /* Create the tasks and co-routines that toggle the display segments. */\r
148         vCreateFlashTasksAndCoRoutines();\r
149 \r
150         /* Create a 'dice' task to control the left hand display. */\r
151         xTaskCreate( vDiceTask, ( signed char * ) "Dice1", configMINIMAL_STACK_SIZE, ( void * ) configLEFT_DISPLAY, mainDICE_PRIORITY, NULL );\r
152 \r
153         /* Create a 'dice' task to control the right hand display. */\r
154         xTaskCreate( vDiceTask, ( signed char * ) "Dice2", configMINIMAL_STACK_SIZE, ( void * ) configRIGHT_DISPLAY, mainDICE_PRIORITY, NULL );\r
155 \r
156         /* Start the scheduler running. */\r
157         vTaskStartScheduler();\r
158 \r
159         /* If this loop is executed then there was insufficient heap memory for the\r
160         idle task to be created - causing vTaskStartScheduler() to return. */\r
161         while( 1 );\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 static void prvSetupHardware( void )\r
166 {\r
167         /* Setup interrupt hardware - interrupts are kept disabled for now to\r
168         prevent any interrupts attempting to cause a context switch before the\r
169         scheduler has been started. */\r
170         InitIrqLevels();\r
171         portDISABLE_INTERRUPTS();\r
172         __set_il( 7 );  \r
173 \r
174         /* Set Port3 as output (7Segment Display). */\r
175         DDR03  = 0xff;\r
176 \r
177         /* Use Port 5 as I/O-Port. */\r
178         ADER1  = 0;\r
179         PDR05  = 0x7f;\r
180 \r
181         /* Set Port5 as output (7Segment Display). */\r
182         DDR05  = 0xff;\r
183 \r
184         /* Disable inputs on the following ports. */\r
185         PIER02 = 0x00;\r
186         PDR02  = 0x00;\r
187         DDR02  = 0xff;\r
188         PIER03 = 0x00;\r
189         PDR03  = 0xff;\r
190         PIER05 = 0x00;\r
191         PDR05  = 0x00;\r
192         PIER06 = 0x00;\r
193         PDR06  = 0x00;\r
194         DDR06  = 0xff;\r
195 \r
196         /* Enable P00_0/INT8 and P00_1/INT9 as input. */\r
197         PIER00 = 0x03;\r
198 \r
199         /* Enable external interrupt 8. */\r
200         PIER00_IE0 = 1;\r
201         \r
202         /* LB0, LA0 = 11 -> falling edge. */\r
203         ELVRL1_LB8 = 1;\r
204         ELVRL1_LA8 = 1;\r
205 \r
206         /* Reset and enable the interrupt request. */\r
207         EIRR1_ER8 = 0;\r
208         ENIR1_EN8 = 1;\r
209 \r
210         /* Enable external interrupt 9. */\r
211         PIER00_IE1 = 1;\r
212         \r
213         /* LB1, LA1 = 11 -> falling edge. */\r
214         ELVRL1_LB9 = 1;\r
215         ELVRL1_LA9 = 1;\r
216 \r
217         /* Reset and enable the interrupt request. */\r
218         EIRR1_ER9 = 0;\r
219         ENIR1_EN9 = 1;  \r
220 }\r
221 \r