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
|
/* Board includes */
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "semphr.h"
#define BLOCK_
void ToggleLED_Timer(void*);
void DetectButtonPress(void*);
void ToggleLED_IPC(void*);
void initHW();
xQueueHandle pbq;
int main(void)
{
initHW();
/* Create IPC variables */
pbq = xQueueCreate(10, sizeof(int));
if (pbq == 0) {
while(1); /* fatal error */
}
/* Create tasks */
xTaskCreate(
ToggleLED_Timer, /* Function pointer */
"Task1", /* Task name - for debugging only*/
configMINIMAL_STACK_SIZE, /* Stack depth in words */
(void*) NULL, /* Pointer to tasks arguments (parameter) */
tskIDLE_PRIORITY + 2UL, /* Task priority*/
NULL /* Task handle */
);
xTaskCreate(
DetectButtonPress,
"Task2",
configMINIMAL_STACK_SIZE,
(void*) NULL,
tskIDLE_PRIORITY + 2UL,
NULL);
xTaskCreate(
ToggleLED_IPC,
"Task3",
configMINIMAL_STACK_SIZE,
(void*) NULL,
tskIDLE_PRIORITY + 2UL,
NULL);
/* Start the RTOS Scheduler */
vTaskStartScheduler();
/* HALT */
while(1);
}
/* TASK 1: Toggle LED via RTOS Timer */
void ToggleLED_Timer(void *pvParameters){
while (1) {
GPIO_ToggleBits(GPIOD, GPIO_Pin_12);
/*
Delay for a period of time. vTaskDelay() places the task into
the Blocked state until the period has expired.
The delay period is spacified in 'ticks'. We can convert
yhis in milisecond with the constant portTICK_RATE_MS.
*/
vTaskDelay(1500 / portTICK_RATE_MS);
}
}
/* TASK 2: Detect Button Press
* And Signal Event via Inter-Process Communication (IPC)
*/
void DetectButtonPress(void *pvParameters){
int sig = 1;
while (1) {
/* Detect Button Press */
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)>0) {
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)>0)
vTaskDelay(100 / portTICK_RATE_MS); /* Button Debounce Delay */
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0)
vTaskDelay(100 / portTICK_RATE_MS); /* Button Debounce Delay */
xQueueSendToBack(pbq, &sig, 0); /* Send Message */
}
}
}
/* TASK 3: Toggle LED via Inter-Process Communication (IPC) */
void ToggleLED_IPC(void *pvParameters) {
int sig;
portBASE_TYPE status;
while (1) {
status = xQueueReceive(pbq, &sig, portMAX_DELAY); /* Receive Message */
/* portMAX_DELAY blocks task indefinitely if queue is empty */
if(status == pdTRUE) {
GPIO_ToggleBits(GPIOD,GPIO_Pin_14);
}
}
}
/* Init HW */
void initHW()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure2;
// Init LED
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
// Init PushButton
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure2);
}
|