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