1 /*
2 * Copyright (c) 2006-2020, YICHIP Development Team
3 * @file yc_gpio.c
4 * @brief source file for setting gpio
5 *
6 * Change Logs:
7 * Date Author Version Notes
8 * 2021-01-20 yangzhengfeng V1.0.2 Update library function
9 * 2021-07-29 xubo V1.0.3 Update library function
10 */
11
12 #include "yc_gpio.h"
13
14 uint8_t const UnMapTb[256] = {
15 0u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x00 to 0x0F */
16 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x10 to 0x1F */
17 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x20 to 0x2F */
18 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x30 to 0x3F */
19 6u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x40 to 0x4F */
20 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x50 to 0x5F */
21 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x60 to 0x6F */
22 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x70 to 0x7F */
23 7u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x80 to 0x8F */
24 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x90 to 0x9F */
25 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xA0 to 0xAF */
26 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xB0 to 0xBF */
27 6u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xC0 to 0xCF */
28 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xD0 to 0xDF */
29 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xE0 to 0xEF */
30 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u /* 0xF0 to 0xFF */
31 };
32
UnMap(uint16_t x)33 uint8_t UnMap(uint16_t x)
34 {
35 uint8_t lx = x;
36 uint8_t hx = x >> 8;
37 if(lx)
38 {
39 return UnMapTb[lx];
40 }
41 else
42 {
43 return UnMapTb[hx] + 8;
44 }
45 }
46
47 /**
48 * @method GPIO_Config
49 * @brief config gpio function(Only one can be configured at a time)
50 * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
51 * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)(Only one can be configured at a time)
52 * @param function:gpio function
53 * @retval none
54 */
GPIO_Config(GPIO_TypeDef GPIOx,GPIO_Pin_TypeDef GPIO_Pin,GPIO_FUN_TYPEDEF function)55 void GPIO_Config(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin, GPIO_FUN_TYPEDEF function)
56 {
57 _ASSERT(IS_GPIO_PORT(GPIOx));
58 _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
59 _ASSERT(IS_GPIO_FUN(function));
60
61 MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, GPIO_Pin)] = function;
62 }
63
64 /**
65 * @method GPIO_Init
66 * @brief gpio mode Init
67 * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
68 * @param GPIO_InitStruct:GPIO_InitStruct
69 * @retval none
70 */
GPIO_Init(GPIO_TypeDef GPIOx,GPIO_InitTypeDef * GPIO_InitStruct)71 void GPIO_Init(GPIO_TypeDef GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
72 {
73 _ASSERT(IS_GPIO_PORT(GPIOx));
74 _ASSERT(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
75 _ASSERT(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
76
77 for(uint8_t i = 0; i < GPIO_PIN_NUM; i++)
78 {
79 if(GPIO_InitStruct->GPIO_Pin & (BIT0<<i))
80 {
81 MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, (BIT0<<i))] = GPIO_InitStruct->GPIO_Mode << 6;
82 }
83 }
84 }
85
86 /**
87 * @method GPIO_PullUpCmd
88 * @brief gpio pull up
89 * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
90 * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_7)
91 * @param NewState: new state of the port pin Pull Up.(ENABLE or DISABLE)
92 * @retval none
93 */
GPIO_PullUpCmd(GPIO_TypeDef GPIOx,GPIO_Pin_TypeDef GPIO_Pin,FunctionalState NewState)94 void GPIO_PullUpCmd(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin, FunctionalState NewState)
95 {
96 _ASSERT(IS_GPIO_PORT(GPIOx));
97 _ASSERT(IS_GPIO_PIN(GPIO_Pin));
98 _ASSERT(IS_FUNCTIONAL_STATE(NewState));
99 uint8_t i = 0;
100
101 if (ENABLE == NewState)
102 {
103 for(i = 0; i<GPIO_PIN_NUM; i++)
104 {
105 MGPIO->CTRL.bit[GPIO_GetNum(GPIOx,(GPIO_Pin_TypeDef)(BIT0 << i))].MODE = GPIO_Mode_IPU;
106 }
107 }
108 else if (DISABLE == NewState)
109 {
110 for(i = 0; i<GPIO_PIN_NUM; i++)
111 {
112 MGPIO->CTRL.bit[GPIO_GetNum(GPIOx, (GPIO_Pin_TypeDef)(BIT0 << i))].MODE = GPIO_Mode_IN_FLOATING;
113 }
114 }
115 }
116
117 /**
118 * @method GPIO_ReadInputData
119 * @brief Reads the GPIO input data for 2byte.
120 * @param GPIOx_IN: where x can be (GPIOA_IN...GPIOF_IN) to select the GPIO group.
121 * @retval GPIO input data.
122 */
GPIO_ReadInputData(GPIO_TypeDef GPIOx)123 uint16_t GPIO_ReadInputData(GPIO_TypeDef GPIOx)
124 {
125 _ASSERT(IS_GPIO_PORT(GPIOx));
126
127 return MGPIO->IN_LEVEL.reg[GPIOx];
128 }
129
130 /**
131 * @method GPIO_ReadInputDataBit
132 * @brief Reads the GPIO input data(status) for bit.
133 * @param GPIOx_IN: where x can be (GPIOA_IN...GPIOF_IN) to select the GPIO group.
134 * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
135 * @retval The input bit
136 */
GPIO_ReadInputDataBit(GPIO_TypeDef GPIOx,GPIO_Pin_TypeDef GPIO_Pin)137 uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
138 {
139 _ASSERT(IS_GPIO_PORT(GPIOx));
140 _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
141
142 if (((MGPIO->IN_LEVEL.reg[GPIOx]) & GPIO_Pin) != (uint32_t)Bit_RESET)
143 {
144 return (uint8_t)Bit_SET;
145 }
146 return (uint8_t)Bit_RESET;
147 }
148
149 /**
150 * @method GPIO_ReadOutputData
151 * @brief Reads the GPIO output data(status) for byte.
152 * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
153 * @retval GPIO output data(status).
154 */
GPIO_ReadOutputData(GPIO_TypeDef GPIOx)155 uint16_t GPIO_ReadOutputData(GPIO_TypeDef GPIOx)
156 {
157 _ASSERT(IS_GPIO_PORT(GPIOx));
158
159 return MGPIO->IN_LEVEL.reg[GPIOx];
160 }
161
162 /**
163 * @method GPIO_ReadOutputDataBit
164 * @brief Reads the GPIO output data(status) for bit.
165 * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
166 * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
167 * @retval The output status
168 */
GPIO_ReadOutputDataBit(GPIO_TypeDef GPIOx,GPIO_Pin_TypeDef GPIO_Pin)169 uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
170 {
171 _ASSERT(IS_GPIO_PORT(GPIOx));
172 _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
173
174 if (((MGPIO->IN_LEVEL.reg[GPIOx]) & GPIO_Pin) != (uint32_t)Bit_RESET)
175 {
176 return (uint8_t)Bit_SET;
177 }
178 return (uint8_t)Bit_RESET;
179 }
180
181 /**
182 * @method GPIO_ResetBit
183 * @brief Reset the GPIO bit data(status) for bit.
184 * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
185 * @param GPIO_Pin: select the pin to reset.(GPIO_Pin_0...GPIO_Pin_15)
186 * @retval none
187 */
GPIO_ResetBit(GPIO_TypeDef GPIOx,GPIO_Pin_TypeDef GPIO_Pin)188 void GPIO_ResetBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
189 {
190 _ASSERT(IS_GPIO_PORT(GPIOx));
191 _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
192 MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, GPIO_Pin)] = OUTPUT_LOW;
193 }
194 /**
195 * @method GPIO_ResetBits
196 * @brief Reset the GPIO bit data(status) for bit.
197 * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
198 * @param GPIO_Pin: select the pin to reset.(GPIO_Pin_0...GPIO_Pin_15)
199 * @retval none
200 */
GPIO_ResetBits(GPIO_TypeDef GPIOx,uint16_t GPIO_Pin)201 void GPIO_ResetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
202 {
203 _ASSERT(IS_GPIO_PORT(GPIOx));
204 _ASSERT(IS_GPIO_PIN(GPIO_Pin));
205 for(uint8_t i = 0; i < GPIO_PIN_NUM; i++)
206 {
207 if(GPIO_Pin & (BIT0<<i))
208 {
209 MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, (BIT0<<i))] = OUTPUT_LOW;
210 }
211 }
212 }
213
214 /**
215 * @method GPIO_SetBit
216 * @brief Set the GPIO bit data(status) for bit.
217 * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
218 * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
219 * @retval none
220 */
GPIO_SetBit(GPIO_TypeDef GPIOx,GPIO_Pin_TypeDef GPIO_Pin)221 void GPIO_SetBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
222 {
223 _ASSERT(IS_GPIO_PORT(GPIOx));
224 _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
225 MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, GPIO_Pin)] = OUTPUT_HIGH;
226 }
227
228 /**
229 * @method GPIO_SetBits
230 * @brief Set the GPIO bit data(status) for bit.
231 * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
232 * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
233 * @retval none
234 */
GPIO_SetBits(GPIO_TypeDef GPIOx,uint16_t GPIO_Pin)235 void GPIO_SetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
236 {
237 _ASSERT(IS_GPIO_PORT(GPIOx));
238 _ASSERT(IS_GPIO_PIN(GPIO_Pin));
239 for(uint8_t i = 0; i < GPIO_PIN_NUM; i++)
240 {
241 if(GPIO_Pin & (BIT0<<i))
242 {
243 MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, (BIT0<<i))] = OUTPUT_HIGH;
244 }
245 }
246 }
247
248 /**
249 * @method GPIO_Write
250 * @brief Write the GPIO group data(status) for bit.
251 * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
252 * @param value: select the value to read.(0 or 1)
253 * @retval none
254 */
GPIO_Write(GPIO_TypeDef GPIOx,GPIO_Pin_TypeDef GPIO_Pin)255 void GPIO_Write(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
256 {
257 _ASSERT(IS_GPIO_PORT(GPIOx));
258 _ASSERT(IS_GPIO_PIN(GPIO_Pin));
259 GPIO_SetBits(GPIOx, GPIO_Pin);
260 GPIO_ResetBits(GPIOx, (GPIO_Pin_TypeDef)(~GPIO_Pin));
261 }
262
263 /**
264 * @method GPIO_WriteBit
265 * @brief Write the GPIO bit data(status) for bit.
266 * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
267 * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
268 * @param BitVal: select the value to read.(0 or 1)
269 * @retval none
270 */
GPIO_WriteBit(GPIO_TypeDef GPIOx,GPIO_Pin_TypeDef GPIO_Pin,BitAction BitVal)271 void GPIO_WriteBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin, BitAction BitVal)
272 {
273 _ASSERT(IS_GPIO_PORT(GPIOx));
274 _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
275
276 if (BitVal == Bit_SET)
277 GPIO_SetBit(GPIOx, GPIO_Pin);
278 else if (BitVal == Bit_RESET)
279 GPIO_ResetBit(GPIOx, GPIO_Pin);
280 }
281
282
283 /**
284 * @method GPIO_ODSet
285 * @brief Set the GPIO OD MODE
286 * @param GPIOx_Drv: where x can be (GPIOA_Drv...GPIOE_Drv) to select the GPIO_Drv group.
287 * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
288 * @param Drvtype: select the value to set DRV value.(0x00....0x11)
289 * @retval none
290 */
GPIO_ODSet(GPIO_OD_TypeDef GPIOx_OD,GPIO_ODTypeDef GPIO_OD_Set)291 void GPIO_ODSet(GPIO_OD_TypeDef GPIOx_OD, GPIO_ODTypeDef GPIO_OD_Set)
292 {
293 _ASSERT(IS_GPIO_OD(GPIOx_OD));
294 _ASSERT(IS_GPIO_MODE_OUT(GPIO_OD_Set));
295
296 (MGPIO->OD_CTRL.reg) |= (GPIO_OD_Set << GPIOx_OD);
297 }
298 /************************ (C) COPYRIGHT Yichip Microelectronics *****END OF FILE****/
299