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