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