1 /*!
2  * @file        apm32f4xx_gpio.h
3  *
4  * @brief       This file provides all the GPIO firmware functions
5  *
6  * @version     V1.0.2
7  *
8  * @date        2022-06-23
9  *
10  * @attention
11  *
12  *  Copyright (C) 2021-2022 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be usefull and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 /* Define to prevent recursive inclusion */
27 #ifndef __APM32F4XX_GPIO_H
28 #define __APM32F4XX_GPIO_H
29 
30 #ifdef __cplusplus
31   extern "C" {
32 #endif
33 
34 /* Includes */
35 #include "apm32f4xx.h"
36 
37 /** @addtogroup APM32F4xx_StdPeriphDriver
38   @{
39 */
40 
41 /** @addtogroup GPIO_Driver
42   @{
43 */
44 
45 /** @defgroup GPIO_Enumerations
46   @{
47 */
48 
49 /**
50  * @brief GPIO Configuration Mode enumeration
51  */
52 typedef enum
53 {
54     GPIO_MODE_IN   = 0x00, /*!< GPIO Input Mode */
55     GPIO_MODE_OUT  = 0x01, /*!< GPIO Output Mode */
56     GPIO_MODE_AF   = 0x02, /*!< GPIO Alternate function Mode */
57     GPIO_MODE_AN   = 0x03  /*!< GPIO Analog Mode */
58 } GPIO_MODE_T;
59 
60 /**
61  * @brief GPIO Output type enumeration
62  */
63 typedef enum
64 {
65     GPIO_OTYPE_PP = 0x00,   /*!< GPIO push-pull mode */
66     GPIO_OTYPE_OD = 0x01    /*!< GPIO open drain mode */
67 } GPIO_OTYPE_T;
68 
69 /**
70  * @brief GPIO Output Maximum frequency enumeration
71  */
72 typedef enum
73 {
74     GPIO_SPEED_2MHz      = 0x00,      /*!< Low speed */
75     GPIO_SPEED_25MHz     = 0x01,      /*!< Medium speed */
76     GPIO_SPEED_50MHz     = 0x02,      /*!< Fast speed */
77     GPIO_SPEED_100MHz    = 0x03       /*!< High speed */
78 } GPIO_SPEED_T;
79 
80 /**
81  * @brief GPIO Configuration PullUp PullDown enumeration
82  */
83 typedef enum
84 {
85     GPIO_PUPD_NOPULL = 0x00,    /*!< GPIO no pull mode */
86     GPIO_PUPD_UP     = 0x01,    /*!< GPIO pull-up mode */
87     GPIO_PUPD_DOWN   = 0x02     /*!< GPIO pull-down mode */
88 } GPIO_PUPD_T;
89 
90 /**
91  * @brief GPIO Pins
92  */
93 typedef enum
94 {
95     GPIO_PIN_0   = ((uint16_t)BIT0),    /*!< GPIO pin 0 selected */
96     GPIO_PIN_1   = ((uint16_t)BIT1),    /*!< GPIO pin 1 selected */
97     GPIO_PIN_2   = ((uint16_t)BIT2),    /*!< GPIO pin 2 selected */
98     GPIO_PIN_3   = ((uint16_t)BIT3),    /*!< GPIO pin 3 selected */
99     GPIO_PIN_4   = ((uint16_t)BIT4),    /*!< GPIO pin 4 selected */
100     GPIO_PIN_5   = ((uint16_t)BIT5),    /*!< GPIO pin 5 selected */
101     GPIO_PIN_6   = ((uint16_t)BIT6),    /*!< GPIO pin 6 selected */
102     GPIO_PIN_7   = ((uint16_t)BIT7),    /*!< GPIO pin 7 selected */
103     GPIO_PIN_8   = ((uint16_t)BIT8),    /*!< GPIO pin 8 selected */
104     GPIO_PIN_9   = ((uint16_t)BIT9),    /*!< GPIO pin 9 selected */
105     GPIO_PIN_10  = ((uint16_t)BIT10),   /*!< GPIO pin 10 selected */
106     GPIO_PIN_11  = ((uint16_t)BIT11),   /*!< GPIO pin 11 selected */
107     GPIO_PIN_12  = ((uint16_t)BIT12),   /*!< GPIO pin 12 selected */
108     GPIO_PIN_13  = ((uint16_t)BIT13),   /*!< GPIO pin 13 selected */
109     GPIO_PIN_14  = ((uint16_t)BIT14),   /*!< GPIO pin 14 selected */
110     GPIO_PIN_15  = ((uint16_t)BIT15),   /*!< GPIO pin 15 selected */
111     GPIO_PIN_ALL = ((uint32_t)0XFFFF),  /*!< GPIO all pins selected */
112 } GPIO_PIN_T;
113 
114 /**
115  * @brief GPIO Pin sources
116  */
117 typedef enum
118 {
119     GPIO_PIN_SOURCE_0,  /*!< GPIO pin source 0 */
120     GPIO_PIN_SOURCE_1,  /*!< GPIO pin source 1 */
121     GPIO_PIN_SOURCE_2,  /*!< GPIO pin source 2 */
122     GPIO_PIN_SOURCE_3,  /*!< GPIO pin source 3 */
123     GPIO_PIN_SOURCE_4,  /*!< GPIO pin source 4 */
124     GPIO_PIN_SOURCE_5,  /*!< GPIO pin source 5 */
125     GPIO_PIN_SOURCE_6,  /*!< GPIO pin source 6 */
126     GPIO_PIN_SOURCE_7,  /*!< GPIO pin source 7 */
127     GPIO_PIN_SOURCE_8,  /*!< GPIO pin source 8 */
128     GPIO_PIN_SOURCE_9,  /*!< GPIO pin source 9 */
129     GPIO_PIN_SOURCE_10, /*!< GPIO pin source 10 */
130     GPIO_PIN_SOURCE_11, /*!< GPIO pin source 11 */
131     GPIO_PIN_SOURCE_12, /*!< GPIO pin source 12 */
132     GPIO_PIN_SOURCE_13, /*!< GPIO pin source 13 */
133     GPIO_PIN_SOURCE_14, /*!< GPIO pin source 14 */
134     GPIO_PIN_SOURCE_15, /*!< GPIO pin source 15 */
135 } GPIO_PIN_SOURCE_T;
136 
137 /**
138  * @brief GPIO Alternat function selection
139  */
140 typedef enum
141 {
142     GPIO_AF_RTC_50Hz            = 0x00, /*!< RTC_50Hz Alternate Function mapping */
143     GPIO_AF_MCO                 = 0x00, /*!< MCO (MCO1 and MCO2) Alternate Function mapping */
144     GPIO_AF_TAMPER              = 0x00, /*!< TAMPER (TAMPER_1 and TAMPER_2) Alternate Function mapping */
145     GPIO_AF_SWJ                 = 0x00, /*!< SWJ (SWD and JTAG) Alternate Function mapping */
146     GPIO_AF_TRACE               = 0x00, /*!< TRACE Alternate Function mapping */
147 
148     GPIO_AF_TMR1                = 0x01, /*!< TMR1 Alternate Function mapping */
149     GPIO_AF_TMR2                = 0x01, /*!< TMR2 Alternate Function mapping */
150     GPIO_AF_LPTMR               = 0x01, /*!< LPTMR Alternate Function mapping */
151 
152     GPIO_AF_TMR3                = 0x02, /*!< TMR3 Alternate Function mapping */
153     GPIO_AF_TMR4                = 0x02, /*!< TMR4 Alternate Function mapping */
154     GPIO_AF_TMR5                = 0x02, /*!< TMR5 Alternate Function mapping */
155 
156     GPIO_AF_TMR8                = 0x03, /*!< TMR8 Alternate Function mapping */
157     GPIO_AF_TMR9                = 0x03, /*!< TMR9 Alternate Function mapping */
158     GPIO_AF_TMR10               = 0x03, /*!< TMR10 Alternate Function mapping */
159     GPIO_AF_TMR11               = 0x03, /*!< TMR11 Alternate Function mapping */
160 
161     GPIO_AF_I2C1                = 0x04, /*!< I2C1 Alternate Function mapping */
162     GPIO_AF_I2C2                = 0x04, /*!< I2C2 Alternate Function mapping */
163     GPIO_AF_I2C3                = 0x04, /*!< I2C3 Alternate Function mapping */
164     GPIO_AF_FMPI2C              = 0x04, /*!< FMPI2C Alternate Function mapping */
165 
166     GPIO_AF_SPI1                = 0x05, /*!< SPI1/I2S1 Alternate Function mapping */
167     GPIO_AF_SPI2                = 0x05, /*!< SPI2/I2S2 Alternate Function mapping */
168     GPIO_AF5_SPI3               = 0x05, /*!< SPI3/I2S3 Alternate Function mapping */
169     GPIO_AF_SPI4                = 0x05, /*!< SPI4/I2S4 Alternate Function mapping */
170     GPIO_AF_SPI5                = 0x05, /*!< SPI5 Alternate Function mapping */
171     GPIO_AF_SPI6                = 0x05, /*!< SPI6 Alternate Function mapping */
172 
173     GPIO_AF_SPI3                = 0x06, /*!< SPI3/I2S3 Alternate Function mapping */
174     GPIO_AF6_SPI1               = 0x06, /*!< SPI1 Alternate Function mapping */
175     GPIO_AF6_SPI2               = 0x06, /*!< SPI2 Alternate Function mapping */
176     GPIO_AF6_SPI4               = 0x06, /*!< SPI4 Alternate Function mapping */
177     GPIO_AF6_SPI5               = 0x06, /*!< SPI5 Alternate Function mapping */
178     GPIO_AF_SAI1                = 0x06, /*!< SAI1 Alternate Function mapping */
179     GPIO_AF_I2S2ext             = 0x06, /*!< I2S2ext_SD Alternate Function mapping */
180 
181     GPIO_AF_USART1              = 0x07, /*!< USART1 Alternate Function mapping */
182     GPIO_AF_USART2              = 0x07, /*!< USART2 Alternate Function mapping */
183     GPIO_AF_USART3              = 0x07, /*!< USART3 Alternate Function mapping */
184     GPIO_AF7_SPI3               = 0x07, /*!< SPI3/I2S3ext Alternate Function mapping */
185 
186     GPIO_AF_UART4               = 0x08, /*!< UART4 Alternate Function mapping */
187     GPIO_AF_UART5               = 0x08, /*!< UART5 Alternate Function mapping */
188     GPIO_AF_USART6              = 0x08, /*!< USART6 Alternate Function mapping */
189     GPIO_AF_UART7               = 0x08, /*!< UART7 Alternate Function mapping */
190     GPIO_AF_UART8               = 0x08, /*!< UART8 Alternate Function mapping */
191     GPIO_AF8_USART3             = 0x08, /*!< USART3 Alternate Function mapping */
192     GPIO_AF8_CAN1               = 0x08, /*!< CAN1 Alternate Function mapping */
193 
194     GPIO_AF_CAN1                = 0x09, /*!< CAN1 Alternate Function mapping */
195     GPIO_AF_CAN2                = 0x09, /*!< CAN2 Alternate Function mapping */
196     GPIO_AF_TMR12               = 0x09, /*!< TMR12 Alternate Function mapping */
197     GPIO_AF_TMR13               = 0x09, /*!< TMR13 Alternate Function mapping */
198     GPIO_AF_TMR14               = 0x09, /*!< TMR14 Alternate Function mapping */
199     GPIO_AF9_I2C2               = 0x09, /*!< I2C2 Alternate Function mapping */
200     GPIO_AF9_I2C3               = 0x09, /*!< I2C3 Alternate Function mapping */
201 
202     GPIO_AF_OTG_FS              = 0x0A, /*!< OTG_FS Alternate Function mapping */
203     GPIO_AF_OTG_HS              = 0x0A, /*!< OTG_HS Alternate Function mapping */
204 
205     GPIO_AF_ETH                 = 0x0B, /*!< ETHERNET Alternate Function mapping */
206 
207     GPIO_AF_FSMC                = 0x0C, /*!< FSMC Alternate Function mapping */
208     GPIO_AF_OTG_HS_FS           = 0x0C, /*!< OTG HS configured in FS, Alternate Function mapping */
209     GPIO_AF_SDIO                = 0x0C, /*!< SDIO Alternate Function mapping */
210 
211     GPIO_AF_DCMI                = 0x0D, /*!< DCMI Alternate Function mapping */
212 
213     GPIO_AF14_RNG               = 0x0E, /*!< RNG Alternate Function mapping */
214 
215     GPIO_AF_EVENTOUT            = 0x0F, /*!< EVENTOUT Alternate Function mapping */
216 } GPIO_AF_T;
217 
218 /**@} end of group GPIO_Enumerations*/
219 
220 /** @addtogroup GPIO_Macros Macros
221   @{
222 */
223 #define GPIO_Mode_AIN           GPIO_Mode_AN
224 
225 #define GPIO_AF_I2S3ext         GPIO_AF7_SPI3
226 #define GPIO_AF_OTG1_FS         GPIO_AF_OTG_FS
227 #define GPIO_AF_OTG2_HS         GPIO_AF_OTG_HS
228 #define GPIO_AF_OTG2_FS         GPIO_AF_OTG_HS_FS
229 
230 /**@} end of group GPIO_Macros*/
231 
232 /** @addtogroup GPIO_Structure Data Structure
233   @{
234 */
235 
236 /**
237  * @brief GPIO Init structure definition
238  */
239 typedef struct
240 {
241     uint16_t      pin;      //!< Specifies the GPIO pins to be configured.
242     GPIO_MODE_T   mode;     //!< Specifies the operating mode for the selected pins.
243     GPIO_SPEED_T  speed;    //!< Specifies the speed for the selected pins.
244     GPIO_OTYPE_T  otype;    //!< Specifies the operating output type for the selected pins.
245     GPIO_PUPD_T   pupd;     //!< Specifies the operating Pull-up/Pull down for the selected pins.
246 } GPIO_Config_T;
247 
248 /**@} end of group GPIO_Structure*/
249 
250 /** @defgroup GPIO_Functions
251   @{
252 */
253 
254 /* Reset and common Configuration */
255 void GPIO_Reset(GPIO_T* port);
256 void GPIO_Config(GPIO_T* port, GPIO_Config_T* gpioConfig);
257 void GPIO_ConfigStructInit(GPIO_Config_T* gpioConfig);
258 
259 /* GPIO Read functions */
260 uint8_t GPIO_ReadInputBit(GPIO_T* port, uint16_t pin);
261 uint8_t GPIO_ReadOutputBit(GPIO_T* port, uint16_t pin);
262 
263 uint16_t GPIO_ReadInputPort(GPIO_T* port);
264 uint16_t GPIO_ReadOutputPort(GPIO_T* port);
265 
266 /* GPIO Write functions */
267 void GPIO_SetBit(GPIO_T* port, uint16_t pin);
268 void GPIO_ResetBit(GPIO_T* port, uint16_t pin);
269 void GPIO_WriteBitValue(GPIO_T* port, uint16_t pin, uint8_t bitVal);
270 void GPIO_WriteOutputPort(GPIO_T* port, uint16_t portValue);
271 
272 /* GPIO Other functions */
273 void GPIO_ToggleBit(GPIO_T* port, uint16_t pin);
274 void GPIO_ConfigPinLock(GPIO_T* port, uint16_t pin);
275 void GPIO_ConfigPinAF(GPIO_T* port, GPIO_PIN_SOURCE_T gpioPinSource, GPIO_AF_T gpioAf);
276 
277 #ifdef __cplusplus
278 }
279 #endif
280 
281 #endif /*__APM32F4XX_GPIO_H */
282 
283 /**@} end of group GPIO_Enumerations */
284 /**@} end of group GPIO_Driver */
285 /**@} end of group APM32F4xx_StdPeriphDriver */
286