]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX600_RX63N-RDK_Renesas/RTOSDemo/main-blinky.c
5e6d43314ceb9601476da3271cb1a8f449861a6e
[freertos] / FreeRTOS / Demo / RX600_RX63N-RDK_Renesas / RTOSDemo / main-blinky.c
1 /*\r
2     FreeRTOS V8.0.0 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* \r
67  * This is a very simple demo that creates two tasks, one queue, and one \r
68  * software timer.  For a much more complete and complex example select either \r
69  * the Debug or Debug_with_optimisation build configurations within the HEW,\r
70  * which build main_full.c in place of this file.\r
71  * \r
72  * One task (the queue receive task) blocks on the queue to wait for data to \r
73  * arrive, toggling LED0 each time '100' is received.  The other task (the \r
74  * queue send task) repeatedly blocks for a fixed period before sending '100' \r
75  * to the queue (causing the first task to toggle the LED). \r
76  *\r
77  * The software timer is configured to auto-reload.  The timer callback \r
78  * function periodically toggles LED1.\r
79  */\r
80 \r
81 /* Kernel includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 #include "timers.h"\r
85 #include "queue.h"\r
86 \r
87 /* Priorities at which the tasks are created. */\r
88 #define configQUEUE_RECEIVE_TASK_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
89 #define configQUEUE_SEND_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
90 \r
91 /* The rate at which data is sent to the queue, specified in milliseconds. */\r
92 #define mainQUEUE_SEND_PERIOD_MS                        ( 500 / portTICK_PERIOD_MS )\r
93 \r
94 /* The period of the software timer, specified in milliseconds. */\r
95 #define mainSOFTWARE_TIMER_PERIOD_MS            ( 150 / portTICK_PERIOD_MS )\r
96 \r
97 /* The number of items the queue can hold.  This is 1 as the receive task\r
98 will remove items as they are added so the send task should always find the\r
99 queue empty. */\r
100 #define mainQUEUE_LENGTH                                        ( 1 )\r
101 \r
102 /* The LEDs toggle by the task and timer respectively. */\r
103 #define mainTASK_LED                                            ( 0 )\r
104 #define mainTIMER_LED                                           ( 1 )\r
105 \r
106 /*\r
107  * The tasks as defined at the top of this file.\r
108  */\r
109 static void prvQueueReceiveTask( void *pvParameters );\r
110 static void prvQueueSendTask( void *pvParameters );\r
111 \r
112 /*\r
113  * The callback function used by the software timer.\r
114  */\r
115 static void prvBlinkyTimerCallback( TimerHandle_t xTimer );\r
116 \r
117 /* The queue used by both tasks. */\r
118 static QueueHandle_t xQueue = NULL;\r
119 \r
120 /* This variable is not used by this simple Blinky example.  It is defined \r
121 purely to allow the project to link as it is used by the full project. */\r
122 volatile unsigned long ulHighFrequencyTickCount = 0UL;\r
123 /*-----------------------------------------------------------*/\r
124 \r
125 void main(void)\r
126 {\r
127 TimerHandle_t xTimer;\r
128 \r
129         /* Turn all LEDs off. */\r
130         vParTestInitialise();\r
131         \r
132         /* Create the queue. */\r
133         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
134 \r
135         /* Create the software timer, as described at the top of this file. */\r
136         xTimer = xTimerCreate( "BlinkyTimer",                                   /* Just a text name to make debugging easier - not used by the scheduler. */\r
137                                                         mainSOFTWARE_TIMER_PERIOD_MS,   /* The timer period. */\r
138                                                         pdTRUE,                                                 /* Set to pdTRUE for periodic timer, or pdFALSE for one-shot timer. */\r
139                                                         NULL,                                                   /* The timer ID is not required. */\r
140                                                         prvBlinkyTimerCallback );               /* The function executed when the timer expires. */\r
141                                                         \r
142         if( xTimer != NULL )\r
143         {\r
144                 /* Start the timer - it will not actually start running until the\r
145                 scheduler has started.  The block time is set to 0, although, because\r
146                 xTimerStart() is being called before the scheduler has been started,\r
147                 the any block time specified would be ignored anyway. */\r
148                 xTimerStart( xTimer, 0UL );\r
149         }\r
150         \r
151         if( xQueue != NULL )\r
152         {\r
153                 /* Start the two tasks as described at the top of this file. */\r
154                 xTaskCreate( prvQueueReceiveTask,                                       /* The function that implements the task. */\r
155                                          "Rx",                                                                  /* Just a text name to make debugging easier - not used by the scheduler. */\r
156                                          configMINIMAL_STACK_SIZE,                              /* The size of the task stack, in words. */\r
157                                          NULL,                                                                  /* The task parameter is not used. */\r
158                                          configQUEUE_RECEIVE_TASK_PRIORITY,     /* The priority assigned to the task when it is created. */\r
159                                          NULL );                                                                /* The task handle is not used. */\r
160                                          \r
161                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
162 \r
163                 /* Start the tasks running. */\r
164                 vTaskStartScheduler();\r
165         }\r
166         \r
167         /* If all is well we will never reach here as the scheduler will now be\r
168         running.  If we do reach here then it is likely that there was insufficient\r
169         heap available for the idle task to be created. */\r
170         for( ;; );\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 static void prvQueueSendTask( void *pvParameters )\r
175 {\r
176 TickType_t xNextWakeTime;\r
177 const unsigned long ulValueToSend = 100UL;\r
178 \r
179         /* Initialise xNextWakeTime - this only needs to be done once. */\r
180         xNextWakeTime = xTaskGetTickCount();\r
181 \r
182         for( ;; )\r
183         {\r
184                 /* Place this task in the blocked state until it is time to run again. \r
185                 The block state is specified in ticks, the constant used converts ticks\r
186                 to ms. */\r
187                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_PERIOD_MS );\r
188 \r
189                 /* Send to the queue - causing the queue receive task to flash its LED.  0\r
190                 is used so the send does not block - it shouldn't need to as the queue\r
191                 should always be empty here. */\r
192                 xQueueSend( xQueue, &ulValueToSend, 0 );\r
193         }\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 static void prvQueueReceiveTask( void *pvParameters )\r
198 {\r
199 unsigned long ulReceivedValue;\r
200 \r
201         for( ;; )\r
202         {\r
203                 /* Wait until something arives in the queue - this will block \r
204                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
205                 FreeRTOSConfig.h. */\r
206                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
207 \r
208                 /*  To get here something must have arrived, but is it the expected\r
209                 value?  If it is, toggle the LED. */\r
210                 if( ulReceivedValue == 100UL )\r
211                 {\r
212                         vParTestToggleLED( mainTASK_LED );\r
213                 }\r
214         }\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static void prvBlinkyTimerCallback( TimerHandle_t xTimer )\r
219 {\r
220         /* The software timer does nothing but toggle an LED. */\r
221         vParTestToggleLED( mainTIMER_LED );\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 void vApplicationSetupTimerInterrupt( void )\r
226 {\r
227         /* Enable compare match timer 0. */\r
228         MSTP( CMT0 ) = 0;\r
229         \r
230         /* Interrupt on compare match. */\r
231         CMT0.CMCR.BIT.CMIE = 1;\r
232         \r
233         /* Set the compare match value. */\r
234         CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );\r
235         \r
236         /* Divide the PCLK by 8. */\r
237         CMT0.CMCR.BIT.CKS = 0;\r
238         \r
239         /* Enable the interrupt... */\r
240         _IEN( _CMT0_CMI0 ) = 1;\r
241         \r
242         /* ...and set its priority to the application defined kernel priority. */\r
243         _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;\r
244         \r
245         /* Start the timer. */\r
246         CMT.CMSTR0.BIT.STR0 = 1;\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 /* This function is explained by the comments above its prototype at the top\r
251 of this file. */\r
252 void vApplicationMallocFailedHook( void )\r
253 {\r
254         for( ;; );\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 /* This function is explained by the comments above its prototype at the top\r
259 of this file. */\r
260 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
261 {\r
262         for( ;; );\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 /* This function is explained by the comments above its prototype at the top\r
267 of this file. */\r
268 void vApplicationIdleHook( void )\r
269 {\r
270         /* Just to prevent the variable getting optimised away. */\r
271         ( void ) ulHighFrequencyTickCount;\r
272 }\r
273 /*-----------------------------------------------------------*/\r