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