/*
 * Copyright (c) 2021 Pimax. All rights reserved.
 * Author          Date          ChangeLog
 * Great Master    2021-03-26    First version
 * Great Master    2021-04-20    add handler register function
*/
#include "gpio.h"
#include "app_error.h"
#include "nrf_log.h"


ret_code_t gpio_init(nrf_drv_gpiote_evt_handler_t handler)
{
    ret_code_t err_code;

    err_code = nrf_drv_gpiote_init();

    /* output pin configure */
    nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
    err_code |= nrf_drv_gpiote_out_init(DRY_BAT_CTRL_PIN, &out_config);
    err_code |= nrf_drv_gpiote_out_init(LI_BAT_CTRL_PIN, &out_config);
    err_code |= nrf_drv_gpiote_out_init(BAT_CHARGE_CTRL_PIN, &out_config);
    
    /* led */
#ifdef BOARD_PCA10040
    
#endif

    /* interrupt pin configure */
    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
    in_config.pull = NRF_GPIO_PIN_PULLUP;

    //err_code |= nrf_drv_gpiote_in_init(BAT_CHG_INT_PIN, &in_config, handler);
    //nrf_drv_gpiote_in_event_enable(BAT_CHG_INT_PIN, true);

    in_config.sense = NRF_GPIOTE_POLARITY_LOTOHI;
    in_config.pull = NRF_GPIO_PIN_PULLDOWN;
    err_code |= nrf_drv_gpiote_in_init(IMU_RDY_INT_PIN, &in_config, handler);
    nrf_drv_gpiote_in_event_enable(IMU_RDY_INT_PIN, true);

    //err_code |= nrf_drv_gpiote_in_init(NVBUS_DET_INT_PIN, &in_config, handler);
    //nrf_drv_gpiote_in_event_enable(NVBUS_DET_INT_PIN, true);

    return err_code;
}


