/*
 * drivers_test.h
 *
 *  Created on: Mar 1, 2022
 *      Author: invcuser5
 */

#ifndef DRIVERS_UT_INC_DRIVERS_TEST_H_
#define DRIVERS_UT_INC_DRIVERS_TEST_H_


#include "exo_io_al_uart_common.h"
#include "exo_io_al_i2c_common.h"
#include "exo_io_al_spi_common.h"
#include "exo_io_al_can_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_eth_common.h"
#include "exo_io_al_fmc_nor_flash_common.h"
#include "math.h"
#include "string.h"


#define ADXL345_SENSOR_ADDR		0x53
#define TX_TEST_ID  1
#define RX_TEST_ID  2

#define BUFFER_SIZE         ((uint32_t)0x1000)
#define WRITE_READ_ADDR     ((uint32_t)0x0800)
#define START_ADDR  0
#define END_ADDR    2047 //Buff size
#define SDRAM_ADD 0xC0000000
typedef enum
{
	TEST_PASS=1,
	TEST_FAIL=2
}driver_ut_test_sts;

void can_test(void);
void drivers_test_main(void);
void io_hal_uart_test(void);
void io_hal_uart_tx_block_test(void);
void io_hal_uart_tx_intr_test(void);
void io_hal_uart_tx_dma_test(void);
void io_hal_i2c_test();
void io_hal_i2c_tx_rx_test();
void io_hal_spi_test();
void io_hal_spi_tx_test(void);
uint8_t i2c_test_adxl345(uint8_t id);
uint8_t spi_test_adxl345(uint8_t *spi_data);
uint8_t uart_test(uint8_t* data, uint8_t *rx_data, size_t *rx_len);
uint8_t core_can_test(uint8_t *data, uint8_t *rx_data, size_t *rx_len);
uint8_t ioal_uart_echo_test(ioal_uart_hdle* huart);
int8_t fmc_sdram_test();
void io_hal_spi_tx_rx_test(void);
void spi_to_uart_test();
#ifdef SPI_TO_UART_ENB
void max14830_tx_test_fucn();
#endif



/*
 *
 * GEN2
 *
 *
 */

void payload_board();
void hotswap_cntlr_test_payload_board_hsc(uint8_t slv_add);

void power_sense_test(uint8_t addr);
void temp_sensor_test(uint8_t addr);





void power_brd_test();
void power_sensor_pbps(uint8_t slv_add);
void adm1176_hotswap_control_pbhsc(uint8_t addr, uint8_t on_off);
//HAL_StatusTypeDef i2c_read_reg(I2C_HandleTypeDef *hi2c, uint16_t slv_addr,uint8_t reg, uint8_t *data, uint16_t Size);
void mcp23008_gpio_exp(uint8_t slv_add);
void hotswap_cntlr_test_pbhsc(uint8_t slv_add);
void gpio_exp_pcal6408a_payload_board(uint8_t slv_add);
void gen2_payload_board();
void payload_board_1();
void m24c16_eeprom();

uint8_t eeprom_test(uint8_t slv_add);


void temp_sensor_test(uint8_t addr);

void power_sense_test(uint8_t addr);

#define TEST_SW_H

#define MCP98243_SLV_ADDR         0x32
#define DS620_SLV_ADDR            0x90
#define PCAL6408A_SLV_ADDR        0x42
#define INA230_SLV_ADDR           0x82
#define ADM1176_SLV_ADDR_1        0x80
#define ADM1176_SLV_ADDR_2        0x9E  //8a
//#define ETH_SWITCH_ADDR           0x04
#define ETH_SWITCH_ADDR           (0x5C<<1)

#define MCP98243_REG              0x00      //capability register 0x00
#define DS620_REG                 0xAC     //configuration register MSB byte
#define PCAL6408A_REG             0x01      //output port register
#define INA230_REG                0x00      //configuration register
#define ETH_REG                   0x00      //c
//#define ADM1176_REG
#define CONTROL             0x83
#define V_FULL_SCALE 26.35
#define I_FULL_SCALE 105.84

#define SENSE_RES 0.002
#define I2C_TEST_TIMEOUT 500



/***************************************************************************//**
 *   @file   AD7291.h
 *   @brief  Header file of AD7291 Driver.
 *   @author DBogdan (dragos.bogdan@analog.com)
********************************************************************************
 * Copyright 2012(c) Analog Devices, Inc.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  - Neither the name of Analog Devices, Inc. nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *  - The use of this software may or may not infringe the patent rights
 *    of one or more patent holders.  This license does not release you
 *    from the requirement that you obtain separate licenses from these
 *    patent holders to use this software.
 *  - Use of the software either in source or binary form, must be run
 *    on or directly connected to an Analog Devices Inc. component.
 *
 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/******************************************************************************/
/* AD7291                                                                     */
/******************************************************************************/

/* Slave Address */

//#define AD7291_ADDRESS                  0x20    // AS0 = H; AS1 = H.
//#define AD7291_ADDRESS                  0x22    // AS0 = H; AS1 = NC.
//#define AD7291_ADDRESS                  0x23    // AS0 = H; AS1 = L.
//#define AD7291_ADDRESS                  0x28    // AS0 = NC; AS1 = H.
//#define AD7291_ADDRESS                  0x2A    // AS0 = NC; AS1 = NC.
//#define AD7291_ADDRESS                  0x2B    // AS0 = NC; AS1 = L.
//#define AD7291_ADDRESS                  0x2C    // AS0 = L; AS1 = H.
//#define AD7291_ADDRESS                  0x2E    // AS0 = L; AS1 = NC.
//#define AD7291_ADDRESS                  0x2F    // AS0 = L; AS1 = L.

/* AD7291 Registers Definition */
#define AD7291_REG_COMMAND				0x00
#define AD7291_REG_VOLTAGE				0x01
#define AD7291_REG_T_SENSE				0x02
#define AD7291_REG_T_AVERAGE			0x03
#define AD7291_REG_CH0_DATA_HIGH		0x04
#define AD7291_REG_CH0_DATA_LOW			0x05
#define AD7291_REG_CH0_HYST				0x06
#define AD7291_REG_CH1_DATA_HIGH		0x07
#define AD7291_REG_CH1_DATA_LOW			0x08
#define AD7291_REG_CH1_HYST				0x09
#define AD7291_REG_CH2_DATA_HIGH		0x0A
#define AD7291_REG_CH2_DATA_LOW			0x0B
#define AD7291_REG_CH2_HYST				0x0C
#define AD7291_REG_CH3_DATA_HIGH		0x0D
#define AD7291_REG_CH3_DATA_LOW			0x0E
#define AD7291_REG_CH3_HYST				0x0F
#define AD7291_REG_CH4_DATA_HIGH		0x10
#define AD7291_REG_CH4_DATA_LOW			0x11
#define AD7291_REG_CH4_HYST				0x12
#define AD7291_REG_CH5_DATA_HIGH		0x13
#define AD7291_REG_CH5_DATA_LOW			0x14
#define AD7291_REG_CH5_HYST				0x15
#define AD7291_REG_CH6_DATA_HIGH		0x16
#define AD7291_REG_CH6_DATA_LOW			0x17
#define AD7291_REG_CH6_HYST				0x18
#define AD7291_REG_CH7_DATA_HIGH		0x19
#define AD7291_REG_CH7_DATA_LOW			0x1A
#define AD7291_REG_CH7_HYST				0x2B
#define AD7291_REG_T_SENSE_HIGH			0x1C
#define AD7291_REG_T_SENSE_LOW			0x1D
#define AD7291_REG_T_SENSE_HYST			0x1E
#define AD7291_REG_VOLTAGE_ALERT_STATUS	0x1F
#define AD7291_REG_T_ALERT_STATUS		0x20

/* AD7291_REG_COMMAND Definition */
#define AD7291_COMMAND_CH(x)            (1 << (15 - x))
#define AD7291_COMMAND_TSENSE           (1 << 7)
#define AD7291_COMMAND_DELAY            (1 << 5)
#define AD7291_COMMAND_EXT_REF          (1 << 4)
#define AD7291_COMMAND_ALERT_POL_LOW    (1 << 3)
#define AD7291_COMMAND_ALERT_POL_HIGH   (0 << 3)
#define AD7291_COMMAND_CLR_ALERT        (1 << 2)
#define AD7291_COMMAND_RESET            (1 << 1)
#define AD7291_COMMAND_AUTOCYCLE        (1 << 0)

/******************************************************************************/
/* Functions Prototypes                                                       */
/******************************************************************************/

/* Initializes the I2C communication peripheral and configures AD7291. */
unsigned char AD7291_Init(void);

/* Writes data into a register. */
//void AD7291_SetRegisterValue(unsigned char registerAddress,unsigned short registerValue);

/* Performs a number of reads from a register. */
//void AD7291_GetRegisterValue(unsigned char registerAddress,unsigned short* registerValue,unsigned char readsNumber);

/* Reads the temperature data and converts the result in degrees Celsius. */
float AD7291_GetDegreesTemp(void);

/* Reads the voltage conversion results for the enabled channels. */
//void AD7291_GetVoltageConversionResults(float* data);


unsigned char I2C_Write(unsigned char slaveAddress,unsigned char* dataBuffer,unsigned char bytesNumber, unsigned char stopBit);
unsigned char I2C_Read(unsigned char slaveAddress, unsigned char* dataBuffer,unsigned char bytesNumber, unsigned char stopBit);







void EEPROM_Write (uint16_t page, uint16_t offset, uint8_t *data, uint16_t size);
void EEPROM_Read (uint16_t page, uint16_t offset, uint8_t *data, uint16_t size);
void EEPROM_PageErase (uint16_t page);

void EEPROM_Write_NUM (uint16_t page, uint16_t offset, float  fdata);
float EEPROM_Read_NUM (uint16_t page, uint16_t offset);

void power_board_test_ps1_on();
void power_board_test_ps1_off();
void power_board_test_ps2_on();
void power_board_test_ps2_off();

void edge1_power_board_sel_on();
void edge1_power_board_sel_off();
void edge2_power_board_sel_on();
void edge2_power_board_sel_off();






#endif /* DRIVERS_UT_INC_DRIVERS_TEST_H_ */
