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