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