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