
/*
 * @file exo_io_al_adc_common.c
 *
 * @brief This file contains abstracted function definition for ADC interface
 *
 * @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 <stdint.h>
#include <unistd.h>
#include "exo_io_al_adc_common.h"
#include "exo_hal_io_al_common.h"
#include "exo_common.h"

#ifndef LINUX_TEMP_PORT
#include "exo_io_al_stm32f7xx_adc.h"
#else
#include "exo_io_al_linux_adc.h"
#endif


extern void *intf_inst_hdle_ptr[MAX_IO_INST_ID];

/**
 * @brief  This API initializes the ADC according to default
 *         parameters.
 */
hal_ret_sts io_hal_adc_init(ioal_adc_hdle *ioal_hadc, io_inst_id inst_id)
{
	hal_ret_sts sts=HAL_IO_INIT_INVALID_INSTANCE;

	switch(inst_id)
	{
		case IOAL_INST_ADC1:
			if(HAL_SCS == io_hal_common_adc_init(ioal_hadc))
			{
				intf_inst_hdle_ptr[IOAL_INST_ADC1] =(void*)ioal_hadc;
			}
			else
			{

				sts = HAL_IO_INIT_ERR;
			}
			break;

		default:
			sts = HAL_IO_INIT_INVALID_INSTANCE;
			break;

	}

	return sts;
}

/**
 * @brief  This API starts the ADC interface
 */
hal_ret_sts io_hal_adc_start(ioal_adc_hdle *hadc)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS == io_hal_common_adc_start(hadc))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

/**
 * @brief  This API stop the ADC interface
 */
hal_ret_sts io_hal_adc_stop(ioal_adc_hdle *hadc)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS == io_hal_common_adc_stop(hadc))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

/**
 * @brief  this API start the ADC pollforconversion.
 */
hal_ret_sts io_hal_adc_pollforconversion(ioal_adc_hdle *hadc, uint32_t Timeout)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS ==  io_hal_common_adc_pollforconversion(hadc,Timeout))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

/**
 * @brief  this API used to getvalue from adc.
 */
uint32_t io_hal_adc_getvalue(ioal_adc_hdle *hadc)
{
	uint32_t ret_sts;
	ret_sts=io_hal_common_adc_getvalue(hadc);

	return ret_sts;
}

#ifndef LINUX_TEMP_PORT

/**
 * @brief  this API config the channel in adc.
 */
hal_ret_sts io_hal_adc_configchannel(ioal_adc_hdle *hadc, adc_channelconftypedef* sconfig)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS == io_hal_common_adc_configchannel(hadc,sconfig))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

/**
 * @brief this API configures the analog watchdog in ADC.
 */
hal_ret_sts io_hal_adc_analogwdgconfig(ioal_adc_hdle *hadc, adc_channelconftypedef* sconfig)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS == io_hal_common_adc_analogwdgconfig(hadc,sconfig))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

/**
 * @brief this API return the ADC state.
 */

hal_ret_sts io_hal_adc_getstate(ioal_adc_hdle *hadc)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS == io_hal_common_adc_getstate(hadc))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

/**
 * @brief this API return the ADC error code.
 */
hal_ret_sts io_hal_adc_geterror(ioal_adc_hdle *hadc)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS == io_hal_common_adc_geterror(hadc))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

/**
 * @brief  this API start the ADC pollforevent.
 */
hal_ret_sts io_hal_adc_pollforevent(ioal_adc_hdle *hadc, uint32_t EventType, uint32_t Timeout)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS == io_hal_common_adc_pollforevent(hadc, EventType,Timeout))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

/**
 * @brief this API start the ADC_it.
 */
hal_ret_sts io_hal_adc_start_it(ioal_adc_hdle *hadc)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS == io_hal_common_adc_start_it(hadc))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

/**
 * @brief this API stop the ADC_it.
 */
hal_ret_sts io_hal_adc_stop_it(ioal_adc_hdle *hadc)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS == io_hal_common_adc_stop_it(hadc))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

/**
 * @brief this API start the ADC in dma mode.
 */
hal_ret_sts io_hal_adc_start_dma(ioal_adc_hdle *hadc, uint32_t* pData, uint32_t Length)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS == io_hal_common_adc_start_dma(hadc, pData, Length))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

/**
 * @brief this API stop the ADC in dma mode.
 */
hal_ret_sts io_hal_adc_stop_dma(ioal_adc_hdle *hadc)
{
	hal_ret_sts ret_sts = HAL_MAX_ERR;
	if(HAL_SCS == io_hal_common_adc_stop_dma(hadc))
	{
		ret_sts = HAL_SCS;

		hadc->intf_gen_info.state = IO_BUSY_STATE;
	}
	else
	{
		ret_sts = HAL_IO_TX_ERR;
	}
	return ret_sts;
}

#endif
