1 /* 2 * Copyright 2021 MindMotion Microelectronics Co., Ltd. 3 * All rights reserved. 4 * 5 * SPDX-License-Identifier: BSD-3-Clause 6 */ 7 8 #ifndef __HAL_GPIO_H__ 9 #define __HAL_GPIO_H__ 10 11 #include "hal_common.h" 12 13 /*! 14 * @addtogroup GPIO 15 * @{ 16 */ 17 18 /*! 19 * @addtogroup GPIO_PIN 20 * @brief GPIO pin mask codes. 21 * @{ 22 */ 23 #define GPIO_PIN_0 (0x0001u) /*!< Pin 0 selected. */ 24 #define GPIO_PIN_1 (0x0002u) /*!< Pin 1 selected. */ 25 #define GPIO_PIN_2 (0x0004u) /*!< Pin 2 selected. */ 26 #define GPIO_PIN_3 (0x0008u) /*!< Pin 3 selected. */ 27 #define GPIO_PIN_4 (0x0010u) /*!< Pin 4 selected. */ 28 #define GPIO_PIN_5 (0x0020u) /*!< Pin 5 selected. */ 29 #define GPIO_PIN_6 (0x0040u) /*!< Pin 6 selected. */ 30 #define GPIO_PIN_7 (0x0080u) /*!< Pin 7 selected. */ 31 #define GPIO_PIN_8 (0x0100u) /*!< Pin 8 selected. */ 32 #define GPIO_PIN_9 (0x0200u) /*!< Pin 9 selected. */ 33 #define GPIO_PIN_10 (0x0400u) /*!< Pin 10 selected. */ 34 #define GPIO_PIN_11 (0x0800u) /*!< Pin 11 selected. */ 35 #define GPIO_PIN_12 (0x1000u) /*!< Pin 12 selected. */ 36 #define GPIO_PIN_13 (0x2000u) /*!< Pin 13 selected. */ 37 #define GPIO_PIN_14 (0x4000u) /*!< Pin 14 selected. */ 38 #define GPIO_PIN_15 (0x8000u) /*!< Pin 15 selected. */ 39 #define GPIO_PINS_ALL (0xFFFFu) /*!< All pins selected. */ 40 /*! 41 * @} 42 */ 43 44 /*! 45 * @addtogroup GPIO_AF 46 * @brief GPIO pin alternative function. 47 * @{ 48 */ 49 #define GPIO_AF_0 (0x00u) /*!< Alternative function 0. */ 50 #define GPIO_AF_1 (0x01u) /*!< Alternative function 1. */ 51 #define GPIO_AF_2 (0x02u) /*!< Alternative function 2. */ 52 #define GPIO_AF_3 (0x03u) /*!< Alternative function 3. */ 53 #define GPIO_AF_4 (0x04u) /*!< Alternative function 4. */ 54 #define GPIO_AF_5 (0x05u) /*!< Alternative function 5. */ 55 #define GPIO_AF_6 (0x06u) /*!< Alternative function 6. */ 56 #define GPIO_AF_7 (0x07u) /*!< Alternative function 7. */ 57 #define GPIO_AF_8 (0x08u) /*!< Alternative function 8. */ 58 #define GPIO_AF_9 (0x09u) /*!< Alternative function 9. */ 59 #define GPIO_AF_10 (0x0Au) /*!< Alternative function 10. */ 60 #define GPIO_AF_11 (0x0Bu) /*!< Alternative function 11. */ 61 #define GPIO_AF_12 (0x0Cu) /*!< Alternative function 12. */ 62 #define GPIO_AF_13 (0x0Du) /*!< Alternative function 13. */ 63 #define GPIO_AF_14 (0x0Eu) /*!< Alternative function 14. */ 64 #define GPIO_AF_15 (0x0Fu) /*!< Alternative function 15. */ 65 /*! 66 * @} 67 */ 68 69 /*! 70 * @brief GPIO maximum speed type. 71 */ 72 typedef enum 73 { 74 GPIO_Speed_50MHz = 1u, /*!< Maximum speed is 50MHz. */ 75 GPIO_Speed_20MHz = 2u, /*!< Maximum speed is 20MHz. */ 76 GPIO_Speed_10MHz = 3u, /*!< Maximum speed is 10MHz. */ 77 } GPIO_Speed_Type; 78 79 /*! 80 * @brief Port input / output mode. 81 */ 82 typedef enum 83 { 84 GPIO_PinMode_In_Analog = 0x00u, /*!< Analog input. */ 85 GPIO_PinMode_In_Floating = 0x04u, /*!< Floating input. */ 86 GPIO_PinMode_In_PullDown = 0x28u, /*!< Pull down input. */ 87 GPIO_PinMode_In_PullUp = 0x48u, /*!< Pull up input. */ 88 GPIO_PinMode_Out_OpenDrain = 0x14u, /*!< Universal open drain output. */ 89 GPIO_PinMode_Out_PushPull = 0x10u, /*!< Universal push-pull output. */ 90 GPIO_PinMode_AF_OpenDrain = 0x1Cu, /*!< Multiplex open drain output. */ 91 GPIO_PinMode_AF_PushPull = 0x18u, /*!< Multiplexed push-pull output. */ 92 } GPIO_PinMode_Type; 93 94 /*! 95 * @brief This type of structure instance is used to keep the settings when calling the @ref GPIO_Init() to initialize the GPIO module. 96 */ 97 typedef struct 98 { 99 uint16_t Pins; /*!< GPIO operating pins. */ 100 GPIO_Speed_Type Speed; /*!< GPIO pin speed mode. */ 101 GPIO_PinMode_Type PinMode; /*!< GPIO pin input / output functional mode. */ 102 } GPIO_Init_Type; 103 104 /*! 105 * @brief Initialize the GPIO module. 106 * 107 * @param GPIOx GPIO instance. 108 * @param init Pointer to the initialization structure. See to @ref GPIO_Init_Type. 109 * @return None. 110 */ 111 void GPIO_Init(GPIO_Type * GPIOx, GPIO_Init_Type * init); 112 113 /*! 114 * @brief Set the specified data port bit. 115 * 116 * @param GPIOx GPIO instance. 117 * @param pins GPIO operating pins. See to @ref GPIO_PIN. 118 * @return None. 119 */ 120 void GPIO_SetBits(GPIO_Type * GPIOx, uint16_t pins); 121 122 /*! 123 * @brief Clear the specified data port bit. 124 * 125 * @param GPIOx GPIO instance. 126 * @param pins GPIO operating pins. See to @ref GPIO_PIN. 127 * @return None. 128 */ 129 void GPIO_ClearBits(GPIO_Type * GPIOx, uint16_t pins); 130 131 /*! 132 * @brief Writes 0 or 1 to the specified bit. 133 * 134 * @param GPIOx GPIO instance. 135 * @param pins GPIO operating pins. See to @ref GPIO_PIN. 136 * @param val Decide whether to set or clear. 137 * @return None. 138 */ 139 void GPIO_WriteBit(GPIO_Type * GPIOx, uint16_t pins, uint16_t val); 140 141 /*! 142 * @brief Write 0 or 1 for multiple I/O ports. 143 * 144 * @param GPIOx GPIO instance. 145 * @param val Decide whether to set or clear. 146 * @return None. 147 */ 148 void GPIO_WriteBits(GPIO_Type * GPIOx, uint16_t val); 149 150 /*! 151 * @brief Keep the set I/O configuration can not be changed. 152 * 153 * @param GPIOx GPIO instance. 154 * @param pins GPIO operating pins. See to @ref GPIO_PIN. 155 * @param enable_lock enable 'true' to enable the lock, 'false' to disable the lock. 156 * @return None. 157 */ 158 void GPIO_PinLock(GPIO_Type * GPIOx, uint16_t pins, bool enable_lock); 159 160 /*! 161 * @brief Lock the corresponding bit of the select port configuration register. 162 * 163 * @param GPIOx GPIO instance. 164 * @param pins GPIO operating pins. See to @ref GPIO_PIN. 165 * @return None. 166 */ 167 void GPIO_PinLockConf(GPIO_Type * GPIOx, uint16_t pins); 168 169 /*! 170 * @brief Read the value once entered before by the specified pin. 171 * 172 * @param GPIOx GPIO instance. 173 * @param pins GPIO operating pins. See to @ref GPIO_PIN. 174 * @return Status of a pin's logic level, 0 for low level or 1 for high level. 175 */ 176 bool GPIO_ReadInDataBit(GPIO_Type * GPIOx, uint16_t pins); 177 178 /*! 179 * @brief Read the value previously output by the specified pin. 180 * 181 * @param GPIOx GPIO instance. 182 * @param pins GPIO operating pins. See to @ref GPIO_PIN. 183 * @return Status of a pin's logic level, 0 for low level or 1 for high level. 184 */ 185 bool GPIO_ReadOutDataBit(GPIO_Type * GPIOx, uint16_t pins); 186 187 /*! 188 * @brief Read the specified GPIO port input. 189 * 190 * @param GPIOx GPIO instance. 191 * @return A hexadecimal number. 192 */ 193 uint16_t GPIO_ReadInData(GPIO_Type * GPIOx); 194 195 /*! 196 * @brief Read the specified GPIO port output. 197 * 198 * @param GPIOx GPIO instance. 199 * @return A hexadecimal number. 200 */ 201 uint16_t GPIO_ReadOutData(GPIO_Type * GPIOx); 202 203 /*! 204 * @brief Pin alternate function configuration of GPIO. 205 * 206 * @param GPIOx GPIO instance. 207 * @param pins GPIO operating pins. See to @ref GPIO_PIN. 208 * @param alternate_function Reuse the unified I/O port, but the function is different. 209 * @return None. 210 */ 211 void GPIO_PinAFConf(GPIO_Type* GPIOx, uint16_t pins, uint8_t alternate_function); 212 213 /*! 214 * @} 215 */ 216 217 #endif /* __HAL_GPIO_H__ */ 218 219