/*
 * ADS124S08_Port.c
 *
 *  Created on: Nov 10, 2024
 *      Author: zafst
 */

#include "ADS124S08_Port.h"

/*MCU specific includes */
#include "stm32u3xx_hal.h"
#include "spi.h"

/*Function Prototypes*/
e_ADS124S08_State ADS124S08_PORT_Write_ChipSelect(e_ADS124S08_ChipID ChipID, e_ADS124S08_CS_State State);

/* Portable Functions*/
e_ADS124S08_State ADS124S08_PORT_Read_Register(e_ADS124S08_ChipID ChipID, uint8_t* CommandData,uint8_t NumRegs,uint8_t* Data)
{

	if(ADS124S08_PORT_Write_ChipSelect(ChipID, CS_SELECTED) != ADS124S08_OK)
	{
		return ADS124S08_ERROR;
	}
	if(HAL_SPI_Transmit(&hspi2, CommandData, 2, HAL_MAX_DELAY) != HAL_OK)
	{
		return ADS124S08_ERROR;
	}
	if(HAL_SPI_Receive(&hspi2, Data, NumRegs, HAL_MAX_DELAY) != HAL_OK)
	{
		return ADS124S08_ERROR;
	}
	if(ADS124S08_PORT_Write_ChipSelect(ChipID, CS_NOT_SELECTED) != ADS124S08_OK)
	{
		return ADS124S08_ERROR;
	}

	return ADS124S08_OK;
}

e_ADS124S08_State ADS124S08_PORT_Write_Register(e_ADS124S08_ChipID ChipID, uint8_t *CommandData, uint8_t NumRegs,uint8_t* Data)
{
	if(ADS124S08_PORT_Write_ChipSelect(ChipID, CS_SELECTED) != ADS124S08_OK)
	{
		return ADS124S08_ERROR;
	}
	if(HAL_SPI_Transmit(&hspi2, CommandData, 2, HAL_MAX_DELAY) != HAL_OK)
	{
		return ADS124S08_ERROR;
	}
	if(HAL_SPI_Transmit(&hspi2, Data, NumRegs, HAL_MAX_DELAY) != HAL_OK)
	{
		return ADS124S08_ERROR;
	}
	if(ADS124S08_PORT_Write_ChipSelect(ChipID, CS_NOT_SELECTED) != ADS124S08_OK)
	{
		return ADS124S08_ERROR;
	}

	return ADS124S08_OK;
}

e_ADS124S08_State ADS124S08_PORT_Read_Data(e_ADS124S08_ChipID ChipID, uint8_t Command, uint8_t* Data, uint8_t size)
{
	if(ADS124S08_PORT_Write_ChipSelect(ChipID, CS_SELECTED) != ADS124S08_OK)
	{
		return ADS124S08_ERROR;
	}
	if(HAL_SPI_Transmit(&hspi2, &Command, 1, HAL_MAX_DELAY) != HAL_OK)
	{
		return ADS124S08_ERROR;
	}
	if(HAL_SPI_Receive(&hspi2, Data, size, HAL_MAX_DELAY) != HAL_OK)
	{
		return ADS124S08_ERROR;
	}
	if(ADS124S08_PORT_Write_ChipSelect(ChipID, CS_NOT_SELECTED) != ADS124S08_OK)
	{
		return ADS124S08_ERROR;
	}

	return ADS124S08_OK;
}

e_ADS124S08_State ADS124S08_PORT_SEND_COMMAND(e_ADS124S08_ChipID ChipID, uint8_t Command)
{
	if(ADS124S08_PORT_Write_ChipSelect(ChipID, CS_SELECTED) != ADS124S08_OK)
	{
		return ADS124S08_ERROR;
	}
	if(HAL_SPI_Transmit(&hspi2, &Command, 1, HAL_MAX_DELAY) != HAL_OK)
	{
		return ADS124S08_ERROR;
	}
	if(ADS124S08_PORT_Write_ChipSelect(ChipID, CS_NOT_SELECTED) != ADS124S08_OK)
	{
		return ADS124S08_ERROR;
	}

	return ADS124S08_OK;
}

e_ADS124S08_State ADS124S08_PORT_Write_ChipSelect(e_ADS124S08_ChipID ChipID, e_ADS124S08_CS_State State)
{
	switch(ChipID)
	{
		case ADS124S08_ID0:
		{
			if(State == CS_NOT_SELECTED)
			{

				HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_SET);
			}
			else if(State == CS_SELECTED)
			{
				HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_RESET);

			}
			else
			{
				return ADS124S08_ERROR;
			}

			return ADS124S08_OK;

			break;
		}
		case ADS124S08_ID1:
		{
			if(State == CS_NOT_SELECTED)
			{

				HAL_GPIO_WritePin(ADC_TEMP_CS_GPIO_Port, ADC_TEMP_CS_Pin, GPIO_PIN_SET);
			}
			else if(State == CS_SELECTED)
			{
				HAL_GPIO_WritePin(ADC_TEMP_CS_GPIO_Port, ADC_TEMP_CS_Pin, GPIO_PIN_RESET);

			}
			else
			{
				return ADS124S08_ERROR;
			}

			return ADS124S08_OK;

			break;
		}
		case ADS124S08_ID2:
		{

			return ADS124S08_ERROR;
			break;
		}
		case ADS124S08_ID3:
		{

			return ADS124S08_ERROR;
			break;
		}
		default:
		{
			return ADS124S08_ERROR;
		}
	}
}

e_ADS124S08_State ADS124S08_PORT_CHECK_CRC(uint8_t* Data,uint8_t size,uint8_t CRC_DATA)
{
	uint8_t CRC_OK = 1;

	if(CRC_OK)
	{
		return ADS124S08_OK;
	}
	else
	{
		return ADS124S08_ERROR;
	}
}
