]> begriffs open source - cmsis-freertos/blob - Demo/RL78_E2Studio_GCC/src/main_blinky.c
This is a FreeRTOS header, not RTX.
[cmsis-freertos] / Demo / RL78_E2Studio_GCC / src / main_blinky.c
1 /*
2  * FreeRTOS Kernel V10.0.1
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /******************************************************************************
29  * NOTE 1:  This project provides two demo applications.  A simple blinky style
30  * project, and a more comprehensive test and demo application.  The
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
32  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
33  * in main.c.  This file implements the simply blinky style version.
34  *
35  * NOTE 2:  This file only contains the source code that is specific to the
36  * basic demo.  Generic functions, such FreeRTOS hook functions, and functions
37  * required to configure the hardware, along with an example interrupt service
38  * routine, are defined in main.c.
39  ******************************************************************************
40  *
41  * main_blinky() creates one queue, and two tasks.  It then starts the
42  * scheduler.
43  *
44  * The Queue Send Task:
45  * The queue send task is implemented by the prvQueueSendTask() function in
46  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly
47  * block for 200 milliseconds, before sending the value 100 to the queue that
48  * was created within main_blinky().  Once the value is sent, the task loops
49  * back around to block for another 200 milliseconds.
50  *
51  * The Queue Receive Task:
52  * The queue receive task is implemented by the prvQueueReceiveTask() function
53  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly
54  * blocks on attempts to read data from the queue that was created within
55  * main_blinky().  When data is received, the task checks the value of the
56  * data, and if the value equals the expected 100, toggles the LED.  The 'block
57  * time' parameter passed to the queue receive function specifies that the
58  * task should be held in the Blocked state indefinitely to wait for data to
59  * be available on the queue.  The queue receive task will only leave the
60  * Blocked state when the queue send task writes to the queue.  As the queue
61  * send task writes to the queue every 200 milliseconds, the queue receive
62  * task leaves the Blocked state every 200 milliseconds, and therefore toggles
63  * the LED every 200 milliseconds.
64  */
65
66 /* Standard includes. */
67 #include <stdio.h>
68
69 /* Kernel includes. */
70 #include "FreeRTOS.h"
71 #include "task.h"
72 #include "semphr.h"
73
74 /* Eval board specific definitions. */
75 #include "demo_specific_io.h"
76
77 /* Priorities at which the tasks are created. */
78 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )
79 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )
80
81 /* The rate at which data is sent to the queue.  The 200ms value is converted
82 to ticks using the portTICK_PERIOD_MS constant. */
83 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )
84
85 /* The number of items the queue can hold.  This is 1 as the receive task
86 will remove items as they are added, meaning the send task should always find
87 the queue empty. */
88 #define mainQUEUE_LENGTH                                        ( 1 )
89
90 /* Used to check the task parameter passing in both supported memory models. */
91 #define mainQUEUE_SEND_PARAMETER        ( ( void * ) 0x1234U )
92 #define mainQUEUE_RECEIVE_PARAMETER     ( ( void * ) 0x1122U )
93 /*-----------------------------------------------------------*/
94
95 /*
96  * The tasks as described in the comments at the top of this file.
97  */
98 static void prvQueueReceiveTask( void *pvParameters );
99 static void prvQueueSendTask( void *pvParameters );
100
101 /*
102  * Called by main() to create the simply blinky style application if
103  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
104  */
105 void main_blinky( void );
106
107 /*-----------------------------------------------------------*/
108
109 /* The queue used by both tasks. */
110 static QueueHandle_t xQueue = NULL;
111
112 /*-----------------------------------------------------------*/
113
114 void main_blinky( void )
115 {
116         /* Create the queue. */
117         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
118
119         if( xQueue != NULL )
120         {
121                 /* Start the two tasks as described in the comments at the top of this
122                 file. */
123                 xTaskCreate( prvQueueReceiveTask,                       /* The function that implements the task. */
124                                         "Rx",                                                   /* The text name assigned to the task - for debug only as it is not used by the kernel. */
125                                         configMINIMAL_STACK_SIZE,               /* The size of the stack to allocate to the task. */
126                                         mainQUEUE_RECEIVE_PARAMETER,    /* The parameter passed to the task - just used to check the port in this case. */
127                                         mainQUEUE_RECEIVE_TASK_PRIORITY,/* The priority assigned to the task. */
128                                         NULL );                                                 /* The task handle is not required, so NULL is passed. */
129
130                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );
131
132                 /* Start the tasks and timer running. */
133                 vTaskStartScheduler();
134         }
135
136         /* If all is well, the scheduler will now be running, and the following
137         line will never be reached.  If the following line does execute, then
138         there was insufficient FreeRTOS heap memory available for the idle and/or
139         timer tasks     to be created.  See the memory management section on the
140         FreeRTOS web site for more details.  http://www.freertos.org/a00111.html. */
141         for( ;; );
142 }
143 /*-----------------------------------------------------------*/
144
145 static void prvQueueSendTask( void *pvParameters )
146 {
147 TickType_t xNextWakeTime;
148 const unsigned long ulValueToSend = 100UL;
149
150         /* Check the parameter was passed in correctly. */
151         configASSERT( pvParameters == mainQUEUE_SEND_PARAMETER )
152
153         /* Initialise xNextWakeTime - this only needs to be done once. */
154         xNextWakeTime = xTaskGetTickCount();
155
156         for( ;; )
157         {
158                 /* Place this task in the blocked state until it is time to run again. */
159                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
160
161                 /* Send to the queue - causing the queue receive task to unblock and
162                 toggle the LED.  0 is used as the block time so the sending operation
163                 will not block - it shouldn't need to block as the queue should always
164                 be empty at this point in the code. */
165                 xQueueSend( xQueue, &ulValueToSend, 0U );
166         }
167 }
168 /*-----------------------------------------------------------*/
169
170 static void prvQueueReceiveTask( void *pvParameters )
171 {
172 unsigned long ulReceivedValue;
173 const unsigned long ulExpectedValue = 100UL;
174
175         /* Check the parameter was passed in correctly. */
176         configASSERT( pvParameters == mainQUEUE_RECEIVE_PARAMETER )
177
178         for( ;; )
179         {
180                 /* Wait until something arrives in the queue - this task will block
181                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
182                 FreeRTOSConfig.h. */
183                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
184
185                 /*  To get here something must have been received from the queue, but
186                 is it the expected value?  If it is, toggle the LED. */
187                 if( ulReceivedValue == ulExpectedValue )
188                 {
189                         LED_BIT = !LED_BIT;
190                         ulReceivedValue = 0U;
191                 }
192         }
193 }
194 /*-----------------------------------------------------------*/
195