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