1 /**
2   *********************************************************************************
3   *
4   * @file    ald_gpio.h
5   * @brief   Header file of GPIO module driver
6   *
7   * @version V1.0
8   * @date    30 Jan. 2023
9   * @author  AE Team
10   * @note
11   *          Change Logs:
12   *          Date            Author          Notes
13   *          30 Jan. 2023    Lisq            The first version
14   *
15   * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
16   *
17   * SPDX-License-Identifier: Apache-2.0
18   *
19   * Licensed under the Apache License, Version 2.0 (the License); you may
20   * not use this file except in compliance with the License.
21   * You may obtain a copy of the License at
22   *
23   * www.apache.org/licenses/LICENSE-2.0
24   *
25   * Unless required by applicable law or agreed to in writing, software
26   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
27   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28   * See the License for the specific language governing permissions and
29   * limitations under the License.
30   **********************************************************************************
31   */
32 
33 #ifndef __ALD_GPIO_H__
34 #define __ALD_GPIO_H__
35 
36 #ifdef __cplusplus
37  extern "C" {
38 #endif /* __cplusplus */
39 
40 #include "ald_utils.h"
41 
42 
43 /** @addtogroup ALD
44   * @{
45   */
46 
47 /** @addtogroup GPIO
48   * @{
49   */
50 
51 /**
52   * @defgroup GPIO_Public_Macros GPIO Public Macros
53   * @{
54   */
55 #define ALD_GPIO_PIN_0      (0x1U)
56 #define ALD_GPIO_PIN_1      (0x2U)
57 #define ALD_GPIO_PIN_2      (0x4U)
58 #define ALD_GPIO_PIN_3      (0x8U)
59 #define ALD_GPIO_PIN_4      (0x10U)
60 #define ALD_GPIO_PIN_5      (0x20U)
61 #define ALD_GPIO_PIN_6      (0x40U)
62 #define ALD_GPIO_PIN_7      (0x80U)
63 #define ALD_GPIO_PIN_8      (0x100U)
64 #define ALD_GPIO_PIN_9      (0x200U)
65 #define ALD_GPIO_PIN_10     (0x400U)
66 #define ALD_GPIO_PIN_11     (0x800U)
67 #define ALD_GPIO_PIN_12     (0x1000U)
68 #define ALD_GPIO_PIN_13     (0x2000U)
69 #define ALD_GPIO_PIN_14     (0x4000U)
70 #define ALD_GPIO_PIN_15     (0x8000U)
71 #define ALD_GPIO_PIN_ALL    (0xFFFF)
72 
73 /* Toggle IO */
74 #define ALD_GPIOA_TOGGLE_PIN(x)    (GPIOA->BIR = (x))
75 #define ALD_GPIOB_TOGGLE_PIN(x)    (GPIOB->BIR = (x))
76 #define ALD_GPIOC_TOGGLE_PIN(x)    (GPIOC->BIR = (x))
77 #define ALD_GPIOD_TOGGLE_PIN(x)    (GPIOD->BIR = (x))
78 
79 /* Read IO level */
80 #define ALD_GPIOA_READ_PIN(x)      ((GPIOA->DIN & (x)) ? 1 : 0)
81 #define ALD_GPIOB_READ_PIN(x)      ((GPIOB->DIN & (x)) ? 1 : 0)
82 #define ALD_GPIOC_READ_PIN(x)      ((GPIOC->DIN & (x)) ? 1 : 0)
83 #define ALD_GPIOD_READ_PIN(x)      ((GPIOD->DIN & (x)) ? 1 : 0)
84 
85 /* Set IO as high */
86 #define ALD_GPIOA_SET_PIN(x)       (GPIOA->BSRR = (x))
87 #define ALD_GPIOB_SET_PIN(x)       (GPIOB->BSRR = (x))
88 #define ALD_GPIOC_SET_PIN(x)       (GPIOC->BSRR = (x))
89 #define ALD_GPIOD_SET_PIN(x)       (GPIOD->BSRR = (x))
90 
91 /* Set IO as low */
92 #define ALD_GPIOA_RESET_PIN(x)     (GPIOA->BSRR = ((x) << 16))
93 #define ALD_GPIOB_RESET_PIN(x)     (GPIOB->BSRR = ((x) << 16))
94 #define ALD_GPIOC_RESET_PIN(x)     (GPIOC->BSRR = ((x) << 16))
95 #define ALD_GPIOD_RESET_PIN(x)     (GPIOD->BSRR = ((x) << 16))
96 /**
97   * @}
98   */
99 
100 /**
101   * @defgroup GPIO_Public_Types GPIO Public Types
102   * @{
103   */
104 
105 /**
106   * @brief GPIO mode
107   */
108 typedef enum {
109     ALD_GPIO_MODE_CLOSE  = 0x0U,    /**< Digital close  Analog open */
110     ALD_GPIO_MODE_INPUT  = 0x1U,    /**< Input */
111     ALD_GPIO_MODE_OUTPUT = 0x2U,    /**< Output */
112 } ald_gpio_mode_t;
113 
114 /**
115   * @brief GPIO open-drain or push-pull
116   */
117 typedef enum {
118     ALD_GPIO_PUSH_PULL   = 0x0U,    /**< Push-Pull */
119     ALD_GPIO_OPEN_DRAIN  = 0x2U,    /**< Open-Drain. Can't output high level */
120 } ald_gpio_od_t;
121 
122 /**
123   * @brief GPIO push-up or push-down
124   */
125 typedef enum {
126     ALD_GPIO_FLOATING     = 0x0U,   /**< Floating */
127     ALD_GPIO_PUSH_UP      = 0x1U,   /**< Push-Up */
128     ALD_GPIO_PUSH_DOWN    = 0x2U,   /**< Push-Down */
129     ALD_GPIO_PUSH_UP_DOWN = 0x3U,   /**< Push-Up and Push-Down */
130 } ald_gpio_push_t;
131 
132 /**
133   * @brief GPIO output drive
134   */
135 typedef enum {
136     ALD_GPIO_OUT_DRIVE_NORMAL = 0x0U,   /**< Normal current flow */
137     ALD_GPIO_OUT_DRIVE_STRONG = 0x1U,   /**< Strong current flow */
138 } ald_gpio_out_drive_t;
139 
140 /**
141   * @brief GPIO filter
142   */
143 typedef enum {
144     ALD_GPIO_FILTER_DISABLE = 0x0U, /**< Disable filter */
145     ALD_GPIO_FILTER_ENABLE  = 0x1U, /**< Enable filter */
146 } ald_gpio_filter_t;
147 
148 /**
149   * @brief GPIO type
150   */
151 typedef enum {
152     ALD_GPIO_TYPE_CMOS = 0x0U,  /**< CMOS Type */
153     ALD_GPIO_TYPE_TTL  = 0x1U,  /**< TTL Type */
154 } ald_gpio_type_t;
155 
156 /**
157   * @brief GPIO functions
158   */
159 typedef enum {
160     ALD_GPIO_FUNC_0 = 0U,   /**< function #0 */
161     ALD_GPIO_FUNC_1 = 1U,   /**< function #1 */
162     ALD_GPIO_FUNC_2 = 2U,   /**< function #2 */
163     ALD_GPIO_FUNC_3 = 3U,   /**< function #3 */
164     ALD_GPIO_FUNC_4 = 4U,   /**< function #4 */
165     ALD_GPIO_FUNC_5 = 5U,   /**< function #5 */
166     ALD_GPIO_FUNC_6 = 6U,   /**< function #6 */
167     ALD_GPIO_FUNC_7 = 7U,   /**< function #7 */
168 } ald_gpio_func_t;
169 
170 /**
171   * @brief GPIO Init Structure definition
172   */
173 typedef struct {
174     ald_gpio_mode_t mode;   /**< Specifies the operating mode for the selected pins.
175                      This parameter can be any value of @ref gpio_mode_t */
176     ald_gpio_od_t od;   /**< Specifies the Open-Drain or Push-Pull for the selected pins.
177                      This parameter can be a value of @ref gpio_od_t */
178     ald_gpio_push_t pupd;   /**< Specifies the Pull-up or Pull-Down for the selected pins.
179                      This parameter can be a value of @ref gpio_push_t */
180     ald_gpio_out_drive_t odrv;  /**< Specifies the output MOS driver for the selected pins.
181                      This parameter can be a value of @ref gpio_out_drive_t */
182     ald_gpio_filter_t flt;  /**< Specifies the input filter for the selected pins.
183                      This parameter can be a value of @ref gpio_filter_t */
184     ald_gpio_type_t type;   /**< Specifies the type for the selected pins.
185                      This parameter can be a value of @ref gpio_type_t */
186     ald_gpio_func_t func;   /**< Specifies the function for the selected pins.
187                      This parameter can be a value of @ref gpio_func_t */
188 } ald_gpio_init_t;
189 
190 /**
191   * @brief EXTI trigger style
192   */
193 typedef enum {
194     ALD_EXTI_TRIGGER_RISING_EDGE   = 0U,    /**< Rising edge trigger */
195     ALD_EXTI_TRIGGER_TRAILING_EDGE = 1U,    /**< Trailing edge trigger */
196     ALD_EXTI_TRIGGER_BOTH_EDGE     = 2U,    /**< Rising and trailing edge trigger */
197 } ald_exti_trigger_style_t;
198 
199 /**
200   * @brief EXTI Init Structure definition
201   */
202 typedef struct {
203     type_func_t filter;     /**< Enable filter. */
204     uint8_t filter_time;    /**< Filter duration */
205 } ald_exti_init_t;
206 /**
207   * @}
208   */
209 
210 /**
211   * @defgroup GPIO_Private_Macros GPIO Private Macros
212   * @{
213   */
214 #define ALD_PIN_MASK    0xFFFFU
215 #define ALD_UNLOCK_KEY  0x55AAU
216 
217 #define IS_GPIO_PIN(x)  ((((x) & (uint16_t)0x00) == 0) && ((x) != (uint16_t)0x0))
218 #define IS_GPIO_PORT(GPIOx) ((GPIOx == GPIOA) || \
219                  (GPIOx == GPIOB) || \
220                  (GPIOx == GPIOC) || \
221                  (GPIOx == GPIOD))
222 #define IS_GPIO_MODE(x)     (((x) == ALD_GPIO_MODE_CLOSE) || \
223                                  ((x) == ALD_GPIO_MODE_INPUT) || \
224                                  ((x) == ALD_GPIO_MODE_OUTPUT))
225 #define IS_GPIO_OD(x)       (((x) == ALD_GPIO_PUSH_PULL)  || \
226                                  ((x) == ALD_GPIO_OPEN_DRAIN))
227 #define IS_GPIO_PUPD(x)     (((x) == ALD_GPIO_FLOATING)  || \
228                                  ((x) == ALD_GPIO_PUSH_UP)   || \
229                                  ((x) == ALD_GPIO_PUSH_DOWN) || \
230                                  ((x) == ALD_GPIO_PUSH_UP_DOWN))
231 #define IS_GPIO_ODRV(x)     (((x) == ALD_GPIO_OUT_DRIVE_NORMAL) || \
232                                  ((x) == ALD_GPIO_OUT_DRIVE_STRONG))
233 #define IS_GPIO_FLT(x)      (((x) == ALD_GPIO_FILTER_DISABLE) || \
234                                  ((x) == ALD_GPIO_FILTER_ENABLE))
235 #define IS_GPIO_TYPE(x)     (((x) == ALD_GPIO_TYPE_TTL) || \
236                                  ((x) == ALD_GPIO_TYPE_CMOS))
237 #define IS_TRIGGER_STYLE(x) (((x) == ALD_EXTI_TRIGGER_RISING_EDGE)   || \
238                                  ((x) == ALD_EXTI_TRIGGER_TRAILING_EDGE) || \
239                                  ((x) == ALD_EXTI_TRIGGER_BOTH_EDGE))
240 #define IS_GPIO_FUNC(x)     ((x) <= 7)
241 /**
242   * @}
243   */
244 
245 /** @addtogroup GPIO_Public_Functions
246   * @{
247   */
248 
249 /** @addtogroup GPIO_Public_Functions_Group1
250   * @{
251   */
252 void ald_gpio_init(GPIO_TypeDef *GPIOx, uint16_t pin, ald_gpio_init_t *init);
253 void ald_gpio_init_default(GPIO_TypeDef *GPIOx, uint16_t pin);
254 void ald_gpio_func_default(GPIO_TypeDef *GPIOx);
255 void ald_gpio_exti_init(GPIO_TypeDef *GPIOx, uint16_t pin, ald_exti_init_t *init);
256 /**
257   * @}
258   */
259 
260 /** @addtogroup GPIO_Public_Functions_Group2
261   * @{
262   */
263 uint8_t ald_gpio_read_pin(GPIO_TypeDef *GPIOx, uint16_t pin);
264 void ald_gpio_write_pin(GPIO_TypeDef *GPIOx, uint16_t pin, uint8_t val);
265 void ald_gpio_toggle_pin(GPIO_TypeDef *GPIOx, uint16_t pin);
266 void ald_gpio_toggle_dir(GPIO_TypeDef *GPIOx, uint16_t pin);
267 void ald_gpio_lock_pin(GPIO_TypeDef *GPIOx, uint16_t pin);
268 uint16_t ald_gpio_read_port(GPIO_TypeDef *GPIOx);
269 void ald_gpio_write_port(GPIO_TypeDef *GPIOx, uint16_t val);
270 /**
271   * @}
272   */
273 
274 /** @addtogroup GPIO_Public_Functions_Group3
275   * @{
276   */
277 void ald_gpio_exti_interrupt_config(uint16_t pin, ald_exti_trigger_style_t style, type_func_t status);
278 type_func_t ald_gpio_exti_get_ie_status(uint16_t pin);
279 flag_status_t ald_gpio_exti_get_flag_status(uint16_t pin);
280 void ald_gpio_exti_clear_flag_status(uint16_t pin);
281 /**
282   * @}
283   */
284 
285 /**
286   * @}
287   */
288 
289 /**
290   * @}
291   */
292 
293 /**
294   * @}
295   */
296 #ifdef __cplusplus
297 }
298 #endif /* __cplusplus */
299 
300 #endif /* __ALD_GPIO_H__ */
301