]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_WizC/Demo1/main.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / PIC18_WizC / Demo1 / main.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70 Changes from V3.0.0\r
71 \r
72 Changes from V3.0.1\r
73 */\r
74 \r
75 /*\r
76  * Instead of the normal single demo application, the PIC18F demo is split \r
77  * into several smaller programs of which this is the first.  This enables the \r
78  * demo's to be executed on the RAM limited PIC-devices.\r
79  *\r
80  * The Demo1 project is configured for a PIC18F4620 device.  Main.c starts 9 \r
81  * tasks (including the idle task).\r
82 \r
83  * This first demo is included to do a quick check on the FreeRTOS\r
84  * installation. It is also included to demonstrate a minimal project-setup\r
85  * to use FreeRTOS in a wizC environment.\r
86  *\r
87  * Eight independant tasks are created. All tasks share the same taskcode.\r
88  * Each task blinks a different led on portB. The blinkrate for each task\r
89  * is different, but chosen in such a way that portB will show a binary\r
90  * counter pattern. All blinkrates are derived from a single master-rate.\r
91  * By default, this  masterrate is set to 100 milliseconds. Although such\r
92  * a low value will make it almost impossible to see some of the leds\r
93  * actually blink, it is a good value when using the wizC-simulator.\r
94  * When testing on a real chip, changing the value to eg. 500 milliseconds\r
95  * would be appropiate.\r
96  */\r
97  \r
98 /* Scheduler include files. */\r
99 #include <FreeRTOS.h>\r
100 #include <task.h>\r
101 \r
102 #define mainBLINK_LED_INTERVAL  ( ( portTickType ) 100 / ( portTICK_RATE_MS ) )\r
103 \r
104 /* The LED that is flashed by the B0 task. */\r
105 #define mainBLINK_LED0_PORT             LATD\r
106 #define mainBLINK_LED0_TRIS             TRISD\r
107 #define mainBLINK_LED0_PIN              0\r
108 #define mainBLINK_LED0_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED0_PIN))\r
109 \r
110 /* The LED that is flashed by the B1 task. */\r
111 #define mainBLINK_LED1_PORT             LATD\r
112 #define mainBLINK_LED1_TRIS             TRISD\r
113 #define mainBLINK_LED1_PIN              1\r
114 #define mainBLINK_LED1_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED1_PIN))\r
115 \r
116 /* The LED that is flashed by the B2 task. */\r
117 #define mainBLINK_LED2_PORT             LATD\r
118 #define mainBLINK_LED2_TRIS             TRISD\r
119 #define mainBLINK_LED2_PIN              2\r
120 #define mainBLINK_LED2_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED2_PIN))\r
121 \r
122 /* The LED that is flashed by the B3 task. */\r
123 #define mainBLINK_LED3_PORT             LATD\r
124 #define mainBLINK_LED3_TRIS             TRISD\r
125 #define mainBLINK_LED3_PIN              3\r
126 #define mainBLINK_LED3_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED3_PIN))\r
127 \r
128 /* The LED that is flashed by the B4 task. */\r
129 #define mainBLINK_LED4_PORT             LATD\r
130 #define mainBLINK_LED4_TRIS             TRISD\r
131 #define mainBLINK_LED4_PIN              4\r
132 #define mainBLINK_LED4_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED4_PIN))\r
133 \r
134 /* The LED that is flashed by the B5 task. */\r
135 #define mainBLINK_LED5_PORT             LATD\r
136 #define mainBLINK_LED5_TRIS             TRISD\r
137 #define mainBLINK_LED5_PIN              5\r
138 #define mainBLINK_LED5_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED5_PIN))\r
139 \r
140 /* The LED that is flashed by the B6 task. */\r
141 #define mainBLINK_LED6_PORT             LATD\r
142 #define mainBLINK_LED6_TRIS             TRISD\r
143 #define mainBLINK_LED6_PIN              6\r
144 #define mainBLINK_LED6_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED6_PIN))\r
145 \r
146 /* The LED that is flashed by the B7 task. */\r
147 #define mainBLINK_LED7_PORT             LATD\r
148 #define mainBLINK_LED7_TRIS             TRISD\r
149 #define mainBLINK_LED7_PIN              7\r
150 #define mainBLINK_LED7_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED7_PIN))\r
151 \r
152 typedef struct {\r
153         unsigned char *port;\r
154         unsigned char *tris;\r
155         unsigned char pin;\r
156         portTickType  interval;\r
157 } SBLINK;\r
158 \r
159 const SBLINK sled0 = {&mainBLINK_LED0_PORT, &mainBLINK_LED0_TRIS, mainBLINK_LED0_PIN, mainBLINK_LED0_INTERVAL};\r
160 const SBLINK sled1 = {&mainBLINK_LED1_PORT, &mainBLINK_LED1_TRIS, mainBLINK_LED1_PIN, mainBLINK_LED1_INTERVAL};\r
161 const SBLINK sled2 = {&mainBLINK_LED2_PORT, &mainBLINK_LED2_TRIS, mainBLINK_LED2_PIN, mainBLINK_LED2_INTERVAL};\r
162 const SBLINK sled3 = {&mainBLINK_LED3_PORT, &mainBLINK_LED3_TRIS, mainBLINK_LED3_PIN, mainBLINK_LED3_INTERVAL};\r
163 const SBLINK sled4 = {&mainBLINK_LED4_PORT, &mainBLINK_LED4_TRIS, mainBLINK_LED4_PIN, mainBLINK_LED4_INTERVAL};\r
164 const SBLINK sled5 = {&mainBLINK_LED5_PORT, &mainBLINK_LED5_TRIS, mainBLINK_LED5_PIN, mainBLINK_LED5_INTERVAL};\r
165 const SBLINK sled6 = {&mainBLINK_LED6_PORT, &mainBLINK_LED6_TRIS, mainBLINK_LED6_PIN, mainBLINK_LED6_INTERVAL};\r
166 const SBLINK sled7 = {&mainBLINK_LED7_PORT, &mainBLINK_LED7_TRIS, mainBLINK_LED7_PIN, mainBLINK_LED7_INTERVAL};\r
167 \r
168 /*\r
169  * The task code for the "vBlink" task.\r
170  */\r
171 static portTASK_FUNCTION_PROTO(vBlink, pvParameters);\r
172 \r
173 /*-----------------------------------------------------------*/\r
174 \r
175 /*\r
176  * Creates the tasks, then starts the scheduler.\r
177  */\r
178 void main( void )\r
179 {\r
180         /*\r
181          * Start the blink tasks defined in this file.\r
182          */\r
183         xTaskCreate( vBlink,  "B0", configMINIMAL_STACK_SIZE, &sled0, tskIDLE_PRIORITY, NULL );\r
184         xTaskCreate( vBlink,  "B1", configMINIMAL_STACK_SIZE, &sled1, tskIDLE_PRIORITY, NULL );\r
185         xTaskCreate( vBlink,  "B2", configMINIMAL_STACK_SIZE, &sled2, tskIDLE_PRIORITY, NULL );\r
186         xTaskCreate( vBlink,  "B3", configMINIMAL_STACK_SIZE, &sled3, tskIDLE_PRIORITY, NULL );\r
187         xTaskCreate( vBlink,  "B4", configMINIMAL_STACK_SIZE, &sled4, tskIDLE_PRIORITY, NULL );\r
188         xTaskCreate( vBlink,  "B5", configMINIMAL_STACK_SIZE, &sled5, tskIDLE_PRIORITY, NULL );\r
189         xTaskCreate( vBlink,  "B6", configMINIMAL_STACK_SIZE, &sled6, tskIDLE_PRIORITY, NULL );\r
190         xTaskCreate( vBlink,  "B7", configMINIMAL_STACK_SIZE, &sled7, tskIDLE_PRIORITY, NULL );\r
191 \r
192         /*\r
193          * Start the scheduler.\r
194          */\r
195         vTaskStartScheduler( );\r
196         \r
197         while(1)        /* This point should never be reached. */\r
198         {\r
199         }\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 static portTASK_FUNCTION(vBlink, pvParameters)\r
204 {\r
205         unsigned char   *Port           = ((SBLINK *)pvParameters)->port;\r
206         unsigned char   *Tris           = ((SBLINK *)pvParameters)->tris;\r
207         unsigned char   Pin                     = ((SBLINK *)pvParameters)->pin;\r
208         portTickType    Interval        = ((SBLINK *)pvParameters)->interval;\r
209         \r
210         portTickType    xLastWakeTime;\r
211 \r
212         /*\r
213          * Initialize the hardware\r
214          */\r
215         *Tris &= ~(1<<Pin);     // Set the pin that is used by this task to ouput\r
216         *Port &= ~(1<<Pin);     // Drive the pin low\r
217         \r
218         /*\r
219          * Initialise the xLastWakeTime variable with the current time.\r
220          */\r
221         xLastWakeTime = xTaskGetTickCount();\r
222 \r
223         /*\r
224          * Cycle for ever, delaying then toggle the LED.\r
225          */\r
226         for( ;; )\r
227         {\r
228                 /*\r
229                  * Wait until it is time to toggle\r
230                  */\r
231                 vTaskDelayUntil( &xLastWakeTime, Interval );\r
232 \r
233                 /*\r
234                  * Toggle the LED for visual feedback.\r
235                  */\r
236                 *Port ^= 1<<Pin;\r
237         }\r
238 }\r