-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmutexTestmain.c
103 lines (95 loc) · 2.33 KB
/
mutexTestmain.c
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
/**
* 作者: Roy.yu
* 时间: 2016.8.23
* 版本: V0.1
* Licence: GNU GENERAL PUBLIC LICENSE
*/
#include "easyRTOS.h"
#include "easyRTOSkernel.h"
#include "easyRTOSport.h"
#include "easyRTOSTimer.h"
#include "easyRTOSSem.h"
#include "stdlib.h"
#define IDLE_STACK_SIZE_BYTES 256
#define TEST_STACK_SIZE_BYTES 512
#define SEM_STACK_SIZE_BYTES 512
NEAR static uint8_t idleTaskStack[IDLE_STACK_SIZE_BYTES];
NEAR static uint8_t testTaskStack[TEST_STACK_SIZE_BYTES];
NEAR static uint8_t semTaskStack[SEM_STACK_SIZE_BYTES];
EASYRTOS_TCB testTcb;
EASYRTOS_TCB semTcb;
EASYRTOS_SEM semSemMutex;
void testTaskFunc (uint32_t param);
void semTaskFunc (uint32_t param);
uint8_t flag=0;
int main( void )
{
ERESULT status;
//内部时钟 16M
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
status = easyRTOSInit(&idleTaskStack[0], IDLE_STACK_SIZE_BYTES);
if (status == EASYRTOS_OK)
{
/* 使能系统时钟 */
archInitSystemTickTimer();
/* 创建任务 */
status += eTaskCreat(&testTcb,
10,
testTaskFunc,
0,
&testTaskStack[0],
TEST_STACK_SIZE_BYTES,
"TEST",
1);
status += eTaskCreat(&semTcb,
9,
semTaskFunc,
1,
&semTaskStack[0],
SEM_STACK_SIZE_BYTES,
"SEM",
2);
semSemMutex = eSemCreateMutex();
if (status == EASYRTOS_OK)
{
easyRTOSStart();
}
}
return 0;
}
void testTaskFunc (uint32_t param)
{
GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_OUT_PP_HIGH_FAST);
while (1)
{
if (eSemTake (&semSemMutex, 0) == EASYRTOS_OK)
{
if (!flag)GPIO_WriteReverse(GPIOD, GPIO_PIN_2);
flag = 1;
eSemGive(&semSemMutex);
}
}
}
void semTaskFunc (uint32_t param)
{
while (1)
{
if (eSemTake(&semSemMutex, 0) == EASYRTOS_OK)
{
eTimerDelay(DELAY_S(param));
if (eSemTake(&semSemMutex, 0) == EASYRTOS_OK)
{
eTimerDelay(DELAY_S(param));
if (eSemTake(&semSemMutex, 0) == EASYRTOS_OK)
{
eTimerDelay(DELAY_S(param));
eSemGive(&semSemMutex);
}
eSemGive(&semSemMutex);
}
eSemGive(&semSemMutex);
flag=0;
}
eTimerDelay(DELAY_S(param));
}
}