-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBlinky.c
More file actions
124 lines (109 loc) · 4.92 KB
/
Blinky.c
File metadata and controls
124 lines (109 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*---------------------------------------------------------------------------
* Copyright (c) 2025 Arm Limited (or its affiliates). All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Name: Blinky.c
* Purpose: RTX example program
*
*---------------------------------------------------------------------------*/
#include "main.h"
#include <stdio.h>
#include "cmsis_os2.h"
#include "cmsis_vio.h"
static osThreadId_t tid_thrLED; // Thread id of thread: LED
static osThreadId_t tid_thrButton; // Thread id of thread: Button
volatile uint32_t g_ledSet = 0; // Variable to store virtual LED value:
// 0 = LED0 off, LED1 off
// 1 = LED0 on, LED1 off
// 2 = LED0 off, LED1 on
// 3 = LED0 on, LED1 on
// Create thread attribute to show thread name in the XRTOS viewer:
const osThreadAttr_t app_main_attr = {.name = "MainThread"};
const osThreadAttr_t thrLED_attr = {.name = "LEDThread", .stack_size = 768U}; // LED thread requires more stack than default
const osThreadAttr_t thrButton_attr = {.name = "ButtonThread"};
/*---------------------------------------------------------------------------
thrLED: blink LED
*---------------------------------------------------------------------------*/
__NO_RETURN static void thrLED (void *arg) {
uint32_t active_flag = 0U;
(void)arg;
for (;;) {
if (osThreadFlagsWait(1U, osFlagsWaitAny, 0U) == 1U) {
active_flag ^= 1U;
}
if (active_flag == 1U) {
vioSetSignal(vioLED0, vioLEDoff); // Switch LED0 off
vioSetSignal(vioLED1, vioLEDon); // Switch LED1 on
g_ledSet = 2; // set global variable to '2'
printf("LED0 Off, LED1 On\r\n"); // Print message
osDelay(100U); // Delay 100 ms
vioSetSignal(vioLED0, vioLEDon); // Switch LED0 on
vioSetSignal(vioLED1, vioLEDoff); // Switch LED1 off
g_ledSet = 1; // set global variable to '1'
printf("LED0 On, LED1 Off\r\n"); // Print message
osDelay(100U); // Delay 100 ms
}
else {
vioSetSignal(vioLED0, vioLEDon); // Switch LED0 on
g_ledSet = 1; // set global variable to '1'
printf("LED0 On\r\n"); // Print message
osDelay(500U); // Delay 500 ms
vioSetSignal(vioLED0, vioLEDoff); // Switch LED0 off
g_ledSet = 0; // set global variable to '0'
printf("LED0 Off\r\n"); // Print message
osDelay(500U); // Delay 500 ms
}
}
}
/*---------------------------------------------------------------------------
thrButton: check Button state
*---------------------------------------------------------------------------*/
__NO_RETURN static void thrButton (void *arg) {
uint32_t last = 0U;
uint32_t state;
(void)arg;
for (;;) {
state = (vioGetSignal(vioBUTTON0)); // Get pressed Button state
if (state != last) {
if (state == 1U) {
osThreadFlagsSet(tid_thrLED, 1U); // Set flag for thrLED
}
last = state;
}
osDelay(100U);
}
}
/*---------------------------------------------------------------------------
* Application main thread
*---------------------------------------------------------------------------*/
static void app_main_thread (void *argument) {
(void)argument;
tid_thrLED = osThreadNew(thrLED, NULL, &thrLED_attr); // Create LED thread
if (tid_thrLED == NULL) { /* add error handling */ }
tid_thrButton = osThreadNew(thrButton, NULL, &thrButton_attr); // Create Button thread
if (tid_thrButton == NULL) { /* add error handling */ }
for (;;) { // Loop forever
}
}
/*-----------------------------------------------------------------------------
* Application initialization
*----------------------------------------------------------------------------*/
int app_main (void) {
osKernelInitialize(); /* Initialize CMSIS-RTOS2 */
osThreadNew(app_main_thread, NULL, &app_main_attr);
osKernelStart(); /* Start thread execution */
return 0;
}