]> begriffs open source - cmsis-freertos/blob - Demo/MB96350_Softune_Dice_Kit/main.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / MB96350_Softune_Dice_Kit / main.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70
71 /*****
72  *
73  * See http://www.freertos.org/Documentation/FreeRTOS-documentation-and-book.html
74  * for an introductory guide to using real time kernels, and FreeRTOS in
75  * particular.
76  *
77  *****
78  *
79  * The DICE-KIT-16FX has two 7 segment displays and two buttons that can
80  * generate interrupts.  This example uses this IO as follows:
81  *
82  *
83  * - Left 7 segment display -
84  *
85  * 7 'flash' tasks are created, each of which toggles a single segment of the
86  * left display.  Each task executes at a fixed frequency, with a different
87  * frequency being used by each task.
88  *
89  * When button SW2 is pressed an interrupt is generated that wakes up a 'dice'
90  * task.  The dice task suspends the 7 tasks that are accessing the left display
91  * before simulating a dice being thrown by generating a random number between
92  * 1 and 6.  After the number has been generated the task sleeps for 5 seconds,
93  * if SW2 is pressed again within the 5 seconds another random number is
94  * generated, if SW2 is not pressed within the 5 seconds then the 7 tasks are
95  * un-suspended and will once again toggle the segments of the left hand display.
96  *
97  *
98  * - Right 7 segment display -
99  *
100  * Control of the right side 7 segment display is very similar to that of the
101  * left, except co-routines are used to toggle the segments instead of tasks,
102  * and button SW3 is used instead of SW2.
103  *
104  *
105  * - Notes -
106  *
107  * Only one dice task is actually defined.  Two instances of this single
108  * definition are created, the first to simulate a dice being thrown on the left
109  * display, and the other to simulate a dice being thrown on the right display.
110  * The task parameter is used to let the dice tasks know which display to
111  * control.
112  *
113  * Both dice tasks and the flash tasks operate completely independently under
114  * the control of FreeRTOS.  11 tasks and 7 co-routines are created in total,
115  * including the idle task.
116  *
117  * The co-routines all execute within a single low priority task.
118  *
119  *
120  *
121  * When this demo is executing as expected:
122  *
123  * + Every segment of both displays will toggle at a fixed frequency - with each
124  *   segment using a different frequency.
125  * + When a button is pushed the segment toggling will temporarily stop and the
126  *   dice 'throw' will start whereby the display will show a fast changing random
127  *   number for a few seconds before the dice value is chosen and displayed.
128  * + If the button is not pushed again within five seconds of the dice value being
129  *   displayed the segment toggling will commence again.
130  *
131  *****/
132
133 /* Kernel includes. */
134 #include "FreeRTOS.h"
135 #include "task.h"
136
137 /* Demo includes. */
138 #include "DiceTask.h"
139 #include "ParTest.h"
140 #include "Flash.h"
141
142 /* The priority at which the dice task execute. */
143 #define mainDICE_PRIORITY                       ( tskIDLE_PRIORITY + 2 )
144
145 /*
146  * Sets up the MCU IO for the 7 segment displays and the button inputs.
147  */
148 static void prvSetupHardware( void );
149
150 /*
151  * The function that creates the flash tasks and co-routines (the tasks and
152  * co-routines that toggle the 7 segment display segments.
153  */
154 extern vCreateFlashTasksAndCoRoutines( void );
155
156 /*-----------------------------------------------------------*/
157
158 void main( void )
159 {
160         /* Setup the MCU IO. */
161         prvSetupHardware();
162
163         /* Create the tasks and co-routines that toggle the display segments. */
164         vCreateFlashTasksAndCoRoutines();
165
166         /* Create a 'dice' task to control the left hand display. */
167         xTaskCreate( vDiceTask, "Dice1", configMINIMAL_STACK_SIZE, ( void * ) configLEFT_DISPLAY, mainDICE_PRIORITY, NULL );
168
169         /* Create a 'dice' task to control the right hand display. */
170         xTaskCreate( vDiceTask, "Dice2", configMINIMAL_STACK_SIZE, ( void * ) configRIGHT_DISPLAY, mainDICE_PRIORITY, NULL );
171
172         /* Start the scheduler running. */
173         vTaskStartScheduler();
174
175         /* If this loop is executed then there was insufficient heap memory for the
176         idle task to be created - causing vTaskStartScheduler() to return. */
177         while( 1 );
178 }
179 /*-----------------------------------------------------------*/
180
181 static void prvSetupHardware( void )
182 {
183         /* Setup interrupt hardware - interrupts are kept disabled for now to
184         prevent any interrupts attempting to cause a context switch before the
185         scheduler has been started. */
186         InitIrqLevels();
187         portDISABLE_INTERRUPTS();
188         __set_il( 7 );
189
190         /* Set Port3 as output (7Segment Display). */
191         DDR03  = 0xff;
192
193         /* Use Port 5 as I/O-Port. */
194         ADER1  = 0;
195         PDR05  = 0x7f;
196
197         /* Set Port5 as output (7Segment Display). */
198         DDR05  = 0xff;
199
200         /* Disable inputs on the following ports. */
201         PIER02 = 0x00;
202         PDR02  = 0x00;
203         DDR02  = 0xff;
204         PIER03 = 0x00;
205         PDR03  = 0xff;
206         PIER05 = 0x00;
207         PDR05  = 0x00;
208         PIER06 = 0x00;
209         PDR06  = 0x00;
210         DDR06  = 0xff;
211
212         /* Enable P00_0/INT8 and P00_1/INT9 as input. */
213         PIER00 = 0x03;
214
215         /* Enable external interrupt 8. */
216         PIER00_IE0 = 1;
217
218         /* LB0, LA0 = 11 -> falling edge. */
219         ELVRL1_LB8 = 1;
220         ELVRL1_LA8 = 1;
221
222         /* Reset and enable the interrupt request. */
223         EIRR1_ER8 = 0;
224         ENIR1_EN8 = 1;
225
226         /* Enable external interrupt 9. */
227         PIER00_IE1 = 1;
228
229         /* LB1, LA1 = 11 -> falling edge. */
230         ELVRL1_LB9 = 1;
231         ELVRL1_LA9 = 1;
232
233         /* Reset and enable the interrupt request. */
234         EIRR1_ER9 = 0;
235         ENIR1_EN9 = 1;
236 }
237