STM32F4xx HAL Documentation
Hardware Abstraction Layer for STM32F4 familiy
Loading...
Searching...
No Matches
stm32f4xx_hal_gpio.c
Go to the documentation of this file.
1
107/* Includes ------------------------------------------------------------------*/
108#include "stm32f4xx_hal.h"
109
119#ifdef HAL_GPIO_MODULE_ENABLED
120
121/* Private typedef -----------------------------------------------------------*/
122/* Private define ------------------------------------------------------------*/
127#define GPIO_NUMBER 16U
131/* Private macro -------------------------------------------------------------*/
132/* Private variables ---------------------------------------------------------*/
133/* Private function prototypes -----------------------------------------------*/
134/* Private functions ---------------------------------------------------------*/
135/* Exported functions --------------------------------------------------------*/
164void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
165{
166 uint32_t position;
167 uint32_t ioposition = 0x00U;
168 uint32_t iocurrent = 0x00U;
169 uint32_t temp = 0x00U;
170
171 /* Check the parameters */
172 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
173 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
174 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
175
176 /* Configure the port pins */
177 for(position = 0U; position < GPIO_NUMBER; position++)
178 {
179 /* Get the IO position */
180 ioposition = 0x01U << position;
181 /* Get the current IO position */
182 iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
183
184 if(iocurrent == ioposition)
185 {
186 /*--------------------- GPIO Mode Configuration ------------------------*/
187 /* In case of Output or Alternate function mode selection */
188 if(((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) || \
189 (GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
190 {
191 /* Check the Speed parameter */
192 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
193 /* Configure the IO Speed */
194 temp = GPIOx->OSPEEDR;
195 temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
196 temp |= (GPIO_Init->Speed << (position * 2U));
197 GPIOx->OSPEEDR = temp;
198
199 /* Configure the IO Output Type */
200 temp = GPIOx->OTYPER;
201 temp &= ~(GPIO_OTYPER_OT_0 << position) ;
202 temp |= (((GPIO_Init->Mode & OUTPUT_TYPE) >> OUTPUT_TYPE_Pos) << position);
203 GPIOx->OTYPER = temp;
204 }
205
206 if((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG)
207 {
208 /* Check the parameters */
209 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
210
211 /* Activate the Pull-up or Pull down resistor for the current IO */
212 temp = GPIOx->PUPDR;
213 temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
214 temp |= ((GPIO_Init->Pull) << (position * 2U));
215 GPIOx->PUPDR = temp;
216 }
217
218 /* In case of Alternate function mode selection */
219 if((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
220 {
221 /* Check the Alternate function parameter */
222 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
223 /* Configure Alternate function mapped with the current IO */
224 temp = GPIOx->AFR[position >> 3U];
225 temp &= ~(0xFU << ((uint32_t)(position & 0x07U) * 4U)) ;
226 temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & 0x07U) * 4U));
227 GPIOx->AFR[position >> 3U] = temp;
228 }
229
230 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
231 temp = GPIOx->MODER;
232 temp &= ~(GPIO_MODER_MODER0 << (position * 2U));
233 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
234 GPIOx->MODER = temp;
235
236 /*--------------------- EXTI Mode Configuration ------------------------*/
237 /* Configure the External Interrupt or event for the current IO */
238 if((GPIO_Init->Mode & EXTI_MODE) != 0x00U)
239 {
240 /* Enable SYSCFG Clock */
242
243 temp = SYSCFG->EXTICR[position >> 2U];
244 temp &= ~(0x0FU << (4U * (position & 0x03U)));
245 temp |= ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U)));
246 SYSCFG->EXTICR[position >> 2U] = temp;
247
248 /* Clear Rising Falling edge configuration */
249 temp = EXTI->RTSR;
250 temp &= ~((uint32_t)iocurrent);
251 if((GPIO_Init->Mode & TRIGGER_RISING) != 0x00U)
252 {
253 temp |= iocurrent;
254 }
255 EXTI->RTSR = temp;
256
257 temp = EXTI->FTSR;
258 temp &= ~((uint32_t)iocurrent);
259 if((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00U)
260 {
261 temp |= iocurrent;
262 }
263 EXTI->FTSR = temp;
264
265 temp = EXTI->EMR;
266 temp &= ~((uint32_t)iocurrent);
267 if((GPIO_Init->Mode & EXTI_EVT) != 0x00U)
268 {
269 temp |= iocurrent;
270 }
271 EXTI->EMR = temp;
272
273 /* Clear EXTI line configuration */
274 temp = EXTI->IMR;
275 temp &= ~((uint32_t)iocurrent);
276 if((GPIO_Init->Mode & EXTI_IT) != 0x00U)
277 {
278 temp |= iocurrent;
279 }
280 EXTI->IMR = temp;
281 }
282 }
283 }
284}
285
294void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
295{
296 uint32_t position;
297 uint32_t ioposition = 0x00U;
298 uint32_t iocurrent = 0x00U;
299 uint32_t tmp = 0x00U;
300
301 /* Check the parameters */
302 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
303
304 /* Configure the port pins */
305 for(position = 0U; position < GPIO_NUMBER; position++)
306 {
307 /* Get the IO position */
308 ioposition = 0x01U << position;
309 /* Get the current IO position */
310 iocurrent = (GPIO_Pin) & ioposition;
311
312 if(iocurrent == ioposition)
313 {
314 /*------------------------- EXTI Mode Configuration --------------------*/
315 tmp = SYSCFG->EXTICR[position >> 2U];
316 tmp &= (0x0FU << (4U * (position & 0x03U)));
317 if(tmp == ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U))))
318 {
319 /* Clear EXTI line configuration */
320 EXTI->IMR &= ~((uint32_t)iocurrent);
321 EXTI->EMR &= ~((uint32_t)iocurrent);
322
323 /* Clear Rising Falling edge configuration */
324 EXTI->FTSR &= ~((uint32_t)iocurrent);
325 EXTI->RTSR &= ~((uint32_t)iocurrent);
326
327 /* Configure the External Interrupt or event for the current IO */
328 tmp = 0x0FU << (4U * (position & 0x03U));
329 SYSCFG->EXTICR[position >> 2U] &= ~tmp;
330 }
331
332 /*------------------------- GPIO Mode Configuration --------------------*/
333 /* Configure IO Direction in Input Floating Mode */
334 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2U));
335
336 /* Configure the default Alternate Function in current IO */
337 GPIOx->AFR[position >> 3U] &= ~(0xFU << ((uint32_t)(position & 0x07U) * 4U)) ;
338
339 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
340 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
341
342 /* Configure the default value IO Output Type */
343 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ;
344
345 /* Configure the default value for IO Speed */
346 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
347 }
348 }
349}
350
375GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
376{
377 GPIO_PinState bitstatus;
378
379 /* Check the parameters */
380 assert_param(IS_GPIO_PIN(GPIO_Pin));
381
382 if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
383 {
384 bitstatus = GPIO_PIN_SET;
385 }
386 else
387 {
388 bitstatus = GPIO_PIN_RESET;
389 }
390 return bitstatus;
391}
392
410void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
411{
412 /* Check the parameters */
413 assert_param(IS_GPIO_PIN(GPIO_Pin));
415
416 if(PinState != GPIO_PIN_RESET)
417 {
418 GPIOx->BSRR = GPIO_Pin;
419 }
420 else
421 {
422 GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
423 }
424}
425
433void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
434{
435 uint32_t odr;
436
437 /* Check the parameters */
438 assert_param(IS_GPIO_PIN(GPIO_Pin));
439
440 /* get current Output Data Register value */
441 odr = GPIOx->ODR;
442
443 /* Set selected pins that were at low level, and reset ones that were high */
444 GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
445}
446
458HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
459{
460 __IO uint32_t tmp = GPIO_LCKR_LCKK;
461
462 /* Check the parameters */
463 assert_param(IS_GPIO_PIN(GPIO_Pin));
464
465 /* Apply lock key write sequence */
466 tmp |= GPIO_Pin;
467 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
468 GPIOx->LCKR = tmp;
469 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
470 GPIOx->LCKR = GPIO_Pin;
471 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
472 GPIOx->LCKR = tmp;
473 /* Read LCKR register. This read is mandatory to complete key lock sequence */
474 tmp = GPIOx->LCKR;
475
476 /* Read again in order to confirm lock is active */
477 if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
478 {
479 return HAL_OK;
480 }
481 else
482 {
483 return HAL_ERROR;
484 }
485}
486
492void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
493{
494 /* EXTI line interrupt detected */
495 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
496 {
497 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
498 HAL_GPIO_EXTI_Callback(GPIO_Pin);
499 }
500}
501
507__weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
508{
509 /* Prevent unused argument(s) compilation warning */
510 UNUSED(GPIO_Pin);
511 /* NOTE: This function Should not be modified, when the callback is needed,
512 the HAL_GPIO_EXTI_Callback could be implemented in the user file
513 */
514}
515
525#endif /* HAL_GPIO_MODULE_ENABLED */
#define GPIO_GET_INDEX(__GPIOx__)
#define IS_GPIO_AF(AF)
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
De-initializes the GPIOx peripheral registers to their default reset values.
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
EXTI line detection callbacks.
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
This function handles EXTI interrupt request.
HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Locks GPIO Pins configuration registers.
GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Reads the specified input port pin.
void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
Sets or clears the selected data port bit.
void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Toggles the specified GPIO pins.
#define __HAL_GPIO_EXTI_GET_IT(__EXTI_LINE__)
Checks whether the specified EXTI line is asserted or not.
#define __HAL_GPIO_EXTI_CLEAR_IT(__EXTI_LINE__)
Clears the EXTI's line pending bits.
GPIO_PinState
GPIO Bit SET and Bit RESET enumeration.
@ GPIO_PIN_SET
@ GPIO_PIN_RESET
#define MODE_OUTPUT
#define EXTI_MODE
#define EXTI_EVT
#define MODE_AF
#define MODE_ANALOG
#define TRIGGER_FALLING
#define EXTI_IT
#define GPIO_MODE
#define TRIGGER_RISING
#define GPIO_NUMBER
#define OUTPUT_TYPE
#define OUTPUT_TYPE_Pos
#define IS_GPIO_PULL(PULL)
#define IS_GPIO_PIN_ACTION(ACTION)
#define IS_GPIO_SPEED(SPEED)
#define IS_GPIO_MODE(MODE)
#define IS_GPIO_PIN(PIN)
#define __HAL_RCC_SYSCFG_CLK_ENABLE()
#define assert_param(expr)
This file contains all the functions prototypes for the HAL module driver.
HAL_StatusTypeDef
HAL Status structures definition
@ HAL_ERROR
@ HAL_OK
#define UNUSED(X)
GPIO Init structure definition