]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_WizC/Demo1/main.c
04d1bc1edfbc336c006a42e988046762b65ffdc0
[freertos] / FreeRTOS / Demo / PIC18_WizC / Demo1 / main.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29 Changes from V3.0.0\r
30 \r
31 Changes from V3.0.1\r
32 */\r
33 \r
34 /*\r
35  * Instead of the normal single demo application, the PIC18F demo is split \r
36  * into several smaller programs of which this is the first.  This enables the \r
37  * demo's to be executed on the RAM limited PIC-devices.\r
38  *\r
39  * The Demo1 project is configured for a PIC18F4620 device.  Main.c starts 9 \r
40  * tasks (including the idle task).\r
41 \r
42  * This first demo is included to do a quick check on the FreeRTOS\r
43  * installation. It is also included to demonstrate a minimal project-setup\r
44  * to use FreeRTOS in a wizC environment.\r
45  *\r
46  * Eight independant tasks are created. All tasks share the same taskcode.\r
47  * Each task blinks a different led on portB. The blinkrate for each task\r
48  * is different, but chosen in such a way that portB will show a binary\r
49  * counter pattern. All blinkrates are derived from a single master-rate.\r
50  * By default, this  masterrate is set to 100 milliseconds. Although such\r
51  * a low value will make it almost impossible to see some of the leds\r
52  * actually blink, it is a good value when using the wizC-simulator.\r
53  * When testing on a real chip, changing the value to eg. 500 milliseconds\r
54  * would be appropiate.\r
55  */\r
56  \r
57 /* Scheduler include files. */\r
58 #include <FreeRTOS.h>\r
59 #include <task.h>\r
60 \r
61 #define mainBLINK_LED_INTERVAL  ( ( TickType_t ) 100 / ( portTICK_PERIOD_MS ) )\r
62 \r
63 /* The LED that is flashed by the B0 task. */\r
64 #define mainBLINK_LED0_PORT             LATD\r
65 #define mainBLINK_LED0_TRIS             TRISD\r
66 #define mainBLINK_LED0_PIN              0\r
67 #define mainBLINK_LED0_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED0_PIN))\r
68 \r
69 /* The LED that is flashed by the B1 task. */\r
70 #define mainBLINK_LED1_PORT             LATD\r
71 #define mainBLINK_LED1_TRIS             TRISD\r
72 #define mainBLINK_LED1_PIN              1\r
73 #define mainBLINK_LED1_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED1_PIN))\r
74 \r
75 /* The LED that is flashed by the B2 task. */\r
76 #define mainBLINK_LED2_PORT             LATD\r
77 #define mainBLINK_LED2_TRIS             TRISD\r
78 #define mainBLINK_LED2_PIN              2\r
79 #define mainBLINK_LED2_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED2_PIN))\r
80 \r
81 /* The LED that is flashed by the B3 task. */\r
82 #define mainBLINK_LED3_PORT             LATD\r
83 #define mainBLINK_LED3_TRIS             TRISD\r
84 #define mainBLINK_LED3_PIN              3\r
85 #define mainBLINK_LED3_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED3_PIN))\r
86 \r
87 /* The LED that is flashed by the B4 task. */\r
88 #define mainBLINK_LED4_PORT             LATD\r
89 #define mainBLINK_LED4_TRIS             TRISD\r
90 #define mainBLINK_LED4_PIN              4\r
91 #define mainBLINK_LED4_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED4_PIN))\r
92 \r
93 /* The LED that is flashed by the B5 task. */\r
94 #define mainBLINK_LED5_PORT             LATD\r
95 #define mainBLINK_LED5_TRIS             TRISD\r
96 #define mainBLINK_LED5_PIN              5\r
97 #define mainBLINK_LED5_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED5_PIN))\r
98 \r
99 /* The LED that is flashed by the B6 task. */\r
100 #define mainBLINK_LED6_PORT             LATD\r
101 #define mainBLINK_LED6_TRIS             TRISD\r
102 #define mainBLINK_LED6_PIN              6\r
103 #define mainBLINK_LED6_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED6_PIN))\r
104 \r
105 /* The LED that is flashed by the B7 task. */\r
106 #define mainBLINK_LED7_PORT             LATD\r
107 #define mainBLINK_LED7_TRIS             TRISD\r
108 #define mainBLINK_LED7_PIN              7\r
109 #define mainBLINK_LED7_INTERVAL ((mainBLINK_LED_INTERVAL) << (mainBLINK_LED7_PIN))\r
110 \r
111 typedef struct {\r
112         unsigned char *port;\r
113         unsigned char *tris;\r
114         unsigned char pin;\r
115         TickType_t  interval;\r
116 } SBLINK;\r
117 \r
118 const SBLINK sled0 = {&mainBLINK_LED0_PORT, &mainBLINK_LED0_TRIS, mainBLINK_LED0_PIN, mainBLINK_LED0_INTERVAL};\r
119 const SBLINK sled1 = {&mainBLINK_LED1_PORT, &mainBLINK_LED1_TRIS, mainBLINK_LED1_PIN, mainBLINK_LED1_INTERVAL};\r
120 const SBLINK sled2 = {&mainBLINK_LED2_PORT, &mainBLINK_LED2_TRIS, mainBLINK_LED2_PIN, mainBLINK_LED2_INTERVAL};\r
121 const SBLINK sled3 = {&mainBLINK_LED3_PORT, &mainBLINK_LED3_TRIS, mainBLINK_LED3_PIN, mainBLINK_LED3_INTERVAL};\r
122 const SBLINK sled4 = {&mainBLINK_LED4_PORT, &mainBLINK_LED4_TRIS, mainBLINK_LED4_PIN, mainBLINK_LED4_INTERVAL};\r
123 const SBLINK sled5 = {&mainBLINK_LED5_PORT, &mainBLINK_LED5_TRIS, mainBLINK_LED5_PIN, mainBLINK_LED5_INTERVAL};\r
124 const SBLINK sled6 = {&mainBLINK_LED6_PORT, &mainBLINK_LED6_TRIS, mainBLINK_LED6_PIN, mainBLINK_LED6_INTERVAL};\r
125 const SBLINK sled7 = {&mainBLINK_LED7_PORT, &mainBLINK_LED7_TRIS, mainBLINK_LED7_PIN, mainBLINK_LED7_INTERVAL};\r
126 \r
127 /*\r
128  * The task code for the "vBlink" task.\r
129  */\r
130 static portTASK_FUNCTION_PROTO(vBlink, pvParameters);\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 /*\r
135  * Creates the tasks, then starts the scheduler.\r
136  */\r
137 void main( void )\r
138 {\r
139         /*\r
140          * Start the blink tasks defined in this file.\r
141          */\r
142         xTaskCreate( vBlink,  "B0", configMINIMAL_STACK_SIZE, &sled0, tskIDLE_PRIORITY, NULL );\r
143         xTaskCreate( vBlink,  "B1", configMINIMAL_STACK_SIZE, &sled1, tskIDLE_PRIORITY, NULL );\r
144         xTaskCreate( vBlink,  "B2", configMINIMAL_STACK_SIZE, &sled2, tskIDLE_PRIORITY, NULL );\r
145         xTaskCreate( vBlink,  "B3", configMINIMAL_STACK_SIZE, &sled3, tskIDLE_PRIORITY, NULL );\r
146         xTaskCreate( vBlink,  "B4", configMINIMAL_STACK_SIZE, &sled4, tskIDLE_PRIORITY, NULL );\r
147         xTaskCreate( vBlink,  "B5", configMINIMAL_STACK_SIZE, &sled5, tskIDLE_PRIORITY, NULL );\r
148         xTaskCreate( vBlink,  "B6", configMINIMAL_STACK_SIZE, &sled6, tskIDLE_PRIORITY, NULL );\r
149         xTaskCreate( vBlink,  "B7", configMINIMAL_STACK_SIZE, &sled7, tskIDLE_PRIORITY, NULL );\r
150 \r
151         /*\r
152          * Start the scheduler.\r
153          */\r
154         vTaskStartScheduler( );\r
155         \r
156         while(1)        /* This point should never be reached. */\r
157         {\r
158         }\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 static portTASK_FUNCTION(vBlink, pvParameters)\r
163 {\r
164         unsigned char   *Port           = ((SBLINK *)pvParameters)->port;\r
165         unsigned char   *Tris           = ((SBLINK *)pvParameters)->tris;\r
166         unsigned char   Pin                     = ((SBLINK *)pvParameters)->pin;\r
167         TickType_t      Interval        = ((SBLINK *)pvParameters)->interval;\r
168         \r
169         TickType_t      xLastWakeTime;\r
170 \r
171         /*\r
172          * Initialize the hardware\r
173          */\r
174         *Tris &= ~(1<<Pin);     // Set the pin that is used by this task to ouput\r
175         *Port &= ~(1<<Pin);     // Drive the pin low\r
176         \r
177         /*\r
178          * Initialise the xLastWakeTime variable with the current time.\r
179          */\r
180         xLastWakeTime = xTaskGetTickCount();\r
181 \r
182         /*\r
183          * Cycle for ever, delaying then toggle the LED.\r
184          */\r
185         for( ;; )\r
186         {\r
187                 /*\r
188                  * Wait until it is time to toggle\r
189                  */\r
190                 vTaskDelayUntil( &xLastWakeTime, Interval );\r
191 \r
192                 /*\r
193                  * Toggle the LED for visual feedback.\r
194                  */\r
195                 *Port ^= 1<<Pin;\r
196         }\r
197 }\r