/*
 * exo_ahw_vdp_uart_conv_max14830.c
 *
 *  Created on: May 16, 2023
 *      Author: Invcuser_63
 */

#include <stdio.h>
#include <unistd.h>
#include "exo_ahw_vdp_uart_conv_max14830.h"
#include "exo_osal_mem_management.h"
#include "exo_ahw_al_linux_wrpr.h"
#include "stm32f7xx_hal_ext.h"
#include "max14830.h"
#include "exo_io_al_i2c_common.h"
#include "exo_io_al_spi_common.h"
#include "exo_osal.h"
#include "exo_common.h"
#include "exo_pinmap.h"

extern ahw_io_map *ahw_io_lookup_tble[MAX_AH_INST_ID];
extern void *intf_inst_hdle_ptr[MAX_IO_INST_ID];

/*
 * @brief This API is
 */
static uint32_t max14830_timer()
{
	uint32_t timer;
	timer = HAL_GetTick();
	return timer;
}

/*
 * @brief This API is for semaphore take
 */
static int8_t max14830_semphore_take(void* sem_id,uint32_t tick_time)
{
	int8_t rslt;
	if(os_sem_take(sem_id,tick_time) == ral_success)
	{
		rslt = HAL_SCS;
	}
	else
	{
		rslt = HAL_ERROR;
	}
	return rslt;
}

/*
 * @brief This API is for semaphore give
 */
static int8_t max14830_semphore_give(void* sem_id)
{
	int8_t rslt;
	if(os_sem_give(sem_id) == ral_success)
	{
		rslt = HAL_SCS;
	}
	else
	{
		rslt = HAL_ERROR;
	}
	return rslt;
}


/*
 * @brief This API is for delay
 */
static int8_t max14830_delay(uint32_t delay)
{
	int8_t rslt;
	if(os_delay(delay) == (os_status_t)HAL_SCS)
	{
		rslt = HAL_SCS;
	}
	else
	{
		rslt = HAL_ERROR;
	}
	return rslt;
}

/*
 * @brief This API is for starting the timer
 */
static int8_t max14830_timer_start(void* timer_hdle,uint32_t time)
{
	int8_t rslt;
	if(os_timer_start(timer_hdle,time) == ral_success)
	{
		rslt = HAL_SCS;
	}
	else
	{
		rslt = HAL_ERROR;
	}
	return rslt;
}

/*
 * @brief This API is for stoping the timer
 */
static int8_t max14830_timer_stop(void* timer_hdle)
{
	int8_t rslt;
	if(os_timer_stop(timer_hdle) == ral_success)
	{
		rslt = HAL_SCS;
	}
	else
	{
		rslt = HAL_ERROR;
	}
	return rslt;
}
#if 0
/**
 * @brief This API is used for i2c read.
 */
static uint8_t max14830_i2c_read(void* intf_hdl, uint16_t slv_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
	ioal_i2c_hdle *hi2c = (ioal_i2c_hdle*)intf_hdl;
	int8_t rslt;
	if(HAL_SCS == io_hal_i2c_transmit(hi2c, (uint16)slv_addr, &reg_addr, 1, 500))
	{
		if(HAL_SCS == io_hal_i2c_receive(hi2c, (uint16)slv_addr, data, len, 500))
		{
			rslt = MAX14830_SCS;
		}
		else
		{
			rslt =MAX14830_COM_ERR;
		}
	}
	else
	{
		rslt =MAX14830_COM_ERR;
	}
	return rslt;
}
/**
 * @brief This API is used for i2c write.
 */
static uint8_t max14830_i2c_write(void* intf_hdl, uint16_t slv_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
	ioal_i2c_hdle *hi2c = (ioal_i2c_hdle*)intf_hdl;
	int8_t rslt;
	uint8_t t_data[3];
	t_data[0]=reg_addr;
	t_data[1]=data[0];
	t_data[2]=data[1];
	if(data==NULL)
	{
		rslt = io_hal_i2c_transmit(hi2c, (uint16)slv_addr, &reg_addr, 1, 500);
		return rslt;
	}
	if(HAL_SCS == io_hal_i2c_transmit(hi2c, (uint16)slv_addr, t_data, 3, 500))
	{
		rslt=MAX14830_SCS;
	}
	else
	{
		rslt =MAX14830_COM_ERR;
	}
	return rslt;
}
#endif 
/**
 * @brief This API is used for i2c read.
 */
static uint8_t max14830_spi_read(void* intf_hdl, uint8_t *data, uint16_t len,uint32_t timeout)
{
	ioal_spi_hdle *hspi = (ioal_spi_hdle*)intf_hdl;
	int8_t rslt;
		if(HAL_SCS == io_hal_spi_receive(hspi, data, len, 100))
		{
			rslt = MAX14830_SCS;
		}
		else
		{
			rslt =MAX14830_COM_ERR;
		}

	return rslt;
}

/**
 * @brief This API is used for i2c write.
 */
static uint8_t max14830_spi_write(void* intf_hdl, uint8_t *data, uint16_t len,uint32_t timeout)
{
	ioal_spi_hdle *hspi = (ioal_spi_hdle*)intf_hdl;
	int8_t rslt;
	if(HAL_SCS == io_hal_spi_transmit(hspi,data,len, 100))
	{
		rslt=MAX14830_SCS;
	}
	else
	{
		rslt =MAX14830_COM_ERR;
	}
	return rslt;
}

/**
 * @brief This API is used for i2c write.
 */
static uint8_t max14830_spi_read_write(void* intf_hdl, uint8_t *tx_data,uint8_t *rx_data, uint16_t len,uint32_t timeout)
{
    ioal_spi_hdle *hspi = (ioal_spi_hdle*)intf_hdl;
    int8_t rslt;

    if(HAL_SCS == io_hal_spi_transmit_receive(hspi,tx_data,rx_data,len, 100))
    {
        rslt=MAX14830_SCS;
    }
    else
    {
        rslt =MAX14830_COM_ERR;
    }
    return rslt;
}

/*
 * @brief This API is to read the gpio pin
 */
static uint8_t max14830_gpio_read(uint32_t gpio_Pin)
{
	uint8_t rslt;
	if((e_max14830_gpio_pin_state)io_hal_gpio_readpin(gpio_Pin) == MAX14830_GPIO_RESET)


	{
		rslt = MAX14830_GPIO_RESET;
	}
	else
	{
		rslt = MAX14830_GPIO_SET;
	}
	return rslt;
}

/*
 * @brief This API is to send the queue
 */
static uint8_t max14830_queue_send(void* queue_hdl,void* msg_ptr,uint32_t time)
{
    uint8_t rslt;
    if(os_queue_send(queue_hdl,msg_ptr,time) == ral_success)
    {
        rslt = HAL_SCS;
    }
    else
    {
        rslt = HAL_ERROR;
    }
    return rslt;
}

/*
 * @brief This API is to write the gpio pin
 */
static void max14830_gpio_write(uint32_t gpio_Pin,e_max14830_gpio_pin_state pin_state)
{
	io_hal_gpio_writepin(gpio_Pin,pin_state);

}

/*
 * @brief This API is for dynamic memory allocation
 */
void* max14830_malloc(uint32_t size)
{

    void* ptr=NULL;
    ptr=os_malloc(size);
    return ptr;

}

/**
 * @brief Maps AHAL UART conversion channels to MAX14830 channels.
 *
 * This function takes an AHAL UART conversion channel identifier and maps it
 * to the corresponding MAX14830 channel identifier.
 *
 * @param port The AHAL UART conversion channel to be mapped.
 *             - AHAL_UART_CONV_CH0
 *             - AHAL_UART_CONV_CH1
 *             - AHAL_UART_CONV_CH2
 *             - AHAL_UART_CONV_CH3
 *
 * @return The corresponding MAX14830 channel identifier:
 *         - MAX14830_CH0 for AHAL_UART_CONV_CH0
 *         - MAX14830_CH1 for AHAL_UART_CONV_CH1
 *         - MAX14830_CH2 for AHAL_UART_CONV_CH2
 *         - MAX14830_CH3 for AHAL_UART_CONV_CH3
 *         If the port is not a valid AHAL UART conversion channel, returns 0xff.
 */
static uint8_t max14830_ahal_port_mapping(uint8_t port)
{
    uint8_t ret_val = 0xff;
    switch(port)
    {
    case AHAL_UART_CONV_CH0:
        ret_val = MAX14830_CH0;
        break;
    case AHAL_UART_CONV_CH1:
        ret_val = MAX14830_CH1;
        break;
    case AHAL_UART_CONV_CH2:
        ret_val = MAX14830_CH2;
        break;
    case AHAL_UART_CONV_CH3:
        ret_val = MAX14830_CH3;
        break;
    }
    return ret_val;
}

/**
 * @brief This API initializes the
 */
hal_ret_sts ahw_vdp_uart_conv_init(s_ahw_max14830_hdle *huart_conv)
{
    hal_ret_sts sts = HAL_MAX_ERR;
	s_max14830_hdle *uart_conv = (s_max14830_hdle*)os_malloc(sizeof(s_max14830_hdle));

		uart_conv->spi_read = max14830_spi_read;
		uart_conv->spi_write = max14830_spi_write;
		uart_conv->spi_read_write = max14830_spi_read_write;

	timer = max14830_timer;
	sem_take = max14830_semphore_take;
	sem_give = max14830_semphore_give;
	delay_ms = max14830_delay;
	timer_start = max14830_timer_start;
	timer_stop = max14830_timer_stop;
	malloc_func = max14830_malloc;
	queue_send = max14830_queue_send;
	uart_conv->gpio_read = max14830_gpio_read;
	uart_conv->gpio_write = max14830_gpio_write;
	uart_conv->cs_pin = SPI1_CS;
	uart_conv->irq_pin =SPI_UART_IRQ;
	uart_conv->rst_pin = SPI_UART_RST;
	uart_conv->io_intf_hdle = intf_inst_hdle_ptr[ahw_io_lookup_tble[huart_conv->ahw_gen_info.ahw_inst_id]->io_instance_id];
	//uart_conv->slave_address=ahw_io_lookup_tble[huart_conv->ahw_gen_info.ahw_inst_id]->slv_addr;
	huart_conv->ahw_gen_info.vdp_inst_hdle =(void*)uart_conv;
	huart_conv->ahw_gen_info.io_intf_hdle = uart_conv->io_intf_hdle;
	//huart_conv->ahw_gen_info.slave_address = uart_conv->slave_address;

	/*s_max14830_init_cfg *spi_to_uart_cfg
	spi_to_uart_cfg = (s_max14830_init_cfg)huart_conv_cfg;*/
	if(HAL_SCS == (hal_ret_sts)max14830_init(uart_conv))
		{
			sts = HAL_SCS;
		}
		else
		{
			sts = HAL_AH_DRIVER_ERR;
		}
    return sts;
}

/**
 * @brief This API is to initialize the port configurations
 */
hal_ret_sts ahw_vdp_uart_conv_init_intf(s_ahw_max14830_hdle *huart_conv,s_ahw_max14830_init_cfg *init_cfg)
{
	hal_ret_sts sts;
	s_max14830_hdle *vdh_uart_conv;
	s_max14830_uart_init_cfg init_data;
	init_data.baudrate=init_cfg->baudrate;
	init_data.chnl_id=init_cfg->chnl_id;
	init_data.stop_bit=init_cfg->stop_bit;
	init_data.wrd_len=init_cfg->wrd_len;
	init_data.parity_bit=init_cfg->parity_bit;
	vdh_uart_conv = (s_max14830_hdle*)huart_conv->ahw_gen_info.vdp_inst_hdle;
	if(HAL_SCS ==(hal_ret_sts)max14830_uart_init(vdh_uart_conv,&init_data))
	{
		sts = HAL_SCS;
	}
	else
	{
		sts = HAL_AH_DRIVER_ERR;
	}
	return sts;
}

/**
 * @brief This API transmit the data
 */
hal_ret_sts ahw_vdp_uart_conv_transmit(s_ahw_max14830_hdle *huart_conv,uint8_t port,uint8_t *tx_buff,uint16_t tx_len)
{
	hal_ret_sts sts;
	uint8_t uart_port = max14830_ahal_port_mapping(port);
	s_max14830_hdle *vdh_uart_conv;
	vdh_uart_conv = (s_max14830_hdle*)huart_conv->ahw_gen_info.vdp_inst_hdle;
	max14830_uart_tx(vdh_uart_conv,uart_port,tx_buff,tx_len);
#if 0
	if(HAL_SCS ==(hal_ret_sts)max14830_uart_tx(vdh_uart_conv,port,tx_buff,tx_len))
	{
		sts = HAL_SCS;
	}
	else
	{
		sts = HAL_AH_DRIVER_ERR;
	}
	return sts;
#endif
	sts = HAL_SCS;
	return sts;
}


/**
 * @brief This API receive the data
 */
hal_ret_sts ahw_vdp_uart_conv_receive(s_ahw_max14830_hdle *huart_conv,uint8_t port,uint16_t rx_len,max14830_callback_fptr_t callback_func)
{
	hal_ret_sts sts;
	s_max14830_hdle *vdh_uart_conv;
	uint8_t uart_port = max14830_ahal_port_mapping(port);
	vdh_uart_conv = (s_max14830_hdle*)huart_conv->ahw_gen_info.vdp_inst_hdle;
	vdh_uart_conv->rcv0_ptr=huart_conv->rcv0_ptr;
	vdh_uart_conv->rcv1_ptr=huart_conv->rcv1_ptr;
	vdh_uart_conv->rcv2_ptr=huart_conv->rcv2_ptr;
	vdh_uart_conv->rcv3_ptr=huart_conv->rcv3_ptr;
	if(HAL_SCS ==(hal_ret_sts)max14830_uart_rx_intr(vdh_uart_conv,uart_port,rx_len,callback_func))
	{
		sts = HAL_SCS;
	}
	else
	{
		sts = HAL_AH_DRIVER_ERR;
	}
	return sts;
}

hal_ret_sts ahw_vdp_uart_conv_self_test(s_ahw_max14830_hdle *huart_conv)
{
	hal_ret_sts sts;
	s_max14830_hdle *vdh_uart_conv;
	vdh_uart_conv = (s_max14830_hdle*)huart_conv->ahw_gen_info.vdp_inst_hdle;
	if(HAL_SCS == (hal_ret_sts)max14830_self_test(vdh_uart_conv))
	{
		sts = HAL_SCS;
	}
	else
	{
		sts = HAL_AH_DRIVER_ERR;
	}
	return sts;
}
