]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_MPLAB/main3.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / PIC18_MPLAB / main3.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * THIS DEMO APPLICATION REQUIRES A LOOPBACK CONNECTOR TO BE FITTED TO THE PIC\r
31  * USART PORT - connect pin 2 to pin 3 on J2.\r
32  *\r
33  * Instead of the normal single demo application, the PIC18F demo is split\r
34  * into several smaller programs of which this is the third.  This enables the\r
35  * demo's to be executed on the RAM limited 40 pin devices.  The 64 and 80 pin\r
36  * devices require a more costly development platform and are not so readily\r
37  * available.\r
38  *\r
39  * The RTOSDemo3 project is configured for a PIC18F452 device.  Main3.c starts\r
40  * 5 tasks (including the idle task).\r
41  *\r
42  * The first task repeatedly transmits a string of characters on the PIC USART\r
43  * port.  The second task receives the characters, checking that the correct\r
44  * sequence is maintained (i.e. what is transmitted is identical to that\r
45  * received).  Each transmitted and each received character causes an LED to\r
46  * flash.  See demo/common/minimal/comtest. c for more information.\r
47  *\r
48  * The third task continuously performs a 32 bit calculation.  This is a good\r
49  * test of the context switch mechanism as the 8 bit architecture requires\r
50  * the use of several file registers to perform the 32 bit operations.  See\r
51  * demo/common/minimal/integer. c for more information.\r
52  *\r
53  * The third task is the check task.  This periodically checks that the other\r
54  * tasks are still running and have not experienced any errors.  If no errors\r
55  * have been reported by either the comms or integer tasks an LED is flashed\r
56  * with a frequency mainNO_ERROR_CHECK_PERIOD.  If an error is discovered the\r
57  * frequency is increased to mainERROR_FLASH_RATE.\r
58  *\r
59  * The check task also provides a visual indication of a system reset by\r
60  * flashing the one remaining LED (mainRESET_LED) when it starts.  After\r
61  * this initial flash mainRESET_LED should remain off.\r
62  *\r
63  * http://www.FreeRTOS.org contains important information on the use of the\r
64  * PIC18F port.\r
65  */\r
66 \r
67 /*\r
68 Changes from V2.0.0\r
69 \r
70         + Delay periods are now specified using variables and constants of\r
71           TickType_t rather than unsigned long.\r
72 */\r
73 \r
74 /* Scheduler include files. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 \r
78 /* Demo app include files. */\r
79 #include "partest.h"\r
80 #include "serial.h"\r
81 #include "comtest.h"\r
82 #include "integer.h"\r
83 \r
84 /* Priority definitions for the LED tasks.  Other tasks just use the idle\r
85 priority. */\r
86 #define mainCOMM_TEST_PRIORITY                  ( tskIDLE_PRIORITY + ( unsigned portBASE_TYPE ) 2 )\r
87 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + ( unsigned portBASE_TYPE ) 3 )\r
88 \r
89 /* The period between executions of the check task before and after an error\r
90 has been discovered.  If an error has been discovered the check task runs\r
91 more frequently - increasing the LED flash rate. */\r
92 #define mainNO_ERROR_CHECK_PERIOD               ( ( TickType_t ) 1000 / portTICK_PERIOD_MS )\r
93 #define mainERROR_CHECK_PERIOD                  ( ( TickType_t ) 100 / portTICK_PERIOD_MS )\r
94 \r
95 /* The period for which mainRESET_LED remain on every reset. */\r
96 #define mainRESET_LED_PERIOD                    ( ( TickType_t ) 500 / portTICK_PERIOD_MS )\r
97 \r
98 /* The LED that is toggled whenever a character is transmitted.\r
99 mainCOMM_TX_RX_LED + 1 will be toggled every time a character is received. */\r
100 #define mainCOMM_TX_RX_LED                              ( ( unsigned portBASE_TYPE ) 2 )\r
101 \r
102 /* The LED that is flashed by the check task at a rate that indicates the\r
103 error status. */\r
104 #define mainCHECK_TASK_LED                              ( ( unsigned portBASE_TYPE ) 1 )\r
105 \r
106 /* The LED that is flashed once upon every reset. */\r
107 #define mainRESET_LED                                   ( ( unsigned portBASE_TYPE ) 0 )\r
108 \r
109 /* Constants required for the communications. */\r
110 #define mainCOMMS_QUEUE_LENGTH                  ( ( unsigned portBASE_TYPE ) 5 )\r
111 #define mainBAUD_RATE                                   ( ( unsigned long ) 57600 )\r
112 /*-----------------------------------------------------------*/\r
113 \r
114 /*\r
115  * Task function which periodically checks the other tasks for errors.  Flashes\r
116  * an LED at a rate that indicates whether an error has ever been detected.\r
117  */\r
118 static void vErrorChecks( void *pvParameters );\r
119 \r
120 /*-----------------------------------------------------------*/\r
121 \r
122 /* Creates the tasks, then starts the scheduler. */\r
123 void main( void )\r
124 {\r
125         /* Initialise the required hardware. */\r
126         vParTestInitialise();\r
127 \r
128         /* Initialise the block memory allocator. */\r
129         vPortInitialiseBlocks();\r
130 \r
131         /* Start the standard comtest tasks as defined in demo/common/minimal. */\r
132         vAltStartComTestTasks( mainCOMM_TEST_PRIORITY, mainBAUD_RATE, mainCOMM_TX_RX_LED );\r
133 \r
134         /* Start the standard 32bit calculation task as defined in\r
135         demo/common/minimal. */\r
136         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
137 \r
138         /* Start the check task defined in this file. */\r
139         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
140 \r
141         /* Start the scheduler.  This will never return. */\r
142         vTaskStartScheduler();\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 static void vErrorChecks( void *pvParameters )\r
147 {\r
148 TickType_t xDelayTime = mainNO_ERROR_CHECK_PERIOD;\r
149 volatile unsigned long ulDummy = 3UL;\r
150 \r
151         /* Toggle the LED so we can see when a reset occurs. */\r
152         vParTestSetLED( mainRESET_LED, pdTRUE );\r
153         vTaskDelay( mainRESET_LED_PERIOD );\r
154         vParTestSetLED( mainRESET_LED, pdFALSE );\r
155 \r
156         /* Cycle for ever, delaying then checking all the other tasks are still\r
157         operating without error. */\r
158         for( ;; )\r
159         {\r
160                 /* Wait until it is time to check the other tasks. */\r
161                 vTaskDelay( xDelayTime );\r
162 \r
163                 /* Perform an integer calculation - just to ensure the registers\r
164                 get used.  The result is not important. */\r
165                 ulDummy *= 3UL;\r
166 \r
167                 /* Check all the other tasks are running, and running without ever\r
168                 having an error.  The delay period is lowered if an error is reported,\r
169                 causing the LED to flash at a higher rate. */\r
170                 if( xAreIntegerMathsTaskStillRunning() == pdFALSE )\r
171                 {\r
172                         xDelayTime = mainERROR_CHECK_PERIOD;\r
173                 }\r
174 \r
175                 if( xAreComTestTasksStillRunning() == pdFALSE )\r
176                 {\r
177                         xDelayTime = mainERROR_CHECK_PERIOD;\r
178                 }\r
179 \r
180                 /* Flash the LED for visual feedback.  The rate of the flash will\r
181                 indicate the health of the system. */\r
182                 vParTestToggleLED( mainCHECK_TASK_LED );\r
183         }\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r