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