]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX200_RX210-RSK_Renesas/RTOSDemo/main-blinky.c
fa1fc5216c59bc8af347b906eb5636042b5b25f7
[freertos] / FreeRTOS / Demo / RX200_RX210-RSK_Renesas / RTOSDemo / main-blinky.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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 \r
75 void main(void)\r
76 {\r
77 extern void HardwareSetup( void );\r
78 \r
79         /* Renesas provided CPU configuration routine.  The clocks are configured in\r
80         here. */\r
81         HardwareSetup();\r
82         \r
83         /* Turn all LEDs off. */\r
84         vParTestInitialise();\r
85         \r
86         /* Create the queue. */\r
87         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
88 \r
89         if( xQueue != NULL )\r
90         {\r
91                 /* Start the two tasks as described at the top of this file. */\r
92                 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, configQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
93                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
94 \r
95                 /* Start the tasks running. */\r
96                 vTaskStartScheduler();\r
97         }\r
98         \r
99         /* If all is well the next line of code will not be reached as the scheduler \r
100         will be running.  If the next line is reached then it is likely that there was \r
101         insufficient heap available for the idle task to be created. */\r
102         for( ;; );\r
103 }\r
104 /*-----------------------------------------------------------*/\r
105 \r
106 static void prvQueueSendTask( void *pvParameters )\r
107 {\r
108 TickType_t xNextWakeTime;\r
109 const unsigned long ulValueToSend = 100UL;\r
110 \r
111         /* Initialise xNextWakeTime - this only needs to be done once. */\r
112         xNextWakeTime = xTaskGetTickCount();\r
113 \r
114         for( ;; )\r
115         {\r
116                 /* Place this task in the blocked state until it is time to run again. \r
117                 The block state is specified in ticks, the constant used converts ticks\r
118                 to ms. */\r
119                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
120 \r
121                 /* Send to the queue - causing the queue receive task to flash its LED.  0\r
122                 is used so the send does not block - it shouldn't need to as the queue\r
123                 should always be empty here (it should always be empty because the task\r
124                 removing items from the queue has a higher priority than the task adding\r
125                 things to the queue). */\r
126                 xQueueSend( xQueue, &ulValueToSend, 0UL );\r
127         }\r
128 }\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 static void prvQueueReceiveTask( void *pvParameters )\r
132 {\r
133 unsigned long ulReceivedValue;\r
134 \r
135         for( ;; )\r
136         {\r
137                 /* Wait until something arives in the queue - this will block \r
138                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
139                 FreeRTOSConfig.h. */\r
140                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
141 \r
142                 /*  To get here something must have arrived, but is it the expected\r
143                 value?  If it is, toggle the LED. */\r
144                 if( ulReceivedValue == 100UL )\r
145                 {\r
146                         vParTestToggleLED( 0 );\r
147                 }\r
148         }\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 /* A callback function named vApplicationSetupTimerInterrupt() must be defined\r
153 to configure a tick interrupt source, and configTICK_VECTOR set in \r
154 FreeRTOSConfig.h to install the tick interrupt handler in the correct position\r
155 in the vector table.  This example uses a compare match timer.  It can be\r
156 into any FreeRTOS project, provided the same compare match timer is available. */\r
157 void vApplicationSetupTimerInterrupt( void )\r
158 {\r
159         /* Enable compare match timer 0. */\r
160         MSTP( CMT0 ) = 0;\r
161         \r
162         /* Interrupt on compare match. */\r
163         CMT0.CMCR.BIT.CMIE = 1;\r
164         \r
165         /* Set the compare match value. */\r
166         CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );\r
167         \r
168         /* Divide the PCLK by 8. */\r
169         CMT0.CMCR.BIT.CKS = 0;\r
170         \r
171         /* Enable the interrupt... */\r
172         _IEN( _CMT0_CMI0 ) = 1;\r
173         \r
174         /* ...and set its priority to the application defined kernel priority. */\r
175         _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;\r
176         \r
177         /* Start the timer. */\r
178         CMT.CMSTR0.BIT.STR0 = 1;\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 /* If configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h, then this\r
183 function will be called if pvPortMalloc() returns NULL because it has exhausted\r
184 the available FreeRTOS heap space.  See http://www.freertos.org/a00111.html. */\r
185 void vApplicationMallocFailedHook( void )\r
186 {\r
187         for( ;; );\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 in \r
192 FreeRTOSConfig.h, then this function will be called if a task overflows its \r
193 stack space.  See \r
194 http://www.freertos.org/Stacks-and-stack-overflow-checking.html. */\r
195 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
196 {\r
197         for( ;; );\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 /* If configUSE_IDLE_HOOK is set to 1 in FreeRTOSConfig.h, then this function\r
202 will be called on each iteration of the idle task.  See \r
203 http://www.freertos.org/a00016.html */\r
204 void vApplicationIdleHook( void )\r
205 {\r
206         /* Just to prevent the variable getting optimised away. */\r
207         ( void ) ulHighFrequencyTickCount;\r
208 }\r
209 /*-----------------------------------------------------------*/\r