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