]> begriffs open source - cmsis-freertos/blob - Demo/PIC18_WizC/Demo1/main.c
Initial commit
[cmsis-freertos] / Demo / PIC18_WizC / Demo1 / main.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /*
71 Changes from V3.0.0
72
73 Changes from V3.0.1
74 */
75
76 /*
77  * Instead of the normal single demo application, the PIC18F demo is split 
78  * into several smaller programs of which this is the first.  This enables the 
79  * demo's to be executed on the RAM limited PIC-devices.
80  *
81  * The Demo1 project is configured for a PIC18F4620 device.  Main.c starts 9 
82  * tasks (including the idle task).
83
84  * This first demo is included to do a quick check on the FreeRTOS
85  * installation. It is also included to demonstrate a minimal project-setup
86  * to use FreeRTOS in a wizC environment.
87  *
88  * Eight independant tasks are created. All tasks share the same taskcode.
89  * Each task blinks a different led on portB. The blinkrate for each task
90  * is different, but chosen in such a way that portB will show a binary
91  * counter pattern. All blinkrates are derived from a single master-rate.
92  * By default, this  masterrate is set to 100 milliseconds. Although such
93  * a low value will make it almost impossible to see some of the leds
94  * actually blink, it is a good value when using the wizC-simulator.
95  * When testing on a real chip, changing the value to eg. 500 milliseconds
96  * would be appropiate.
97  */
98  
99 /* Scheduler include files. */
100 #include <FreeRTOS.h>
101 #include <task.h>
102
103 #define mainBLINK_LED_INTERVAL  ( ( TickType_t ) 100 / ( portTICK_PERIOD_MS ) )
104
105 /* The LED that is flashed by the B0 task. */
106 #define mainBLINK_LED0_PORT             LATD
107 #define mainBLINK_LED0_TRIS             TRISD
108 #define mainBLINK_LED0_PIN              0
109 #define mainBLINK_LED0_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED0_PIN))
110
111 /* The LED that is flashed by the B1 task. */
112 #define mainBLINK_LED1_PORT             LATD
113 #define mainBLINK_LED1_TRIS             TRISD
114 #define mainBLINK_LED1_PIN              1
115 #define mainBLINK_LED1_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED1_PIN))
116
117 /* The LED that is flashed by the B2 task. */
118 #define mainBLINK_LED2_PORT             LATD
119 #define mainBLINK_LED2_TRIS             TRISD
120 #define mainBLINK_LED2_PIN              2
121 #define mainBLINK_LED2_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED2_PIN))
122
123 /* The LED that is flashed by the B3 task. */
124 #define mainBLINK_LED3_PORT             LATD
125 #define mainBLINK_LED3_TRIS             TRISD
126 #define mainBLINK_LED3_PIN              3
127 #define mainBLINK_LED3_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED3_PIN))
128
129 /* The LED that is flashed by the B4 task. */
130 #define mainBLINK_LED4_PORT             LATD
131 #define mainBLINK_LED4_TRIS             TRISD
132 #define mainBLINK_LED4_PIN              4
133 #define mainBLINK_LED4_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED4_PIN))
134
135 /* The LED that is flashed by the B5 task. */
136 #define mainBLINK_LED5_PORT             LATD
137 #define mainBLINK_LED5_TRIS             TRISD
138 #define mainBLINK_LED5_PIN              5
139 #define mainBLINK_LED5_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED5_PIN))
140
141 /* The LED that is flashed by the B6 task. */
142 #define mainBLINK_LED6_PORT             LATD
143 #define mainBLINK_LED6_TRIS             TRISD
144 #define mainBLINK_LED6_PIN              6
145 #define mainBLINK_LED6_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED6_PIN))
146
147 /* The LED that is flashed by the B7 task. */
148 #define mainBLINK_LED7_PORT             LATD
149 #define mainBLINK_LED7_TRIS             TRISD
150 #define mainBLINK_LED7_PIN              7
151 #define mainBLINK_LED7_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED7_PIN))
152
153 typedef struct {
154         unsigned char *port;
155         unsigned char *tris;
156         unsigned char pin;
157         TickType_t  interval;
158 } SBLINK;
159
160 const SBLINK sled0 = {&mainBLINK_LED0_PORT, &mainBLINK_LED0_TRIS, mainBLINK_LED0_PIN, mainBLINK_LED0_INTERVAL};
161 const SBLINK sled1 = {&mainBLINK_LED1_PORT, &mainBLINK_LED1_TRIS, mainBLINK_LED1_PIN, mainBLINK_LED1_INTERVAL};
162 const SBLINK sled2 = {&mainBLINK_LED2_PORT, &mainBLINK_LED2_TRIS, mainBLINK_LED2_PIN, mainBLINK_LED2_INTERVAL};
163 const SBLINK sled3 = {&mainBLINK_LED3_PORT, &mainBLINK_LED3_TRIS, mainBLINK_LED3_PIN, mainBLINK_LED3_INTERVAL};
164 const SBLINK sled4 = {&mainBLINK_LED4_PORT, &mainBLINK_LED4_TRIS, mainBLINK_LED4_PIN, mainBLINK_LED4_INTERVAL};
165 const SBLINK sled5 = {&mainBLINK_LED5_PORT, &mainBLINK_LED5_TRIS, mainBLINK_LED5_PIN, mainBLINK_LED5_INTERVAL};
166 const SBLINK sled6 = {&mainBLINK_LED6_PORT, &mainBLINK_LED6_TRIS, mainBLINK_LED6_PIN, mainBLINK_LED6_INTERVAL};
167 const SBLINK sled7 = {&mainBLINK_LED7_PORT, &mainBLINK_LED7_TRIS, mainBLINK_LED7_PIN, mainBLINK_LED7_INTERVAL};
168
169 /*
170  * The task code for the "vBlink" task.
171  */
172 static portTASK_FUNCTION_PROTO(vBlink, pvParameters);
173
174 /*-----------------------------------------------------------*/
175
176 /*
177  * Creates the tasks, then starts the scheduler.
178  */
179 void main( void )
180 {
181         /*
182          * Start the blink tasks defined in this file.
183          */
184         xTaskCreate( vBlink,  "B0", configMINIMAL_STACK_SIZE, &sled0, tskIDLE_PRIORITY, NULL );
185         xTaskCreate( vBlink,  "B1", configMINIMAL_STACK_SIZE, &sled1, tskIDLE_PRIORITY, NULL );
186         xTaskCreate( vBlink,  "B2", configMINIMAL_STACK_SIZE, &sled2, tskIDLE_PRIORITY, NULL );
187         xTaskCreate( vBlink,  "B3", configMINIMAL_STACK_SIZE, &sled3, tskIDLE_PRIORITY, NULL );
188         xTaskCreate( vBlink,  "B4", configMINIMAL_STACK_SIZE, &sled4, tskIDLE_PRIORITY, NULL );
189         xTaskCreate( vBlink,  "B5", configMINIMAL_STACK_SIZE, &sled5, tskIDLE_PRIORITY, NULL );
190         xTaskCreate( vBlink,  "B6", configMINIMAL_STACK_SIZE, &sled6, tskIDLE_PRIORITY, NULL );
191         xTaskCreate( vBlink,  "B7", configMINIMAL_STACK_SIZE, &sled7, tskIDLE_PRIORITY, NULL );
192
193         /*
194          * Start the scheduler.
195          */
196         vTaskStartScheduler( );
197         
198         while(1)        /* This point should never be reached. */
199         {
200         }
201 }
202 /*-----------------------------------------------------------*/
203
204 static portTASK_FUNCTION(vBlink, pvParameters)
205 {
206         unsigned char   *Port           = ((SBLINK *)pvParameters)->port;
207         unsigned char   *Tris           = ((SBLINK *)pvParameters)->tris;
208         unsigned char   Pin                     = ((SBLINK *)pvParameters)->pin;
209         TickType_t      Interval        = ((SBLINK *)pvParameters)->interval;
210         
211         TickType_t      xLastWakeTime;
212
213         /*
214          * Initialize the hardware
215          */
216         *Tris &= ~(1<<Pin);     // Set the pin that is used by this task to ouput
217         *Port &= ~(1<<Pin);     // Drive the pin low
218         
219         /*
220          * Initialise the xLastWakeTime variable with the current time.
221          */
222         xLastWakeTime = xTaskGetTickCount();
223
224         /*
225          * Cycle for ever, delaying then toggle the LED.
226          */
227         for( ;; )
228         {
229                 /*
230                  * Wait until it is time to toggle
231                  */
232                 vTaskDelayUntil( &xLastWakeTime, Interval );
233
234                 /*
235                  * Toggle the LED for visual feedback.
236                  */
237                 *Port ^= 1<<Pin;
238         }
239 }