-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread_LED.c
More file actions
170 lines (151 loc) · 5.81 KB
/
Thread_LED.c
File metadata and controls
170 lines (151 loc) · 5.81 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************************************
* @file Thread_LED.c
* @author Ashraf Suyyagh
* @version V1.0.0
* @date 17-January-2016
* @brief This file initializes one LED as an output, implements the LED thread
* which toggles and LED, and function which creates and starts the thread
******************************************************************************
*/
#include "cmsis_os.h" // ARM::CMSIS:RTOS:Keil RTX
#include "stm32f4xx_hal.h"
#include "my_headers.h"
void Thread_LED (void const *argument); // thread function
//osThreadId tid_Thread_LED; // thread id
osThreadDef(Thread_LED, osPriorityNormal, 1, 0);
GPIO_InitTypeDef LED_configuration;
int mode = 0;
int delayTime = 50;
uint8_t value_pwm;
/*----------------------------------------------------------------------------
* Create the thread within RTOS context
*---------------------------------------------------------------------------*/
int start_Thread_LED (void) {
tid_Thread_LED = osThreadCreate(osThread(Thread_LED ), NULL); // Start LED_Thread
if (!tid_Thread_LED) return(-1);
return(0);
}
/*----------------------------------------------------------------------------
* Thread 'LED_Thread': PWM
*---------------------------------------------------------------------------*/
uint8_t s_led_mode = 0;
void Thread_LED (void const *argument)
{
while(1)
{
if(s_led_mode == 0)
{
//value_pwm = 8;
//osSignalWait(0x01, osWaitForever);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12 ,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14 ,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
osDelay(10-8);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12 ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14 ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
osDelay(8);
//HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
}
else if(s_led_mode == 1){ //roate clockwise
rotateClockwise();
}
else if(s_led_mode == 2){
rotateCounterClockwise();
}
else if (s_led_mode == 3){
turnLEDsOff();
}
else {
//error
}
//osDelay(1000);
//HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
}
}
/*----------------------------------------------------------------------------
* Initialize the GPIO associated with the LED
*---------------------------------------------------------------------------*/
void initializeLED_IO (void){
__HAL_RCC_GPIOD_CLK_ENABLE();
LED_configuration.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
LED_configuration.Mode = GPIO_MODE_OUTPUT_PP;
LED_configuration.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
LED_configuration.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOD, &LED_configuration);
}
void rotateClockwise(void){
int count = 0;
if (count == 0){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
osDelay(delayTime);
}
else if (count == 1){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
osDelay(delayTime);
}
else if (count == 2){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
osDelay(delayTime);
}
else if (count == 3){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
osDelay(delayTime);
count = 0;
}
}
void rotateCounterClockwise(void){
int count = 0;
if (count == 0){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
osDelay(delayTime);
}
else if (count == 1){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
osDelay(delayTime);
}
else if (count == 2){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
osDelay(delayTime);
}
else if (count == 3){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
osDelay(delayTime);
count = 0;
}
}
void turnLEDsOff(){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
}
/*----------------------------------------------------------------------------
*
*---------------------------------------------------------------------------*/