This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathf103rb_gpio.cpp
196 lines (166 loc) · 4.36 KB
/
f103rb_gpio.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @file f103rb_gpio.cpp
* @author ZiTe <[email protected]>
*/
#include "f103rb_gpio.hpp"
namespace F103RB
{
GPIO::GPIO(GPIO_PortPinTypeDef port_Pin,
GPIOMode_TypeDef mode,
GPIOSpeed_TypeDef speed,
bool immediatelyInit)
{
this->_Port_Pin = port_Pin;
this->_Mode = mode;
this->_Speed = speed;
if (immediatelyInit)
this->Init();
}
GPIO::GPIO(GPIO_PortPinTypeDef port_Pin,
GPIOMode_TypeDef mode,
GPIO_ValueTypeDef init_Value,
GPIOSpeed_TypeDef speed,
bool immediatelyInit)
{
this->_Port_Pin = port_Pin;
this->_Mode = mode;
this->_Speed = speed;
if (immediatelyInit)
this->Init();
if (this->Is_OutputPin())
this->Set(init_Value);
}
void GPIO::Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = this->_Mode;
GPIO_InitStructure.GPIO_Speed = this->_Speed;
GPIO_InitStructure.GPIO_Pin = this->Get_Pin();
GPIO_Init(this->Get_Port(), &GPIO_InitStructure);
}
void GPIO::Set(GPIO_ValueTypeDef value)
{
switch (value)
{
case LOW:
(this->Get_Port())->BRR |= this->Get_Pin(); // Set value LOW
break;
case HIGH:
(this->Get_Port())->BSRR |= this->Get_Pin(); // Set value HIGH
break;
}
}
void GPIO::Set(uint8_t value)
{
this->Set(this->Convert_uint8_t_to_GPIO_Value_TypeDef(value));
}
void GPIO::Toggle(void)
{
(this->Get_Port())->ODR ^= this->Get_Pin();
}
GPIO_ValueTypeDef GPIO::Get(void)
{
GPIO_ValueTypeDef value;
if (this->Is_OutputPin())
{
value = this->Get_Output();
// value = GPIO_ReadOutputDataBit(this->Get_Port(), this->Get_Pin());
}
else
{
value = this->Get_Input();
// value = GPIO_ReadInputDataBit(this->Get_Port(), this->Get_Pin());
}
return value;
}
GPIO_ValueTypeDef GPIO::Get_Input(void)
{
GPIO_ValueTypeDef value;
uint32_t IDR_value = (this->Get_Port())->IDR & this->Get_Pin();
if (IDR_value == (uint32_t)Bit_RESET)
value = LOW;
else
value = HIGH;
return value;
}
GPIO_ValueTypeDef GPIO::Get_Output(void)
{
GPIO_ValueTypeDef value;
uint32_t ODR_value = (this->Get_Port())->ODR & this->Get_Pin();
if (ODR_value == (uint32_t)Bit_RESET)
value = LOW;
else
value = HIGH;
return value;
}
GPIO_TypeDef *GPIO::Get_Port(void)
{
uint8_t u8_Port_Pin = (uint8_t)this->_Port_Pin;
if (u8_Port_Pin <= (uint8_t)PA15) // Port-A: 0~15
return GPIOA;
else if (u8_Port_Pin <= (uint8_t)PB15) // Port-B: 16~31
return GPIOB;
else if (u8_Port_Pin <= (uint8_t)PC15) // Port-C: 32~47
return GPIOC;
else if (u8_Port_Pin <= (uint8_t)PD15) // Port-D: 48~63
return GPIOD;
else if (u8_Port_Pin <= (uint8_t)PE15) // Port-E: 64~79
return GPIOE;
}
uint16_t GPIO::Get_Pin(void)
{
uint8_t offset = 0;
uint8_t u8_Port_Pin = (uint8_t)this->_Port_Pin;
if (u8_Port_Pin <= (uint8_t)PA15) // Port-A: 0~15
offset = (uint8_t)PA0;
else if (u8_Port_Pin <= (uint8_t)PB15) // Port-B: 16~31
offset = (uint8_t)PB0;
else if (u8_Port_Pin <= (uint8_t)PC15) // Port-C: 32~47
offset = (uint8_t)PC0;
else if (u8_Port_Pin <= (uint8_t)PD15) // Port-D: 48~63
offset = (uint8_t)PD0;
else if (u8_Port_Pin <= (uint8_t)PE15) // Port-E: 64~79
offset = (uint8_t)PE0;
return (uint16_t)(0x0001 << (u8_Port_Pin - offset));
}
/**
* @brief Convert uint8_t to GPIO_Value_TypeDef.
* @param value: The value in uint8_t. This parameter should be 0 or 1.
* @return The converted GPIO_Value_TypeDef value.
*/
GPIO_ValueTypeDef GPIO::Convert_uint8_t_to_GPIO_Value_TypeDef(uint8_t value)
{
GPIO_ValueTypeDef gpioValue;
if (value == 0)
{
gpioValue = (GPIO_ValueTypeDef)LOW;
}
else
{
gpioValue = (GPIO_ValueTypeDef)HIGH;
}
return gpioValue;
}
bool GPIO::Is_OutputPin()
{
bool value;
switch (this->_Mode)
{
case GPIO_Mode_Out_OD:
case GPIO_Mode_Out_PP:
case GPIO_Mode_AF_OD:
case GPIO_Mode_AF_PP:
// This GPIO is output.
value = true;
break;
case GPIO_Mode_AIN:
case GPIO_Mode_IN_FLOATING:
case GPIO_Mode_IPD:
case GPIO_Mode_IPU:
// This GPIO is input.
value = false;
break;
}
return value;
}
}