]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX600_RX630-RSK_Renesas/RTOSDemo/main-blinky.c
cf9f9802c19e843d21364e8c528b0ac7ffd76d61
[freertos] / FreeRTOS / Demo / RX600_RX630-RSK_Renesas / RTOSDemo / main-blinky.c
1 /*\r
2  * FreeRTOS Kernel V10.1.1\r
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* \r
29  * This is a very simple demo that creates two tasks and one queue.  One task\r
30  * (the queue receive task) blocks on the queue to wait for data to arrive, \r
31  * toggling an LED each time '100' is received.  The other task (the queue send\r
32  * task) repeatedly blocks for a fixed period before sending '100' to the queue\r
33  * (causing the first task to toggle the LED). \r
34  *\r
35  * For a much more complete and complex example select either the Debug or\r
36  * Debug_with_optimisation build configurations within the HEW IDE. \r
37 */\r
38 \r
39 /* Hardware specific includes. */\r
40 #include "iodefine.h"\r
41 \r
42 /* Kernel includes. */\r
43 #include "FreeRTOS.h"\r
44 #include "task.h"\r
45 #include "queue.h"\r
46 \r
47 /* Priorities at which the tasks are created. */\r
48 #define configQUEUE_RECEIVE_TASK_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
49 #define configQUEUE_SEND_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
50 \r
51 /* The rate at which data is sent to the queue, specified in milliseconds. */\r
52 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 500 / portTICK_PERIOD_MS )\r
53 \r
54 /* The number of items the queue can hold.  This is 1 as the receive task\r
55 will remove items as they are added so the send task should always find the\r
56 queue empty. */\r
57 #define mainQUEUE_LENGTH                                        ( 1 )\r
58 \r
59 /*\r
60  * The tasks as defined at the top of this file.\r
61  */\r
62 static void prvQueueReceiveTask( void *pvParameters );\r
63 static void prvQueueSendTask( void *pvParameters );\r
64 \r
65 /* The queue used by both tasks. */\r
66 static QueueHandle_t xQueue = NULL;\r
67 \r
68 /* This variable is not used by this simple Blinky example.  It is defined \r
69 purely to allow the project to link as it is used by the full build \r
70 configuration. */\r
71 volatile unsigned long ulHighFrequencyTickCount = 0UL;\r
72 /*-----------------------------------------------------------*/\r
73 \r
74 void main(void)\r
75 {\r
76 extern void HardwareSetup( void );\r
77 \r
78         /* Renesas provided CPU configuration routine.  The clocks are configured in\r
79         here. */\r
80         HardwareSetup();\r
81         \r
82         /* Turn all LEDs off. */\r
83         vParTestInitialise();\r
84         \r
85         /* Create the queue. */\r
86         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
87 \r
88         if( xQueue != NULL )\r
89         {\r
90                 /* Start the two tasks as described at the top of this file. */\r
91                 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, configQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
92                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
93 \r
94                 /* Start the tasks running. */\r
95                 vTaskStartScheduler();\r
96         }\r
97         \r
98         /* If all is well the next line of code will not be reached as the scheduler \r
99         will be running.  If the next line is reached then it is likely that there was \r
100         insufficient heap available for the idle task to be created. */\r
101         for( ;; );\r
102 }\r
103 /*-----------------------------------------------------------*/\r
104 \r
105 static void prvQueueSendTask( void *pvParameters )\r
106 {\r
107 TickType_t xNextWakeTime;\r
108 const unsigned long ulValueToSend = 100UL;\r
109 \r
110         /* Initialise xNextWakeTime - this only needs to be done once. */\r
111         xNextWakeTime = xTaskGetTickCount();\r
112 \r
113         for( ;; )\r
114         {\r
115                 /* Place this task in the blocked state until it is time to run again. \r
116                 The block state is specified in ticks, the constant used converts ticks\r
117                 to ms. */\r
118                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
119 \r
120                 /* Send to the queue - causing the queue receive task to flash its LED.  0\r
121                 is used so the send does not block - it shouldn't need to as the queue\r
122                 should always be empty here (it should always be empty because the task\r
123                 removing items from the queue has a higher priority than the task adding\r
124                 things to the queue). */\r
125                 xQueueSend( xQueue, &ulValueToSend, 0UL );\r
126         }\r
127 }\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 static void prvQueueReceiveTask( void *pvParameters )\r
131 {\r
132 unsigned long ulReceivedValue;\r
133 \r
134         for( ;; )\r
135         {\r
136                 /* Wait until something arives in the queue - this will block \r
137                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
138                 FreeRTOSConfig.h. */\r
139                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
140 \r
141                 /*  To get here something must have arrived, but is it the expected\r
142                 value?  If it is, toggle the LED. */\r
143                 if( ulReceivedValue == 100UL )\r
144                 {\r
145                         vParTestToggleLED( 0 );\r
146                 }\r
147         }\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 /* A callback function named vApplicationSetupTimerInterrupt() must be defined\r
152 to configure a tick interrupt source, and configTICK_VECTOR set in \r
153 FreeRTOSConfig.h to install the tick interrupt handler in the correct position\r
154 in the vector table.  This example uses a compare match timer.  It can be\r
155 into any FreeRTOS project, provided the same compare match timer is available. */\r
156 void vApplicationSetupTimerInterrupt( void )\r
157 {\r
158         /* Enable compare match timer 0. */\r
159         MSTP( CMT0 ) = 0;\r
160         \r
161         /* Interrupt on compare match. */\r
162         CMT0.CMCR.BIT.CMIE = 1;\r
163         \r
164         /* Set the compare match value. */\r
165         CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );\r
166         \r
167         /* Divide the PCLK by 8. */\r
168         CMT0.CMCR.BIT.CKS = 0;\r
169         \r
170         /* Enable the interrupt... */\r
171         _IEN( _CMT0_CMI0 ) = 1;\r
172         \r
173         /* ...and set its priority to the application defined kernel priority. */\r
174         _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;\r
175         \r
176         /* Start the timer. */\r
177         CMT.CMSTR0.BIT.STR0 = 1;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 /* If configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h, then this\r
182 function will be called if pvPortMalloc() returns NULL because it has exhausted\r
183 the available FreeRTOS heap space.  See http://www.freertos.org/a00111.html. */\r
184 void vApplicationMallocFailedHook( void )\r
185 {\r
186         for( ;; );\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 in \r
191 FreeRTOSConfig.h, then this function will be called if a task overflows its \r
192 stack space.  See \r
193 http://www.freertos.org/Stacks-and-stack-overflow-checking.html. */\r
194 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
195 {\r
196         for( ;; );\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 /* If configUSE_IDLE_HOOK is set to 1 in FreeRTOSConfig.h, then this function\r
201 will be called on each iteration of the idle task.  See \r
202 http://www.freertos.org/a00016.html */\r
203 void vApplicationIdleHook( void )\r
204 {\r
205         /* Just to prevent the variable getting optimised away. */\r
206         ( void ) ulHighFrequencyTickCount;\r
207 }\r
208 /*-----------------------------------------------------------*/\r