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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/*
* Copyright (c) 2006-2020, YICHIP Development Team
* @file yc_gpio.c
* @brief source file for setting gpio
*
* Change Logs:
* Date Author Version Notes
* 2021-01-20 yangzhengfeng V1.0.2 Update library function
* 2021-07-29 xubo V1.0.3 Update library function
*/
#include "yc_gpio.h"
uint8_t const UnMapTb[256] = {
0u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x00 to 0x0F */
4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x10 to 0x1F */
5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x20 to 0x2F */
4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u,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_t hx = x >> 8;
if(lx)
{
return UnMapTb[lx];
}
else
{
return UnMapTb[hx] + 8;
}
}
/**
* @method GPIO_Config
* @brief config gpio function(Only one can be configured at a time)
* @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
* @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)(Only one can be configured at a time)
* @param function:gpio function
* @retval none
*/
void GPIO_Config(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin, GPIO_FUN_TYPEDEF function)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
_ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
_ASSERT(IS_GPIO_FUN(function));
MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, GPIO_Pin)] = function;
}
/**
* @method GPIO_Init
* @brief gpio mode Init
* @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
* @param GPIO_InitStruct:GPIO_InitStruct
* @retval none
*/
void GPIO_Init(GPIO_TypeDef GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
_ASSERT(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
_ASSERT(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
for(uint8_t i = 0; i < GPIO_PIN_NUM; i++)
{
if(GPIO_InitStruct->GPIO_Pin & (BIT0<<i))
{
MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, (BIT0<<i))] = GPIO_InitStruct->GPIO_Mode << 6;
}
}
}
/**
* @method GPIO_PullUpCmd
* @brief gpio pull up
* @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
* @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_7)
* @param NewState: new state of the port pin Pull Up.(ENABLE or DISABLE)
* @retval none
*/
void GPIO_PullUpCmd(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin, FunctionalState NewState)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
_ASSERT(IS_GPIO_PIN(GPIO_Pin));
_ASSERT(IS_FUNCTIONAL_STATE(NewState));
uint8_t i = 0;
if (ENABLE == NewState)
{
for(i = 0; i<GPIO_PIN_NUM; i++)
{
MGPIO->CTRL.bit[GPIO_GetNum(GPIOx,(GPIO_Pin_TypeDef)(BIT0 << i))].MODE = GPIO_Mode_IPU;
}
}
else if (DISABLE == NewState)
{
for(i = 0; i<GPIO_PIN_NUM; i++)
{
MGPIO->CTRL.bit[GPIO_GetNum(GPIOx, (GPIO_Pin_TypeDef)(BIT0 << i))].MODE = GPIO_Mode_IN_FLOATING;
}
}
}
/**
* @method GPIO_ReadInputData
* @brief Reads the GPIO input data for 2byte.
* @param GPIOx_IN: where x can be (GPIOA_IN...GPIOF_IN) to select the GPIO group.
* @retval GPIO input data.
*/
uint16_t GPIO_ReadInputData(GPIO_TypeDef GPIOx)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
return MGPIO->IN_LEVEL.reg[GPIOx];
}
/**
* @method GPIO_ReadInputDataBit
* @brief Reads the GPIO input data(status) for bit.
* @param GPIOx_IN: where x can be (GPIOA_IN...GPIOF_IN) to select the GPIO group.
* @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
* @retval The input bit
*/
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
_ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
if (((MGPIO->IN_LEVEL.reg[GPIOx]) & GPIO_Pin) != (uint32_t)Bit_RESET)
{
return (uint8_t)Bit_SET;
}
return (uint8_t)Bit_RESET;
}
/**
* @method GPIO_ReadOutputData
* @brief Reads the GPIO output data(status) for byte.
* @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
* @retval GPIO output data(status).
*/
uint16_t GPIO_ReadOutputData(GPIO_TypeDef GPIOx)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
return MGPIO->IN_LEVEL.reg[GPIOx];
}
/**
* @method GPIO_ReadOutputDataBit
* @brief Reads the GPIO output data(status) for bit.
* @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
* @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
* @retval The output status
*/
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
_ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
if (((MGPIO->IN_LEVEL.reg[GPIOx]) & GPIO_Pin) != (uint32_t)Bit_RESET)
{
return (uint8_t)Bit_SET;
}
return (uint8_t)Bit_RESET;
}
/**
* @method GPIO_ResetBit
* @brief Reset the GPIO bit data(status) for bit.
* @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
* @param GPIO_Pin: select the pin to reset.(GPIO_Pin_0...GPIO_Pin_15)
* @retval none
*/
void GPIO_ResetBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
_ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, GPIO_Pin)] = OUTPUT_LOW;
}
/**
* @method GPIO_ResetBits
* @brief Reset the GPIO bit data(status) for bit.
* @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
* @param GPIO_Pin: select the pin to reset.(GPIO_Pin_0...GPIO_Pin_15)
* @retval none
*/
void GPIO_ResetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
_ASSERT(IS_GPIO_PIN(GPIO_Pin));
for(uint8_t i = 0; i < GPIO_PIN_NUM; i++)
{
if(GPIO_Pin & (BIT0<<i))
{
MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, (BIT0<<i))] = OUTPUT_LOW;
}
}
}
/**
* @method GPIO_SetBit
* @brief Set the GPIO bit data(status) for bit.
* @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
* @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
* @retval none
*/
void GPIO_SetBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
_ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, GPIO_Pin)] = OUTPUT_HIGH;
}
/**
* @method GPIO_SetBits
* @brief Set the GPIO bit data(status) for bit.
* @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
* @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
* @retval none
*/
void GPIO_SetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
_ASSERT(IS_GPIO_PIN(GPIO_Pin));
for(uint8_t i = 0; i < GPIO_PIN_NUM; i++)
{
if(GPIO_Pin & (BIT0<<i))
{
MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, (BIT0<<i))] = OUTPUT_HIGH;
}
}
}
/**
* @method GPIO_Write
* @brief Write the GPIO group data(status) for bit.
* @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
* @param value: select the value to read.(0 or 1)
* @retval none
*/
void GPIO_Write(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
_ASSERT(IS_GPIO_PIN(GPIO_Pin));
GPIO_SetBits(GPIOx, GPIO_Pin);
GPIO_ResetBits(GPIOx, (GPIO_Pin_TypeDef)(~GPIO_Pin));
}
/**
* @method GPIO_WriteBit
* @brief Write the GPIO bit data(status) for bit.
* @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
* @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
* @param BitVal: select the value to read.(0 or 1)
* @retval none
*/
void GPIO_WriteBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin, BitAction BitVal)
{
_ASSERT(IS_GPIO_PORT(GPIOx));
_ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
if (BitVal == Bit_SET)
GPIO_SetBit(GPIOx, GPIO_Pin);
else if (BitVal == Bit_RESET)
GPIO_ResetBit(GPIOx, GPIO_Pin);
}
/**
* @method GPIO_ODSet
* @brief Set the GPIO OD MODE
* @param GPIOx_Drv: where x can be (GPIOA_Drv...GPIOE_Drv) to select the GPIO_Drv group.
* @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
* @param Drvtype: select the value to set DRV value.(0x00....0x11)
* @retval none
*/
void GPIO_ODSet(GPIO_OD_TypeDef GPIOx_OD, GPIO_ODTypeDef GPIO_OD_Set)
{
_ASSERT(IS_GPIO_OD(GPIOx_OD));
_ASSERT(IS_GPIO_MODE_OUT(GPIO_OD_Set));
(MGPIO->OD_CTRL.reg) |= (GPIO_OD_Set << GPIOx_OD);
}
/************************ (C) COPYRIGHT Yichip Microelectronics *****END OF FILE****/
|