]> begriffs open source - freertos/blob - Demo/PIC18_WizC/Demo1/main.c
Update in preparation for the V4.3.1 release.
[freertos] / Demo / PIC18_WizC / Demo1 / main.c
1 /*\r
2         FreeRTOS.org V4.3.1 - Copyright (C) 2003-2007 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         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /*\r
37 Changes from V3.0.0\r
38 \r
39 Changes from V3.0.1\r
40 */\r
41 \r
42 /*\r
43  * Instead of the normal single demo application, the PIC18F demo is split \r
44  * into several smaller programs of which this is the first.  This enables the \r
45  * demo's to be executed on the RAM limited PIC-devices.\r
46  *\r
47  * The Demo1 project is configured for a PIC18F4620 device.  Main.c starts 9 \r
48  * tasks (including the idle task).\r
49 \r
50  * This first demo is included to do a quick check on the FreeRTOS\r
51  * installation. It is also included to demonstrate a minimal project-setup\r
52  * to use FreeRTOS in a wizC environment.\r
53  *\r
54  * Eight independant tasks are created. All tasks share the same taskcode.\r
55  * Each task blinks a different led on portB. The blinkrate for each task\r
56  * is different, but chosen in such a way that portB will show a binary\r
57  * counter pattern. All blinkrates are derived from a single master-rate.\r
58  * By default, this  masterrate is set to 100 milliseconds. Although such\r
59  * a low value will make it almost impossible to see some of the leds\r
60  * actually blink, it is a good value when using the wizC-simulator.\r
61  * When testing on a real chip, changing the value to eg. 500 milliseconds\r
62  * would be appropiate.\r
63  */\r
64  \r
65 /* Scheduler include files. */\r
66 #include <FreeRTOS.h>\r
67 #include <task.h>\r
68 \r
69 #define mainBLINK_LED_INTERVAL  ( ( portTickType ) 100 / ( portTICK_RATE_MS ) )\r
70 \r
71 /* The LED that is flashed by the B0 task. */\r
72 #define mainBLINK_LED0_PORT             LATD\r
73 #define mainBLINK_LED0_TRIS             TRISD\r
74 #define mainBLINK_LED0_PIN              0\r
75 #define mainBLINK_LED0_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED0_PIN))\r
76 \r
77 /* The LED that is flashed by the B1 task. */\r
78 #define mainBLINK_LED1_PORT             LATD\r
79 #define mainBLINK_LED1_TRIS             TRISD\r
80 #define mainBLINK_LED1_PIN              1\r
81 #define mainBLINK_LED1_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED1_PIN))\r
82 \r
83 /* The LED that is flashed by the B2 task. */\r
84 #define mainBLINK_LED2_PORT             LATD\r
85 #define mainBLINK_LED2_TRIS             TRISD\r
86 #define mainBLINK_LED2_PIN              2\r
87 #define mainBLINK_LED2_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED2_PIN))\r
88 \r
89 /* The LED that is flashed by the B3 task. */\r
90 #define mainBLINK_LED3_PORT             LATD\r
91 #define mainBLINK_LED3_TRIS             TRISD\r
92 #define mainBLINK_LED3_PIN              3\r
93 #define mainBLINK_LED3_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED3_PIN))\r
94 \r
95 /* The LED that is flashed by the B4 task. */\r
96 #define mainBLINK_LED4_PORT             LATD\r
97 #define mainBLINK_LED4_TRIS             TRISD\r
98 #define mainBLINK_LED4_PIN              4\r
99 #define mainBLINK_LED4_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED4_PIN))\r
100 \r
101 /* The LED that is flashed by the B5 task. */\r
102 #define mainBLINK_LED5_PORT             LATD\r
103 #define mainBLINK_LED5_TRIS             TRISD\r
104 #define mainBLINK_LED5_PIN              5\r
105 #define mainBLINK_LED5_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED5_PIN))\r
106 \r
107 /* The LED that is flashed by the B6 task. */\r
108 #define mainBLINK_LED6_PORT             LATD\r
109 #define mainBLINK_LED6_TRIS             TRISD\r
110 #define mainBLINK_LED6_PIN              6\r
111 #define mainBLINK_LED6_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED6_PIN))\r
112 \r
113 /* The LED that is flashed by the B7 task. */\r
114 #define mainBLINK_LED7_PORT             LATD\r
115 #define mainBLINK_LED7_TRIS             TRISD\r
116 #define mainBLINK_LED7_PIN              7\r
117 #define mainBLINK_LED7_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED7_PIN))\r
118 \r
119 typedef struct {\r
120         unsigned char *port;\r
121         unsigned char *tris;\r
122         unsigned char pin;\r
123         portTickType  interval;\r
124 } SBLINK;\r
125 \r
126 const SBLINK sled0 = {&mainBLINK_LED0_PORT, &mainBLINK_LED0_TRIS, mainBLINK_LED0_PIN, mainBLINK_LED0_INTERVAL};\r
127 const SBLINK sled1 = {&mainBLINK_LED1_PORT, &mainBLINK_LED1_TRIS, mainBLINK_LED1_PIN, mainBLINK_LED1_INTERVAL};\r
128 const SBLINK sled2 = {&mainBLINK_LED2_PORT, &mainBLINK_LED2_TRIS, mainBLINK_LED2_PIN, mainBLINK_LED2_INTERVAL};\r
129 const SBLINK sled3 = {&mainBLINK_LED3_PORT, &mainBLINK_LED3_TRIS, mainBLINK_LED3_PIN, mainBLINK_LED3_INTERVAL};\r
130 const SBLINK sled4 = {&mainBLINK_LED4_PORT, &mainBLINK_LED4_TRIS, mainBLINK_LED4_PIN, mainBLINK_LED4_INTERVAL};\r
131 const SBLINK sled5 = {&mainBLINK_LED5_PORT, &mainBLINK_LED5_TRIS, mainBLINK_LED5_PIN, mainBLINK_LED5_INTERVAL};\r
132 const SBLINK sled6 = {&mainBLINK_LED6_PORT, &mainBLINK_LED6_TRIS, mainBLINK_LED6_PIN, mainBLINK_LED6_INTERVAL};\r
133 const SBLINK sled7 = {&mainBLINK_LED7_PORT, &mainBLINK_LED7_TRIS, mainBLINK_LED7_PIN, mainBLINK_LED7_INTERVAL};\r
134 \r
135 /*\r
136  * The task code for the "vBlink" task.\r
137  */\r
138 static portTASK_FUNCTION_PROTO(vBlink, pvParameters);\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 /*\r
143  * Creates the tasks, then starts the scheduler.\r
144  */\r
145 void main( void )\r
146 {\r
147         /*\r
148          * Start the blink tasks defined in this file.\r
149          */\r
150         xTaskCreate( vBlink,  "B0", configMINIMAL_STACK_SIZE, &sled0, tskIDLE_PRIORITY, NULL );\r
151         xTaskCreate( vBlink,  "B1", configMINIMAL_STACK_SIZE, &sled1, tskIDLE_PRIORITY, NULL );\r
152         xTaskCreate( vBlink,  "B2", configMINIMAL_STACK_SIZE, &sled2, tskIDLE_PRIORITY, NULL );\r
153         xTaskCreate( vBlink,  "B3", configMINIMAL_STACK_SIZE, &sled3, tskIDLE_PRIORITY, NULL );\r
154         xTaskCreate( vBlink,  "B4", configMINIMAL_STACK_SIZE, &sled4, tskIDLE_PRIORITY, NULL );\r
155         xTaskCreate( vBlink,  "B5", configMINIMAL_STACK_SIZE, &sled5, tskIDLE_PRIORITY, NULL );\r
156         xTaskCreate( vBlink,  "B6", configMINIMAL_STACK_SIZE, &sled6, tskIDLE_PRIORITY, NULL );\r
157         xTaskCreate( vBlink,  "B7", configMINIMAL_STACK_SIZE, &sled7, tskIDLE_PRIORITY, NULL );\r
158 \r
159         /*\r
160          * Start the scheduler.\r
161          */\r
162         vTaskStartScheduler( );\r
163         \r
164         while(1)        /* This point should never be reached. */\r
165         {\r
166         }\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static portTASK_FUNCTION(vBlink, pvParameters)\r
171 {\r
172         unsigned char   *Port           = ((SBLINK *)pvParameters)->port;\r
173         unsigned char   *Tris           = ((SBLINK *)pvParameters)->tris;\r
174         unsigned char   Pin                     = ((SBLINK *)pvParameters)->pin;\r
175         portTickType    Interval        = ((SBLINK *)pvParameters)->interval;\r
176         \r
177         portTickType    xLastWakeTime;\r
178 \r
179         /*\r
180          * Initialize the hardware\r
181          */\r
182         *Tris &= ~(1<<Pin);     // Set the pin that is used by this task to ouput\r
183         *Port &= ~(1<<Pin);     // Drive the pin low\r
184         \r
185         /*\r
186          * Initialise the xLastWakeTime variable with the current time.\r
187          */\r
188         xLastWakeTime = xTaskGetTickCount();\r
189 \r
190         /*\r
191          * Cycle for ever, delaying then toggle the LED.\r
192          */\r
193         for( ;; )\r
194         {\r
195                 /*\r
196                  * Wait until it is time to toggle\r
197                  */\r
198                 vTaskDelayUntil( &xLastWakeTime, Interval );\r
199 \r
200                 /*\r
201                  * Toggle the LED for visual feedback.\r
202                  */\r
203                 *Port ^= 1<<Pin;\r
204         }\r
205 }\r