]> git.sur5r.net Git - freertos/blob - Demo/RX200_RX210-RSK_Renesas/RTOSDemo/main-blinky.c
Change version numbers to V7.1.0.
[freertos] / Demo / RX200_RX210-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 and one queue.  One task\r
56  * (the queue receive task) blocks on the queue to wait for data to arrive, \r
57  * toggling an LED each time '100' is received.  The other task (the queue send\r
58  * task) repeatedly blocks for a fixed period before sending '100' to the queue\r
59  * (causing the first task to toggle the LED). \r
60  *\r
61  * For a much more complete and complex example select either the Debug or\r
62  * Debug_with_optimisation build configurations within the HEW IDE. \r
63 */\r
64 \r
65 /* Hardware specific includes. */\r
66 #include "iodefine.h"\r
67 \r
68 /* Kernel includes. */\r
69 #include "FreeRTOS.h"\r
70 #include "task.h"\r
71 #include "queue.h"\r
72 \r
73 /* Priorities at which the tasks are created. */\r
74 #define configQUEUE_RECEIVE_TASK_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
75 #define configQUEUE_SEND_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
76 \r
77 /* The rate at which data is sent to the queue, specified in milliseconds. */\r
78 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 500 / portTICK_RATE_MS )\r
79 \r
80 /* The number of items the queue can hold.  This is 1 as the receive task\r
81 will remove items as they are added so the send task should always find the\r
82 queue empty. */\r
83 #define mainQUEUE_LENGTH                                        ( 1 )\r
84 \r
85 /*\r
86  * The tasks as defined at the top of this file.\r
87  */\r
88 static void prvQueueReceiveTask( void *pvParameters );\r
89 static void prvQueueSendTask( void *pvParameters );\r
90 \r
91 /* The queue used by both tasks. */\r
92 static xQueueHandle xQueue = NULL;\r
93 \r
94 /* This variable is not used by this simple Blinky example.  It is defined \r
95 purely to allow the project to link as it is used by the full build \r
96 configuration. */\r
97 volatile unsigned long ulHighFrequencyTickCount = 0UL;\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 void main(void)\r
102 {\r
103 extern void HardwareSetup( void );\r
104 \r
105         /* Renesas provided CPU configuration routine.  The clocks are configured in\r
106         here. */\r
107         HardwareSetup();\r
108         \r
109         /* Turn all LEDs off. */\r
110         vParTestInitialise();\r
111         \r
112         /* Create the queue. */\r
113         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
114 \r
115         if( xQueue != NULL )\r
116         {\r
117                 /* Start the two tasks as described at the top of this file. */\r
118                 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, configQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
119                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
120 \r
121                 /* Start the tasks running. */\r
122                 vTaskStartScheduler();\r
123         }\r
124         \r
125         /* If all is well the next line of code will not be reached as the scheduler \r
126         will be running.  If the next line is reached then it is likely that there was \r
127         insufficient heap available for the idle task to be created. */\r
128         for( ;; );\r
129 }\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 static void prvQueueSendTask( void *pvParameters )\r
133 {\r
134 portTickType xNextWakeTime;\r
135 const unsigned long ulValueToSend = 100UL;\r
136 \r
137         /* Initialise xNextWakeTime - this only needs to be done once. */\r
138         xNextWakeTime = xTaskGetTickCount();\r
139 \r
140         for( ;; )\r
141         {\r
142                 /* Place this task in the blocked state until it is time to run again. \r
143                 The block state is specified in ticks, the constant used converts ticks\r
144                 to ms. */\r
145                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
146 \r
147                 /* Send to the queue - causing the queue receive task to flash its LED.  0\r
148                 is used so the send does not block - it shouldn't need to as the queue\r
149                 should always be empty here (it should always be empty because the task\r
150                 removing items from the queue has a higher priority than the task adding\r
151                 things to the queue). */\r
152                 xQueueSend( xQueue, &ulValueToSend, 0UL );\r
153         }\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 static void prvQueueReceiveTask( void *pvParameters )\r
158 {\r
159 unsigned long ulReceivedValue;\r
160 \r
161         for( ;; )\r
162         {\r
163                 /* Wait until something arives in the queue - this will block \r
164                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
165                 FreeRTOSConfig.h. */\r
166                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
167 \r
168                 /*  To get here something must have arrived, but is it the expected\r
169                 value?  If it is, toggle the LED. */\r
170                 if( ulReceivedValue == 100UL )\r
171                 {\r
172                         vParTestToggleLED( 0 );\r
173                 }\r
174         }\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 /* A callback function named vApplicationSetupTimerInterrupt() must be defined\r
179 to configure a tick interrupt source, and configTICK_VECTOR set in \r
180 FreeRTOSConfig.h to install the tick interrupt handler in the correct position\r
181 in the vector table.  This example uses a compare match timer.  It can be\r
182 into any FreeRTOS project, provided the same compare match timer is available. */\r
183 void vApplicationSetupTimerInterrupt( void )\r
184 {\r
185         /* Enable compare match timer 0. */\r
186         MSTP( CMT0 ) = 0;\r
187         \r
188         /* Interrupt on compare match. */\r
189         CMT0.CMCR.BIT.CMIE = 1;\r
190         \r
191         /* Set the compare match value. */\r
192         CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );\r
193         \r
194         /* Divide the PCLK by 8. */\r
195         CMT0.CMCR.BIT.CKS = 0;\r
196         \r
197         /* Enable the interrupt... */\r
198         _IEN( _CMT0_CMI0 ) = 1;\r
199         \r
200         /* ...and set its priority to the application defined kernel priority. */\r
201         _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;\r
202         \r
203         /* Start the timer. */\r
204         CMT.CMSTR0.BIT.STR0 = 1;\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 /* If configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h, then this\r
209 function will be called if pvPortMalloc() returns NULL because it has exhausted\r
210 the available FreeRTOS heap space.  See http://www.freertos.org/a00111.html. */\r
211 void vApplicationMallocFailedHook( void )\r
212 {\r
213         for( ;; );\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 in \r
218 FreeRTOSConfig.h, then this function will be called if a task overflows its \r
219 stack space.  See \r
220 http://www.freertos.org/Stacks-and-stack-overflow-checking.html. */\r
221 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
222 {\r
223         for( ;; );\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 /* If configUSE_IDLE_HOOK is set to 1 in FreeRTOSConfig.h, then this function\r
228 will be called on each iteration of the idle task.  See \r
229 http://www.freertos.org/a00016.html */\r
230 void vApplicationIdleHook( void )\r
231 {\r
232         /* Just to prevent the variable getting optimised away. */\r
233         ( void ) ulHighFrequencyTickCount;\r
234 }\r
235 /*-----------------------------------------------------------*/\r