-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgp_drive.c
119 lines (95 loc) · 1.97 KB
/
gp_drive.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
//// Library used to setup GPIOs for more details, visit:
https://youtu.be/e0JfbZEditA
*/
#include "stm32f10x.h"
#include "gp_drive.h"
void init_GP(unsigned short PORT,unsigned short PIN,unsigned short DIR,unsigned short OPT)
{
volatile unsigned long * CR;
unsigned short tPin = PIN;
unsigned short offset = 0x00;
if(PIN > 7)
{
tPin -=8;
offset = 0x01;
}
if(PORT == 1)
{
RCC_APBENR |= 0x4;
CR = (volatile unsigned long *)(&GPIO_A + offset);
}
else if(PORT == 2)
{
RCC_APBENR |= 0x8;
CR = (volatile unsigned long *)(&GPIO_B+ offset);
}
else if(PORT == 3)
{
RCC_APBENR |= 0x10;
CR = (volatile unsigned long *)(&GPIO_C + offset);
}
*CR &= ~(0xf<<(tPin)*4);
*CR |= ((DIR<<(tPin*4)) | (OPT<<(tPin*4+2)));
}
int R_GP(unsigned short PORT,unsigned short pin)
{
volatile unsigned long * IDR;
unsigned long offset = 0x02;
int state;
if(PORT == 1)
{
IDR = (volatile unsigned long *)(&GPIO_A + offset);
}
else if(PORT == 2)
{
IDR = (volatile unsigned long *)(&GPIO_B+ offset);
}
else if(PORT == 3)
{
IDR = (volatile unsigned long *)(&GPIO_C+ offset);
}
state = ((*IDR & (1<<pin))>>pin) ;
return state;
}
void W_GP(unsigned short PORT,unsigned short pin, unsigned short STATUS)
{
volatile unsigned long * ODR;
unsigned long offset = 0x03;
if(PORT == 1)
{
ODR = (volatile unsigned long *)(&GPIO_A + offset);
}
else if(PORT == 2)
{
ODR = (volatile unsigned long *)(&GPIO_B+ offset);
}
else if(PORT == 3)
{
ODR = (volatile unsigned long *)(&GPIO_C+ offset);
}
STATUS ? (*ODR |= (STATUS<<pin)) : (*ODR &= ~(1<<pin));
}
void toggle_GP(unsigned short Port,unsigned short pin)
{
if(R_GP(Port,pin))
{
W_GP(Port,pin,0);
}
else
{
W_GP(Port,pin,1);
}
}
void PINc(unsigned short pin, unsigned short STATUS)
{
STATUS ? (GPIOC->ODR |= (STATUS<<pin)) : (GPIOC->ODR &= ~(1<<pin));
}
void B_init(void)
{
init_GP(PC,13,OUT50,O_GP_PP);
}
void BLED(unsigned short state)
{
W_GP(PC,13,state);
}