]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX600_RX63N-RDK_Renesas/RTOSDemo/main-blinky.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / RX600_RX63N-RDK_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, one queue, and one \r
31  * software timer.  For a much more complete and complex example select either \r
32  * the Debug or Debug_with_optimisation build configurations within the HEW,\r
33  * which build main_full.c in place of this file.\r
34  * \r
35  * One task (the queue receive task) blocks on the queue to wait for data to \r
36  * arrive, toggling LED0 each time '100' is received.  The other task (the \r
37  * queue send task) repeatedly blocks for a fixed period before sending '100' \r
38  * to the queue (causing the first task to toggle the LED). \r
39  *\r
40  * The software timer is configured to auto-reload.  The timer callback \r
41  * function periodically toggles LED1.\r
42  */\r
43 \r
44 /* Kernel includes. */\r
45 #include "FreeRTOS.h"\r
46 #include "task.h"\r
47 #include "timers.h"\r
48 #include "queue.h"\r
49 \r
50 /* Priorities at which the tasks are created. */\r
51 #define configQUEUE_RECEIVE_TASK_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
52 #define configQUEUE_SEND_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
53 \r
54 /* The rate at which data is sent to the queue, specified in milliseconds. */\r
55 #define mainQUEUE_SEND_PERIOD_MS                        ( 500 / portTICK_PERIOD_MS )\r
56 \r
57 /* The period of the software timer, specified in milliseconds. */\r
58 #define mainSOFTWARE_TIMER_PERIOD_MS            ( 150 / portTICK_PERIOD_MS )\r
59 \r
60 /* The number of items the queue can hold.  This is 1 as the receive task\r
61 will remove items as they are added so the send task should always find the\r
62 queue empty. */\r
63 #define mainQUEUE_LENGTH                                        ( 1 )\r
64 \r
65 /* The LEDs toggle by the task and timer respectively. */\r
66 #define mainTASK_LED                                            ( 0 )\r
67 #define mainTIMER_LED                                           ( 1 )\r
68 \r
69 /*\r
70  * The tasks as defined at the top of this file.\r
71  */\r
72 static void prvQueueReceiveTask( void *pvParameters );\r
73 static void prvQueueSendTask( void *pvParameters );\r
74 \r
75 /*\r
76  * The callback function used by the software timer.\r
77  */\r
78 static void prvBlinkyTimerCallback( TimerHandle_t xTimer );\r
79 \r
80 /* The queue used by both tasks. */\r
81 static QueueHandle_t xQueue = NULL;\r
82 \r
83 /* This variable is not used by this simple Blinky example.  It is defined \r
84 purely to allow the project to link as it is used by the full project. */\r
85 volatile unsigned long ulHighFrequencyTickCount = 0UL;\r
86 /*-----------------------------------------------------------*/\r
87 \r
88 void main(void)\r
89 {\r
90 TimerHandle_t xTimer;\r
91 \r
92         /* Turn all LEDs off. */\r
93         vParTestInitialise();\r
94         \r
95         /* Create the queue. */\r
96         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
97 \r
98         /* Create the software timer, as described at the top of this file. */\r
99         xTimer = xTimerCreate( "BlinkyTimer",                                   /* Just a text name to make debugging easier - not used by the scheduler. */\r
100                                                         mainSOFTWARE_TIMER_PERIOD_MS,   /* The timer period. */\r
101                                                         pdTRUE,                                                 /* Set to pdTRUE for periodic timer, or pdFALSE for one-shot timer. */\r
102                                                         NULL,                                                   /* The timer ID is not required. */\r
103                                                         prvBlinkyTimerCallback );               /* The function executed when the timer expires. */\r
104                                                         \r
105         if( xTimer != NULL )\r
106         {\r
107                 /* Start the timer - it will not actually start running until the\r
108                 scheduler has started.  The block time is set to 0, although, because\r
109                 xTimerStart() is being called before the scheduler has been started,\r
110                 the any block time specified would be ignored anyway. */\r
111                 xTimerStart( xTimer, 0UL );\r
112         }\r
113         \r
114         if( xQueue != NULL )\r
115         {\r
116                 /* Start the two tasks as described at the top of this file. */\r
117                 xTaskCreate( prvQueueReceiveTask,                                       /* The function that implements the task. */\r
118                                          "Rx",                                                                  /* Just a text name to make debugging easier - not used by the scheduler. */\r
119                                          configMINIMAL_STACK_SIZE,                              /* The size of the task stack, in words. */\r
120                                          NULL,                                                                  /* The task parameter is not used. */\r
121                                          configQUEUE_RECEIVE_TASK_PRIORITY,     /* The priority assigned to the task when it is created. */\r
122                                          NULL );                                                                /* The task handle is not used. */\r
123                                          \r
124                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
125 \r
126                 /* Start the tasks running. */\r
127                 vTaskStartScheduler();\r
128         }\r
129         \r
130         /* If all is well we will never reach here as the scheduler will now be\r
131         running.  If we do reach here then it is likely that there was insufficient\r
132         heap available for the idle task to be created. */\r
133         for( ;; );\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 static void prvQueueSendTask( void *pvParameters )\r
138 {\r
139 TickType_t xNextWakeTime;\r
140 const unsigned long ulValueToSend = 100UL;\r
141 \r
142         /* Initialise xNextWakeTime - this only needs to be done once. */\r
143         xNextWakeTime = xTaskGetTickCount();\r
144 \r
145         for( ;; )\r
146         {\r
147                 /* Place this task in the blocked state until it is time to run again. \r
148                 The block state is specified in ticks, the constant used converts ticks\r
149                 to ms. */\r
150                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_PERIOD_MS );\r
151 \r
152                 /* Send to the queue - causing the queue receive task to flash its LED.  0\r
153                 is used so the send does not block - it shouldn't need to as the queue\r
154                 should always be empty here. */\r
155                 xQueueSend( xQueue, &ulValueToSend, 0 );\r
156         }\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 static void prvQueueReceiveTask( void *pvParameters )\r
161 {\r
162 unsigned long ulReceivedValue;\r
163 \r
164         for( ;; )\r
165         {\r
166                 /* Wait until something arives in the queue - this will block \r
167                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
168                 FreeRTOSConfig.h. */\r
169                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
170 \r
171                 /*  To get here something must have arrived, but is it the expected\r
172                 value?  If it is, toggle the LED. */\r
173                 if( ulReceivedValue == 100UL )\r
174                 {\r
175                         vParTestToggleLED( mainTASK_LED );\r
176                 }\r
177         }\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 static void prvBlinkyTimerCallback( TimerHandle_t xTimer )\r
182 {\r
183         /* The software timer does nothing but toggle an LED. */\r
184         vParTestToggleLED( mainTIMER_LED );\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 void vApplicationSetupTimerInterrupt( void )\r
189 {\r
190         /* Enable compare match timer 0. */\r
191         MSTP( CMT0 ) = 0;\r
192         \r
193         /* Interrupt on compare match. */\r
194         CMT0.CMCR.BIT.CMIE = 1;\r
195         \r
196         /* Set the compare match value. */\r
197         CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );\r
198         \r
199         /* Divide the PCLK by 8. */\r
200         CMT0.CMCR.BIT.CKS = 0;\r
201         \r
202         /* Enable the interrupt... */\r
203         _IEN( _CMT0_CMI0 ) = 1;\r
204         \r
205         /* ...and set its priority to the application defined kernel priority. */\r
206         _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;\r
207         \r
208         /* Start the timer. */\r
209         CMT.CMSTR0.BIT.STR0 = 1;\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 /* This function is explained by the comments above its prototype at the top\r
214 of this file. */\r
215 void vApplicationMallocFailedHook( void )\r
216 {\r
217         for( ;; );\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 /* This function is explained by the comments above its prototype at the top\r
222 of this file. */\r
223 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
224 {\r
225         for( ;; );\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 /* This function is explained by the comments above its prototype at the top\r
230 of this file. */\r
231 void vApplicationIdleHook( void )\r
232 {\r
233         /* Just to prevent the variable getting optimised away. */\r
234         ( void ) ulHighFrequencyTickCount;\r
235 }\r
236 /*-----------------------------------------------------------*/\r