

#include "nrfx_gpiote.h"
#include "GPIO.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "SEGGER_RTT.h"
#include "boards.h"


//MPU9250 Interrupt handler
void mpu9250Interrupt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
    nrfx_gpiote_out_toggle(GPIO_0);    
}

void gpio_init(void)
{
    //SEGGER_RTT_printf(0, "Entering gpio init\n");
    
	uint32_t err_code = 0;

    if(!nrfx_gpiote_is_init())
    {
        err_code = nrfx_gpiote_init();
        //SEGGER_RTT_printf(0, "%d is the error code\n",err_code);
        APP_ERROR_CHECK(err_code);
    }

    //Test output stuff
    nrfx_gpiote_out_config_t out_config = NRFX_GPIOTE_CONFIG_OUT_SIMPLE(false);
 
    err_code = nrfx_gpiote_out_init(GPIO_0, &out_config);
    err_code = nrfx_gpiote_out_init(mpu_AD0, &out_config);
    nrfx_gpiote_out_clear(mpu_AD0);
    err_code = nrfx_gpiote_out_init(mpu_FSYNC, &out_config);
    // err_code = nrfx_gpiote_out_init(LED_BRIGHT, &out_config);
    // nrfx_gpiote_out_clear(LED_BRIGHT);
    err_code = nrfx_gpiote_out_init(LED_1, &out_config);
    err_code = nrfx_gpiote_out_init(LED_2, &out_config);
    err_code = nrfx_gpiote_out_init(LED_3, &out_config);

    //Test Input stuff
    nrfx_gpiote_in_config_t in_config = NRFX_GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
    in_config.pull = NRF_GPIO_PIN_NOPULL;

    err_code = nrfx_gpiote_in_init(mpu_INT, &in_config, mpu9250Interrupt_handler);
    err_code = nrfx_gpiote_in_init(mpu_AUX_DA, &in_config, null);
    err_code = nrfx_gpiote_in_init(mpu_AUX_CL, &in_config, null);


    APP_ERROR_CHECK(err_code);

    nrfx_gpiote_in_event_enable(mpu_INT, true);

    APP_ERROR_CHECK(err_code);
    //end test output stuff

}
