123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- /**
- ******************************************************************************
- * @file py32f002b_hal_gpio.c
- * @author MCU Application Team
- * @brief GPIO HAL module driver.
- * This file provides firmware functions to manage the following
- * functionalities of the General Purpose Input/Output (GPIO) peripheral:
- * + Initialization and de-initialization functions
- * + IO operation functions
- *
- @verbatim
- ==============================================================================
- ##### GPIO Peripheral features #####
- ==============================================================================
- [..]
- (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
- configured by software in several modes:
- (++) Input mode
- (++) Analog mode
- (++) Output mode
- (++) Alternate function mode
- (++) External interrupt/event lines
- (+) During and just after reset, the alternate functions and external interrupt
- lines are not active and the I/O ports are configured in input floating mode.
- (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
- activated or not.
- (+) In Output or Alternate mode, each IO can be configured on open-drain or push-pull
- type and the IO speed can be selected depending on the VDD value.
- (+) The microcontroller IO pins are connected to onboard peripherals/modules through a
- multiplexer that allows only one peripheral alternate function (AF) connected
- to an IO pin at a time. In this way, there can be no conflict between peripherals
- sharing the same IO pin.
- (+) All ports have external interrupt/event capability. To use external interrupt
- lines, the port must be configured in input mode. All available GPIO pins are
- connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
- (+) The external interrupt/event controller consists of up to 28 edge detectors
- (16 lines are connected to GPIO) for generating event/interrupt requests (each
- input line can be independently configured to select the type (interrupt or event)
- and the corresponding trigger event (rising or falling or both). Each line can
- also be masked independently.
- ##### How to use this driver #####
- ==============================================================================
- [..]
- (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
- (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
- (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
- (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
- structure.
- (++) In case of Output or alternate function mode selection: the speed is
- configured through "Speed" member from GPIO_InitTypeDef structure.
- (++) In alternate mode is selection, the alternate function connected to the IO
- is configured through "Alternate" member from GPIO_InitTypeDef structure.
- (++) Analog mode is required when a pin is to be used as ADC channel
- or DAC output.
- (++) In case of external interrupt/event selection the "Mode" member from
- GPIO_InitTypeDef structure select the type (interrupt or event) and
- the corresponding trigger event (rising or falling or both).
- (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
- mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
- HAL_NVIC_EnableIRQ().
- (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
- (#) To set/reset the level of a pin configured in output mode use
- HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
- (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
- (#) During and just after reset, the alternate functions are not
- active and the GPIO pins are configured in input floating mode (except JTAG
- pins).
- (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
- when the LSE oscillator is off. The LSE has priority over the GPIO function.
- (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
- general purpose PF0 and PF1, respectively, when the HSE oscillator is off.
- The HSE has priority over the GPIO function.
- @endverbatim
- ******************************************************************************
- *
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2023 Puya Semiconductor Co.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by Puya under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2016 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "py32f0xx_hal.h"
- /** @addtogroup PY32F002B_HAL_Driver
- * @{
- */
- /** @addtogroup GPIO
- * @{
- */
- /** MISRA C:2012 deviation rule has been granted for following rules:
- * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
- * range of the shift operator in following API :
- * HAL_GPIO_Init
- * HAL_GPIO_DeInit
- */
- #ifdef HAL_GPIO_MODULE_ENABLED
- /* Private typedef -----------------------------------------------------------*/
- /* Private defines ------------------------------------------------------------*/
- /** @defgroup GPIO_Private_Constants GPIO Private Constants
- * @{
- */
- #define GPIO_MODE (0x00000003u)
- #define EXTI_MODE (0x10000000u)
- #define GPIO_MODE_IT (0x00010000u)
- #define GPIO_MODE_EVT (0x00020000u)
- #define RISING_EDGE (0x00100000u)
- #define FALLING_EDGE (0x00200000u)
- #define GPIO_OUTPUT_TYPE (0x00000010u)
- #define GPIO_NUMBER (16u)
- /**
- * @}
- */
- #define __GPIO_LOCK_KEY_Msk (0x00010000U)
- /* Private macros ------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Exported functions --------------------------------------------------------*/
- /** @addtogroup GPIO_Exported_Functions
- * @{
- */
- /** @addtogroup GPIO_Exported_Functions_Group1
- * @brief Initialization and Configuration functions
- *
- @verbatim
- ===============================================================================
- ##### Initialization and de-initialization functions #####
- ===============================================================================
- @endverbatim
- * @{
- */
- /**
- * @brief Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
- * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
- * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
- * the configuration information for the specified GPIO peripheral.
- * @retval None
- */
- void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
- {
- uint32_t position = 0x00u;
- uint32_t iocurrent;
- uint32_t temp;
- /* Check the parameters */
- assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
- assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
- assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
- assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
- /* Configure the port pins */
- while (((GPIO_Init->Pin) >> position) != 0x00u)
- {
- /* Get current io position */
- iocurrent = (GPIO_Init->Pin) & (1uL << position);
- if (iocurrent != 0x00u)
- {
- /*--------------------- GPIO Mode Configuration ------------------------*/
- /* In case of Alternate function mode selection */
- if ((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
- {
- /* Check the Alternate function parameters */
- assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
- assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
- /* Configure Alternate function mapped with the current IO */
- temp = GPIOx->AFR[position >> 3u];
- temp &= ~(0xFu << ((position & 0x07u) * 4u));
- temp |= ((GPIO_Init->Alternate) << ((position & 0x07u) * 4u));
- GPIOx->AFR[position >> 3u] = temp;
- }
- /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
- temp = GPIOx->MODER;
- temp &= ~(GPIO_MODER_MODE0 << (position * 2u));
- temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2u));
- GPIOx->MODER = temp;
- /* In case of Output or Alternate function mode selection */
- if ((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
- (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
- {
- /* Check the Speed parameter */
- assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
- /* Configure the IO Speed */
- temp = GPIOx->OSPEEDR;
- temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
- temp |= (GPIO_Init->Speed << (position * 2u));
- GPIOx->OSPEEDR = temp;
- /* Configure the IO Output Type */
- temp = GPIOx->OTYPER;
- temp &= ~(GPIO_OTYPER_OT0 << position) ;
- temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4u) << position);
- GPIOx->OTYPER = temp;
- }
- /* Activate the Pull-up or Pull down resistor for the current IO */
- temp = GPIOx->PUPDR;
- temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
- temp |= ((GPIO_Init->Pull) << (position * 2u));
- GPIOx->PUPDR = temp;
- /*--------------------- EXTI Mode Configuration ------------------------*/
- /* Configure the External Interrupt or event for the current IO */
- if ((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
- {
- temp = EXTI->EXTICR[position >> 2u];
- temp &= ~(0x0FuL << (8u * (position & 0x03u)));
- temp |= (GPIO_GET_INDEX(GPIOx) << (8u * (position & 0x03u)));
- EXTI->EXTICR[position >> 2u] = temp;
- /* Clear EXTI line configuration */
- temp = EXTI->IMR;
- temp &= ~(iocurrent);
- if ((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
- {
- temp |= iocurrent;
- }
- EXTI->IMR = temp;
- temp = EXTI->EMR;
- temp &= ~(iocurrent);
- if ((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
- {
- temp |= iocurrent;
- }
- EXTI->EMR = temp;
- /* Clear Rising Falling edge configuration */
- temp = EXTI->RTSR;
- temp &= ~(iocurrent);
- if ((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
- {
- temp |= iocurrent;
- }
- EXTI->RTSR = temp;
- temp = EXTI->FTSR;
- temp &= ~(iocurrent);
- if ((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
- {
- temp |= iocurrent;
- }
- EXTI->FTSR = temp;
- }
- }
- position++;
- }
- }
- /**
- * @brief De-initialize the GPIOx peripheral registers to their default reset values.
- * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
- * @param GPIO_Pin specifies the port bit to be written.
- * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
- * @retval None
- */
- void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
- {
- uint32_t position = 0x00u;
- uint32_t iocurrent;
- uint32_t tmp;
- /* Check the parameters */
- assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
- assert_param(IS_GPIO_PIN(GPIO_Pin));
- /* Configure the port pins */
- while ((GPIO_Pin >> position) != 0x00u)
- {
- /* Get current io position */
- iocurrent = (GPIO_Pin) & (1uL << position);
- if (iocurrent != 0x00u)
- {
- /*------------------------- EXTI Mode Configuration --------------------*/
- /* Clear the External Interrupt or Event for the current IO */
- tmp = EXTI->EXTICR[position >> 2u];
- tmp &= (0x0FuL << (8u * (position & 0x03u)));
- if (tmp == (GPIO_GET_INDEX(GPIOx) << (8u * (position & 0x03u))))
- {
- /* Clear EXTI line configuration */
- EXTI->IMR &= ~(iocurrent);
- EXTI->EMR &= ~(iocurrent);
- /* Clear Rising Falling edge configuration */
- EXTI->RTSR &= ~(iocurrent);
- EXTI->FTSR &= ~(iocurrent);
- tmp = 0x0FuL << (8u * (position & 0x03u));
- EXTI->EXTICR[position >> 2u] &= ~tmp;
- }
- /*------------------------- GPIO Mode Configuration --------------------*/
- /* Configure IO in Analog Mode */
- GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2u));
- /* Configure the default Alternate Function in current IO */
- GPIOx->AFR[position >> 3u] &= ~(0xFu << ((position & 0x07u) * 4u)) ;
- /* Configure the default value for IO Speed */
- GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
- /* Configure the default value IO Output Type */
- GPIOx->OTYPER &= ~(GPIO_OTYPER_OT0 << position) ;
- /* Deactivate the Pull-up and Pull-down resistor for the current IO */
- GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
- }
- position++;
- }
- }
- /**
- * @}
- */
- /** @addtogroup GPIO_Exported_Functions_Group2
- * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
- *
- @verbatim
- ===============================================================================
- ##### IO operation functions #####
- ===============================================================================
- @endverbatim
- * @{
- */
- /**
- * @brief Read the specified input port pin.
- * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
- * @param GPIO_Pin specifies the port bit to read.
- * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
- * @retval The input port pin value.
- */
- GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
- {
- GPIO_PinState bitstatus;
- /* Check the parameters */
- assert_param(IS_GPIO_PIN(GPIO_Pin));
- if ((GPIOx->IDR & GPIO_Pin) != 0x00u)
- {
- bitstatus = GPIO_PIN_SET;
- }
- else
- {
- bitstatus = GPIO_PIN_RESET;
- }
- return bitstatus;
- }
- /**
- * @brief Set or clear the selected data port bit.
- * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
- * accesses. In this way, there is no risk of an IRQ occurring between
- * the read and the modify access.
- * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
- * @param GPIO_Pin specifies the port bit to be written.
- * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
- * @param PinState specifies the value to be written to the selected bit.
- * This parameter can be one of the GPIO_PinState enum values:
- * @arg GPIO_PIN_RESET: to clear the port pin
- * @arg GPIO_PIN_SET: to set the port pin
- * @retval None
- */
- void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
- {
- /* Check the parameters */
- assert_param(IS_GPIO_PIN(GPIO_Pin));
- assert_param(IS_GPIO_PIN_ACTION(PinState));
- if (PinState != GPIO_PIN_RESET)
- {
- GPIOx->BSRR = (uint32_t)GPIO_Pin;
- }
- else
- {
- GPIOx->BRR = (uint32_t)GPIO_Pin;
- }
- }
- /**
- * @brief Toggle the specified GPIO pin.
- * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
- * @param GPIO_Pin specifies the pin to be toggled.
- * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
- * @retval None
- */
- void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
- {
- /* Check the parameters */
- assert_param(IS_GPIO_PIN(GPIO_Pin));
- if ((GPIOx->ODR & GPIO_Pin) != 0x00u)
- {
- GPIOx->BRR = (uint32_t)GPIO_Pin;
- }
- else
- {
- GPIOx->BSRR = (uint32_t)GPIO_Pin;
- }
- }
- /**
- * @brief Lock GPIO Pins configuration registers.
- * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
- * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
- * @note The configuration of the locked GPIO pins can no longer be modified
- * until the next reset.
- * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
- * @param GPIO_Pin specifies the port bits to be locked.
- * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
- * @retval None
- */
- HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
- {
- __IO uint32_t tmp = GPIO_LCKR_LCKK;
- /* Check the parameters */
- assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
- assert_param(IS_GPIO_PIN(GPIO_Pin));
- /* Apply lock key write sequence */
- tmp |= GPIO_Pin;
- /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
- GPIOx->LCKR = tmp;
- /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
- GPIOx->LCKR = GPIO_Pin;
- /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
- GPIOx->LCKR = tmp;
- /* Read LCKK register. This read is mandatory to complete key lock sequence */
- tmp = GPIOx->LCKR;
- /* read again in order to confirm lock is active */
- if ((GPIOx->LCKR & __GPIO_LOCK_KEY_Msk) != 0x00u)
- {
- return HAL_OK;
- }
- else
- {
- return HAL_ERROR;
- }
- }
- __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(GPIO_Pin);
- /* NOTE: This function should not be modified, when the callback is needed,
- the HAL_GPIO_EXTI_Callback could be implemented in the user file
- */
- }
- /**
- * @brief Handle EXTI interrupt request.
- * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
- * @retval None
- */
- void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
- {
- /* EXTI line interrupt detected */
- if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
- {
- __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
- HAL_GPIO_EXTI_Callback(GPIO_Pin);
- }
- }
- /**
- * @}
- */
- /**
- * @}
- */
- #endif /* HAL_GPIO_MODULE_ENABLED */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT Puya *****END OF FILE****/
|