[bsp layer][nrf52832][nrf52840][nrf52810][nrf52820][bsp_gpio] GPIO configuration and use

Publisher:温馨如家Latest update time:2022-08-22 Source: csdnKeywords:nrf52832  nrf52840  nrf52810  GPIO Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

GPIO — General purpose input/output

General Purpose Input/Output (GPIO) is organized as a port of up to 32 I/Os (package dependent), allowing up to 32 pins to be accessed and controlled through a single port. Each GPIO can be individually accessed

The GPIOs have the following user-configurable characteristics:


Supports up to 32 GPIOs

8 GPIO and analog channels for SAADC, COMP or LPCOMP input

Configurable output driver strength

Internal pull-up and pull-down resistors

Wake-up from high or low trigger on all pins

Interrupt triggered by a change of state on any pin

All pins can be used by the PPI task/event system

One or more GPIO outputs can be controlled via PPI and GPIOTE channels

All pins can be individually mapped to interface blocks for layout flexibility

GPIO state changes captured on the SENSE signal can be stored in the LATCH register

The GPIO port peripheral implements up to 32 pins, PIN0 to PIN 31. Each of these pins can be individually configured in the PIN_CNF[n] register (n=0…31).


**The following parameters can be configured through these registers: **


Direction (input/output)

Driver Strength

Enable pull-up and pull-down resistors

Pin detection

Input buffer disconnected

Analog Input (for selected pins)

The PIN_CNF register is a reserved register. For more information on the reserved registers, refer to the POWER - POWER supply chapter on page 78.


Pin Configuration

Pins can be individually configured to detect high or low levels on their input levels through the SENSE field of the PIN_CNF[n] register.


When the correct level is detected on any such configured pin, the sensing mechanism will set the high detection signal.


Each pin has a separate detect signal. The default behavior, as defined in the DETECTMODE register, is that the detect signals from all pins on the GPIO port are combined into a common detect signal that is routed throughout the system and can then be utilized by other peripherals.

See Figure 21: GPIO Port and GPIO Pin Details on page 112. This mechanism works in both ON and OFF modes.


insert image description here

Download the official manual:

https://infocenter.nordicsemi.com/pdf/nRF52810_PS_v1.3.pdf

https://infocenter.nordicsemi.com/pdf/nRF52820_PS_v1.2.pdf

https://infocenter.nordicsemi.com/pdf/nRF52832_PS_v1.7.pdf

https://infocenter.nordicsemi.com/pdf/nRF52840_PS_v1.5.pdf


/********************************************************************************

* @file    bsp_gpio.c

* @author jianqiang.xue

* @version V1.0.0

* @date    2021-04-09

* @brief gpio initialization

********************************************************************************/


/* Includes ------------------------------------------------------------------*/

#include


#include "RTE_Components.h"

#include CMSIS_device_header

#include "nrf_gpio.h"

#include "nrf_drv_gpiote.h"


#include "sdk_common.h"


#include "bsp_exti.h"

#include "bsp_gpio.h"


/* Private Includes ----------------------------------------------------------*/

#include "business_gpio.h"

#include "business_function.h"


/* Private Variables ---------------------------------------------------------*/

/* Private Function Prototypes -----------------------------------------------*/

static nrf_gpiote_polarity_t get_exti_event(bsp_gpio_exti_int_event_t exti_type)

{

    if (BSP_GPIO_EXTI_INT_LOWFALL == exti_type)

    {

        return NRF_GPIOTE_POLARITY_HITOLO;

    }

    else if (BSP_GPIO_EXTI_INT_HIGHRISE == exti_type)

    {

        return NRF_GPIOTE_POLARITY_LOTOHI;

    }

    else if (BSP_GPIO_EXTI_INT_FALLRISE == exti_type)

    {

        return NRF_GPIOTE_POLARITY_TOGGLE;

    }

    else

    {

        return NRF_GPIOTE_POLARITY_TOGGLE;

    }

}


static nrf_gpio_pin_pull_t get_nrf_pull(bsp_gpio_pin_pull_t pull)

{

    if (pull == BSP_GPIO_PIN_PULLUP)

    {

        return NRF_GPIO_PIN_PULLUP;

    }

    else if(pull == BSP_GPIO_PIN_PULLDOWN)

    {

        return NRF_GPIO_PIN_PULLDOWN;

    }

    else

    {

        return NRF_GPIO_PIN_NOPULL;

    }

}


/* Public Function Prototypes ------------------------------------------------*/

/**

 * @brief [Deinitialization] Disable the specified pin function (restore to floating input)

 * @note   NULL

 * @param  *gpiox: NULL

 * @param pin: pin number

 * @retval None

 */

void bsp_gpio_deinit(void *gpiox, uint8_t pin)

{

    nrf_gpio_cfg_default(pin);

}


/**

 * @brief [Initialization] Set the pin to output mode

 * @note   NULL

 * @param  *gpiox: NULL

 * @param pin: pin number

 * @param out_mode: BSP_GPIO_PIN_OUT_OD open-drain output, BSP_GPIO_PIN_OUT_PP push-to-disable output, BSP_GPIO_PIN_AF_OD multiplexed open-drain, BSP_GPIO_PIN_AF_PP multiplexed push-to-disable

 * @retval None

 */

void bsp_gpio_init_output(void *gpiox, uint8_t pin, bsp_gpio_pin_out_t out_mode)

{

    nrf_gpio_cfg_output(pin);

}


/**

 * @brief [Initialization] Set the pin to input mode

 * @note   NULL

 * @param  *gpiox: NULL

 * @param pin: pin number

 * @param pull: BSP_GPIO_PIN_NOPULL no pull-up or pull-down, BSP_GPIO_PIN_PULLUP pull-up input, BSP_GPIO_PIN_PULLDOWN pull-down input

 * @retval None

 */

void bsp_gpio_init_input(void *gpiox, uint8_t pin, bsp_gpio_pin_pull_t pull)

{

    nrf_gpio_cfg_input(pin, get_nrf_pull(pull));

}


extern void gpiote_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action);

/**

  * @brief [Initialization] Set the pin to [input + interrupt] mode

  * @param  gpiox           -- NULL

  * @param pin -- pin number

  * @param  irqn            -- NULL

  * @param exti_type -- BSP_GPIO_EXTI_INT_LEVEL level trigger, BSP_GPIO_EXTI_INT_EDGE edge trigger

  * @param exti_event -- BSP_GPIO_EXTI_INT_LOWFALL low level trigger (falling edge), BSP_GPIO_EXTI_INT_HIGHRISE high level trigger (rising edge), BSP_GPIO_EXTI_INT_FALLRISE high or low level trigger or any level change

  * @param pull -- BSP_GPIO_PIN_NOPULL no pull-up or pull-down, BSP_GPIO_PIN_PULLUP pull-up input, BSP_GPIO_PIN_PULLDOWN pull-down input

  */

void bsp_gpio_init_input_exit(void *gpiox, uint8_t pin, uint8_t irqn,

                              bsp_gpio_exti_int_type_t exti_type,

                              bsp_gpio_exti_int_event_t exti_event,

                              bsp_gpio_pin_pull_t pull)

{

    static bool flag = 0;

    ret_code_t err_code;


    if (flag == 0)

    {

        err_code = nrf_drv_gpiote_init();

        APP_ERROR_CHECK(err_code);

        flag = 1;

    }


    nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(exti_type);

    config.pull = get_nrf_pull(pull);

    config.sense = get_exit_event(exit_event);

    err_code = nrf_drv_gpiote_in_init(pin, &config, gpiote_event_handler);

    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_event_enable(pin, true);

}


/**

 * @brief Set the pin level status

 * @note   NULL

 * @param  *gpiox: NULL

 * @param pin: pin number

 * @param state: BSP_GPIO_PIN_RESET low level, BSP_GPIO_PIN_SET high level

 * @retval None

 */

void bsp_gpio_set_pin(void *gpiox, uint8_t pin, bsp_gpio_pin_state_t state)

{

    if (state)

    {

        nrf_gpio_pin_set(pin);

    }

    else

    {

        nrf_gpio_pin_clear(pin);

    }

}


/**

 * @brief Flip pin level state

 * @note   NULL

 * @param  *gpiox: NULL

 * @param pin: pin number

 * @retval None

 */

void bsp_gpio_set_toggle(void *gpiox, uint8_t pin)

{

    nrf_gpio_pin_toggle(pin);

}


/**

 * @brief Get the specified gpio status

 * @note   NULL

 * @param  *gpiox: NULL

 * @param pin: pin number

 * @retval 0 -- low level, 1 -- high level

 */

bool bsp_gpio_get_state(void *gpiox, uint8_t pin)

{

    return (bool)nrf_gpio_pin_read(pin);

}


/**

 * @brief Multiplex the specified pin as an ADC pin (multiplex analog input)

 * @note   NULL

 * @param  *gpiox: NULL

 * @param pin: pin number

 * @retval None

 */

void bsp_gpio_init_adc(void *gpiox, uint8_t pin)

{

}


/**

 * @brief Initialize i2c pins

 * @note   NULL

 * @param  *gpiox: NULL

 * @param pin: pin number

 * @param arf: reuse value

 * @retval None

 */

void bsp_gpio_init_i2c(void *gpiox, uint8_t pin, uint8_t arf)

{

}


/**

 * @brief Initialize uart pin

 * @note   NULL

 * @param  *gpiox: NULL

 * @param pin: pin number

 * @param arf: reuse value

 * @retval None

 */

void bsp_gpio_init_uart(void *gpiox, uint8_t pin, uint8_t arf)

{

}


/**

 * @brief Initialize tim_ch pin

 * @note   NULL

 * @param  *gpiox: NULL

 * @param pin: pin number

 * @param arf: reuse value

 * @retval None

 */

void bsp_gpio_init_tim(void *gpiox, uint8_t pin, uint8_t arf)

{

}


/********************************************************************************

* @file    bsp_gpio.h

* @author jianqiang.xue

* @version V1.0.0

* @date    2021-04-09

* @brief GPIO control

********************************************************************************/


#ifndef __BSP_GPIO_H

#define __BSP_GPIO_H


/* Includes ------------------------------------------------------------------*/

#include

#include


/* Public enum ---------------------------------------------------------------*/

/**

 * @brief  GPIO Bit SET and Bit RESET enumeration

 */

typedef enum

{

    BSP_GPIO_PIN_RESET = 0U,

    BSP_GPIO_PIN_SET

} bsp_gpio_pin_state_t;


typedef enum

{

    BSP_GPIO_PIN_NOPULL = 0U,

    BSP_GPIO_PIN_PULLUP,

    BSP_GPIO_PIN_PULLDOWN

} bsp_gpio_pin_pull_t;


typedef enum

{

    BSP_GPIO_PIN_OUT_OD = 0U,

    BSP_GPIO_PIN_OUT_PP,

    BSP_GPIO_PIN_AF_OD,

    BSP_GPIO_PIN_AF_PP

} bsp_gpio_pin_out_t;


typedef enum

{

    BSP_GPIO_EXTI_INT_LEVEL = 0U, // Level trigger

    BSP_GPIO_EXTI_INT_EDGE, // Edge triggered

} bsp_gpio_exti_int_type_t;


typedef enum

{

    BSP_GPIO_EXTI_INT_LOWFALL = 0U, // Low level trigger (falling edge)

    BSP_GPIO_EXTI_INT_HIGHRISE, // High level trigger (rising and falling edge)

    BSP_GPIO_EXTI_INT_FALLRISE // High or low level trigger or any level change

} bsp_gpio_exti_int_event_t;


/* Public Function Prototypes ------------------------------------------------*/


// General pin initialization


void bsp_gpio_deinit(void *gpiox, uint8_t pin);

void bsp_gpio_init_output(void *gpiox, uint8_t pin, bsp_gpio_pin_out_t out_mode);

void bsp_gpio_init_input(void *gpiox, uint8_t pin, bsp_gpio_pin_pull_t pull);

void bsp_gpio_init_input_exit(void *gpiox, uint8_t pin, uint8_t irqn, bsp_gpio_exti_int_type_t exti_type, bsp_gpio_exti_int_event_t exti_event, bsp_gpio_pin_pull_t pull);


// Initialize peripheral pins


void bsp_gpio_init_adc(void *gpiox, uint8_t pin);

void bsp_gpio_init_i2c(void *gpiox, uint8_t pin, uint8_t arf);

[1] [2]
Keywords:nrf52832  nrf52840  nrf52810  GPIO Reference address:[bsp layer][nrf52832][nrf52840][nrf52810][nrf52820][bsp_gpio] GPIO configuration and use

Previous article:[BSP layer] [nrf52832] [nrf52840] [nrf52810] [nrf52820] [bsp_exti] exti/gpioe configuration and use
Next article:[nrf51][nrf52] Developing ibeacon guide

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号