GPIO Task Event blocks (GPIOTE)
A GPIOTE block enables GPIOs on Port 0 to generate events on pin state change which can be used to carry out tasks through the PPI system. A GPIO can also be driven to change state on system events using the PPI system. Low power detection of pin state changes on Port 0 is possible when in System ON or System OFF.
GPIOTE块使端口0上的GPIOs能够在引脚状态变化上产生事件,这些事件可以用于通过PPI系统执行任务。 还可以使用PPI系统驱动GPIO来改变系统事件的状态。 当系统开启或系统关闭时,端口0上的引脚状态变化的低功耗检测是可能的。
Instance | Number of GPIOTE channels |
---|---|
GPIOTE | 4 |
GPIOTE提供了四个通道,四个通道通过GONFIG[0]——CONFIG[3]来配置,这四个通道可通过单独设置,分别和普通的GPIO绑定。当需要使用GPIOTE中断功能时可以设置相关寄存器的相应位让某个通道做为event,同时配置event触发动作。比如绑定的GPIO有上升沿或者下降沿触发event,然后配置中断使能寄存器,配置让其event时触发中断。
官方手册下载:
https://infocenter.nordicsemi.com/pdf/nRF51802_PS_v1.2.pdf
https://infocenter.nordicsemi.com/pdf/nRF51822_PS_v3.4.pdf
https://infocenter.nordicsemi.com/pdf/nRF51422_PS_v3.3.pdf
/********************************************************************************
* @file bsp_exti.c
* @author jianqiang.xue
* @version V1.0.0
* @date 2021-04-09
* @brief NULL
********************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include "RTE_Components.h"
#include CMSIS_device_header
#include "nrf_drv_gpiote.h"
/* Private Includes ----------------------------------------------------------*/
/* Private Define ------------------------------------------------------------*/
/* Private Variables ---------------------------------------------------------*/
bool bsp_exti_init = false;
typedef void(*bsp_gpio_exti_callback)(void *gpiox, uint16_t gpio_pin);
static bsp_gpio_exti_callback g_irq_callback;
/* Public Function Prototypes ------------------------------------------------*/
/**
* @brief 设置外部中断NVIC
* @note NULL
* @param irqn: 中断号(在typedef enum IRQn中,例如:USART1_IRQn)
* @param priority: 中断优先级
* @retval None
*/
void bsp_exit_set(uint8_t irqn, uint32_t priority)
{
bsp_exti_init = true;
nrf_drv_gpiote_in_event_enable(irqn, true);
}
/**
* @brief 清除外部中断设置
* @note NULL
* @param irqn: 中断号(在typedef enum IRQn中,例如:USART1_IRQn)
* @retval None
*/
void bsp_exit_clear_set(uint8_t irqn)
{
nrf_drv_gpiote_in_event_enable(irqn, false);
}
/**
* @brief 清除外部中断标志位
* @note NULL
* @param *gpiox: 预留
* @param pin: 引脚号
* @retval None
*/
void bsp_exit_clear_flag(void *gpiox, uint8_t pin)
{
}
/**
* @brief 注册外部中事件回调函数
* @note NULL
* @param *event: 事件函数
* @retval None
*/
bool bsp_gpio_exit_irq_register_callback(void *event)
{
if (g_irq_callback != NULL)
{
return false;
}
else
{
g_irq_callback = (bsp_gpio_exti_callback)event;
}
return true;
}
void gpiote_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
if (g_irq_callback)
{
g_irq_callback(NULL, pin);
}
}
/********************************************************************************
* @file bsp_exti.h
* @author jianqiang.xue
* @version V1.0.0
* @date 2021-04-09
* @brief NULL
********************************************************************************/
#ifndef __BSP_EXTI_H
#define __BSP_EXTI_H
/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
/* Public Function Prototypes ------------------------------------------------*/
void bsp_exit_set(uint8_t irqn, uint32_t priority);
void bsp_exit_clear_set(uint8_t irqn);
void bsp_exit_clear_flag(void *gpiox, uint8_t pin);
// 引脚外部中断回调
bool bsp_gpio_exit_irq_register_callback(void *event);
#endif