]> git.sur5r.net Git - freertos/blob - Demo/PIC18_WizC/Demo1/main.c
Change to the file headers only.
[freertos] / Demo / PIC18_WizC / Demo1 / main.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55 Changes from V3.0.0\r
56 \r
57 Changes from V3.0.1\r
58 */\r
59 \r
60 /*\r
61  * Instead of the normal single demo application, the PIC18F demo is split \r
62  * into several smaller programs of which this is the first.  This enables the \r
63  * demo's to be executed on the RAM limited PIC-devices.\r
64  *\r
65  * The Demo1 project is configured for a PIC18F4620 device.  Main.c starts 9 \r
66  * tasks (including the idle task).\r
67 \r
68  * This first demo is included to do a quick check on the FreeRTOS\r
69  * installation. It is also included to demonstrate a minimal project-setup\r
70  * to use FreeRTOS in a wizC environment.\r
71  *\r
72  * Eight independant tasks are created. All tasks share the same taskcode.\r
73  * Each task blinks a different led on portB. The blinkrate for each task\r
74  * is different, but chosen in such a way that portB will show a binary\r
75  * counter pattern. All blinkrates are derived from a single master-rate.\r
76  * By default, this  masterrate is set to 100 milliseconds. Although such\r
77  * a low value will make it almost impossible to see some of the leds\r
78  * actually blink, it is a good value when using the wizC-simulator.\r
79  * When testing on a real chip, changing the value to eg. 500 milliseconds\r
80  * would be appropiate.\r
81  */\r
82  \r
83 /* Scheduler include files. */\r
84 #include <FreeRTOS.h>\r
85 #include <task.h>\r
86 \r
87 #define mainBLINK_LED_INTERVAL  ( ( portTickType ) 100 / ( portTICK_RATE_MS ) )\r
88 \r
89 /* The LED that is flashed by the B0 task. */\r
90 #define mainBLINK_LED0_PORT             LATD\r
91 #define mainBLINK_LED0_TRIS             TRISD\r
92 #define mainBLINK_LED0_PIN              0\r
93 #define mainBLINK_LED0_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED0_PIN))\r
94 \r
95 /* The LED that is flashed by the B1 task. */\r
96 #define mainBLINK_LED1_PORT             LATD\r
97 #define mainBLINK_LED1_TRIS             TRISD\r
98 #define mainBLINK_LED1_PIN              1\r
99 #define mainBLINK_LED1_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED1_PIN))\r
100 \r
101 /* The LED that is flashed by the B2 task. */\r
102 #define mainBLINK_LED2_PORT             LATD\r
103 #define mainBLINK_LED2_TRIS             TRISD\r
104 #define mainBLINK_LED2_PIN              2\r
105 #define mainBLINK_LED2_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED2_PIN))\r
106 \r
107 /* The LED that is flashed by the B3 task. */\r
108 #define mainBLINK_LED3_PORT             LATD\r
109 #define mainBLINK_LED3_TRIS             TRISD\r
110 #define mainBLINK_LED3_PIN              3\r
111 #define mainBLINK_LED3_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED3_PIN))\r
112 \r
113 /* The LED that is flashed by the B4 task. */\r
114 #define mainBLINK_LED4_PORT             LATD\r
115 #define mainBLINK_LED4_TRIS             TRISD\r
116 #define mainBLINK_LED4_PIN              4\r
117 #define mainBLINK_LED4_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED4_PIN))\r
118 \r
119 /* The LED that is flashed by the B5 task. */\r
120 #define mainBLINK_LED5_PORT             LATD\r
121 #define mainBLINK_LED5_TRIS             TRISD\r
122 #define mainBLINK_LED5_PIN              5\r
123 #define mainBLINK_LED5_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED5_PIN))\r
124 \r
125 /* The LED that is flashed by the B6 task. */\r
126 #define mainBLINK_LED6_PORT             LATD\r
127 #define mainBLINK_LED6_TRIS             TRISD\r
128 #define mainBLINK_LED6_PIN              6\r
129 #define mainBLINK_LED6_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED6_PIN))\r
130 \r
131 /* The LED that is flashed by the B7 task. */\r
132 #define mainBLINK_LED7_PORT             LATD\r
133 #define mainBLINK_LED7_TRIS             TRISD\r
134 #define mainBLINK_LED7_PIN              7\r
135 #define mainBLINK_LED7_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED7_PIN))\r
136 \r
137 typedef struct {\r
138         unsigned char *port;\r
139         unsigned char *tris;\r
140         unsigned char pin;\r
141         portTickType  interval;\r
142 } SBLINK;\r
143 \r
144 const SBLINK sled0 = {&mainBLINK_LED0_PORT, &mainBLINK_LED0_TRIS, mainBLINK_LED0_PIN, mainBLINK_LED0_INTERVAL};\r
145 const SBLINK sled1 = {&mainBLINK_LED1_PORT, &mainBLINK_LED1_TRIS, mainBLINK_LED1_PIN, mainBLINK_LED1_INTERVAL};\r
146 const SBLINK sled2 = {&mainBLINK_LED2_PORT, &mainBLINK_LED2_TRIS, mainBLINK_LED2_PIN, mainBLINK_LED2_INTERVAL};\r
147 const SBLINK sled3 = {&mainBLINK_LED3_PORT, &mainBLINK_LED3_TRIS, mainBLINK_LED3_PIN, mainBLINK_LED3_INTERVAL};\r
148 const SBLINK sled4 = {&mainBLINK_LED4_PORT, &mainBLINK_LED4_TRIS, mainBLINK_LED4_PIN, mainBLINK_LED4_INTERVAL};\r
149 const SBLINK sled5 = {&mainBLINK_LED5_PORT, &mainBLINK_LED5_TRIS, mainBLINK_LED5_PIN, mainBLINK_LED5_INTERVAL};\r
150 const SBLINK sled6 = {&mainBLINK_LED6_PORT, &mainBLINK_LED6_TRIS, mainBLINK_LED6_PIN, mainBLINK_LED6_INTERVAL};\r
151 const SBLINK sled7 = {&mainBLINK_LED7_PORT, &mainBLINK_LED7_TRIS, mainBLINK_LED7_PIN, mainBLINK_LED7_INTERVAL};\r
152 \r
153 /*\r
154  * The task code for the "vBlink" task.\r
155  */\r
156 static portTASK_FUNCTION_PROTO(vBlink, pvParameters);\r
157 \r
158 /*-----------------------------------------------------------*/\r
159 \r
160 /*\r
161  * Creates the tasks, then starts the scheduler.\r
162  */\r
163 void main( void )\r
164 {\r
165         /*\r
166          * Start the blink tasks defined in this file.\r
167          */\r
168         xTaskCreate( vBlink,  "B0", configMINIMAL_STACK_SIZE, &sled0, tskIDLE_PRIORITY, NULL );\r
169         xTaskCreate( vBlink,  "B1", configMINIMAL_STACK_SIZE, &sled1, tskIDLE_PRIORITY, NULL );\r
170         xTaskCreate( vBlink,  "B2", configMINIMAL_STACK_SIZE, &sled2, tskIDLE_PRIORITY, NULL );\r
171         xTaskCreate( vBlink,  "B3", configMINIMAL_STACK_SIZE, &sled3, tskIDLE_PRIORITY, NULL );\r
172         xTaskCreate( vBlink,  "B4", configMINIMAL_STACK_SIZE, &sled4, tskIDLE_PRIORITY, NULL );\r
173         xTaskCreate( vBlink,  "B5", configMINIMAL_STACK_SIZE, &sled5, tskIDLE_PRIORITY, NULL );\r
174         xTaskCreate( vBlink,  "B6", configMINIMAL_STACK_SIZE, &sled6, tskIDLE_PRIORITY, NULL );\r
175         xTaskCreate( vBlink,  "B7", configMINIMAL_STACK_SIZE, &sled7, tskIDLE_PRIORITY, NULL );\r
176 \r
177         /*\r
178          * Start the scheduler.\r
179          */\r
180         vTaskStartScheduler( );\r
181         \r
182         while(1)        /* This point should never be reached. */\r
183         {\r
184         }\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 static portTASK_FUNCTION(vBlink, pvParameters)\r
189 {\r
190         unsigned char   *Port           = ((SBLINK *)pvParameters)->port;\r
191         unsigned char   *Tris           = ((SBLINK *)pvParameters)->tris;\r
192         unsigned char   Pin                     = ((SBLINK *)pvParameters)->pin;\r
193         portTickType    Interval        = ((SBLINK *)pvParameters)->interval;\r
194         \r
195         portTickType    xLastWakeTime;\r
196 \r
197         /*\r
198          * Initialize the hardware\r
199          */\r
200         *Tris &= ~(1<<Pin);     // Set the pin that is used by this task to ouput\r
201         *Port &= ~(1<<Pin);     // Drive the pin low\r
202         \r
203         /*\r
204          * Initialise the xLastWakeTime variable with the current time.\r
205          */\r
206         xLastWakeTime = xTaskGetTickCount();\r
207 \r
208         /*\r
209          * Cycle for ever, delaying then toggle the LED.\r
210          */\r
211         for( ;; )\r
212         {\r
213                 /*\r
214                  * Wait until it is time to toggle\r
215                  */\r
216                 vTaskDelayUntil( &xLastWakeTime, Interval );\r
217 \r
218                 /*\r
219                  * Toggle the LED for visual feedback.\r
220                  */\r
221                 *Port ^= 1<<Pin;\r
222         }\r
223 }\r