1 /**
2   *********************************************************************************
3   *
4   * @file    ald_gpio.h
5   * @brief   Header file of GPIO module driver
6   *
7   * @version V1.0
8   * @date    07 Nov 2017
9   * @author  AE Team
10   * @note
11   *
12   * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
13   *
14   * SPDX-License-Identifier: Apache-2.0
15   *
16   * Licensed under the Apache License, Version 2.0 (the License); you may
17   * not use this file except in compliance with the License.
18   * You may obtain a copy of the License at
19   *
20   * www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing, software
23   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25   * See the License for the specific language governing permissions and
26   * limitations under the License.
27   *
28   *********************************************************************************
29   */
30 
31 #ifndef __ALD_GPIO_H__
32 #define __ALD_GPIO_H__
33 
34 #ifdef __cplusplus
35  extern "C" {
36 #endif
37 
38 #include "utils.h"
39 
40 
41 /** @addtogroup ES32FXXX_ALD
42   * @{
43   */
44 
45 /** @addtogroup GPIO
46   * @{
47   */
48 
49 /**
50   * @defgroup GPIO_Public_Macros GPIO Public Macros
51   * @{
52   */
53 #define GPIO_PIN_0	(1U << 0)
54 #define GPIO_PIN_1	(1U << 1)
55 #define GPIO_PIN_2	(1U << 2)
56 #define GPIO_PIN_3	(1U << 3)
57 #define GPIO_PIN_4	(1U << 4)
58 #define GPIO_PIN_5	(1U << 5)
59 #define GPIO_PIN_6	(1U << 6)
60 #define GPIO_PIN_7	(1U << 7)
61 #define GPIO_PIN_8	(1U << 8)
62 #define GPIO_PIN_9	(1U << 9)
63 #define GPIO_PIN_10	(1U << 10)
64 #define GPIO_PIN_11	(1U << 11)
65 #define GPIO_PIN_12	(1U << 12)
66 #define GPIO_PIN_13	(1U << 13)
67 #define GPIO_PIN_14	(1U << 14)
68 #define GPIO_PIN_15	(1U << 15)
69 #define GPIO_PIN_ALL	(0xFFFFU)
70 /**
71   * @}
72   */
73 
74 /**
75   * @defgroup GPIO_Public_Types GPIO Public Types
76   * @{
77   */
78 
79 /**
80   * @brief GPIO mode
81   */
82 typedef enum {
83 	GPIO_MODE_CLOSE  = 0x0U,	/**< Digital close  Analog open */
84 	GPIO_MODE_INPUT  = 0x1U,	/**< Input */
85 	GPIO_MODE_OUTPUT = 0x2U,	/**< Output */
86 } gpio_mode_t;
87 
88 /**
89   * @brief GPIO open-drain or push-pull
90   */
91 typedef enum {
92 	GPIO_PUSH_PULL   = 0x0U,	/**< Push-Pull */
93 	GPIO_OPEN_DRAIN  = 0x2U,	/**< Open-Drain. Can't output high level */
94 	GPIO_OPEN_SOURCE = 0x3U,	/**< Open-Source. Can't output low level */
95 } gpio_odos_t;
96 
97 /**
98   * @brief GPIO push-up or push-down
99   */
100 typedef enum {
101 	GPIO_FLOATING     = 0x0U,	/**< Floating */
102 	GPIO_PUSH_UP      = 0x1U,	/**< Push-Up */
103 	GPIO_PUSH_DOWN    = 0x2U,	/**< Push-Down */
104 	GPIO_PUSH_UP_DOWN = 0x3U,	/**< Push-Up and Push-Down */
105 } gpio_push_t;
106 
107 /**
108   * @brief GPIO output drive
109   */
110 typedef enum {
111 	GPIO_OUT_DRIVE_NORMAL = 0x0U,	/**< Normal current flow */
112 	GPIO_OUT_DRIVE_STRONG = 0x1U,	/**< Strong current flow */
113 } gpio_out_drive_t;
114 
115 /**
116   * @brief GPIO filter
117   */
118 typedef enum {
119 	GPIO_FILTER_DISABLE = 0x0U,	/**< Disable filter */
120 	GPIO_FILTER_ENABLE  = 0x1U,	/**< Enable filter */
121 } gpio_filter_t;
122 
123 /**
124   * @brief GPIO type
125   */
126 typedef enum {
127 	GPIO_TYPE_CMOS = 0x0U,	/**< CMOS Type */
128 	GPIO_TYPE_TTL  = 0x1U,	/**< TTL Type */
129 } gpio_type_t;
130 
131 /**
132   * @brief GPIO functions
133   */
134 typedef enum {
135 	GPIO_FUNC_0 = 0U,	/**< function #0 */
136 	GPIO_FUNC_1 = 1U,	/**< function #1 */
137 	GPIO_FUNC_2 = 2U,	/**< function #2 */
138 	GPIO_FUNC_3 = 3U,	/**< function #3 */
139 	GPIO_FUNC_4 = 4U,	/**< function #4 */
140 	GPIO_FUNC_5 = 5U,	/**< function #5 */
141 	GPIO_FUNC_6 = 6U,	/**< function #6 */
142 	GPIO_FUNC_7 = 7U,	/**< function #7 */
143 } gpio_func_t;
144 
145 
146 /**
147   * @brief GPIO Init Structure definition
148   */
149 typedef struct {
150 	gpio_mode_t mode;	/**< Specifies the operating mode for the selected pins.
151 				     This parameter can be any value of @ref gpio_mode_t */
152 	gpio_odos_t odos;	/**< Specifies the Open-Drain or Push-Pull for the selected pins.
153 				     This parameter can be a value of @ref gpio_odos_t */
154 	gpio_push_t pupd;	/**< Specifies the Pull-up or Pull-Down for the selected pins.
155 				     This parameter can be a value of @ref gpio_push_t */
156 	gpio_out_drive_t odrv;	/**< Specifies the output driver for the selected pins.
157 				     This parameter can be a value of @ref gpio_out_drive_t */
158 	gpio_filter_t flt;	/**< Specifies the input filter for the selected pins.
159 				     This parameter can be a value of @ref gpio_filter_t */
160 	gpio_type_t type;	/**< Specifies the type for the selected pins.
161 				     This parameter can be a value of @ref gpio_type_t */
162 	gpio_func_t func;	/**< Specifies the function for the selected pins.
163 				     This parameter can be a value of @ref gpio_func_t */
164 } gpio_init_t;
165 
166 /**
167   * @brief EXTI trigger style
168   */
169 typedef enum {
170 	EXTI_TRIGGER_RISING_EDGE   = 0U,	/**< Rising edge trigger */
171 	EXTI_TRIGGER_TRAILING_EDGE = 1U,	/**< Trailing edge trigger */
172 	EXTI_TRIGGER_BOTH_EDGE     = 2U,	/**< Rising and trailing edge trigger */
173 } exti_trigger_style_t;
174 
175 /**
176   * @brief EXTI filter clock select
177   */
178 typedef enum {
179 	EXTI_FILTER_CLOCK_10K = 0U,	/**< cks = 10KHz */
180 	EXTI_FILTER_CLOCK_32K = 1U,	/**< cks = 32KHz */
181 } exti_filter_clock_t;
182 
183 /**
184   * @brief EXTI Init Structure definition
185   */
186 typedef struct {
187 	type_func_t filter;		/**< Enable filter. */
188 	exti_filter_clock_t cks;	/**< Filter clock select. */
189 	uint8_t filter_time;		/**< Filter duration */
190 } exti_init_t;
191 /**
192   * @}
193   */
194 
195 /**
196   * @defgroup GPIO_Private_Macros GPIO Private Macros
197   * @{
198   */
199 #define PIN_MASK	0xFFFFU
200 #define UNLOCK_KEY	0x55AAU
201 
202 #define IS_GPIO_PIN(x)	((((x) & (uint16_t)0x00) == 0) && ((x) != (uint16_t)0x0))
203 #define IS_GPIO_PORT(GPIOx)	((GPIOx == GPIOA) || \
204 				 (GPIOx == GPIOB) || \
205 				 (GPIOx == GPIOC) || \
206 				 (GPIOx == GPIOD) || \
207 				 (GPIOx == GPIOE) || \
208 				 (GPIOx == GPIOF) || \
209 				 (GPIOx == GPIOG) || \
210 				 (GPIOx == GPIOH))
211 #define IS_GPIO_MODE(x)		(((x) == GPIO_MODE_CLOSE) || \
212                                  ((x) == GPIO_MODE_INPUT) || \
213                                  ((x) == GPIO_MODE_OUTPUT))
214 #define IS_GPIO_ODOS(x)		(((x) == GPIO_PUSH_PULL)  || \
215                                  ((x) == GPIO_OPEN_DRAIN) || \
216                                  ((x) == GPIO_OPEN_SOURCE))
217 #define IS_GPIO_PUPD(x)		(((x) == GPIO_FLOATING)  || \
218                                  ((x) == GPIO_PUSH_UP)   || \
219                                  ((x) == GPIO_PUSH_DOWN) || \
220                                  ((x) == GPIO_PUSH_UP_DOWN))
221 #define IS_GPIO_ODRV(x)		(((x) == GPIO_OUT_DRIVE_NORMAL) || \
222                                  ((x) == GPIO_OUT_DRIVE_STRONG))
223 #define IS_GPIO_FLT(x)		(((x) == GPIO_FILTER_DISABLE) || \
224                                  ((x) == GPIO_FILTER_ENABLE))
225 #define IS_GPIO_TYPE(x)		(((x) == GPIO_TYPE_TTL) || \
226                                  ((x) == GPIO_TYPE_CMOS))
227 #define IS_TRIGGER_STYLE(x)	(((x) == EXTI_TRIGGER_RISING_EDGE)   || \
228                                  ((x) == EXTI_TRIGGER_TRAILING_EDGE) || \
229                                  ((x) == EXTI_TRIGGER_BOTH_EDGE))
230 #define IS_EXTI_FLTCKS_TYPE(x)	(((x) == EXTI_FILTER_CLOCK_10K) || \
231                                  ((x) == EXTI_FILTER_CLOCK_32K))
232 #define IS_GPIO_FUNC(x)		((x) <= 7)
233 /**
234   * @}
235   */
236 
237 /** @addtogroup GPIO_Public_Functions
238   * @{
239   */
240 
241 /** @addtogroup GPIO_Public_Functions_Group1
242   * @{
243   */
244 void ald_gpio_init(GPIO_TypeDef *GPIOx, uint16_t pin, gpio_init_t *init);
245 void ald_gpio_init_default(GPIO_TypeDef *GPIOx, uint16_t pin);
246 void ald_gpio_func_default(GPIO_TypeDef *GPIOx);
247 void ald_gpio_exti_init(GPIO_TypeDef *GPIOx, uint16_t pin, exti_init_t *init);
248 /**
249   * @}
250   */
251 
252 /** @addtogroup GPIO_Public_Functions_Group2
253   * @{
254   */
255 uint8_t ald_gpio_read_pin(GPIO_TypeDef *GPIOx, uint16_t pin);
256 void ald_gpio_write_pin(GPIO_TypeDef *GPIOx, uint16_t pin, uint8_t val);
257 void ald_gpio_toggle_pin(GPIO_TypeDef *GPIOx, uint16_t pin);
258 void ald_gpio_toggle_dir(GPIO_TypeDef *GPIOx, uint16_t pin);
259 void ald_gpio_lock_pin(GPIO_TypeDef *GPIOx, uint16_t pin);
260 uint16_t ald_gpio_read_port(GPIO_TypeDef *GPIOx);
261 void ald_gpio_write_port(GPIO_TypeDef *GPIOx, uint16_t val);
262 /**
263   * @}
264   */
265 
266 /** @addtogroup GPIO_Public_Functions_Group3
267   * @{
268   */
269 void ald_gpio_exti_interrupt_config(uint16_t pin, exti_trigger_style_t style, type_func_t status);
270 flag_status_t ald_gpio_exti_get_flag_status(uint16_t pin);
271 void ald_gpio_exti_clear_flag_status(uint16_t pin);
272 /**
273   * @}
274   */
275 
276 /**
277   * @}
278   */
279 
280 /**
281   * @}
282   */
283 
284 /**
285   * @}
286   */
287 #ifdef __cplusplus
288 }
289 #endif
290 
291 #endif /* __ALD_GPIO_H__ */
292