/*
 * @file exo_ahw_al_common.c
 *
 * @brief This file has function declaration for application hardware management
 *
 * @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include "exo_ahw_al_common.h"
#include "exo_hal_common.h"
#include "exo_ahw_al_gpio_exp_common.h"
#include "exo_ahw_al_ext_flash_common.h"
#include "exo_common.h"


void* ahw_inst_hdle_ptr[MAX_AH_INST_ID];

ahw_al_gpio_exp_hdle ps_gpx;
ahw_al_gpio_exp_hdle hgpio_exp_obc_1;
ahw_al_gpio_exp_hdle hgpio_exp_obc_2;
ahw_al_gpio_exp_hdle hgpio_exp_edge_1;
ahw_al_gpio_exp_hdle hgpio_exp_edge_2;
ahw_al_gpio_exp_hdle hgpio_exp_gps;

ahw_al_gpio_exp_hdle hgpio_exp_edge1_gen2;
ahw_al_gpio_exp_hdle hgpio_exp_edge2_gen2;
ahw_al_gpio_exp_hdle hgpio_exp_edge2_gen3;
ahw_al_gpio_exp_hdle hgpio_exp_edge2_gen4;
ahw_al_gpio_exp_hdle hgpio_exp_ps;
ahw_al_gpio_exp_hdle hgpio_exp_nic1;
ahw_al_gpio_exp_hdle hgpio_exp_nic2;
ahw_al_gpio_exp_hdle hgpio_exp_nic3;
ahw_al_gpio_exp_hdle hgpio_exp_ssd1;
ahw_al_gpio_exp_hdle hgpio_exp_ssd2;
ahw_al_gpio_exp_hdle hgpio_exp_bck_plane;
ahw_al_gpio_exp_hdle hgpio_exp_pwr_brd1;
ahw_al_gpio_exp_hdle hgpio_exp_pwr_brd2;
/**
 * @brief HAL initialization function
 */
hal_ret_sts ah_hal_init(void)
{
	hal_ret_sts sts = 1;
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO Application Hardware Abstraction Layer Framework Initialize"));
#endif
#ifdef COREBOARD
    ahw_al_ext_mem_driver_init(EXT_MEM_QSPI_FLASH);
    ahw_al_ext_mem_driver_init(EXT_MEM_MT28EW128_FMC);
    ahw_al_ext_mem_driver_init(EXT_MEM_M24C16_EEPROM_1);
    ahw_al_ext_mem_driver_init(EXT_MEM_M24C16_EEPROM_2);
    sts=ahw_al_gpio_exp_init(&ps_gpx,GPIO_EXPANDER_PCAL6408A_PS);
	sts=ahw_al_gpio_exp_init(&hgpio_exp_obc_1,GPIO_EXPANDER_MCP23008_OBC_1);
	sts=ahw_al_gpio_exp_init(&hgpio_exp_obc_2,GPIO_EXPANDER_MCP23008_OBC_2);
	sts=ahw_al_gpio_exp_init(&hgpio_exp_edge_1,GPIO_EXPANDER_MCP23008_EDGE_1);
	sts=ahw_al_gpio_exp_init(&hgpio_exp_edge_2,GPIO_EXPANDER_MCP23008_EDGE_2);
	sts=ahw_al_gpio_exp_init(&hgpio_exp_gps,GPIO_EXPANDER_MCP23008_GPS);

	sts = ahw_al_gpio_exp_init(&hgpio_exp_edge1_gen2,GPIO_EXPANDER_MCP23008_EDGE_1_GEN_2);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_edge2_gen2,GPIO_EXPANDER_MCP23008_EDGE_2_GEN_2);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_edge2_gen3,GPIO_EXPANDER_MCP23008_EDGE_3_GEN_2);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_edge2_gen4,GPIO_EXPANDER_MCP23008_EDGE_4_GEN_2);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_ps,GPIO_EXPANDER_PCAL6408A_PS_GEN2);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_nic1,GPIO_EXPANDER_MCP23008_NIC_1);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_nic2,GPIO_EXPANDER_MCP23008_NIC_2);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_nic3,GPIO_EXPANDER_MCP23008_NIC_3);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_ssd1,GPIO_EXPANDER_MCP23008_SSD_1);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_ssd2,GPIO_EXPANDER_MCP23008_SSD_2);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_bck_plane,GPIO_EXPANDER_MCP23008_BACK_PLANE);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_pwr_brd1,GPIO_EXPANDER_MCP23008_POWER_BOARD_1);
	sts = ahw_al_gpio_exp_init(&hgpio_exp_pwr_brd2,GPIO_EXPANDER_MCP23008_POWER_BOARD_2);

#endif    
#ifdef STM32F769xx
    ahw_al_ext_mem_driver_init(EXT_MEM_QSPI_FLASH);
#endif
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO Application Hardware Abstraction Layer Framework Initialization completed successfully"));
#endif
    return sts;
}

/**
 * @brief HAL initialization function
 */
void ahal_set_hdle(ahw_inst_id_t id, void* ahw_hdle)
{
	if(id<MAX_AH_INST_ID)
	{
		ahw_inst_hdle_ptr[id]=ahw_hdle;
	}

}
/**
 * @brief HAL function to return AHW handle from AHW id
 */
void* ahal_get_hdle(ahw_inst_id_t id)
{
	void* ret = NULL;
	if(id<MAX_AH_INST_ID)
	{
      ret = ahw_inst_hdle_ptr[id];
	}
	return ret;
}
