]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R4_RM48_CCS5/main_blinky.c
1beca16664603417b87084995645b10f6024c144
[freertos] / FreeRTOS / Demo / CORTEX_R4_RM48_CCS5 / main_blinky.c
1 volatile unsigned long ulRx = 0, ulTx = 0;\r
2 /*\r
3     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
4 \r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
9      *    Complete, revised, and edited pdf reference manuals are also       *\r
10      *    available.                                                         *\r
11      *                                                                       *\r
12      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
13      *    ensuring you get running as quickly as possible and with an        *\r
14      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
15      *    the FreeRTOS project to continue with its mission of providing     *\r
16      *    professional grade, cross platform, de facto standard solutions    *\r
17      *    for microcontrollers - completely free of charge!                  *\r
18      *                                                                       *\r
19      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
20      *                                                                       *\r
21      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
22      *                                                                       *\r
23     ***************************************************************************\r
24 \r
25 \r
26     This file is part of the FreeRTOS distribution.\r
27 \r
28     FreeRTOS is free software; you can redistribute it and/or modify it under\r
29     the terms of the GNU General Public License (version 2) as published by the\r
30     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
31     >>>NOTE<<< The modification to the GPL is included to allow you to\r
32     distribute a combined work that includes FreeRTOS without being obliged to\r
33     provide the source code for proprietary components outside of the FreeRTOS\r
34     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
35     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
36     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
37     more details. You should have received a copy of the GNU General Public\r
38     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
39     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
40     by writing to Richard Barry, contact details for whom are available on the\r
41     FreeRTOS WEB site.\r
42 \r
43     1 tab == 4 spaces!\r
44     \r
45     ***************************************************************************\r
46      *                                                                       *\r
47      *    Having a problem?  Start by reading the FAQ "My application does   *\r
48      *    not run, what could be wrong?                                      *\r
49      *                                                                       *\r
50      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
51      *                                                                       *\r
52     ***************************************************************************\r
53 \r
54     \r
55     http://www.FreeRTOS.org - Documentation, training, latest information, \r
56     license and contact details.\r
57     \r
58     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
59     including FreeRTOS+Trace - an indispensable productivity tool.\r
60 \r
61     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
62     the code with commercial support, indemnification, and middleware, under \r
63     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
64     provide a safety engineered and independently SIL3 certified version under \r
65     the SafeRTOS brand: http://www.SafeRTOS.com.\r
66 */\r
67 \r
68 /******************************************************************************\r
69  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
70  * project, and a more comprehensive test and demo application.  The\r
71  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
72  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
73  * in main.c.  This file implements the simply blinky style version.\r
74  *\r
75  * NOTE 2:  This file only contains the source code that is specific to the\r
76  * basic demo.  Generic functions, such FreeRTOS hook functions, and functions\r
77  * required to configure the hardware, are defined in main.c.\r
78  ******************************************************************************\r
79  *\r
80  * main_blinky() creates one queue, and two tasks.  It then starts the\r
81  * scheduler.\r
82  *\r
83  * The Queue Send Task:\r
84  * The queue send task is implemented by the prvQueueSendTask() function in\r
85  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
86  * block for 200 milliseconds, before sending the value 100 to the queue that\r
87  * was created within main_blinky().  Once the value is sent, the task loops\r
88  * back around to block for another 200 milliseconds.\r
89  *\r
90  * The Queue Receive Task:\r
91  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
92  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly\r
93  * blocks on attempts to read data from the queue that was created within\r
94  * main_blinky().  When data is received, the task checks the value of the\r
95  * data, and if the value equals the expected 100, toggles the LED.  The 'block\r
96  * time' parameter passed to the queue receive function specifies that the\r
97  * task should be held in the Blocked state indefinitely to wait for data to\r
98  * be available on the queue.  The queue receive task will only leave the\r
99  * Blocked state when the queue send task writes to the queue.  As the queue\r
100  * send task writes to the queue every 200 milliseconds, the queue receive\r
101  * task leaves the Blocked state every 200 milliseconds, and therefore toggles\r
102  * the LED every 200 milliseconds.\r
103  */\r
104 \r
105 /* Standard includes. */\r
106 #include <stdio.h>\r
107 \r
108 /* Kernel includes. */\r
109 #include "FreeRTOS.h"\r
110 #include "task.h"\r
111 #include "semphr.h"\r
112 \r
113 /* Common demo includes. */\r
114 #include "partest.h"\r
115 \r
116 /* Priorities at which the tasks are created. */\r
117 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
118 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
119 \r
120 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
121 to ticks using the portTICK_RATE_MS constant. */\r
122 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_RATE_MS )\r
123 \r
124 /* The number of items the queue can hold.  This is 1 as the receive task\r
125 will remove items as they are added, meaning the send task should always find\r
126 the queue empty. */\r
127 #define mainQUEUE_LENGTH                                        ( 1 )\r
128 \r
129 /* Values passed to the two tasks just to check the task parameter \r
130 functionality. */\r
131 #define mainQUEUE_SEND_PARAMETER                        ( 0x1111UL )\r
132 #define mainQUEUE_RECEIVE_PARAMETER                     ( 0x22UL )\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 /*\r
137  * The tasks as described in the comments at the top of this file.\r
138  */\r
139 static void prvQueueReceiveTask( void *pvParameters );\r
140 static void prvQueueSendTask( void *pvParameters );\r
141 \r
142 /*\r
143  * Called by main() to create the simply blinky style application if\r
144  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
145  */\r
146 void main_blinky( void );\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 \r
150 /* The queue used by both tasks. */\r
151 static xQueueHandle xQueue = NULL;\r
152 \r
153 /*-----------------------------------------------------------*/\r
154 \r
155 void main_blinky( void )\r
156 {\r
157         /* Create the queue. */\r
158         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
159 \r
160         if( xQueue != NULL )\r
161         {\r
162                 /* Start the two tasks as described in the comments at the top of this\r
163                 file. */\r
164                 xTaskCreate( prvQueueReceiveTask,                                       /* The function that implements the task. */\r
165                                         ( signed char * ) "Rx",                                 /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
166                                         configMINIMAL_STACK_SIZE,                               /* The size of the stack to allocate to the task. */\r
167                                         ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */\r
168                                         mainQUEUE_RECEIVE_TASK_PRIORITY,                /* The priority assigned to the task. */\r
169                                         NULL );                                                                 /* The task handle is not required, so NULL is passed. */\r
170 \r
171                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
172 \r
173                 /* Start the tasks and timer running. */\r
174                 vTaskStartScheduler();\r
175         }\r
176 \r
177         /* If all is well, the scheduler will now be running, and the following\r
178         line will never be reached.  If the following line does execute, then\r
179         there was insufficient FreeRTOS heap memory available for the idle and/or\r
180         timer tasks     to be created.  See the memory management section on the\r
181         FreeRTOS web site for more details. */\r
182         for( ;; );\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 static void prvQueueSendTask( void *pvParameters )\r
187 {\r
188 portTickType xNextWakeTime;\r
189 const unsigned long ulValueToSend = 100UL;\r
190 \r
191         /* Check the task parameter is as expected. */\r
192         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );\r
193 \r
194         /* Initialise xNextWakeTime - this only needs to be done once. */\r
195         xNextWakeTime = xTaskGetTickCount();\r
196 \r
197         for( ;; )\r
198         {\r
199                 /* Place this task in the blocked state until it is time to run again.\r
200                 The block time is specified in ticks, the constant used converts ticks\r
201                 to ms.  While in the Blocked state this task will not consume any CPU\r
202                 time. */\r
203                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
204 \r
205                 /* Send to the queue - causing the queue receive task to unblock and\r
206                 toggle the LED.  0 is used as the block time so the sending operation\r
207                 will not block - it shouldn't need to block as the queue should always\r
208                 be empty at this point in the code. */\r
209                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
210 ulTx++;\r
211         }\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 static void prvQueueReceiveTask( void *pvParameters )\r
216 {\r
217 unsigned long ulReceivedValue;\r
218 \r
219         /* Check the task parameter is as expected. */\r
220         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );\r
221 \r
222         for( ;; )\r
223         {\r
224                 /* Wait until something arrives in the queue - this task will block\r
225                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
226                 FreeRTOSConfig.h. */\r
227                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
228 \r
229                 /*  To get here something must have been received from the queue, but\r
230                 is it the expected value?  If it is, toggle the LED. */\r
231                 if( ulReceivedValue == 100UL )\r
232                 {\r
233                         vParTestToggleLED( 0 );\r
234                         ulReceivedValue = 0U;\r
235 ulRx++;\r
236                 }\r
237         }\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r