OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [demo_freeRTOS/] [blinky_demo/] [main_blinky.c] - Blame information for rev 22

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 22 zero_gravi
/*
2
 * FreeRTOS Kernel V10.3.0
3
 * Copyright (C) 2020 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
#ifdef RUN_FREERTOS_DEMO
29
 
30
/******************************************************************************
31
 * NOTE 1:  This project provides two demo applications.  A simple blinky
32
 * style project, and a more comprehensive test and demo application.  The
33
 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
34
 * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
35
 * in main.c.  This file implements the simply blinky style version.
36
 *
37
 * NOTE 2:  This file only contains the source code that is specific to the
38
 * basic demo.  Generic functions, such FreeRTOS hook functions, and functions
39
 * required to configure the hardware are defined in main.c.
40
 ******************************************************************************
41
 *
42
 * main_blinky() creates one queue, and two tasks.  It then starts the
43
 * scheduler.
44
 *
45
 * The Queue Send Task:
46
 * The queue send task is implemented by the prvQueueSendTask() function in
47
 * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly
48
 * block for 1000 milliseconds, before sending the value 100 to the queue that
49
 * was created within main_blinky().  Once the value is sent, the task loops
50
 * back around to block for another 1000 milliseconds...and so on.
51
 *
52
 * The Queue Receive Task:
53
 * The queue receive task is implemented by the prvQueueReceiveTask() function
54
 * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly
55
 * blocks on attempts to read data from the queue that was created within
56
 * main_blinky().  When data is received, the task checks the value of the
57
 * data, and if the value equals the expected 100, writes 'Blink' to the UART
58
 * (the UART is used in place of the LED to allow easy execution in QEMU).  The
59
 * 'block time' parameter passed to the queue receive function specifies that
60
 * the task should be held in the Blocked state indefinitely to wait for data to
61
 * be available on the queue.  The queue receive task will only leave the
62
 * Blocked state when the queue send task writes to the queue.  As the queue
63
 * send task writes to the queue every 1000 milliseconds, the queue receive
64
 * task leaves the Blocked state every 1000 milliseconds, and therefore toggles
65
 * the LED every 200 milliseconds.
66
 */
67
 
68
/* Standard includes. */
69
#include <stdio.h>
70
#include <string.h>
71
#include <unistd.h>
72
 
73
/* Kernel includes. */
74
#include "FreeRTOS.h"
75
#include "task.h"
76
#include "queue.h"
77
 
78
/* Priorities used by the tasks. */
79
#define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )
80
#define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )
81
 
82
/* The rate at which data is sent to the queue.  The 200ms value is converted
83
to ticks using the pdMS_TO_TICKS() macro. */
84
#define mainQUEUE_SEND_FREQUENCY_MS                     pdMS_TO_TICKS( 1000 )
85
 
86
/* The maximum number items the queue can hold.  The priority of the receiving
87
task is above the priority of the sending task, so the receiving task will
88
preempt the sending task and remove the queue items each time the sending task
89
writes to the queue.  Therefore the queue will never have more than one item in
90
it at any time, and even with a queue length of 1, the sending task will never
91
find the queue full. */
92
#define mainQUEUE_LENGTH                                        ( 1 )
93
 
94
/*-----------------------------------------------------------*/
95
 
96
/*
97
 * Called by main when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1 in
98
 * main.c.
99
 */
100
void main_blinky( void );
101
 
102
/*
103
 * The tasks as described in the comments at the top of this file.
104
 */
105
static void prvQueueReceiveTask( void *pvParameters );
106
static void prvQueueSendTask( void *pvParameters );
107
 
108
/*-----------------------------------------------------------*/
109
 
110
/* The queue used by both tasks. */
111
static QueueHandle_t xQueue = NULL;
112
 
113
/*-----------------------------------------------------------*/
114
 
115
void main_blinky( void )
116
{
117
        /* Create the queue. */
118
        xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );
119
 
120
        if( xQueue != NULL )
121
        {
122
                /* Start the two tasks as described in the comments at the top of this
123
                file. */
124
                xTaskCreate( prvQueueReceiveTask,                               /* The function that implements the task. */
125
                                        "Rx",                                                           /* The text name assigned to the task - for debug only as it is not used by the kernel. */
126
                                        configMINIMAL_STACK_SIZE * 2U,          /* The size of the stack to allocate to the task. */
127
                                        NULL,                                                           /* The parameter passed to the task - not used in this case. */
128
                                        mainQUEUE_RECEIVE_TASK_PRIORITY,        /* The priority assigned to the task. */
129
                                        NULL );                                                         /* The task handle is not required, so NULL is passed. */
130
 
131
                xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE * 2U, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
132
 
133
                /* Start the tasks and timer running. */
134
                vTaskStartScheduler();
135
        }
136
 
137
        /* If all is well, the scheduler will now be running, and the following
138
        line will never be reached.  If the following line does execute, then
139
        there was insufficient FreeRTOS heap memory available for the Idle and/or
140
        timer tasks to be created.  See the memory management section on the
141
        FreeRTOS web site for more details on the FreeRTOS heap
142
        http://www.freertos.org/a00111.html. */
143
        for( ;; );
144
}
145
/*-----------------------------------------------------------*/
146
 
147
static void prvQueueSendTask( void *pvParameters )
148
{
149
TickType_t xNextWakeTime;
150
const unsigned long ulValueToSend = 100UL;
151
BaseType_t xReturned;
152
 
153
        /* Remove compiler warning about unused parameter. */
154
        ( void ) pvParameters;
155
 
156
        /* Initialise xNextWakeTime - this only needs to be done once. */
157
        xNextWakeTime = xTaskGetTickCount();
158
 
159
        for( ;; )
160
        {
161
                /* Place this task in the blocked state until it is time to run again. */
162
                vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
163
 
164
                /* Send to the queue - causing the queue receive task to unblock and
165
                toggle the LED.  0 is used as the block time so the sending operation
166
                will not block - it shouldn't need to block as the queue should always
167
                be empty at this point in the code. */
168
                xReturned = xQueueSend( xQueue, &ulValueToSend, 0U );
169
                configASSERT( xReturned == pdPASS );
170
        }
171
}
172
/*-----------------------------------------------------------*/
173
 
174
static void prvQueueReceiveTask( void *pvParameters )
175
{
176
unsigned long ulReceivedValue;
177
const unsigned long ulExpectedValue = 100UL;
178
const char * const pcPassMessage = "Blink\r\n";
179
const char * const pcFailMessage = "Unexpected value received\r\n";
180
extern void vSendString( const char * const pcString );
181
extern void vToggleLED( void );
182
 
183
        /* Remove compiler warning about unused parameter. */
184
        ( void ) pvParameters;
185
 
186
        for( ;; )
187
        {
188
                /* Wait until something arrives in the queue - this task will block
189
                indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
190
                FreeRTOSConfig.h. */
191
                xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
192
 
193
                /*  To get here something must have been received from the queue, but
194
                is it the expected value?  If it is, toggle the LED. */
195
                if( ulReceivedValue == ulExpectedValue )
196
                {
197
                        vSendString( pcPassMessage );
198
                        vToggleLED();
199
                        ulReceivedValue = 0U;
200
                }
201
                else
202
                {
203
                        vSendString( pcFailMessage );
204
                }
205
        }
206
}
207
/*-----------------------------------------------------------*/
208
 
209
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.