/*
 * @file exo_io_al_common.c
 *
 * @brief This file contains function definition 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_io_al_temperature.h"
#include "exo_io_al_common.h"
#include "exo_hal_io_al_common.h"
#include "exo_io_al_i2c_common.h"
#include "exo_io_al_uart_common.h"
#include "exo_io_al_spi_common.h"
#include "exo_io_al_usb_common.h"
#include "exo_io_al_eth_common.h"
#include "exo_io_al_rtc_common.h"
#include "exo_io_al_can_common.h"
#include "exo_io_al_qspi_common.h"
#include "exo_io_al_crc_common.h"
#include "exo_io_al_fmc_nor_flash_common.h"
#include "exo_common.h"
#include "exo_io_al_adc_common.h"
#include "exo_io_al_stm32f7xx_adc.h"

extern void *intf_inst_hdle_ptr[MAX_IO_INST_ID];

ioal_i2c_hdle ioal_hi2c1;
ioal_i2c_hdle ioal_hi2c2;
ioal_i2c_hdle ioal_hi2c3;
ioal_i2c_hdle ioal_hi2c4;
ioal_spi_hdle ioal_hspi1;
ioal_spi_hdle ioal_hspi2;
ioal_uart_hdle ioal_huart6;
ioal_uart_hdle ioal_huart1;
ioal_uart_hdle ioal_huart4;
ioal_uart_hdle ioal_huart5;
ioal_can_hdle ioal_hcan1;
ioal_can_hdle ioal_hcan3;
ioal_can_hdle ioal_hcan2;
ioal_qspi_hdle ioal_hqspi;
ioal_fmc_nor_hdle ioal_nor_flash;
ioal_crc_hdle ioal_hcrc;
ioal_rtc_hdle ioal_hrtc;
ioal_adc_hdle ioal_hadc1;

/**
 * @brief IO-HAL initialization function
 */
hal_ret_sts io_hal_init(void)
{
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO IO AL Framework Initialise"));
#endif
	hal_ret_sts sts = 0;
	io_hal_i2c_init(&ioal_hi2c1,IOAL_INST_I2C1);
#if (defined(COREBOARD) || defined(LINUX_TEMP_PORT))
	io_hal_i2c_init(&ioal_hi2c2,IOAL_INST_I2C2);
	io_hal_i2c_init(&ioal_hi2c3,IOAL_INST_I2C3);
	io_hal_i2c_init(&ioal_hi2c4,IOAL_INST_I2C4);
#endif
#if (defined(JANUS2) || defined(GEN2))
	io_hal_spi_init(&ioal_hspi1,IOAL_INST_SPI1);
#endif
#ifdef STM32F769xx
	io_hal_spi_init(&ioal_hspi2,IOAL_INST_SPI2);
#endif
	//io_hal_usb_device_init();
#ifdef STM32F769xx
	 io_hal_uart_init(&ioal_huart1,IOAL_INST_UART1);
	 io_hal_uart_init(&ioal_huart6,IOAL_INST_UART6);
#ifdef GPS_UART6_ENB
	 io_hal_uart_receive_timeout_enable(&ioal_huart6);
	 io_hal_uart_receive_timeout_config(&ioal_huart6,5000);
#endif
#endif
#if(STM32F765xx)
#if COREBOARD
	 io_hal_uart_init(&ioal_huart4,IOAL_INST_UART4);
	 io_hal_uart_init(&ioal_huart5,IOAL_INST_UART5);
	// io_hal_uart_receive_timeout_enable(&ioal_huart4);
	// io_hal_uart_receive_timeout_config(&ioal_huart4,1000);
#else
	 io_hal_uart_init(&ioal_huart6,IOAL_INST_UART6);
#endif
#endif
#ifdef COREBOARD
	 io_hal_can_init(&ioal_hcan2,IOAL_INST_CAN2);
#else
	 io_hal_can_init(&ioal_hcan3,IOAL_INST_CAN3);
#endif
	 io_hal_rtc_init(&ioal_hrtc,IOAL_INST_RTC1);

	 io_hal_qspi_init(&ioal_hqspi,IOAL_INST_QSPI1);
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO IO AL Framework Initialisation completed successfully"));
#endif
	 io_hal_crc_init(&ioal_hcrc, IOAL_INST_CRC);

     io_hal_adc_init(&ioal_hadc1,IOAL_INST_ADC1);
     return sts;
}

/**
 * @brief Function to get IOAL instance handle pointer
 */


void* get_ioal_inst_hdle(io_inst_id inst_id)
{
return intf_inst_hdle_ptr[inst_id];

}
