Nordic gpiote event

#include "stdint.h"
#include "nrf_delay.h"
#include "nrfx_gpiote.h"
#include "nrf_drv_gpiote.h"
#include "nrf_log.h"
#include "app_bsp.h"

#define CHARGE_STATE		NRF_GPIO_PIN_MAP(0, 29)
#define CHARGE_LED			NRF_GPIO_PIN_MAP(1, 11)

void bsp_gpio_init(void)
{
    ret_code_t err_code;
    //
    //配置充电监测GPIOTE 事件,并做指示
    //
	if (!nrf_drv_gpiote_is_init())
	{
		err_code = nrf_drv_gpiote_init();
		APP_ERROR_CHECK(err_code);
	}

    // 使能gpiote电平变化事件
    nrf_drv_gpiote_in_config_t init_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
    init_config.pull = NRF_GPIO_PIN_PULLUP;


    err_code = nrf_drv_gpiote_in_init(CHARGE_STATE, &init_config, pwr_monitoring_event_callback);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_event_enable(CHARGE_STATE, true);
	
	nrf_delay_ms(10);
	
	nrf_gpio_cfg_output(CHARGE_LED);
	nrf_gpio_pin_write(CHARGE_LED, 0);
}


void pwr_monitoring_event_callback(nrf_drv_gpiote_pin_t pin,nrf_gpiote_polarity_t action)
{
    uint32_t pin_level;
    UNUSED_PARAMETER(pin_level);
    pin_level = nrf_gpio_pin_read(pin);

    if (NRF_GPIOTE_POLARITY_HITOLO == action)
    {
        NRF_LOG_INFO("NRF_GPIOTE_POLARITY_HITOLO");

    }
    else if (NRF_GPIOTE_POLARITY_LOTOHI == action)
    {
        NRF_LOG_INFO("NRF_GPIOTE_POLARITY_LOTOHI");
    }
    else if(NRF_GPIOTE_POLARITY_TOGGLE == action)
    {
        //NRF_LOG_INFO("NRF_GPIOTE_POLARITY_TOGGLE %d",pin_level);
        nrf_gpio_pin_toggle(CHARGE_LED);
    }

}

posted @ 2020-12-11 09:36  duapple  阅读(7)  评论(0编辑  收藏  举报  来源