]> begriffs open source - cmsis-freertos/blob - Demo/MB96350_Softune_Dice_Kit/main.c
Set error state if no delay or already expired
[cmsis-freertos] / Demo / MB96350_Softune_Dice_Kit / main.c
1 /*
2  * FreeRTOS Kernel V10.1.1
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28
29 /*****
30  *
31  * See http://www.freertos.org/Documentation/FreeRTOS-documentation-and-book.html
32  * for an introductory guide to using real time kernels, and FreeRTOS in
33  * particular.
34  *
35  *****
36  *
37  * The DICE-KIT-16FX has two 7 segment displays and two buttons that can
38  * generate interrupts.  This example uses this IO as follows:
39  *
40  *
41  * - Left 7 segment display -
42  *
43  * 7 'flash' tasks are created, each of which toggles a single segment of the
44  * left display.  Each task executes at a fixed frequency, with a different
45  * frequency being used by each task.
46  *
47  * When button SW2 is pressed an interrupt is generated that wakes up a 'dice'
48  * task.  The dice task suspends the 7 tasks that are accessing the left display
49  * before simulating a dice being thrown by generating a random number between
50  * 1 and 6.  After the number has been generated the task sleeps for 5 seconds,
51  * if SW2 is pressed again within the 5 seconds another random number is
52  * generated, if SW2 is not pressed within the 5 seconds then the 7 tasks are
53  * un-suspended and will once again toggle the segments of the left hand display.
54  *
55  *
56  * - Right 7 segment display -
57  *
58  * Control of the right side 7 segment display is very similar to that of the
59  * left, except co-routines are used to toggle the segments instead of tasks,
60  * and button SW3 is used instead of SW2.
61  *
62  *
63  * - Notes -
64  *
65  * Only one dice task is actually defined.  Two instances of this single
66  * definition are created, the first to simulate a dice being thrown on the left
67  * display, and the other to simulate a dice being thrown on the right display.
68  * The task parameter is used to let the dice tasks know which display to
69  * control.
70  *
71  * Both dice tasks and the flash tasks operate completely independently under
72  * the control of FreeRTOS.  11 tasks and 7 co-routines are created in total,
73  * including the idle task.
74  *
75  * The co-routines all execute within a single low priority task.
76  *
77  *
78  *
79  * When this demo is executing as expected:
80  *
81  * + Every segment of both displays will toggle at a fixed frequency - with each
82  *   segment using a different frequency.
83  * + When a button is pushed the segment toggling will temporarily stop and the
84  *   dice 'throw' will start whereby the display will show a fast changing random
85  *   number for a few seconds before the dice value is chosen and displayed.
86  * + If the button is not pushed again within five seconds of the dice value being
87  *   displayed the segment toggling will commence again.
88  *
89  *****/
90
91 /* Kernel includes. */
92 #include "FreeRTOS.h"
93 #include "task.h"
94
95 /* Demo includes. */
96 #include "DiceTask.h"
97 #include "ParTest.h"
98 #include "Flash.h"
99
100 /* The priority at which the dice task execute. */
101 #define mainDICE_PRIORITY                       ( tskIDLE_PRIORITY + 2 )
102
103 /*
104  * Sets up the MCU IO for the 7 segment displays and the button inputs.
105  */
106 static void prvSetupHardware( void );
107
108 /*
109  * The function that creates the flash tasks and co-routines (the tasks and
110  * co-routines that toggle the 7 segment display segments.
111  */
112 extern vCreateFlashTasksAndCoRoutines( void );
113
114 /*-----------------------------------------------------------*/
115
116 void main( void )
117 {
118         /* Setup the MCU IO. */
119         prvSetupHardware();
120
121         /* Create the tasks and co-routines that toggle the display segments. */
122         vCreateFlashTasksAndCoRoutines();
123
124         /* Create a 'dice' task to control the left hand display. */
125         xTaskCreate( vDiceTask, "Dice1", configMINIMAL_STACK_SIZE, ( void * ) configLEFT_DISPLAY, mainDICE_PRIORITY, NULL );
126
127         /* Create a 'dice' task to control the right hand display. */
128         xTaskCreate( vDiceTask, "Dice2", configMINIMAL_STACK_SIZE, ( void * ) configRIGHT_DISPLAY, mainDICE_PRIORITY, NULL );
129
130         /* Start the scheduler running. */
131         vTaskStartScheduler();
132
133         /* If this loop is executed then there was insufficient heap memory for the
134         idle task to be created - causing vTaskStartScheduler() to return. */
135         while( 1 );
136 }
137 /*-----------------------------------------------------------*/
138
139 static void prvSetupHardware( void )
140 {
141         /* Setup interrupt hardware - interrupts are kept disabled for now to
142         prevent any interrupts attempting to cause a context switch before the
143         scheduler has been started. */
144         InitIrqLevels();
145         portDISABLE_INTERRUPTS();
146         __set_il( 7 );
147
148         /* Set Port3 as output (7Segment Display). */
149         DDR03  = 0xff;
150
151         /* Use Port 5 as I/O-Port. */
152         ADER1  = 0;
153         PDR05  = 0x7f;
154
155         /* Set Port5 as output (7Segment Display). */
156         DDR05  = 0xff;
157
158         /* Disable inputs on the following ports. */
159         PIER02 = 0x00;
160         PDR02  = 0x00;
161         DDR02  = 0xff;
162         PIER03 = 0x00;
163         PDR03  = 0xff;
164         PIER05 = 0x00;
165         PDR05  = 0x00;
166         PIER06 = 0x00;
167         PDR06  = 0x00;
168         DDR06  = 0xff;
169
170         /* Enable P00_0/INT8 and P00_1/INT9 as input. */
171         PIER00 = 0x03;
172
173         /* Enable external interrupt 8. */
174         PIER00_IE0 = 1;
175
176         /* LB0, LA0 = 11 -> falling edge. */
177         ELVRL1_LB8 = 1;
178         ELVRL1_LA8 = 1;
179
180         /* Reset and enable the interrupt request. */
181         EIRR1_ER8 = 0;
182         ENIR1_EN8 = 1;
183
184         /* Enable external interrupt 9. */
185         PIER00_IE1 = 1;
186
187         /* LB1, LA1 = 11 -> falling edge. */
188         ELVRL1_LB9 = 1;
189         ELVRL1_LA9 = 1;
190
191         /* Reset and enable the interrupt request. */
192         EIRR1_ER9 = 0;
193         ENIR1_EN9 = 1;
194 }
195