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