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