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