/** 
 * @file    gpio.c
 * @brief   GPIO module
 * @author  Kenny Tran
 * @date    Mar 01 2024
 * @version 0.0.1
 *
 * @section LICENSES
 * Copyright (C) Morgan Advanced Programmable Systems, Inc.
 */


/*--------------------------------------------------------------------------------------------------
 * INCLUDE FILES
 *------------------------------------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>

#include <zephyr/drivers/gpio.h>

#include "../lib/errno.h"

#include "gpio.h"


/*--------------------------------------------------------------------------------------------------
 * MACROS & CONSTANTS
 *------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------
 * TYPE DEFINITIONS & ENUMERATIONS
 *------------------------------------------------------------------------------------------------*/
typedef struct _gpio
{
    struct gpio_dt_spec  spec;
    struct gpio_callback cb;
    bool                 cfg_output;
    bool                 high;  /* NOTE: gpio_pin_get_dt() can't read status if configured
                                         for output.  Temporarily using flag to store state. */
    GPIO_cb_t            user_cb;
    uint32_t             int_flags;
} _gpio_s;



/*--------------------------------------------------------------------------------------------------
 * PRIVATE FUNCTION PROTOTYPES
 *------------------------------------------------------------------------------------------------*/
static void left_button_isr (void);
static void center_button_isr (void);
static void right_button_isr (void);
static void pwr_5v_isr (void);
static void led_ring_pwr_isr (void);
static void led_ring_level_shift_enable_isr (void);
static void led_ring_din_isr (void);
static void battery_check_enable_isr (void);
static void imu_int_isr (void);
static void charger_status_isr (void);


/*--------------------------------------------------------------------------------------------------
 * PRIVATE CONSTANTS & VARIABLES
 *------------------------------------------------------------------------------------------------*/
static _gpio_s _gpio_arr[GPIO_PIN_COUNT] = {
    (_gpio_s) {
        .spec       = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(left_button), gpios, {0}),
        .cb.handler = (gpio_callback_handler_t) left_button_isr,
        .cfg_output = false,
        .high       = false,
        .user_cb    = NULL,
        .int_flags  = 0
    },
    (_gpio_s) {
        .spec       = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(center_button), gpios, {0}),
        .cb.handler = (gpio_callback_handler_t) center_button_isr,
        .cfg_output = false,
        .high       = false,
        .user_cb    = NULL,
        .int_flags  = 0
    },
    (_gpio_s) {
        .spec       = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(right_button), gpios, {0}),
        .cb.handler = (gpio_callback_handler_t) right_button_isr,
        .cfg_output = false,
        .high       = false,
        .user_cb    = NULL,
        .int_flags  = 0
    },
    (_gpio_s) {
        .spec       = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(pwr_5v_enable), gpios, {0}),
        .cb.handler = (gpio_callback_handler_t) pwr_5v_isr,
        .cfg_output = false,
        .high       = false,
        .user_cb    = NULL,
        .int_flags  = 0
    },
    (_gpio_s) {
        .spec       = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(led_ring_pwr), gpios, {0}),
        .cb.handler = (gpio_callback_handler_t) led_ring_pwr_isr,
        .cfg_output = false,
        .high       = false,
        .user_cb    = NULL,
        .int_flags  = 0
    },
    (_gpio_s) {
        .spec       = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(led_ring_level_shift_enable), gpios, {0}),
        .cb.handler = (gpio_callback_handler_t) led_ring_level_shift_enable_isr,
        .cfg_output = false,
        .high       = false,
        .user_cb    = NULL,
        .int_flags  = 0
    },
    (_gpio_s) {
        .spec       = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(led_ring_din), gpios, {0}),
        .cb.handler = (gpio_callback_handler_t) led_ring_din_isr,
        .cfg_output = false,
        .high       = false,
        .user_cb    = NULL,
        .int_flags  = 0
    },
    (_gpio_s) {
        .spec       = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(battery_check_enable), gpios, {0}),
        .cb.handler = (gpio_callback_handler_t) battery_check_enable_isr,
        .cfg_output = false,
        .high       = false,
        .user_cb    = NULL,
        .int_flags  = 0
    },
    (_gpio_s) {
        .spec       = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(imu_int), gpios, {0}),
        .cb.handler = (gpio_callback_handler_t) imu_int_isr,
        .cfg_output = false,
        .high       = false,
        .user_cb    = NULL,
        .int_flags  = 0
    },
    (_gpio_s) {
        .spec       = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(charger_status), gpios, {0}),
        .cb.handler = (gpio_callback_handler_t) charger_status_isr,
        .cfg_output = false,
        .high       = false,
        .user_cb    = NULL,
        .int_flags  = 0
    },
};
static bool _gpio_init = false;


/*--------------------------------------------------------------------------------------------------
 * PUBLIC FUNCTION DEFINITIONS
 *------------------------------------------------------------------------------------------------*/
ERRNO_e GPIO_init (void)
{
    uint32_t i;

    if (_gpio_init)
    {
        return ERRNO_ALREADY_INITIALIZED;
    }

    for (i = 0; i < GPIO_PIN_COUNT; i++)
    {
        if (!gpio_is_ready_dt (&_gpio_arr[i].spec))
        {
            return ERRNO_NOT_READY;
		}
	}

    _gpio_init = true;

    return ERRNO_SUCCESS;
}

ERRNO_e GPIO_cfg (GPIO_pin_e pin, GPIO_direction_e dir, GPIO_pull_e pull, GPIO_init_e init, GPIO_int_edge_e edge, GPIO_cb_t cb)
{
    uint32_t flags = 0;

    if (!_gpio_init)
    {
        return ERRNO_NOT_INITIALIZED;
    }

    if (pin >= GPIO_PIN_COUNT)
    {
        return ERRNO_INVALID_PARAM;
    }

    if (pull == GPIO_PULL_DOWN)
    {
        flags = GPIO_PULL_DOWN;
    }
    else if (pull == GPIO_PULL_UP)
    {
        flags = GPIO_PULL_UP;
    }

    if (gpio_pin_interrupt_configure_dt (&_gpio_arr[pin].spec, GPIO_INT_DISABLE))
    {
        return ERRNO_GPIO_FAILURE;
    }

    switch (dir)
    {
        case GPIO_DIRECTION_IN:
            flags |= GPIO_INPUT;

            if (gpio_pin_configure_dt (&_gpio_arr[pin].spec, flags))
            {
                return ERRNO_GPIO_FAILURE;
            }

            switch (edge)
            {
                case GPIO_INT_RISING_EDGE:
                    /* GPIO interrupt to be triggered on pin rising edge. */
                    flags = GPIO_INT_EDGE_RISING;
                    break;
                case GPIO_INT_FALLING_EDGE:
                    /* GPIO interrupt to be triggered on pin falling edge. */
                    flags = GPIO_INT_EDGE_FALLING;
                    break;
                case GPIO_INT_BOTH_EDGES:
                    /* GPIO interrupt to be triggered on pin rising or falling edge. */
                    flags = GPIO_INT_EDGE_BOTH;
                    break;
                case GPIO_INT_PL_LOW:
                    /* GPIO interrupt to be triggered on pin physical level low. */
                    flags = GPIO_INT_LEVEL_LOW;
                    break;
                case GPIO_INT_PL_HIGH:
                    /* GPIO interrupt to be triggered on pin physical level high. */
                    flags = GPIO_INT_LEVEL_HIGH;
                    break;
                case GPIO_INT_STATE_CHANGE_LL_LOW:
                    /* GPIO interrupt to be triggered on pin state change to logical level 0. */
                    flags = GPIO_INT_EDGE_TO_INACTIVE;
                    break;
                case GPIO_INT_STATE_CHANGE_LL_HIGH:
                    /* GPIO interrupt to be triggered on pin state change to logical level 1. */
                    flags = GPIO_INT_EDGE_TO_ACTIVE;
                    break;
                case GPIO_INT_LL_LOW:
                    /* GPIO interrupt to be triggered on pin logical level 0. */
                    flags = GPIO_INT_LEVEL_INACTIVE;
                    break;
                case GPIO_INT_LL_HIGH:
                    /* GPIO interrupt to be triggered on pin logical level 1. */
                    flags = GPIO_INT_LEVEL_ACTIVE;
                    break;
            }

            /* Preserve interrupt flag settings. */
            _gpio_arr[pin].int_flags = flags;

            if (gpio_pin_interrupt_configure_dt (&_gpio_arr[pin].spec, flags))
            {
                return ERRNO_GPIO_FAILURE;
            }

            /* Set the user callback. */
            _gpio_arr[pin].user_cb = cb;
            
            gpio_init_callback (&_gpio_arr[pin].cb, (gpio_callback_handler_t) _gpio_arr[pin].cb.handler, BIT(_gpio_arr[pin].spec.pin));
            
            if (gpio_add_callback (_gpio_arr[pin].spec.port, &_gpio_arr[pin].cb))
            {
                return ERRNO_GPIO_FAILURE;
            }

            _gpio_arr[pin].cfg_output = false;

            break;

        case GPIO_DIRECTION_OUT:
            if (init == GPIO_INIT_LOW)
            {
                flags |= GPIO_OUTPUT_INACTIVE;
                _gpio_arr[pin].high = false;
            }
            else
            {
                flags |= GPIO_OUTPUT_ACTIVE;
                _gpio_arr[pin].high = true;
            }

            if (gpio_pin_configure_dt (&_gpio_arr[pin].spec, flags))
            {
                return ERRNO_GPIO_FAILURE;
            }

            _gpio_arr[pin].cfg_output = true;

            break;
    }
    
    return ERRNO_SUCCESS;
}

ERRNO_e GPIO_level_set (GPIO_pin_e pin, bool high)
{
    if (pin >= GPIO_PIN_COUNT)
    {
        return ERRNO_INVALID_PARAM;
    }

    /* Check that pin is not configured for input. */
    if (!_gpio_arr[pin].cfg_output)
    {
        return ERRNO_GPIO_FAILURE;
    }

    if (gpio_pin_set_dt (&_gpio_arr[pin].spec, high))
    {
        return ERRNO_GPIO_FAILURE;
    }

    _gpio_arr[pin].high = high;

    return ERRNO_SUCCESS;
}

bool GPIO_level_get (GPIO_pin_e pin)
{
    if (pin >= GPIO_PIN_COUNT)
    {
        /* TODO: Add asserts. */
        while (1)
            ;
    }

    /* FIXME: When configured as output, can't read back GPIO state.  Using flag to store and readback. */
    if (_gpio_arr[pin].cfg_output)
    {
        return _gpio_arr[pin].high;
    }

    return (bool) gpio_pin_get_dt (&_gpio_arr[pin].spec);
}


/*--------------------------------------------------------------------------------------------------
 * PRIVATE FUNCTION DEFINITIONS
 *------------------------------------------------------------------------------------------------*/
static void left_button_isr (void)
{
    /* Disable interrupts to clear. */
    (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_BUTTON_LEFT].spec, GPIO_INT_DISABLE);

    /* Invoke user callback if not null. */
    if (_gpio_arr[GPIO_PIN_BUTTON_LEFT].user_cb)
    {
        _gpio_arr[GPIO_PIN_BUTTON_LEFT].user_cb ();
    }

    /* Reconfigure preserved interrupt settings.*/
    if (_gpio_arr[GPIO_PIN_BUTTON_LEFT].int_flags)
    {
        (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_BUTTON_LEFT].spec, _gpio_arr[GPIO_PIN_BUTTON_LEFT].int_flags);
    }
}

static void center_button_isr (void)
{
    /* Disable interrupts to clear. */
    (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_BUTTON_CENTER].spec, GPIO_INT_DISABLE);

    /* Invoke user callback if not null. */
    if (_gpio_arr[GPIO_PIN_BUTTON_CENTER].user_cb)
    {
        _gpio_arr[GPIO_PIN_BUTTON_CENTER].user_cb ();
    }

    /* Reconfigure preserved interrupt settings.*/
    if (_gpio_arr[GPIO_PIN_BUTTON_CENTER].int_flags)
    {
        (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_BUTTON_CENTER].spec, _gpio_arr[GPIO_PIN_BUTTON_CENTER].int_flags);
    }
}

static void right_button_isr (void)
{
    /* Disable interrupts to clear. */
    (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_BUTTON_RIGHT].spec, GPIO_INT_DISABLE);

    /* Invoke user callback if not null. */
    if (_gpio_arr[GPIO_PIN_BUTTON_RIGHT].user_cb)
    {
        _gpio_arr[GPIO_PIN_BUTTON_RIGHT].user_cb ();
    }

    /* Reconfigure preserved interrupt settings.*/
    if (_gpio_arr[GPIO_PIN_BUTTON_RIGHT].int_flags)
    {
        (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_BUTTON_RIGHT].spec, _gpio_arr[GPIO_PIN_BUTTON_RIGHT].int_flags);
    }
}

static void pwr_5v_isr (void)
{
    /* Disable interrupts to clear. */
    (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_5V_ENABLE].spec, GPIO_INT_DISABLE);

    /* Invoke user callback if not null. */
    if (_gpio_arr[GPIO_PIN_5V_ENABLE].user_cb)
    {
        _gpio_arr[GPIO_PIN_5V_ENABLE].user_cb ();
    }

    /* Reconfigure preserved interrupt settings.*/
    if (_gpio_arr[GPIO_PIN_5V_ENABLE].int_flags)
    {
        (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_5V_ENABLE].spec, _gpio_arr[GPIO_PIN_5V_ENABLE].int_flags);
    }
}

static void led_ring_pwr_isr (void)
{
    /* Disable interrupts to clear. */
    (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_LED_RING_PWR].spec, GPIO_INT_DISABLE);

    /* Invoke user callback if not null. */
    if (_gpio_arr[GPIO_PIN_LED_RING_PWR].user_cb)
    {
        _gpio_arr[GPIO_PIN_LED_RING_PWR].user_cb ();
    }

    /* Reconfigure preserved interrupt settings.*/
    if (_gpio_arr[GPIO_PIN_LED_RING_PWR].int_flags)
    {
        (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_LED_RING_PWR].spec, _gpio_arr[GPIO_PIN_LED_RING_PWR].int_flags);
    }
}

static void led_ring_level_shift_enable_isr (void)
{
    /* Disable interrupts to clear. */
    (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_LED_RING_LEVEL_SHIFT_ENABLE].spec, GPIO_INT_DISABLE);

    /* Invoke user callback if not null. */
    if (_gpio_arr[GPIO_PIN_LED_RING_LEVEL_SHIFT_ENABLE].user_cb)
    {
        _gpio_arr[GPIO_PIN_LED_RING_LEVEL_SHIFT_ENABLE].user_cb ();
    }

    /* Reconfigure preserved interrupt settings.*/
    if (_gpio_arr[GPIO_PIN_LED_RING_LEVEL_SHIFT_ENABLE].int_flags)
    {
        (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_LED_RING_LEVEL_SHIFT_ENABLE].spec, _gpio_arr[GPIO_PIN_LED_RING_LEVEL_SHIFT_ENABLE].int_flags);
    }
}

static void led_ring_din_isr (void)
{
    /* Disable interrupts to clear. */
    (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_LED_RING_DIN].spec, GPIO_INT_DISABLE);

    /* Invoke user callback if not null. */
    if (_gpio_arr[GPIO_PIN_LED_RING_DIN].user_cb)
    {
        _gpio_arr[GPIO_PIN_LED_RING_DIN].user_cb ();
    }

    /* Reconfigure preserved interrupt settings.*/
    if (_gpio_arr[GPIO_PIN_LED_RING_DIN].int_flags)
    {
        (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_LED_RING_DIN].spec, _gpio_arr[GPIO_PIN_LED_RING_DIN].int_flags);
    }
}

static void battery_check_enable_isr (void)
{
    /* Disable interrupts to clear. */
    (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_BATTERY_CHECK_ENABLE].spec, GPIO_INT_DISABLE);

    /* Invoke user callback if not null. */
    if (_gpio_arr[GPIO_PIN_BATTERY_CHECK_ENABLE].user_cb)
    {
        _gpio_arr[GPIO_PIN_BATTERY_CHECK_ENABLE].user_cb ();
    }

    /* Reconfigure preserved interrupt settings.*/
    if (_gpio_arr[GPIO_PIN_BATTERY_CHECK_ENABLE].int_flags)
    {
        (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_BATTERY_CHECK_ENABLE].spec, _gpio_arr[GPIO_PIN_BATTERY_CHECK_ENABLE].int_flags);
    }
}

static void imu_int_isr (void)
{
    /* Disable interrupts to clear. */
    (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_IMU_INT].spec, GPIO_INT_DISABLE);

    /* Invoke user callback if not null. */
    if (_gpio_arr[GPIO_PIN_IMU_INT].user_cb)
    {
        _gpio_arr[GPIO_PIN_IMU_INT].user_cb ();
    }

    /* Reconfigure preserved interrupt settings.*/
    if (_gpio_arr[GPIO_PIN_IMU_INT].int_flags)
    {
        (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_IMU_INT].spec, _gpio_arr[GPIO_PIN_IMU_INT].int_flags);
    }
}

static void charger_status_isr (void)
{
    /* Disable interrupts to clear. */
    (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_CHARGER_STATUS].spec, GPIO_INT_DISABLE);

    /* Invoke user callback if not null. */
    if (_gpio_arr[GPIO_PIN_CHARGER_STATUS].user_cb)
    {
        _gpio_arr[GPIO_PIN_CHARGER_STATUS].user_cb ();
    }

    /* Reconfigure preserved interrupt settings.*/
    if (_gpio_arr[GPIO_PIN_CHARGER_STATUS].int_flags)
    {
        (void) gpio_pin_interrupt_configure_dt (&_gpio_arr[GPIO_PIN_CHARGER_STATUS].spec, _gpio_arr[GPIO_PIN_CHARGER_STATUS].int_flags);
    }
}
