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 2019 9 * @author AE Team 10 * @note 11 * Change Logs: 12 * Date Author Notes 13 * 07 Nov 2019 AE Team 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 39 40 #include "utils.h" 41 42 43 /** @addtogroup ES32FXXX_ALD 44 * @{ 45 */ 46 47 /** @addtogroup GPIO 48 * @{ 49 */ 50 51 /** 52 * @defgroup GPIO_Public_Macros GPIO Public Macros 53 * @{ 54 */ 55 #define GPIO_PIN_0 (0x1U) 56 #define GPIO_PIN_1 (0x2U) 57 #define GPIO_PIN_2 (0x4U) 58 #define GPIO_PIN_3 (0x8U) 59 #define GPIO_PIN_4 (0x10U) 60 #define GPIO_PIN_5 (0x20U) 61 #define GPIO_PIN_6 (0x40U) 62 #define GPIO_PIN_7 (0x80U) 63 #define GPIO_PIN_8 (0x100U) 64 #define GPIO_PIN_9 (0x200U) 65 #define GPIO_PIN_10 (0x400U) 66 #define GPIO_PIN_11 (0x800U) 67 #define GPIO_PIN_12 (0x1000U) 68 #define GPIO_PIN_13 (0x2000U) 69 #define GPIO_PIN_14 (0x4000U) 70 #define GPIO_PIN_15 (0x8000U) 71 #define 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 #define ALD_GPIOE_TOGGLE_PIN(x) (GPIOE->BIR = (x)) 79 #define ALD_GPIOF_TOGGLE_PIN(x) (GPIOF->BIR = (x)) 80 #define ALD_GPIOG_TOGGLE_PIN(x) (GPIOG->BIR = (x)) 81 #define ALD_GPIOH_TOGGLE_PIN(x) (GPIOH->BIR = (x)) 82 83 /* Read IO level */ 84 #define ALD_GPIOA_READ_PIN(x) ((GPIOA->DIN & (x)) ? 1 : 0) 85 #define ALD_GPIOB_READ_PIN(x) ((GPIOB->DIN & (x)) ? 1 : 0) 86 #define ALD_GPIOC_READ_PIN(x) ((GPIOC->DIN & (x)) ? 1 : 0) 87 #define ALD_GPIOD_READ_PIN(x) ((GPIOD->DIN & (x)) ? 1 : 0) 88 #define ALD_GPIOE_READ_PIN(x) ((GPIOE->DIN & (x)) ? 1 : 0) 89 #define ALD_GPIOF_READ_PIN(x) ((GPIOF->DIN & (x)) ? 1 : 0) 90 #define ALD_GPIOG_READ_PIN(x) ((GPIOG->DIN & (x)) ? 1 : 0) 91 #define ALD_GPIOH_READ_PIN(x) ((GPIOH->DIN & (x)) ? 1 : 0) 92 93 /* Set IO as high */ 94 #define ALD_GPIOA_SET_PIN(x) (GPIOA->BSRR = (x)) 95 #define ALD_GPIOB_SET_PIN(x) (GPIOB->BSRR = (x)) 96 #define ALD_GPIOC_SET_PIN(x) (GPIOC->BSRR = (x)) 97 #define ALD_GPIOD_SET_PIN(x) (GPIOD->BSRR = (x)) 98 #define ALD_GPIOE_SET_PIN(x) (GPIOE->BSRR = (x)) 99 #define ALD_GPIOF_SET_PIN(x) (GPIOF->BSRR = (x)) 100 #define ALD_GPIOG_SET_PIN(x) (GPIOG->BSRR = (x)) 101 #define ALD_GPIOH_SET_PIN(x) (GPIOH->BSRR = (x)) 102 103 /* Set IO as low */ 104 #define ALD_GPIOA_RESET_PIN(x) (GPIOA->BSRR = ((x) << 16)) 105 #define ALD_GPIOB_RESET_PIN(x) (GPIOB->BSRR = ((x) << 16)) 106 #define ALD_GPIOC_RESET_PIN(x) (GPIOC->BSRR = ((x) << 16)) 107 #define ALD_GPIOD_RESET_PIN(x) (GPIOD->BSRR = ((x) << 16)) 108 #define ALD_GPIOE_RESET_PIN(x) (GPIOE->BSRR = ((x) << 16)) 109 #define ALD_GPIOF_RESET_PIN(x) (GPIOF->BSRR = ((x) << 16)) 110 #define ALD_GPIOG_RESET_PIN(x) (GPIOG->BSRR = ((x) << 16)) 111 #define ALD_GPIOH_RESET_PIN(x) (GPIOH->BSRR = ((x) << 16)) 112 /** 113 * @} 114 */ 115 116 /** 117 * @defgroup GPIO_Public_Types GPIO Public Types 118 * @{ 119 */ 120 121 /** 122 * @brief GPIO mode 123 */ 124 typedef enum { 125 GPIO_MODE_CLOSE = 0x0U, /**< Digital close Analog open */ 126 GPIO_MODE_INPUT = 0x1U, /**< Input */ 127 GPIO_MODE_OUTPUT = 0x2U, /**< Output */ 128 } gpio_mode_t; 129 130 /** 131 * @brief GPIO open-drain or push-pull 132 */ 133 typedef enum { 134 GPIO_PUSH_PULL = 0x0U, /**< Push-Pull */ 135 GPIO_OPEN_DRAIN = 0x2U, /**< Open-Drain. Can't output high level */ 136 GPIO_OPEN_SOURCE = 0x3U, /**< Open-Source. Can't output low level */ 137 } gpio_odos_t; 138 139 /** 140 * @brief GPIO push-up or push-down 141 */ 142 typedef enum { 143 GPIO_FLOATING = 0x0U, /**< Floating */ 144 GPIO_PUSH_UP = 0x1U, /**< Push-Up */ 145 GPIO_PUSH_DOWN = 0x2U, /**< Push-Down */ 146 GPIO_PUSH_UP_DOWN = 0x3U, /**< Push-Up and Push-Down */ 147 } gpio_push_t; 148 149 /** 150 * @brief GPIO output drive 151 */ 152 typedef enum { 153 GPIO_OUT_DRIVE_0_1 = 0x0U, /**< 0.1mA */ 154 GPIO_OUT_DRIVE_1 = 0x1U, /**< 1mA */ 155 GPIO_OUT_DRIVE_6 = 0x2U, /**< 6mA */ 156 GPIO_OUT_DRIVE_20 = 0x3U, /**< 20mA */ 157 } gpio_out_drive_t; 158 159 /** 160 * @brief GPIO filter 161 */ 162 typedef enum { 163 GPIO_FILTER_DISABLE = 0x0U, /**< Disable filter */ 164 GPIO_FILTER_ENABLE = 0x1U, /**< Enable filter */ 165 } gpio_filter_t; 166 167 /** 168 * @brief GPIO type 169 */ 170 typedef enum { 171 GPIO_TYPE_CMOS = 0x0U, /**< CMOS Type */ 172 GPIO_TYPE_TTL = 0x1U, /**< TTL Type */ 173 } gpio_type_t; 174 175 /** 176 * @brief GPIO functions 177 */ 178 typedef enum { 179 GPIO_FUNC_0 = 0U, /**< function #0 */ 180 GPIO_FUNC_1 = 1U, /**< function #1 */ 181 GPIO_FUNC_2 = 2U, /**< function #2 */ 182 GPIO_FUNC_3 = 3U, /**< function #3 */ 183 GPIO_FUNC_4 = 4U, /**< function #4 */ 184 GPIO_FUNC_5 = 5U, /**< function #5 */ 185 GPIO_FUNC_6 = 6U, /**< function #6 */ 186 GPIO_FUNC_7 = 7U, /**< function #7 */ 187 } gpio_func_t; 188 189 190 /** 191 * @brief GPIO Init Structure definition 192 */ 193 typedef struct { 194 gpio_mode_t mode; /**< Specifies the operating mode for the selected pins. 195 This parameter can be any value of @ref gpio_mode_t */ 196 gpio_odos_t odos; /**< Specifies the Open-Drain or Push-Pull for the selected pins. 197 This parameter can be a value of @ref gpio_odos_t */ 198 gpio_push_t pupd; /**< Specifies the Pull-up or Pull-Down for the selected pins. 199 This parameter can be a value of @ref gpio_push_t */ 200 gpio_out_drive_t podrv; /**< Specifies the output P-MOS driver for the selected pins. 201 This parameter can be a value of @ref gpio_out_drive_t */ 202 gpio_out_drive_t nodrv; /**< Specifies the output N-MOS driver for the selected pins. 203 This parameter can be a value of @ref gpio_out_drive_t */ 204 gpio_filter_t flt; /**< Specifies the input filter for the selected pins. 205 This parameter can be a value of @ref gpio_filter_t */ 206 gpio_type_t type; /**< Specifies the type for the selected pins. 207 This parameter can be a value of @ref gpio_type_t */ 208 gpio_func_t func; /**< Specifies the function for the selected pins. 209 This parameter can be a value of @ref gpio_func_t */ 210 } gpio_init_t; 211 212 /** 213 * @brief EXTI trigger style 214 */ 215 typedef enum { 216 EXTI_TRIGGER_RISING_EDGE = 0U, /**< Rising edge trigger */ 217 EXTI_TRIGGER_TRAILING_EDGE = 1U, /**< Trailing edge trigger */ 218 EXTI_TRIGGER_BOTH_EDGE = 2U, /**< Rising and trailing edge trigger */ 219 } exti_trigger_style_t; 220 221 /** 222 * @brief EXTI filter clock select 223 */ 224 typedef enum { 225 EXTI_FILTER_CLOCK_10K = 0U, /**< cks = 10KHz */ 226 EXTI_FILTER_CLOCK_32K = 1U, /**< cks = 32KHz */ 227 } exti_filter_clock_t; 228 229 /** 230 * @brief EXTI Init Structure definition 231 */ 232 typedef struct { 233 type_func_t filter; /**< Enable filter. */ 234 exti_filter_clock_t cks; /**< Filter clock select. */ 235 uint8_t filter_time; /**< Filter duration */ 236 } exti_init_t; 237 /** 238 * @} 239 */ 240 241 /** 242 * @defgroup GPIO_Private_Macros GPIO Private Macros 243 * @{ 244 */ 245 #define PIN_MASK 0xFFFFU 246 #define UNLOCK_KEY 0x55AAU 247 248 #define IS_GPIO_PIN(x) ((((x) & (uint16_t)0x00) == 0) && ((x) != (uint16_t)0x0)) 249 #define IS_GPIO_PORT(GPIOx) ((GPIOx == GPIOA) || \ 250 (GPIOx == GPIOB) || \ 251 (GPIOx == GPIOC) || \ 252 (GPIOx == GPIOD) || \ 253 (GPIOx == GPIOE) || \ 254 (GPIOx == GPIOF) || \ 255 (GPIOx == GPIOG) || \ 256 (GPIOx == GPIOH)) 257 #define IS_GPIO_MODE(x) (((x) == GPIO_MODE_CLOSE) || \ 258 ((x) == GPIO_MODE_INPUT) || \ 259 ((x) == GPIO_MODE_OUTPUT)) 260 #define IS_GPIO_ODOS(x) (((x) == GPIO_PUSH_PULL) || \ 261 ((x) == GPIO_OPEN_DRAIN) || \ 262 ((x) == GPIO_OPEN_SOURCE)) 263 #define IS_GPIO_PUPD(x) (((x) == GPIO_FLOATING) || \ 264 ((x) == GPIO_PUSH_UP) || \ 265 ((x) == GPIO_PUSH_DOWN) || \ 266 ((x) == GPIO_PUSH_UP_DOWN)) 267 #define IS_GPIO_ODRV(x) (((x) == GPIO_OUT_DRIVE_0_1) || \ 268 ((x) == GPIO_OUT_DRIVE_1) || \ 269 ((x) == GPIO_OUT_DRIVE_6) || \ 270 ((x) == GPIO_OUT_DRIVE_20)) 271 #define IS_GPIO_FLT(x) (((x) == GPIO_FILTER_DISABLE) || \ 272 ((x) == GPIO_FILTER_ENABLE)) 273 #define IS_GPIO_TYPE(x) (((x) == GPIO_TYPE_TTL) || \ 274 ((x) == GPIO_TYPE_CMOS)) 275 #define IS_TRIGGER_STYLE(x) (((x) == EXTI_TRIGGER_RISING_EDGE) || \ 276 ((x) == EXTI_TRIGGER_TRAILING_EDGE) || \ 277 ((x) == EXTI_TRIGGER_BOTH_EDGE)) 278 #define IS_EXTI_FLTCKS_TYPE(x) (((x) == EXTI_FILTER_CLOCK_10K) || \ 279 ((x) == EXTI_FILTER_CLOCK_32K)) 280 #define IS_GPIO_FUNC(x) ((x) <= 7) 281 /** 282 * @} 283 */ 284 285 /** @addtogroup GPIO_Public_Functions 286 * @{ 287 */ 288 289 /** @addtogroup GPIO_Public_Functions_Group1 290 * @{ 291 */ 292 void ald_gpio_init(GPIO_TypeDef *GPIOx, uint16_t pin, gpio_init_t *init); 293 void ald_gpio_init_default(GPIO_TypeDef *GPIOx, uint16_t pin); 294 void ald_gpio_func_default(GPIO_TypeDef *GPIOx); 295 void ald_gpio_exti_init(GPIO_TypeDef *GPIOx, uint16_t pin, exti_init_t *init); 296 /** 297 * @} 298 */ 299 300 /** @addtogroup GPIO_Public_Functions_Group2 301 * @{ 302 */ 303 uint8_t ald_gpio_read_pin(GPIO_TypeDef *GPIOx, uint16_t pin); 304 void ald_gpio_write_pin(GPIO_TypeDef *GPIOx, uint16_t pin, uint8_t val); 305 void ald_gpio_toggle_pin(GPIO_TypeDef *GPIOx, uint16_t pin); 306 void ald_gpio_toggle_dir(GPIO_TypeDef *GPIOx, uint16_t pin); 307 void ald_gpio_lock_pin(GPIO_TypeDef *GPIOx, uint16_t pin); 308 uint16_t ald_gpio_read_port(GPIO_TypeDef *GPIOx); 309 void ald_gpio_write_port(GPIO_TypeDef *GPIOx, uint16_t val); 310 /** 311 * @} 312 */ 313 314 /** @addtogroup GPIO_Public_Functions_Group3 315 * @{ 316 */ 317 void ald_gpio_exti_interrupt_config(uint16_t pin, exti_trigger_style_t style, type_func_t status); 318 flag_status_t ald_gpio_exti_get_flag_status(uint16_t pin); 319 void ald_gpio_exti_clear_flag_status(uint16_t pin); 320 /** 321 * @} 322 */ 323 324 /** 325 * @} 326 */ 327 328 /** 329 * @} 330 */ 331 332 /** 333 * @} 334 */ 335 #ifdef __cplusplus 336 } 337 #endif 338 339 #endif /* __ALD_GPIO_H__ */ 340