/*
 * drivers_test.c
 *
 */

#define ALL_INTF_ENB

#include "drivers_test.h"
#include "main.h"
#include <stdio.h>
#include "cmsis_os2.h"
#include "exo_hal_io_al_common.h"
#include "exo_io_al_gpio_common.h"
#include "exo_osal.h"
#include "exo_io_al_sos_timer.h"
#include "nvm.h"
#include "exo_ahw_al_spi_to_uart_common.h"
#include "exo_common.h"
#include <sys/time.h>
#include "exo_io_al_adc_common.h"
#include "math.h"
#include "string.h"
#include "exo_io_al_common.h"
#include "stm32f7xx_hal_uart.h"

#define TIME_WAITING_FOR_INPUT ( portMAX_DELAY )

#define SPI_TO_UART_ENB

extern void *intf_inst_hdle_ptr[MAX_IO_INST_ID];

#define ADXL345_SENSOR_ADDR		0x53
#define INTR 1

uint8 uart_test_sts[10];
uint8 i2c_test_sts[10];
volatile int dbg_i = 0;
uint8 dbg_data = 0;
extern ioal_uart_hdle ioal_huart6;
extern ioal_uart_hdle ioal_huart5;
extern ioal_uart_hdle ioal_huart4;
extern ioal_uart_hdle ioal_huart1;
extern ioal_i2c_hdle ioal_hi2c1;
extern ioal_i2c_hdle ioal_hi2c3;
extern ioal_i2c_hdle ioal_hi2c2;
extern ioal_i2c_hdle ioal_hi2c4;
extern ioal_spi_hdle ioal_hspi1;
extern ioal_can_hdle ioal_hcan1;
extern ioal_can_hdle ioal_hcan3;
extern ioal_can_hdle ioal_hcan2;
extern ioal_rtc_hdle ioal_hrtc;
extern ioal_adc_hdle ioal_hadc1;
iohal_can_tx_header TxHeader;
iohal_can_rx_header RxHeader;
uint32_t TxMailbox;
uint8_t TxData[8]={0};
uint8_t RxData[8]={0};
volatile uint8_t countt = 0;
ioal_i2c_hdle ioal_hi2c_id;
ioal_uart_hdle ioal_huart_id;


extern NOR_HandleTypeDef hnor1;
NOR_IDTypeDef nor_id;
/* USER CODE BEGIN PFP */
char txdata[30];
char i2cdata[30];
int len;
int l;
uint8_t rtc_flag = 0;

uint16_t addr=(ADXL345_SENSOR_ADDR << 1);
uint8_t reg=0x0;
uint8_t data=0x0;

HAL_StatusTypeDef i2c_read_reg(I2C_HandleTypeDef *hi2c, uint16_t slv_addr,uint8_t reg, uint8_t *data, uint16_t Size);

extern void *intf_inst_hdle_ptr[MAX_IO_INST_ID];

static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset);

static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset)
{
	uint32_t tmpIndex = 0;

	/* Put in global buffer different values */
	for (tmpIndex = 0; tmpIndex < uwBufferLenght; tmpIndex++ )
	{
		pBuffer[tmpIndex] = tmpIndex;// + uwOffset;
	}
}

int8_t fmc_sdram_test()
{
	int8_t ret;
	/* USER CODE BEGIN PV */
	__IO uint32_t uwWriteReadStatus = 0;

	uint32_t uwIndex = 0;
	uint32_t* aTxBuffer= os_malloc(sizeof(uint32_t)*BUFFER_SIZE);
	uint32_t* aRxBuffer= os_malloc(sizeof(uint32_t)*BUFFER_SIZE);

	/* Fill the buffer to write */
	Fill_Buffer(aTxBuffer, BUFFER_SIZE, 0xA244250F);


	/* Write data to the SDRAM memory */
	for (uwIndex = START_ADDR; uwIndex < END_ADDR; uwIndex++)
	{
		*(__IO uint32_t*) (SDRAM_BANK_ADDR + WRITE_READ_ADDR + 4*uwIndex) = aTxBuffer[uwIndex];
	}

	/* Read back data from the SDRAM memory */
	for (uwIndex = START_ADDR; uwIndex < END_ADDR; uwIndex++)
	{
		aRxBuffer[uwIndex] = *(__IO uint32_t*) (SDRAM_BANK_ADDR + WRITE_READ_ADDR + 4*uwIndex);
	}

	/*##-3- Checking data integrity ############################################*/

	for (uwIndex = 0; (uwIndex < (END_ADDR - START_ADDR)) && (uwWriteReadStatus == 0); uwIndex++)
	{
		if (aRxBuffer[uwIndex] != aTxBuffer[uwIndex])
		{
			uwWriteReadStatus++;
		}
	}
	os_free(aTxBuffer);
	os_free(aRxBuffer);

	ret = (uwWriteReadStatus != PASSED)?-1:1;
	return ret;

}

uint8_t ioal_uart_echo_test(ioal_uart_hdle* huart)
{
	char uart_buff[10];
	while(1)
	{
		io_hal_uart_receive(huart,(uint8_t*)uart_buff,5,500);
		io_hal_uart_transmit(huart,(uint8_t*)uart_buff,5,500);
		memset(uart_buff,0,10);
		HAL_Delay(1);
	}
}

#ifdef SPI_TO_UART_ENB

#if 0
uint8_t recieve_ptr_1[600] ;
uint8_t recieve_ptr_2[600] ;
uint8_t recieve_ptr_3[600] ;
uint8_t recieve_ptr_4[600] ;

void test_max14830_callback_fucn_1()
{
	memset(recieve_ptr_1,0,600);
}

void test_max14830_callback_fucn_2()
{
	memset(recieve_ptr_2,0,600);
}
void test_max14830_callback_fucn_3()
{
	memset(recieve_ptr_3,0,600);
}
void test_max14830_callback_fucn_4()
{
	memset(recieve_ptr_4,0,600);
}

#endif


s_ahw_max14830_hdle hdle;
//s_ahw_max14830_init_cfg init_cfg;
//s_ahw_max14830_init_cfg init_cfg_1;
s_ahw_max14830_init_cfg init_cfg_1;
s_ahw_max14830_init_cfg init_cfg_2;
s_ahw_max14830_init_cfg init_cfg_3;
s_ahw_max14830_init_cfg init_cfg_4;
#if 0
char buff[600];
#endif
extern s_port_reg_cfg_value cfg_val;

void spi_to_uart_thread_creat()
{
	// max14830_start();
}
#if 0
void max14830_tx_test_fucn_1()
{
	while(1)
	{
		uint8_t tx_buff1[]="AAAAA";
		// uint8_t tx_buff[]={0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};
		ahw_al_uart_conv_transmit(UART_EXPANDER_MAX14830_OBC ,MAX14830_CH0,tx_buff1,sizeof(tx_buff1));
		os_delay(1000);
	}

}
#endif

#if 0
void max14830_tx_test_fucn_2()
{
	while(1)
	{
		uint8_t tx_buff2[]="BBBBB";


		// uint8_t tx_buff[]={0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};
		//  ahw_al_uart_conv_transmit(MAX14830_SPI_TO_UART,0x00,tx_test,sizeof(tx_test));
		//  ahw_al_uart_conv_transmit(MAX14830_SPI_TO_UART,0x20,tx_test,sizeof(tx_test));

		ahw_al_uart_conv_transmit(UART_EXPANDER_MAX14830_OBC,MAX14830_CH1,tx_buff2,sizeof(tx_buff2));
		os_delay(1000);
	}

}
#endif

// Test code for transmit
#if 0
void max14830_tx_test_fucn_3()
{
	while(1)
	{
		uint8_t tx_test[] ={"Hello\n"};
		//{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"};
		//{0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};
		ahw_al_uart_conv_transmit(UART_EXPANDER_MAX14830_OBC,MAX14830_CH2,tx_test,sizeof(tx_test));
		os_delay(1000);
	}

}
#endif


#if 0
void max14830_tx_test_fucn_4()
{
	while(1)
	{
		uint8_t tx_buff4[]="DDDD";
		//{0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};
		ahw_al_uart_conv_transmit(UART_EXPANDER_MAX14830_OBC,MAX14830_CH3,tx_buff4,sizeof(tx_buff4));

		os_delay(1000);
	}

}
#endif

#if 1
void max14830_tx_test_fucn()
{
	//    while(1)
	//    {
	//    uint8_t tx_buff[]={0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};
	//        ahw_al_uart_conv_transmit(&hdle,0x00,tx_buff,sizeof(tx_buff));
	//        ahw_al_uart_conv_transmit(&hdle,0x20,tx_buff,sizeof(tx_buff));
	//        ahw_al_uart_conv_transmit(&hdle,0x40,tx_buff,sizeof(tx_buff));
	//        ahw_al_uart_conv_transmit(&hdle,0x60,tx_buff,sizeof(tx_buff));
	//        os_delay(1000);

	//  }
#endif

	// Test code for Receive
#if 0
	//hdle.rcv0_ptr=recieve_ptr_1;
	ahw_al_uart_conv_receive(UART_EXPANDER_MAX14830_OBC,MAX14830_CH0,500,test_max14830_callback_fucn_1,(uint8_t*)recieve_ptr_1);
	//            //os_delay(1000);
	//    hdle.rcv1_ptr=recieve_ptr_2;
	ahw_al_uart_conv_receive(UART_EXPANDER_MAX14830_OBC,MAX14830_CH1,500,test_max14830_callback_fucn_2,(uint8_t*)recieve_ptr_2);
	//            //os_delay(1000);

	//hdle.rcv2_ptr=recieve_ptr_3;
	ahw_al_uart_conv_receive(UART_EXPANDER_MAX14830_OBC,MAX14830_CH2,500,test_max14830_callback_fucn_3,(uint8_t*)recieve_ptr_3);
	//              //  os_delay(1000);
	//
	//    hdle.rcv3_ptr=recieve_ptr_4;
	ahw_al_uart_conv_receive(UART_EXPANDER_MAX14830_OBC,MAX14830_CH3,500,test_max14830_callback_fucn_4,(uint8_t*)recieve_ptr_4);
	//                //os_delay(1000);

#endif
}
#endif
void max14830_uart_init_test_fucn()
{

	init_cfg_1.baudrate=115200;
	init_cfg_1.chnl_id=0x00;
	init_cfg_1.stop_bit=0x00;
	init_cfg_1.wrd_len=0x03;
	init_cfg_1.parity_bit=0x00;
	ahw_al_uart_conv_port_init(UART_EXPANDER_MAX14830_OBC,&init_cfg_1);

	init_cfg_2.baudrate=115200;
	init_cfg_2.chnl_id=0x20;
	init_cfg_2.stop_bit=0x04;
	init_cfg_2.wrd_len=0x03;
	init_cfg_2.parity_bit=0x00;
	ahw_al_uart_conv_port_init(UART_EXPANDER_MAX14830_OBC,&init_cfg_2);


	init_cfg_3.baudrate=115200;
	init_cfg_3.chnl_id=0x40;
	init_cfg_3.stop_bit=0x04;
	init_cfg_3.wrd_len=0x03;
	init_cfg_3.parity_bit=0x00;
	ahw_al_uart_conv_port_init(UART_EXPANDER_MAX14830_OBC,&init_cfg_3);

	init_cfg_4.baudrate=115200;
	init_cfg_4.chnl_id=0x60;
	init_cfg_4.stop_bit=0x04;
	init_cfg_4.wrd_len=0x03;
	init_cfg_4.parity_bit=0x00;
	ahw_al_uart_conv_port_init(UART_EXPANDER_MAX14830_OBC,&init_cfg_4);

}

void spi_to_uart_test()
{
	hdle.ahw_gen_info.ahw_inst_id=UART_EXPANDER_MAX14830_OBC;

	ahw_al_uart_conv_init(&hdle,UART_EXPANDER_MAX14830_OBC);

	max14830_uart_init_test_fucn();

	max14830_tx_test_fucn();

#if 0
	//  os_thread_create(THREAD_TX_1,128 * 4,P_THREAD_TX_1,"tx_1",max14830_tx_test_fucn_1,NULL,NULL,NULL);
	//  os_thread_create(THREAD_TX_2,128 * 4,P_THREAD_TX_2,"tx_2",max14830_tx_test_fucn_2,NULL,NULL,NULL);
	//  os_thread_create(THREAD_TX_3,128 * 4,P_THREAD_TX_3,"tx_3",max14830_tx_test_fucn_3,NULL,NULL,NULL);
	//  os_thread_create(THREAD_TX_4,128 * 4,P_THREAD_TX_4,"tx_4",max14830_tx_test_fucn_4,NULL,NULL,NULL);
	//
	//    os_thread_create(THREAD_TX_1,128 * 4,P_THREAD_TX_1,"tx_1",max14830_tx_test_fucn_1,NULL,NULL,NULL);
	//        os_thread_create(THREAD_TX_2,128 * 4,P_THREAD_TX_2,"tx_2",max14830_tx_test_fucn_2,NULL,NULL,NULL);
	//        os_thread_create(THREAD_TX_3,128 * 4,P_THREAD_TX_3,"tx_3",max14830_tx_test_fucn_3,NULL,NULL,NULL);
	//        os_thread_create(THREAD_TX_4,128 * 4,P_THREAD_TX_4,"tx_4",max14830_tx_test_fucn_4,NULL,NULL,NULL);
#endif
	//  max14830_tx_test_fucn();

}

//#endif



#if 0
void gpio_exp_pcal6408a_payload_board(uint8_t slv_add)
{

	//HAL_StatusTypeDef sts;
	uint8_t rcv_data;
	i2c_read_reg(&hi2c4,slv_add,PCAL6408A_REG, &rcv_data,1);
	printf("PCAL4608A received data====%x\n",rcv_data);


	i2c_read_reg(&hi2c4,slv_add,0x03, &rcv_data,1);
	printf("PCAL4608A received conf data====%x\n",rcv_data);
	/* Output port register 0x01 factory value 0xFF
	 *https://www.nxp.com/docs/en/data-sheet/PCAL6408A.pdf
	 *page number 9
	 */
	if(rcv_data==0xFF)
	{
		printf("PCAL4608A TEST SUCCESS\n");
	}
	else
	{
		printf("PCAL46"
				""
				""
				""
				"08A TEST FAIL\n");
	}

	//ethernet reset sequence


#if(1)

	//configuration register addr =0x03
	//configuring port 2 as input port ===> (feedback PG)
	HAL_StatusTypeDef sts;
	uint8_t arr[2]={0x03,0xA2};//{0x03,0xAD};// LATEST MODIFICATION ASKED BY THEIVEGAN
	sts=HAL_I2C_Master_Transmit(&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
	if(sts!=HAL_OK)
	{
		printf("write failed\n");
	}

	sts = HAL_I2C_Master_Receive (&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
	if(sts!=HAL_OK)
	{
		printf("read failed\n");
	}


	arr[0]=0x01;
	arr[1]=0x18;
	sts=HAL_I2C_Master_Transmit(&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
	if(sts!=HAL_OK)
	{
		printf("write failed\n");
	}

	sts = HAL_I2C_Master_Receive (&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
	if(sts!=HAL_OK)
	{
		printf("read failed\n");
	}
	HAL_Delay(2);


	//while(1)
	{
		arr[0]=0x01;
		//		arr[1]=0x1D;   // ETH_RESET pin is LOW because it will make the LAN IC pins high impedence and it makes the eeprom work
		arr[1]=0x5D;   // ETH_RESET pin is LOW because it will make the LAN IC pins high impedence and it makes the eeprom work

		//  arr[2]={0x03,0x55}; //0 ,2 ,6   55

		sts=HAL_I2C_Master_Transmit(&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("write failed\n");
		}

		sts = HAL_I2C_Master_Receive (&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("read failed\n");
		}
		//HAL_Delay(100);
	}


	//	HAL_Delay(100);
#endif


}
#endif



void drivers_test_main(void)
{
	//	io_hal_uart_test();
	//	io_hal_i2c_test();
	//	io_hal_spi_test();
	return;
}
#if 1

void io_hal_i2c_test()
{
	io_hal_i2c_tx_rx_test();
}

void io_hal_i2c_tx_rx_test()
{
	uint16_t addr=(ADXL345_SENSOR_ADDR << 1);
	uint8_t reg=0x0;
	hal_ret_sts sts = HAL_MAX_ERR;

	while (1)
	{
		sts = io_hal_i2c_transmit(intf_inst_hdle_ptr[IOAL_INST_I2C1], addr, &reg, 1, 200);
		if(HAL_SCS == sts)
		{
			i2c_test_sts[0] = TEST_PASS;
		}
		else
		{
			i2c_test_sts[0] = TEST_FAIL;
		}
		sts = io_hal_i2c_receive(intf_inst_hdle_ptr[IOAL_INST_I2C1], addr, &dbg_data, 1, 200);
		if(HAL_SCS == sts)
		{
			i2c_test_sts[1] = TEST_PASS;
		}
		else
		{
			i2c_test_sts[1] = TEST_FAIL;
		}
		osDelay(1000);
	}
}

void uart_test_intr(void* args)
{

}

void uart_test_intr_rx(void* args)
{

}

ahw_al_gen_info uart_tx_test_ah;
ahw_al_gen_info uart_rx_test_ah;
ahw_al_gen_info spi_test_ah;
void io_hal_uart_test(void)
{
	uart_tx_test_ah.io_intf_id=IOAL_INST_UART6;
	ahal_register_io_cb(&uart_tx_test_ah,UART_TX_CPLT_CB,uart_test_intr,LISR);
	len=sprintf(txdata,"hello world stm32\r\n");
	io_hal_uart_transmit_it(&ioal_huart6,&uart_tx_test_ah,(uint8_t*)txdata,len);
	//	io_hal_uart_tx_block_test();
	//	io_hal_uart_tx_intr_test();
	//  io_hal_uart_tx_dma_test();
	return ;
}

void io_hal_uart_tx_block_test(void)
{
	uint8 pdata[30];
	uint32 size = 25;
	hal_ret_sts sts = HAL_MAX_ERR;

	sprintf((int8*)pdata, "UART6 Block Transmission\n");

	while(1)
	{
		sts = io_hal_uart_transmit(intf_inst_hdle_ptr[IOAL_INST_UART6], pdata, size, 500);
		if(HAL_SCS == sts)
		{
			uart_test_sts[0] = TEST_PASS;
		}
		else
		{
			uart_test_sts[0] = TEST_FAIL;
		}
		osDelay(1000);
	}
	return;
}

void io_hal_uart_tx_intr_test(void)
{
	uint8 pdata[30];
	uint32 size = 25;
	hal_ret_sts sts = HAL_MAX_ERR;
	sprintf((int8*)pdata, "UART6 Intr Transmission\n");

	while(1)
	{
		if(0 == dbg_i)
		{
			dbg_i = 1;
			sts = io_hal_uart_transmit_it(intf_inst_hdle_ptr[IOAL_INST_UART6],NULL, pdata, size);
			if(HAL_SCS == sts)
			{
				uart_test_sts[0] = TEST_PASS;
			}
			else
			{
				uart_test_sts[0] = TEST_FAIL;
			}
			osDelay(1000);
		}
	}
	return;
}

void rtc_print_time()
{
	sys_time_s get_time;
	get_rtc_time(&get_time);

	if((get_time.sec % 30) == 0)
	{
		if(rtc_flag == 0)
		{
			DEBUG_CPRINT(("RTC -%d:%d:%d\n",get_time.hour, get_time.min, get_time.sec));
			rtc_flag = 1;
		}
	}
	else
	{
		rtc_flag = 0;
	}
}


void rtc_set_time_date_nvm_dummy()
{
	sys_time_date_s time_date;
	time_t epoch_time;
	time_date.hour=12;
	time_date.minute=0;
	time_date.seconds=0;
	time_date.date=1;
	time_date.month=RTC_MONTH_JANUARY;
	time_date.year=2025;

	struct tm tm_time_date;
	memset(&tm_time_date,0,sizeof(struct tm));

	tm_time_date.tm_sec = time_date.seconds;
	tm_time_date.tm_min = time_date.minute;
	tm_time_date.tm_hour = time_date.hour;
	tm_time_date.tm_mday = time_date.date;
	tm_time_date.tm_mon = time_date.month;
	tm_time_date.tm_year = time_date.year;

	epoch_time=mktime(&tm_time_date);


	nvm_parameter_write(NVM_DATA,RTC_TIME_DATE_TEMPORARY,(uint8_t*)&epoch_time);
	//nvm_commit();
}


/*
void nvm_rtc_set_time_date()
{
	sys_time_date_s time_date;
	get_crnt_time_date(&time_date);
	nvm_parameter_write(NVM_DATA,RTC_TIME_DATE,(uint8_t*)&time_date);
}

void nvm_rtc_get_time_date(sys_time_date_s *time_date)
{

	nvm_parameter_read(NVM_DATA,RTC_TIME_DATE,(uint8_t*)&time_date);
	if(time_date->year < 23)
	{
	time_date->hour = 0x12;
  	time_date->minute = 0x20;
  	time_date->seconds = 0x10;
  	time_date->month = RTC_MONTH_OCTOBER;
  	time_date->date = 0x1B;
  	time_date->year = 0x14;
	}
}
 */
void rtc_print_date()
{

	sys_date_s get_date;
	crnt_date(&get_date);
	DEBUG_CPRINT(("%d-%d-%d\r\n",get_date.date, get_date.month, get_date.year));
}

uint8_t i2c_test_adxl345(uint8_t id)
{
	hal_ret_sts sts = HAL_MAX_ERR;
	switch (id)
	{
	case 1:
		ioal_hi2c_id = ioal_hi2c1;
		break;
	case 2:
		ioal_hi2c_id = ioal_hi2c2;
		break;
	case 3:
		ioal_hi2c_id = ioal_hi2c3;
		break;
	case 4:
		ioal_hi2c_id = ioal_hi2c4;
		break;
	default:
		sts = HAL_MAX_ERR;
	}

	if (io_hal_i2c_transmit(&ioal_hi2c_id, addr, &reg, 1, 1000) == HAL_SCS)
	{
		if (io_hal_i2c_receive(&ioal_hi2c_id, addr, &data, 1, 1000) == HAL_SCS)
		{
			sts = HAL_SCS;
		}
		else
		{
			sts = HAL_IO_RX_ERR;
		}
	}
	else
	{
		sts = HAL_IO_TX_ERR;                                                                                                                                                                                                                                                                                		sts = HAL_IO_TX_ERR;
	}
	return sts;
}
extern SPI_HandleTypeDef hspi1;
uint8_t spi_test_adxl345(uint8_t *spi_data)
{
	hal_ret_sts sts = HAL_MAX_ERR;
	uint8_t spi_reg = 0x80;
	uint8_t s_data  = 0x00;
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
	if(HAL_SPI_Transmit(&hspi1, &spi_reg, 1, 1000) == HAL_OK)
	{
		if(HAL_SPI_Receive(&hspi1, &s_data, 1, 2000) == HAL_OK)
		{
			if(s_data == 0xE5)
			{
				sts = HAL_SCS;
				memcpy(spi_data,&s_data, sizeof(uint8_t));
			}
			else
			{
				*spi_data = 0x00;
				sts = HAL_IO_RX_ERR;
			}
		}
		else
		{
			*spi_data = 0x00;
			sts = HAL_IO_RX_ERR;
		}
	}
	else
	{
		sts =  HAL_IO_TX_ERR;
	}
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
	return sts;
}

UART_HandleTypeDef ioal_rx_huart;
extern UART_HandleTypeDef huart4;
extern UART_HandleTypeDef huart5;

uint8_t uart_test(uint8_t *data, uint8_t *rx_data, size_t *rx_len)
{
    hal_ret_sts sts = HAL_MAX_ERR;
    const char *tx_str = "Hello World via UART\r\n";
    char temp_rx_buffer[10];
    uint16_t uart_length = 0;

    switch (data[0]) {
        case 1:
        {
        	ioal_huart_id = ioal_huart4;
        	ioal_rx_huart = huart4;
        	uart_tx_test_ah.io_intf_id=IOAL_INST_UART4;
        	ahal_register_io_cb(&uart_tx_test_ah,UART_TX_CPLT_CB,uart_test_intr,HISR);
        }
        break;
        case 2:
        {
        	ioal_huart_id = ioal_huart5;
        	ioal_rx_huart = huart5;
        	uart_tx_test_ah.io_intf_id=IOAL_INST_UART5;
        	ahal_register_io_cb(&uart_tx_test_ah,UART_TX_CPLT_CB,uart_test_intr,HISR);
        }
        break;
        case 3:
        {
        	ioal_huart_id = ioal_huart1;
        	uart_tx_test_ah.io_intf_id=IOAL_INST_UART1;
        	ahal_register_io_cb(&uart_tx_test_ah,UART_TX_CPLT_CB,uart_test_intr,HISR);
        }
        break;
        case 4:
        {
        	ioal_huart_id = ioal_huart6;
        	uart_tx_test_ah.io_intf_id=IOAL_INST_UART6;
        	ahal_register_io_cb(&uart_tx_test_ah,UART_TX_CPLT_CB,uart_test_intr,HISR);
        }
        break;
        default: return HAL_MAX_ERR;
    }
    if (data[1] == TX_TEST_ID)
    {
    	if (io_hal_uart_transmit_it(&ioal_huart_id,&uart_tx_test_ah, (uint8_t *)tx_str, strlen(tx_str)) == HAL_SCS) {
    		sts = HAL_SCS; // Transmission successful
    	}
    	else
    	{
    		sts = HAL_IO_TX_ERR; // Transmission failed
    	}
    }
    else if (data[1] == RX_TEST_ID) {
    	if (HAL_UARTEx_ReceiveToIdle(&ioal_rx_huart,(uint8_t *)temp_rx_buffer,10, &uart_length,   \
    	                                           10000) == HAL_OK)
    	{
    		*rx_len = uart_length;
    		os_memcpy(rx_data,temp_rx_buffer,uart_length); // Copy received data to the input buffer
    		sts = HAL_SCS;
    	}
    	else
    	{
    		*rx_len = 0;
    		sts = HAL_IO_RX_ERR; // No data received
    	}
    }
    return sts;
}

os_queue_handle_ptr can_queue_ptr;
uint8_t can_flag = 0;

uint8_t core_can_test(uint8_t *data, uint8_t *rx_data, size_t *rx_len)
{
    hal_ret_sts sts = HAL_MAX_ERR;
    uint8_t rxp_data[9] = {0};

	if(can_flag == 0)
	{
		os_queue_create(&can_queue_ptr,1,9, "can_test_queue");
		can_flag++;
	}
    TxHeader.DLC = 8;
    TxHeader.ExtId = 0;
    TxHeader.IDE = CAN_ID_STD;
    TxHeader.RTR = CAN_RTR_DATA;
    TxHeader.StdId = 0x111;
    TxHeader.TransmitGlobalTime = DISABLE;
    uint8_t TxData[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};

    if (data[0] == TX_TEST_ID)
    { // Transmit operation
        if (io_hal_can_add_tx_message(&ioal_hcan2, &TxHeader, TxData, &TxMailbox) == HAL_SCS)
        {
            sts = HAL_SCS;  // Transmission successful
        }
        else {
            sts = HAL_IO_TX_ERR;  // Transmission failed
        }
    }
    else if (data[0] == RX_TEST_ID)
    { // Receive operation
		if(os_queue_receive(can_queue_ptr, rxp_data,10000) == ral_success)
          {
			*rx_len = rxp_data[0];
			os_memcpy(rx_data,rxp_data+1,rxp_data[0]);
            sts = HAL_SCS;  // Reception successful
        }
		else
		{
        	*rx_len = 0;
            sts = HAL_IO_RX_ERR;  // Reception failed
        }
    }
    return sts;
}

void can_test(void)
{
#ifdef COREBOARD
	io_hal_can_start(&ioal_hcan2);

#if(INTR)
	io_hal_can_activate_notification(&ioal_hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
#endif
	TxHeader.DLC = 8;
	TxHeader.ExtId = 0;
	TxHeader.IDE = CAN_ID_STD;
	TxHeader.RTR = CAN_RTR_DATA;
	TxHeader.StdId = 0x111;
	TxHeader.TransmitGlobalTime = DISABLE;
	TxData[0] = 0x00;
	TxData[1] = 0x01;
	TxData[2] = 0x02;
	TxData[3] = 0x03;
	TxData[4] = 0x04;
	TxData[5] = 0x05;
	TxData[6] = 0x06;
	TxData[7] = 0x07;
	for(;;)
	{
		//	#if(INTR)
#if(1)
		if (io_hal_can_add_tx_message(&ioal_hcan2, &TxHeader, TxData, &TxMailbox) != HAL_SCS)
		{
			DEBUG_CPRINT(("CAN TX failed\n"));
		}
		while(io_hal_can_get_tx_mailboxes_free_level(&ioal_hcan2) != 3)
			//if(io_hal_can_get_rx_fifo_fill_level(&ioal_hcan2, CAN_RX_FIFO0) != 1)
		{
			//DEBUG_CPRINT(("no data in fifo 0\n"));
		}
		HAL_Delay(2000);
#endif

		/* USER CODE BEGIN 5 */
		/* Infinite loop */

		// DEBUG_CPRINT(("print test\n"));
#if(!INTR)
		if (io_hal_can_add_tx_message(&ioal_hcan1, &TxHeader, TxData, &TxMailbox) != HAL_SCS)
		{
			//Error_Handler();
			DEBUG_CPRINT(("can_tx_error\r\n"));
		}
		while(io_hal_can_get_tx_mailboxes_free_level(&ioal_hcan1) != 3);
#if(0)
		if(io_hal_can_get_rx_fifo_fill_level(&ioal_hcan1, CAN_RX_FIFO0) != 1)
		{
			DEBUG_CPRINT(("no data in fifo 0\n"));
			HAL_Delay(1000);
		}
		io_hal_can_get_rx_message(&ioal_hcan1, CAN_RX_FIFO0, &RxHeader, RxData);
		DEBUG_CPRINT(("CAN transfer polling mode data= %d\r\n",RxData[0]));
#endif
		HAL_Delay(1000);

#else

		if(countt>0)
		{
			DEBUG_CPRINT(("CAN transfer received data %d\r\n",RxData[0]));
			DEBUG_CPRINT(("CAN transfer received data %lx\r\n",RxHeader.StdId));
			countt=0;
			HAL_Delay(1000);
		}
		// os_delay(10);
#endif
#else
		io_hal_can_start(&ioal_hcan1);

#if(INTR)
		io_hal_can_activate_notification(&ioal_hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
#endif
		TxHeader.DLC = 1;
		TxHeader.ExtId = 0;
		TxHeader.IDE = CAN_ID_STD;
		TxHeader.RTR = CAN_RTR_DATA;
		TxHeader.StdId = 0x111;
		TxHeader.TransmitGlobalTime = DISABLE;
		TxData[0] = 0x09;
		for(;;)
		{
			//	#if(INTR)
#if(0)
			if (io_hal_can_add_tx_message(&ioal_hcan1, &TxHeader, TxData, &TxMailbox) != HAL_SCS)
			{
				DEBUG_CPRINT(("CAN TX failed\n"));
			}
			while(io_hal_can_get_tx_mailboxes_free_level(&ioal_hcan1) != 3);
			//if(io_hal_can_get_rx_fifo_fill_level(&ioal_hcan2, CAN_RX_FIFO0) != 1)
			{
				DEBUG_CPRINT(("no data in fifo 0\n"));
			}
			HAL_Delay(1000);
#endif

			/* USER CODE BEGIN 5 */
			/* Infinite loop */

			// DEBUG_CPRINT(("print test\n"));
#if(!INTR)
			if (io_hal_can_add_tx_message(&ioal_hcan1, &TxHeader, TxData, &TxMailbox) != HAL_SCS)
			{
				//Error_Handler();
				DEBUG_CPRINT(("can_tx_error\r\n"));
			}
			while(io_hal_can_get_tx_mailboxes_free_level(&ioal_hcan1) != 3);
#if(0)
			if(io_hal_can_get_rx_fifo_fill_level(&ioal_hcan1, CAN_RX_FIFO0) != 1)
			{
				DEBUG_CPRINT(("no data in fifo 0\n"));
				HAL_Delay(1000);
			}
			io_hal_can_get_rx_message(&ioal_hcan1, CAN_RX_FIFO0, &RxHeader, RxData);
			DEBUG_CPRINT(("CAN transfer polling mode data= %d\r\n",RxData[0]));
#endif
			HAL_Delay(1000);

#else

			if(countt>0)
			{
				DEBUG_CPRINT(("CAN transfer received data %d\r\n",RxData[0]));
				DEBUG_CPRINT(("CAN transfer received data %lx\r\n",RxHeader.StdId));
				countt=0;
				HAL_Delay(1000);
			}
			osDelay(10);
#endif

#endif
		}
	}
	void io_hal_uart_tx_dma_test(void)
	{
		uint8 pdata[30];
		uint32 size = 25;
		hal_ret_sts sts = HAL_MAX_ERR;
		dbg_i = 0;

		sprintf((int8*)pdata, "UART6 DMA Transmission\n");

		while(1)
		{
			if(0 == dbg_i)
			{
				dbg_i = 1;
				sts = io_hal_uart_transmit_dma(intf_inst_hdle_ptr[IOAL_INST_UART6], NULL,pdata, size);
				if(HAL_SCS == sts)
				{
					uart_test_sts[0] = TEST_PASS;
				}
				else
				{
					uart_test_sts[0] = TEST_FAIL;
				}
				osDelay(1000);
			}
		}
		return;
	}






	void io_hal_spi_test()
	{
		HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_4, GPIO_PIN_RESET);   //SET_MAX14830_RESET_PIN_LOW;
		HAL_Delay(1);
		HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_4, GPIO_PIN_SET);     //SET_MAX14830_RESET_PIN_HIGH;

		while (HAL_GPIO_ReadPin(GPIOJ, GPIO_PIN_5) == GPIO_PIN_RESET);

		uint8_t val =0x04;

		uint8_t data[2]={0};
		uint8_t port=0,reg=0x1E;

		data[0] = 0x80 | (port & 0x60) | (reg & 0x1F);
		data[1] = val;
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);    // SET_MAX14830_CS_PIN_LOW;
		io_hal_spi_transmit(&ioal_hspi1, data, 2, 1);  //SPI return from the MAX14830 ignored
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);    // SET_MAX14830_CS_PIN_LOW;

		uint8_t cmd_byte=0;
		val=0;
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);    // SET_MAX14830_CS_PIN_LOW;
		cmd_byte = (port & 0x60) | (reg & 0x1F);
		io_hal_spi_transmit(&ioal_hspi1,&cmd_byte, 1, 100);
		io_hal_spi_receive(&ioal_hspi1,&val,1,100);
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);    // SET_MAX14830_CS_PIN_LOW;
	}

	void io_hal_spi_tx_rx_test(void)
	{
		uint8 reg = 0;
		reg=(reg|0x80);
		//	HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, GPIO_PIN_SET);
		while (1)
		{
			//		HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, GPIO_PIN_RESET);
			io_hal_spi_transmit(&ioal_hspi1, &reg, 1, 100);
			io_hal_spi_receive(&ioal_hspi1, &dbg_data, 1, 200);
			//		HAL_SPI_Transmit(&hspi4,&reg,1,100);
			//		HAL_SPI_Receive(&hspi4,&dbg_data,1,200);
			//		HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, GPIO_PIN_SET);
			//		data=0;
			osDelay(1000);
		}

	}

	int _gettimeofday(struct timeval tv, void* tzvp)
	{
		return 0;  // return non-zero for error
	}

	/*
void rtc_set_time()
{
    hal_ret_sts rtc_sts;
    iohal_rtc_datetypedef sdate;
    iohal_rtc_timetypedef stime;
    iohal_rtc_datetypedef gdate;
    iohal_rtc_timetypedef gtime;

	stime.Hours = 0x12;
	stime.Minutes = 0x29;
	stime.Seconds = 0x0;
	stime.SubSeconds = 0x0;
	stime.SecondFraction = 0x0;

	stime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
	stime.StoreOperation = RTC_STOREOPERATION_RESET;

	sdate.WeekDay = RTC_WEEKDAY_TUESDAY;
	sdate.Month =  11;//RTC_MONTH_NOVEMBER;
	sdate.Date = 2;
	sdate.Year = 22;

	 rtc_sts = io_hal_rtc_set_date(&ioal_hrtc,&sdate,RTC_FORMAT_BIN);
    rtc_sts = io_hal_rtc_set_time(&ioal_hrtc,&stime,RTC_FORMAT_BIN);


	rtc_sts = io_hal_rtc_get_date(&ioal_hrtc,&gdate,RTC_FORMAT_BIN);
   rtc_sts = io_hal_rtc_get_time(&ioal_hrtc,&gtime,RTC_FORMAT_BIN);
   DEBUG_CPRINT(("set time:%d:%d:%d\r\n",gtime.Hours, gtime.Minutes, gtime.Seconds));
}*/
#endif









	/*
	 *
	 *
	 *
	 *  This bellow SW is only for testing purpose
	 *
	 *   POWER_BOARD SENSORS
	 *
	 *
	 *
	 */


	/* USER CODE END 0 */
#define INA230_U5		0X82   //I2C PASS
#define INA230_U21		0x84
#define INA230_U22		0x86
#define INA230_U23		0x92
#define INA230_U26		0x88
#define INA230_U29		0x8E   //0x90
#define INA230_U8		0x80

#define ADM1176_U25		0x8A
#define ADM1176_U36		0x9A   // CHANGED=>A2
#define ADM1176_U11		0x9E
#define ADM1176_U7		0x9C
#define ADM1176_U4		0x94
#define ADM1176_U28		0x8C
#define ADM1176_U14		0x8E
#define ADM1176_U17		0x98
#define ADM1176_U18		0x9A
#define ADM1176_U32		0x96
#define ADM1176_U34		0X80 //0xBA     //0x7E     //0x8D      //0x7E   // CHANGED=>A0

#define MCP23008_U3		0x40   //I2C PASS
#define MCP23008_U19	0x4E   //I2C PASS

#define AD7291_U9		0x56
#define AD7291_U10		0x54          //0x5C
#define AD7291_U12		0x5E

	unsigned char AD7291_ADDRESS=0;

	float volt[8],volt1[8],volt2[8];
	float temperature[3]={0};
	extern I2C_HandleTypeDef hi2c3;
	extern I2C_HandleTypeDef hi2c4;
	extern I2C_HandleTypeDef hi2c1;
	extern I2C_HandleTypeDef hi2c2;

	unsigned char enable=1;

	void power_brd_test()
	{



#if 0
		power_sensor_pbps(INA230_U5);
		power_sensor_pbps(INA230_U21);
		power_sensor_pbps(INA230_U22);
		power_sensor_pbps(INA230_U23);
		power_sensor_pbps(INA230_U26);
		power_sensor_pbps(INA230_U29);
		power_sensor_pbps(INA230_U8);

		mcp23008_gpio_exp(MCP23008_U3);
		mcp23008_gpio_exp(MCP23008_U19);


		adm1176_hotswap_control_pbhsc(ADM1176_U25,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U25);
		adm1176_hotswap_control_pbhsc(ADM1176_U36,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U36);

		adm1176_hotswap_control_pbhsc(ADM1176_U11,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U11);

		adm1176_hotswap_control_pbhsc(ADM1176_U7,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U7);

		adm1176_hotswap_control_pbhsc(ADM1176_U4,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U4);

		adm1176_hotswap_control_pbhsc(ADM1176_U14,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U14);

		adm1176_hotswap_control_pbhsc(ADM1176_U17,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U17);

		adm1176_hotswap_control_pbhsc(ADM1176_U18,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U18);

		adm1176_hotswap_control_pbhsc(ADM1176_U28,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U28);

		adm1176_hotswap_control_pbhsc(ADM1176_U32,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U32);

		adm1176_hotswap_control_pbhsc(ADM1176_U34,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U34);




		mcp23008_gpio_exp(MCP23008_U3);
		mcp23008_gpio_exp(MCP23008_U19);

		AD7291_ADDRESS = AD7291_U9;
		AD7291_Init();
		HAL_Delay(10);
		temperature[0] = AD7291_GetDegreesTemp();
		HAL_Delay(10);
		AD7291_GetVoltageConversionResults(volt);
		HAL_Delay(200);


		if(	enable)
		{
			AD7291_ADDRESS = AD7291_U10;
			AD7291_Init();
			HAL_Delay(10);
			temperature[1] = AD7291_GetDegreesTemp();
			HAL_Delay(10);
			AD7291_GetVoltageConversionResults(volt1);
			HAL_Delay(200);
		}

		AD7291_ADDRESS = AD7291_U12;

		AD7291_Init();
		HAL_Delay(10);
		temperature[2] = AD7291_GetDegreesTemp();
		HAL_Delay(10);
		AD7291_GetVoltageConversionResults(volt2);
		HAL_Delay(200);

#endif


		payload_board_1();


	}

	void payload_board_1()
	{

		// u4,u21,u9 and payload board => gpio_expander => pcal6408a

		power_sensor_pbps(INA230_U21);

		//	adm1176_hotswap_control_pbhsc(ADM1176_U4,0);
		//	hotswap_cntlr_test_pbhsc(ADM1176_U4);



		AD7291_ADDRESS = AD7291_U9;
		//	AD7291_Init();
		HAL_Delay(10);
		//	temperature[0] = AD7291_GetDegreesTemp();
		HAL_Delay(10);
		//AD7291_GetVoltageConversionResults(volt);
		HAL_Delay(200);



		power_sensor_pbps(INA230_U21);







	}

	HAL_StatusTypeDef i2c_read_reg(I2C_HandleTypeDef *hi2c, uint16_t slv_addr,uint8_t reg, uint8_t *data, uint16_t Size);

	void power_sensor_pbps(uint8_t slv_add)
	{


		uint8_t data_arr[2]={0};
		uint16_t data=0;


		i2c_read_reg(&hi2c3,slv_add,INA230_REG, data_arr,2);
		data=(data_arr[0]<<8)|data_arr[1];
		printf("INA230 received data====%x\n",data);

		if(data==0x4127)
		{
			printf("INA230 TEST SUCCESS\n");
		}
		else
		{
			printf("INA230 TEST FAIL\n");
		}
		uint8_t data_arrr[3]={0};
		data_arrr[0]=0x05;
		data_arrr[1]=0x0A;
		data_arrr[2]=0x00;
		HAL_StatusTypeDef sts;
		sts=HAL_I2C_Master_Transmit(&hi2c3,slv_add,data_arrr,3,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("ina230 calib write failed\n");
		}

		i2c_read_reg(&hi2c3,slv_add,0x05, data_arr,2);
		data=(data_arr[0]<<8)|data_arr[1];
		printf("INA230 received calib reg data ====%x\n",data);


		uint16_t data_v=0;
		i2c_read_reg(&hi2c3,slv_add,0x02, data_arr,2);
		data_v=(data_arr[0]<<8)|data_arr[1];
		float voltage = 0.00125*data_v;
		voltage = voltage;
		printf("INA230 received voltage data ====%d\n",data_v);
		//printf("INA230 received voltage data hex ====%x\n",data_v);
		//printf("INA230 received voltage ====%f\n",voltage);


		int16_t data_va=0;
		i2c_read_reg(&hi2c3,slv_add,0x04, data_arr,2);
		data_va=(data_arr[0]<<8)|data_arr[1];
		float current = (data_va/2048.0);
		current = current;
		//printf("INA230 received current data ====%d\n",data_va);
		//printf("INA230 received current data hex ====%x\n",data_va);
		//printf("INA230 received current ====%f\n",current);

		/*
		 * configuration register 0x00 factory value =0x4127
		 * pg number 21
		 */


	}


	void adm1176_hotswap_control_pbhsc(uint8_t addr, uint8_t on_off)
	{
		HAL_StatusTypeDef sts;
		uint8_t reg_data[2]={0};
		reg_data[0]=CONTROL;
		reg_data[1]=on_off;
		sts= HAL_I2C_Master_Transmit(&hi2c3,addr,reg_data,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("write gate failed\n");
		}
		else
		{
			printf("................write gate passed......control=%x..........value===%d\n",reg_data[0],on_off);
		}
	}


	//extern I2C_HandleTypeDef hi2c3;
	unsigned char I2C_wr_fail_address[10] = {0};
	unsigned char I2C_rd_fail_address[10] = {0};

	uint8_t gpio_val=0x53;


	HAL_StatusTypeDef i2c_read_reg(I2C_HandleTypeDef *hi2c, uint16_t slv_addr,uint8_t reg, uint8_t *data, uint16_t Size)
	{
		HAL_StatusTypeDef sts;
		sts=HAL_I2C_Master_Transmit(hi2c,slv_addr,&reg,1,I2C_TEST_TIMEOUT);
		if(sts==HAL_OK)
		{
			sts=HAL_I2C_Master_Receive(hi2c,slv_addr,data,Size,I2C_TEST_TIMEOUT);
			if(sts!=HAL_OK)
			{
				printf("read register I2C Reception failed\n");
			}
		}
		else
		{
			printf("read register I2C transmission failed\n");
		}
		return sts;
	}

	void mcp23008_gpio_exp(uint8_t slv_add)
	{
		static unsigned char i = 0;
#if 1
		HAL_StatusTypeDef sts;

		uint8_t arr[2]={0};
		arr[0]=0x00;
		arr[1]=0xC0;

		sts=HAL_I2C_Master_Transmit(&hi2c3,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("write failed\n");
			I2C_wr_fail_address[i++]=slv_add;
			if(i>=9)
				i=0;

		}

		sts = HAL_I2C_Master_Receive (&hi2c3,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("read failed\n");
			I2C_rd_fail_address[i++]=slv_add;
			if(i>=9)
				i=0;

		}

		arr[0]=0x09;
		arr[1]=0x3F;

		sts=HAL_I2C_Master_Transmit(&hi2c3,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("write failed\n");
			printf("write failed\n");
			I2C_wr_fail_address[i++]=slv_add;
			if(i>=9)
				i=0;

		}

		sts = HAL_I2C_Master_Receive (&hi2c3,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("read failed\n");
			I2C_rd_fail_address[i++]=slv_add;
			if(i>=9)
				i=0;

		}

#else

		HAL_StatusTypeDef sts;


		uint8_t arr[2]={0};
		arr[0]=0x00;
		arr[1]=0xF3;

		sts=HAL_I2C_Master_Transmit(&hi2c3,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("write failed\n");
		}

		sts = HAL_I2C_Master_Receive (&hi2c3,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("read failed\n");
		}

		arr[0]=0x09;
		arr[1]=0x00;

		sts=HAL_I2C_Master_Transmit(&hi2c3,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("write failed\n");
		}

		sts = HAL_I2C_Master_Receive (&hi2c3,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("read failed\n");
		}



		//    uint8_t arr_read = 0x09;
		//    uint8_t data;
		//
		//
		//	sts=HAL_I2C_Master_Transmit(&hi2c1,slv_add,&arr_read,1,I2C_TEST_TIMEOUT);
		//	if(sts!=HAL_OK)
		//	{
		//		printf("write failed\n");
		//	}
		//
		//	sts = HAL_I2C_Master_Receive (&hi2c1,slv_add,&data,1,I2C_TEST_TIMEOUT);
		//	if(sts!=HAL_OK)
		//	{
		//		printf("read failed\n");
		//	}
#endif
	}

	void hotswap_cntlr_test_pbhsc(uint8_t slv_add)
	{
		uint8_t cmd=0x02;
		HAL_StatusTypeDef sts;
		float voltage=0;
		uint16_t volt_raw;
		uint8_t volt_arr[2]={0};
#if(1)
		sts=HAL_I2C_Master_Transmit(&hi2c3,slv_add,&cmd,1,I2C_TEST_TIMEOUT);
		HAL_Delay(200);
		if(sts==HAL_OK)
		{
			sts=HAL_I2C_Master_Receive(&hi2c3,slv_add,volt_arr,2,I2C_TEST_TIMEOUT);
			volt_raw = (volt_arr[0]<<4 | volt_arr[1]>>4);
			printf("ADM1176 1 voltage from register ===%x\n",volt_raw);
			voltage = (V_FULL_SCALE/4096)*(volt_raw);
			voltage=voltage;
			//printf("ADM1176 1 resultant voltage ===%f\n",voltage);
		}
		else
		{
			printf("ADM1176 1 voltage read failed\n");
		}

#endif
		//////////////////////////////////////////////////
		//////////////////////////////////////////////////
#if(1)
		//HAL_Delay(10);
		sts=HAL_I2C_Master_Transmit(&hi2c3,slv_add,&cmd,1,I2C_TEST_TIMEOUT);
		HAL_Delay(200);
		if(sts==HAL_OK)
		{
			sts=HAL_I2C_Master_Receive(&hi2c3,slv_add,volt_arr,2,I2C_TEST_TIMEOUT);
			volt_raw = (volt_arr[0]<<4 | volt_arr[1]>>4);
			printf("ADM1176 2 voltage from register ===%x\n",volt_raw);
			voltage = (V_FULL_SCALE/4096)*(volt_raw);
			voltage=voltage;
			//printf("ADM1176 2 voltage ===%f\n",voltage);
		}
		else
		{
			printf("ADM1176 2 voltage read failed\n");
		}
#endif
	}



	/***************************************************************************//**
	 * @brief Writes data to a slave device.
	 *
	 * @param slaveAddress - Adress of the slave device.
	 * @param dataBuffer - Pointer to a buffer storing the transmission data.
	 * @param bytesNumber - Number of bytes to write.
	 * @param stopBit - Stop condition control.
	 *                  Example: 0 - A stop condition will not be sent;
	 *                           1 - A stop condition will be sent.
	 *
	 * @return status - Number of written bytes.
	 *******************************************************************************/
	unsigned char I2C_wr_fail_address1[10] = {0};
	unsigned char I2C_Write(unsigned char slaveAddress,unsigned char* dataBuffer,unsigned char bytesNumber, unsigned char stopBit)
	{
		HAL_StatusTypeDef sts;
		static unsigned char  i = 0;
		// Add here your code.
		sts=HAL_I2C_Master_Transmit(&hi2c3,slaveAddress,dataBuffer,bytesNumber,I2C_TEST_TIMEOUT);
		if(sts==HAL_OK)
		{
			printf("read success\n");
		}
		else
		{
			I2C_wr_fail_address1[i++]=slaveAddress;
			if(i>=9)
				i=0;

			printf("read failed\n");
		}

		return sts;
	}

	/***************************************************************************//**
	 * @brief Reads data from a slave device.
	 *
	 * @param slaveAddress - Adress of the slave device.
	 * @param dataBuffer - Pointer to a buffer that will store the received data.
	 * @param bytesNumber - Number of bytes to read.
	 * @param stopBit - Stop condition control.
	 *                  Example: 0 - A stop condition will not be sent;
	 *                           1 - A stop condition will be sent.
	 *
	 * @return status - Number of read bytes.
	 *******************************************************************************/
	unsigned char I2C_rd_fail_address1[10]={0};

	unsigned char I2C_Read(unsigned char slaveAddress,  unsigned char* dataBuffer,unsigned char bytesNumber, unsigned char stopBit)
	{
		HAL_StatusTypeDef sts;
		static unsigned char  i = 0;
		sts=HAL_I2C_Master_Receive(&hi2c3,slaveAddress,dataBuffer,bytesNumber,I2C_TEST_TIMEOUT);
		// Add here your code.
		if(sts==HAL_OK)
		{
			printf("read success\n");
		}
		else
		{
			I2C_rd_fail_address1[i++]=slaveAddress;
			if(i>=9)
				i=0;

			printf("read failed\n");
		}

		return sts;
	}


#if 0
	/***************************************************************************//**
	 * @brief Initializes the I2C communication peripheral and configures
	 *        the device.
	 *
	 * @return status - The result of the I2C initialization.
	 *                  Example: 0 - Initialization failed;
	 *                           1 - Initialization succeeded.
	 *******************************************************************************/
	unsigned char AD7291_Init(void)
	{
		unsigned char status = 0;

		//    status = I2C_Init();
		/* It is recommended to enable AD7291_COMMAND_DELAY feature for
       normal operation. */
		//AD7291_SetRegisterValue(AD7291_REG_COMMAND, AD7291_COMMAND_DELAY |0xFF00);   //sakhi_command

		return status;
	}

	/***************************************************************************//**
	 * @brief Writes data into a register.
	 *
	 * @param registerAddress - Address of the register.
	 * @param registerValue - Value of the register.
	 *
	 * @return None.
	 *******************************************************************************/
#if 0
	void AD7291_SetRegisterValue(unsigned char registerAddress,
			unsigned short registerValue)
	{
		unsigned char sendData[3] = {0, 0, 0};

		sendData[0] = registerAddress;
		sendData[1] = ((registerValue & 0xFF00) >> 8);
		sendData[2] = ((registerValue & 0x00FF));
		I2C_Write(AD7291_ADDRESS,
				sendData,
				3,
				1);
	}
#endif
	/***************************************************************************//**
	 * @brief Performs a number of reads from a register.
	 *
	 * @param registerAddress - Address of the register.
	 * @param registerValue - Pointer to a buffer that will store the received data.
	 * @param readsNumber - Number of reads that will be performed.
	 *
	 * @return None.
	 *******************************************************************************/
	void AD7291_GetRegisterValue(unsigned char registerAddress,
			unsigned short* registerValue,
			unsigned char readsNumber)
	{
		unsigned char receiveBuffer[16] = {0, };
		unsigned char read = 0;


		I2C_Write(AD7291_ADDRESS,
				(unsigned char*)&registerAddress,
				1,
				1);

		I2C_Read(AD7291_ADDRESS,
				receiveBuffer,
				(2 * readsNumber),
				1);
		for(read = 0; read < readsNumber; read++)
		{
			registerValue[read] = ((unsigned short)receiveBuffer[read * 2] << 8) +
					receiveBuffer[read * 2 + 1];
		}
	}

	/***************************************************************************//**
	 * @brief Reads the temperature data and converts the result in degrees Celsius.
	 *
	 * @return temperature - Temperature in degrees Celsius.
	 *******************************************************************************/
	float AD7291_GetDegreesTemp(void)
	{
		unsigned short commandValue = 0;
		unsigned short tsenseReg = 0;
		float temperature = 0x0;

		AD7291_GetRegisterValue(AD7291_REG_COMMAND,
				(unsigned short*)&commandValue,
				1);
		commandValue |= AD7291_COMMAND_TSENSE;
		//AD7291_SetRegisterValue(AD7291_REG_COMMAND, commandValue);
		AD7291_GetRegisterValue(AD7291_REG_T_SENSE,
				(unsigned short*)&tsenseReg,
				1);
		tsenseReg = (tsenseReg & 0x0FFF);
		if(tsenseReg & 0x800)
		{
			/* Negative Temperature = (4096 - ADC Code) / 4 */
			tsenseReg = (4096 - tsenseReg);
			temperature = tsenseReg;
			temperature = (temperature / 4);
			temperature = temperature * (-1);
		}
		else
		{
			/* Positive Temperature = ADC Code / 4 */
			temperature = tsenseReg;
			temperature = temperature / 4;
		}

		return temperature;
	}

	/***************************************************************************//**
	 * @brief Reads the voltage conversion results for the enabled channels.
	 *
	 * @param data - Pointer to a buffer that will store the voltage conversion
	 *               results.
	 *               Example: data[0] - channel 0 data;
	 *                        ...
	 *                        data[7] - channel 7 data.
	 *
	 * @return None.
	 *******************************************************************************/
	/*
void AD7291_GetVoltageConversionResults(float* data)
{
    unsigned short commandValue = 0;
	unsigned char mask = 0x80;
    unsigned char channelsNumber = 0;
    unsigned short voltageData[16] = {0, };
    unsigned char channel = 0;
    unsigned char channelAddress = 0;

    AD7291_GetRegisterValue(AD7291_REG_COMMAND,
                            (unsigned short*)&commandValue,
                            1);
    commandValue = (commandValue & 0xFF00) >> 8;
    while(mask)
    {
        if(commandValue & mask)
        {
            channelsNumber++;
        }
        mask = mask >> 1;
    }

    if(channelsNumber)
    {
        AD7291_GetRegisterValue(AD7291_REG_VOLTAGE,
                                voltageData,
                                channelsNumber);
    }
    for(channel = 0; channel < channelsNumber; channel++)
    {
        channelAddress = (voltageData[channel] & 0xF000) >> 12;
        data[channelAddress] = (float)((float)(voltageData[channel] & 0x0FFF) * 2.5) / 4096 ;
    }
}
	 */
#endif

#if 1
	void gpio_exp_pcal6408a_payload_board(uint8_t slv_add)
	{

		//HAL_StatusTypeDef sts;
		uint8_t rcv_data;
		i2c_read_reg(&hi2c4,slv_add,PCAL6408A_REG, &rcv_data,1);
		printf("PCAL4608A received data====%x\n",rcv_data);


		i2c_read_reg(&hi2c4,slv_add,0x03, &rcv_data,1);
		printf("PCAL4608A received conf data====%x\n",rcv_data);
		/* Output port register 0x01 factory value 0xFF
		 *https://www.nxp.com/docs/en/data-sheet/PCAL6408A.pdf
		 *page number 9
		 */
		if(rcv_data==0xFF)
		{
			printf("PCAL4608A TEST SUCCESS\n");
		}
		else
		{
			printf("PCAL46"
					""
					""
					""
					"08A TEST FAIL\n");
		}

		//ethernet reset sequence

#if(0)
		//configuration register addr =0x03
		//configuring port 7,6,5,2,3 as output port
		HAL_StatusTypeDef sts;
		uint8_t arr[2]={0x03,0x13};
		sts=HAL_I2C_Master_Transmit(&hi2c1,slv_add,arr,2,I2C_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("write failed\n");
		}

		//output port register addr =0x01
		//drive the pins 7,6,5,2,3 to low (which includes nRESET pin 6).
		//This is because all GPIO expander pins are by default output high during power on.
		arr[0]=0x01;
		arr[1]=0x00;  //all gpio to active low
		sts=HAL_I2C_Master_Transmit(&hi2c1,slv_add,arr,2,I2C_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("write failed\n");
		}

		HAL_Delay(50);  // 10 after configuring pins as output port
#endif

#if(1)

		//configuration register addr =0x03
		//configuring port 2 as input port ===> (feedback PG)
		HAL_StatusTypeDef sts;
		uint8_t arr[2]={0x03,0xA2};//{0x03,0xAD};// LATEST MODIFICATION ASKED BY THEIVEGAN
		sts=HAL_I2C_Master_Transmit(&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("write failed\n");
		}

		sts = HAL_I2C_Master_Receive (&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("read failed\n");
		}


		arr[0]=0x01;
		arr[1]=0x18;
		sts=HAL_I2C_Master_Transmit(&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("write failed\n");
		}

		sts = HAL_I2C_Master_Receive (&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("read failed\n");
		}
		HAL_Delay(2);


		//while(1)
		{
			arr[0]=0x01;
			//		arr[1]=0x1D;   // ETH_RESET pin is LOW because it will make the LAN IC pins high impedence and it makes the eeprom work
			arr[1]=0x5D;   // ETH_RESET pin is LOW because it will make the LAN IC pins high impedence and it makes the eeprom work

			//  arr[2]={0x03,0x55}; //0 ,2 ,6   55

			sts=HAL_I2C_Master_Transmit(&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
			if(sts!=HAL_OK)
			{
				printf("write failed\n");
			}

			sts = HAL_I2C_Master_Receive (&hi2c4,slv_add,arr,2,I2C_TEST_TIMEOUT);
			if(sts!=HAL_OK)
			{
				printf("read failed\n");
			}
			//HAL_Delay(100);
		}


		//	HAL_Delay(100);
#endif



	}
#endif



	void temp_sensor_test(uint8_t slv_add)
	{
		uint8_t data_arr[2]={0};
		i2c_read_reg(&hi2c1,slv_add,MCP98243_REG,data_arr,2);
		printf("MCP98243received data====%x\n",data_arr[1]);
		/* capability register 0x00 factory value for resolution bit bit4-bit3 =01
		 *https://ww1.microchip.com/downloads/en/DeviceDoc/22153c.pdf
		 *page number 18
		 */
		if((data_arr[1] & 0x18)==0x08)
		{
			printf("MCP98243TEST SUCCESS\n");
		}
		else
		{
			printf("MCP98243 TEST FAIL\n");
		}

	}

	void power_sense_test(uint8_t slv_add)
	{
		uint8_t data_arr[2]={0};
		uint16_t data=0;


		i2c_read_reg(&hi2c1,slv_add,INA230_REG, data_arr,2);
		data=(data_arr[0]<<8)|data_arr[1];
		printf("INA230 received data====%x\n",data);

		if(data==0x4127)
		{
			printf("INA230 TEST SUCCESS\n");
		}
		else
		{
			printf("INA230 TEST FAIL\n");
		}
		uint8_t data_arrr[3]={0};
		data_arrr[0]=0x05;
		data_arrr[1]=0x0A;
		data_arrr[2]=0x00;
		HAL_StatusTypeDef sts;
		sts=HAL_I2C_Master_Transmit(&hi2c1,slv_add,data_arrr,3,I2C_TEST_TIMEOUT);
		if(sts!=HAL_OK)
		{
			printf("ina230 calib write failed\n");
		}

		i2c_read_reg(&hi2c1,slv_add,0x05, data_arr,2);
		data=(data_arr[0]<<8)|data_arr[1];
		printf("INA230 received calib reg data ====%x\n",data);


		uint16_t data_v=0;
		i2c_read_reg(&hi2c1,slv_add,0x02, data_arr,2);
		data_v=(data_arr[0]<<8)|data_arr[1];
		float voltage = 0.00125*data_v;
		voltage = voltage;
		printf("INA230 received voltage data ====%d\n",data_v);
		//printf("INA230 received voltage data hex ====%x\n",data_v);


		int16_t data_va=0;
		i2c_read_reg(&hi2c1,slv_add,0x04, data_arr,2);
		data_va=(data_arr[0]<<8)|data_arr[1];
		float current = (data_va/2048.0);
		current = current;
		//printf("INA230 received current data hex ====%x\n",data_va);

		/*
		 * configuration register 0x00 factory value =0x4127
		 * pg number 21
		 */



	}




	void gen2_payload_board()
	{

		temp_sensor_test(MCP98243_SLV_ADDR);

		power_sense_test(INA230_SLV_ADDR);


		gpio_exp_pcal6408a_payload_board(PCAL6408A_SLV_ADDR);


	}








	/*
	 *
	 *  This is a test sample base code for eeprom
	 *
	 *
	 */

	// Define the I2C
	extern I2C_HandleTypeDef hi2c1;
#define EEPROM_I2C &hi2c1

	// EEPROM ADDRESS (8bits)
#define EEPROM_ADDR 0xA0

	// Define the Page Size and number of pages
#define PAGE_SIZE 16 //64     // in Bytes
#define PAGE_NUM  128    // number of pages



	/*****************************************************************************************************************************************/
	uint8_t bytes_temp[4];

	// function to determine the remaining bytes
	uint16_t bytestowrite (uint16_t size, uint16_t offset)
	{
		if ((size+offset)<PAGE_SIZE) return size;
		else return PAGE_SIZE-offset;
	}

	/* write the data to the EEPROM
	 * @page is the number of the start page. Range from 0 to PAGE_NUM-1
	 * @offset is the start byte offset in the page. Range from 0 to PAGE_SIZE-1
	 * @data is the pointer to the data to write in bytes
	 * @size is the size of the data
	 */
	void EEPROM_Write (uint16_t page, uint16_t offset, uint8_t *data, uint16_t size)
	{

		// Find out the number of bit, where the page addressing starts
		int paddrposition = log(PAGE_SIZE)/log(2);

		// calculate the start page and the end page
		uint16_t startPage = page;
		uint16_t endPage = page + ((size+offset)/PAGE_SIZE);

		// number of pages to be written
		uint16_t numofpages = (endPage-startPage) + 1;
		uint16_t pos=0;

		// write the data
		for (int i=0; i<numofpages; i++)
		{
			/* calculate the address of the memory location
			 * Here we add the page address with the byte address
			 */
			uint16_t MemAddress = startPage<<paddrposition | offset;
			uint16_t bytesremaining = bytestowrite(size, offset);  // calculate the remaining bytes to be written

			HAL_I2C_Mem_Write(EEPROM_I2C, EEPROM_ADDR, MemAddress, 2, &data[pos], bytesremaining, 1000);  // write the data to the EEPROM

			startPage += 1;  // increment the page, so that a new page address can be selected for further write
			offset=0;   // since we will be writing to a new page, so offset will be 0
			size = size-bytesremaining;  // reduce the size of the bytes
			pos += bytesremaining;  // update the position for the data buffer

			HAL_Delay (5);  // Write cycle delay (5ms)
		}
	}

	void float2Bytes(uint8_t * ftoa_bytes_temp,float float_variable)
	{
		union {
			float a;
			uint8_t bytes[4];
		} thing;

		thing.a = float_variable;

		for (uint8_t i = 0; i < 4; i++) {
			ftoa_bytes_temp[i] = thing.bytes[i];
		}

	}

	float Bytes2float(uint8_t * ftoa_bytes_temp)
	{
		union {
			float a;
			uint8_t bytes[4];
		} thing;

		for (uint8_t i = 0; i < 4; i++) {
			thing.bytes[i] = ftoa_bytes_temp[i];
		}

		float float_variable =  thing.a;
		return float_variable;
	}


	/*Write the Float/Integer values to the EEPROM
	 * @page is the number of the start page. Range from 0 to PAGE_NUM-1
	 * @offset is the start byte offset in the page. Range from 0 to PAGE_SIZE-1
	 * @data is the float/integer value that you want to write
	 */

	void EEPROM_Write_NUM (uint16_t page, uint16_t offset, float data)
	{

		float2Bytes(bytes_temp, data);

		EEPROM_Write(page, offset, bytes_temp, 4);
	}

	/* Reads the single Float/Integer values from the EEPROM
	 * @page is the number of the start page. Range from 0 to PAGE_NUM-1
	 * @offset is the start byte offset in the page. Range from 0 to PAGE_SIZE-1
	 * @returns the float/integer value
	 */

	float EEPROM_Read_NUM (uint16_t page, uint16_t offset)
	{
		uint8_t buffer[4];

		EEPROM_Read(page, offset, buffer, 4);

		return (Bytes2float(buffer));
	}

	/* READ the data from the EEPROM
	 * @page is the number of the start page. Range from 0 to PAGE_NUM-1
	 * @offset is the start byte offset in the page. Range from 0 to PAGE_SIZE-1
	 * @data is the pointer to the data to write in bytes
	 * @size is the size of the data
	 */
	void EEPROM_Read (uint16_t page, uint16_t offset, uint8_t *data, uint16_t size)
	{
		int paddrposition = log(PAGE_SIZE)/log(2);

		uint16_t startPage = page;
		uint16_t endPage = page + ((size+offset)/PAGE_SIZE);

		uint16_t numofpages = (endPage-startPage) + 1;
		uint16_t pos=0;

		for (int i=0; i<numofpages; i++)
		{
			uint16_t MemAddress = startPage<<paddrposition | offset;
			uint16_t bytesremaining = bytestowrite(size, offset);
			HAL_I2C_Mem_Read(EEPROM_I2C, EEPROM_ADDR, MemAddress, 2, &data[pos], bytesremaining, 1000);
			startPage += 1;
			offset=0;
			size = size-bytesremaining;
			pos += bytesremaining;
		}
	}

	/* Erase a page in the EEPROM Memory
	 * @page is the number of page to erase
	 * In order to erase multiple pages, just use this function in the for loop
	 */
	void EEPROM_PageErase (uint16_t page)
	{
		// calculate the memory address based on the page number
		int paddrposition = log(PAGE_SIZE)/log(2);
		uint16_t MemAddress = page<<paddrposition;

		// create a buffer to store the reset values
		uint8_t data[PAGE_SIZE];
		memset(data,0xff,PAGE_SIZE);

		// write the data to the EEPROM
		HAL_I2C_Mem_Write(EEPROM_I2C, EEPROM_ADDR, MemAddress, 2, data, PAGE_SIZE, 1000);

		HAL_Delay (5);  // write cycle delay
	}




#define DEV_ADDR 0xa0
	uint8_t dataw1[16] = {1,2,3,4,5,6,7,8,9};// "helo world EPRM1";
	uint8_t dataw2[16] = {11,12,13,14,15,16,17,18,19}; //"helo world EPRM2";
	float dataw3 = 1234.5678;

	uint8_t datar1[100];
	uint8_t datar2[100];
	float datar3;


	uint8_t data_WR =0,erase=0;


#define M24C16_U32 0XA0
#define M24C16_U33 0XA7


	void m24c16_eeprom()
	{


		eeprom_test(M24C16_U32);
		eeprom_test(M24C16_U33);




#if 0
		if(erase)
		{
			for (int i=0; i<512; i++)
			{
				EEPROM_PageErase(i);
			}
		}

		if(data_WR)
		{
			EEPROM_Write(3, 1, dataw1, 9);

			EEPROM_Write(5, 1, dataw2, 9);

			EEPROM_Write_NUM (6, 0, dataw3);
		}



		EEPROM_Read(3, 1, datar1, 10);

		EEPROM_Read(5, 1, datar2, 10);

		datar3 = EEPROM_Read_NUM (6, 0);

#endif


	}




	uint8_t eeprom_test(uint8_t slv_add)
	{
		HAL_StatusTypeDef sts;

		if (slv_add == 0xA7)
		{

			//uint8_t slv_add=0xA7;
			uint8_t write_buffer[2];
			write_buffer[0]=0x00;
			write_buffer[1]=0xaa;
			uint8_t test=0;

			uint8_t read_buffer[2]={0};
			if(test)
			{
				sts=HAL_I2C_Master_Transmit(&hi2c2,slv_add,write_buffer,2,I2C_TEST_TIMEOUT);
				if(sts!=HAL_OK)
				{
					printf("eeprom write failed\n");
				}
			}
			i2c_read_reg(&hi2c2,slv_add, 0x00,read_buffer,1);

		}
		else if (slv_add == 0xA0)
		{

			//uint8_t slv_add=0xA7;
			uint8_t write_buffer[2];
			write_buffer[0]=0x00;
			write_buffer[1]=0xaa;
			uint8_t test=0;

			uint8_t read_buffer[2]={0};
			if(test)
			{
				sts=HAL_I2C_Master_Transmit(&hi2c1,slv_add,write_buffer,2,I2C_TEST_TIMEOUT);
				if(sts!=HAL_OK)
				{
					printf("eeprom write failed\n");
				}
			}
			i2c_read_reg(&hi2c1,slv_add, 0x00,read_buffer,1);

		}
		else
		{
			// DO NOTHING
		}
		return 0;

	}

	/*
	 * This function enable the hot swap controller to power enable PS-1
	 */
	void power_board_test_ps1_on()
	{
		adm1176_hotswap_control_pbhsc(ADM1176_U4,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U4);

	}

	/*
	 *  This function Power off PS-1
	 */
	void power_board_test_ps1_off()
	{
		adm1176_hotswap_control_pbhsc(ADM1176_U4,1);
		hotswap_cntlr_test_pbhsc(ADM1176_U4);

	}

	/*
	 * This function enable the hot swap controller to power enable PS-2
	 */
	void power_board_test_ps2_on()
	{
		adm1176_hotswap_control_pbhsc(ADM1176_U7,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U7);

	}

	/*
	 *  This function Power off PS-2
	 */
	void power_board_test_ps2_off()
	{
		adm1176_hotswap_control_pbhsc(ADM1176_U7,1);
		hotswap_cntlr_test_pbhsc(ADM1176_U7);
	}

	/*
	 * This function enable the hot swap controller to power enable ES-1
	 */
	void edge1_power_board_sel_on()
	{
		adm1176_hotswap_control_pbhsc(ADM1176_U11,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U11);
	}

	/*
	 *  This function Power off ES-2
	 */
	void edge1_power_board_sel_off()
	{
		adm1176_hotswap_control_pbhsc(ADM1176_U11,1);
		hotswap_cntlr_test_pbhsc(ADM1176_U11);
	}

	/*
	 * This function enable the hot swap controller to power enable ES-2
	 */
	void edge2_power_board_sel_on()
	{
		adm1176_hotswap_control_pbhsc(ADM1176_U14,0);
		hotswap_cntlr_test_pbhsc(ADM1176_U14);
	}

	/*
	 *  This function Power off ES-2
	 */
	void edge2_power_board_sel_off()
	{
		adm1176_hotswap_control_pbhsc(ADM1176_U14,1);
		hotswap_cntlr_test_pbhsc(ADM1176_U14);
	}

