1 /**
2 ******************************************************************************
3 * @file stm32mp1xx_hal_gpio.c
4 * @author MCD Application Team
5 * @brief GPIO HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the General Purpose Input/Output (GPIO) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 *
11 @verbatim
12 ==============================================================================
13 ##### GPIO Peripheral features #####
14 ==============================================================================
15 [..]
16 Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each
17 port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software
18 in several modes:
19 (+) Input mode
20 (+) Analog mode
21 (+) Output mode
22 (+) Alternate function mode
23 (+) External interrupt/event lines
24
25 [..]
26 During and just after reset, the alternate functions and external interrupt
27 lines are not active and the I/O ports are configured in input floating mode.
28
29 (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
30 activated or not.
31
32 [..]
33 In Output or Alternate mode, each IO can be configured on open-drain or push-pull
34 type and the IO speed can be selected depending on the VDD value.
35
36 [..]
37 All ports have external interrupt/event capability. To use external interrupt
38 lines, the port must be configured in input mode. All available GPIO pins are
39 connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
40
41 [..]
42 The external interrupt/event controller consists of up to 23 edge detectors
43 (16 lines are connected to GPIO) for generating event/interrupt requests (each
44 input line can be independently configured to select the type (interrupt or event)
45 and the corresponding trigger event (rising or falling or both). Each line can
46 also be masked independently.
47
48 ##### How to use this driver #####
49 ==============================================================================
50 [..]
51 (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
52
53 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
54 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
55 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
56 structure.
57 (++) In case of Output or alternate function mode selection: the speed is
58 configured through "Speed" member from GPIO_InitTypeDef structure.
59 (++) In alternate mode is selection, the alternate function connected to the IO
60 is configured through "Alternate" member from GPIO_InitTypeDef structure.
61 (++) Analog mode is required when a pin is to be used as ADC channel
62 or DAC output.
63 (++) In case of external interrupt/event selection the "Mode" member from
64 GPIO_InitTypeDef structure select the type (interrupt or event) and
65 the corresponding trigger event (rising or falling or both).
66
67 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
68 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
69 HAL_NVIC_EnableIRQ().
70
71 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
72
73 (#) To set/reset the level of a pin configured in output mode use
74 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
75
76 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
77
78
79 (#) During and just after reset, the alternate functions are not
80 active and the GPIO pins are configured in input floating mode (except JTAG
81 pins).
82
83 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
84 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
85 priority over the GPIO function.
86
87 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
88 general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
89 The HSE has priority over the GPIO function.
90
91 @endverbatim
92 ******************************************************************************
93 * @attention
94 *
95 * <h2><center>© Copyright (c) 2019 STMicroelectronics.
96 * All rights reserved.</center></h2>
97 *
98 * This software component is licensed by ST under BSD 3-Clause license,
99 * the "License"; You may not use this file except in compliance with the
100 * License. You may obtain a copy of the License at:
101 * opensource.org/licenses/BSD-3-Clause
102 *
103 ******************************************************************************
104 */
105
106 /* Includes ------------------------------------------------------------------*/
107 #include "stm32mp1xx_hal.h"
108
109 /** @addtogroup STM32MP1xx_HAL_Driver
110 * @{
111 */
112
113 /** @defgroup GPIO GPIO
114 * @brief GPIO HAL module driver
115 * @{
116 */
117
118 /* Private typedef -----------------------------------------------------------*/
119 /* Private defines ------------------------------------------------------------*/
120 /** @addtogroup GPIO_Private_Constants GPIO Private Constants
121 * @{
122 */
123 #define GPIO_MODE ((uint32_t)0x00000003)
124 #define EXTI_MODE ((uint32_t)0x10000000)
125 #define GPIO_MODE_IT ((uint32_t)0x00010000)
126 #define GPIO_MODE_EVT ((uint32_t)0x00020000)
127 #define RISING_EDGE ((uint32_t)0x00100000)
128 #define FALLING_EDGE ((uint32_t)0x00200000)
129 #define GPIO_OUTPUT_TYPE ((uint32_t)0x00000010)
130
131 #define GPIO_NUMBER ((uint32_t)16)
132 /**
133 * @}
134 */
135 /* Private macro -------------------------------------------------------------*/
136 /* Private variables ---------------------------------------------------------*/
137 /* Private function prototypes -----------------------------------------------*/
138 /* Private functions ---------------------------------------------------------*/
139 /* Exported functions --------------------------------------------------------*/
140 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
141 * @{
142 */
143
144 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
145 * @brief Initialization and Configuration functions
146 *
147 @verbatim
148 ===============================================================================
149 ##### Initialization and de-initialization functions #####
150 ===============================================================================
151 [..]
152 This section provides functions allowing to initialize and de-initialize the GPIOs
153 to be ready for use.
154
155 @endverbatim
156 * @{
157 */
158
159 /**
160 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
161 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
162 * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
163 * the configuration information for the specified GPIO peripheral.
164 * @retval None
165 */
HAL_GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_Init)166 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
167 {
168 uint32_t position;
169 uint32_t ioposition;
170 uint32_t iocurrent;
171 uint32_t temp;
172 #if 0 /* OHOS*/
173 EXTI_Core_TypeDef * EXTI_CurrentCPU;
174
175 #if defined(CORE_CM4)
176 EXTI_CurrentCPU = EXTI_C2; /* EXTI for CM4 CPU */
177 #else
178 EXTI_CurrentCPU = EXTI_C1; /* EXTI for CA7 CPU */
179 #endif
180 #endif
181
182 /* Configure the port pins */
183 for(position = 0; position < GPIO_NUMBER; position++)
184 {
185 /* Get the IO position */
186 ioposition = ((uint32_t)0x01) << position;
187 /* Get the current IO position */
188 iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
189
190 if(iocurrent == ioposition)
191 {
192 /*--------------------- GPIO Mode Configuration ------------------------*/
193 /* In case of Alternate function mode selection */
194 if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
195 {
196 /* Configure Alternate function mapped with the current IO */
197 temp = GPIOx->AFR[position >> 3];
198 temp &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
199 temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07) * 4));
200 GPIOx->AFR[position >> 3] = temp;
201 }
202
203 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
204 temp = GPIOx->MODER;
205 temp &= ~(GPIO_MODER_MODER0 << (position * 2));
206 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2));
207 GPIOx->MODER = temp;
208
209 /* In case of Output or Alternate function mode selection */
210 if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
211 (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
212 {
213 /* Configure the IO Speed */
214 temp = GPIOx->OSPEEDR;
215 temp &= ~(GPIO_OSPEEDR_OSPEEDR0 << (position * 2));
216 temp |= (GPIO_Init->Speed << (position * 2));
217 GPIOx->OSPEEDR = temp;
218
219 /* Configure the IO Output Type */
220 temp = GPIOx->OTYPER;
221 temp &= ~(GPIO_OTYPER_OT0 << position) ;
222 temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4) << position);
223 GPIOx->OTYPER = temp;
224 }
225
226 /* Activate the Pull-up or Pull down resistor for the current IO */
227 temp = GPIOx->PUPDR;
228 temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
229 temp |= ((GPIO_Init->Pull) << (position * 2));
230 GPIOx->PUPDR = temp;
231
232 }
233 }
234 }
235
236 /**
237 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
238 * @param GPIOx: where x can be (A..Z) to select the GPIO peripheral.
239 * @param GPIO_Pin: specifies the port bit to be written.
240 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
241 * @retval None
242 */
HAL_GPIO_DeInit(GPIO_TypeDef * GPIOx,uint32_t GPIO_Pin)243 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
244 {
245 uint32_t position;
246 uint32_t ioposition;
247 uint32_t iocurrent;
248 #if 0 /* OHOS*/
249 EXTI_Core_TypeDef * EXTI_CurrentCPU;
250
251 #if defined(CORE_CM4)
252 EXTI_CurrentCPU = EXTI_C2; /* EXTI for CM4 CPU */
253 #else
254 EXTI_CurrentCPU = EXTI_C1; /* EXTI for CA7 CPU */
255 #endif
256 #endif
257
258 /* Configure the port pins */
259 for(position = 0; position < GPIO_NUMBER; position++)
260 {
261 /* Get the IO position */
262 ioposition = ((uint32_t)0x01) << position;
263 /* Get the current IO position */
264 iocurrent = (GPIO_Pin) & ioposition;
265
266 if(iocurrent == ioposition)
267 {
268 /*------------------------- GPIO Mode Configuration --------------------*/
269 /* Configure IO in Analog Mode */
270 GPIOx->MODER |= (GPIO_MODER_MODER0 << (position * 2));
271
272 /* Configure the default Alternate Function in current IO */
273 GPIOx->AFR[position >> 3] &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
274
275 /* Configure the default value for IO Speed */
276 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEEDR0 << (position * 2));
277
278 /* Configure the default value IO Output Type */
279 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT0 << position) ;
280
281 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
282 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
283 }
284 }
285 }
286
287 /**
288 * @}
289 */
290
291 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
292 * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
293 *
294 @verbatim
295 ===============================================================================
296 ##### IO operation functions #####
297 ===============================================================================
298
299 @endverbatim
300 * @{
301 */
302
303 /**
304 * @brief Reads the specified input port pin.
305 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
306 * @param GPIO_Pin: specifies the port bit to read.
307 * This parameter can be GPIO_PIN_x where x can be (0..15).
308 * @retval The input port pin value.
309 */
HAL_GPIO_ReadPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)310 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
311 {
312 GPIO_PinState bitstatus;
313
314 if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
315 {
316 bitstatus = GPIO_PIN_SET;
317 }
318 else
319 {
320 bitstatus = GPIO_PIN_RESET;
321 }
322 return bitstatus;
323 }
324
325 /**
326 * @brief Sets or clears the selected data port bit.
327 *
328 * @note This function uses GPIOx_BSRR register to allow atomic read/modify
329 * accesses. In this way, there is no risk of an IRQ occurring between
330 * the read and the modify access.
331 *
332 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
333 * @param GPIO_Pin: specifies the port bit to be written.
334 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
335 * @param PinState: specifies the value to be written to the selected bit.
336 * This parameter can be one of the GPIO_PinState enum values:
337 * @arg GPIO_PIN_RESET: to clear the port pin
338 * @arg GPIO_PIN_SET: to set the port pin
339 * @retval None
340 */
HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_PinState PinState)341 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
342 {
343 if (PinState != GPIO_PIN_RESET)
344 {
345 GPIOx->BSRR = GPIO_Pin;
346 }
347 else
348 {
349 GPIOx->BSRR = (uint32_t)GPIO_Pin << GPIO_NUMBER;
350 }
351 }
352
353 /**
354 * @brief Toggles the specified GPIO pins.
355 * @param GPIOx: Where x can be (A..K) to select the GPIO peripheral.
356 * @param GPIO_Pin: Specifies the pins to be toggled.
357 * @retval None
358 */
HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)359 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
360 {
361 if ((GPIOx->ODR & GPIO_Pin) != 0x00u)
362 {
363 GPIOx->BRR = (uint32_t)GPIO_Pin;
364 }
365 else
366 {
367 GPIOx->BSRR = (uint32_t)GPIO_Pin;
368 }
369 }
370
HAL_GPIO_ReadDir(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)371 GPIO_DirState HAL_GPIO_ReadDir(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
372 {
373 GPIO_DirState bitstatus;
374
375 if((GPIOx->MODER & GPIO_Pin*2) != (uint32_t)GPIO_PIN_RESET)
376 {
377 bitstatus = GPIO_DIR_OUTPUT;
378 }
379 else
380 {
381 bitstatus = GPIO_DIR_INPUT;
382 }
383 return bitstatus;
384 }
385
386 /**
387 * @}
388 */
389
390
391 /**
392 * @}
393 */
394
395 /**
396 * @}
397 */
398
399 /**
400 * @}
401 */
402
403 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
404