/*
 * shared_var_longmax_28379D.h
 *
 *  Created on: Dec 3, 2019
 *      Author: weizhong
 */

#ifndef SHARED_VAR_LONGMAX_28379D_H_
#define SHARED_VAR_LONGMAX_28379D_H_

/*
 * Shared_variable_Longmax_28379D.c
 *
 *  Created on: Dec 3, 2019
 *      Author: weizhong
 */

// Variable define
/* SPI */
#define             ADC_trans_num         8                             // Size of sData, # of measurements + 1(sampling index)，max 8 since fifo buffer has limit of 16 16-bit data
#define             SPI_use     0                                  // SPI in use 0-A, 1-B
#define             SLAVE                 0                               // SPI slave master mode - slave
#define             MASTER                1                               // SPI master mode
#define             DIS_LOOPBACK          0                               // Disable SPI loopback
#define             EN_LOOPBACK           1                               // Enable SPI loopback
#define             SPI_CPOL              0                               // SPI clock polarity 0 when the clock idles
#define             SPI_CPHA              0                               // SPI clock phase, 0-capture data when rising, 1-capture data when falling
#define             WORD_BIT             16                              // SPI word length
Uint32              sData_32[ADC_trans_num];                                   // Send data buffer for IPC (32-bit because we are transfering floating point
Uint32              rData_32[ADC_trans_num];                                   // receive data buffer for IPC (32-bit because we are transfering floating point
Uint32              rData_16[ADC_trans_num*2];                                   // Receive data buffer for IPC
Uint16              sData_16[ADC_trans_num*2];                              // define 16-bit sending data that is decomposed from sData_32
void                *sData_IPC;                                            // Pointer to sData for IPC comm
int                 EN_SPI_DMA                 = 0;                            // EN_controllerable SPI with DMA - 1, EN_controllerable SPI only - 0
const int           clk       =          500000;                          // SPI CLK, Hz (not effect in slave mode)
const int           fifo_txlvl=          0;                              // FIFO Interrupt Level for transmitting
const int           fifo_rxlvl=          ADC_trans_num*2;                   // FIFO Interrupt Level for receiving, TI SPI word length is 16, floating is 32, so need to send twice as many as floating
const int           SPI_delay =          0;                               // SPI transmit delay bits
const int             burst_tx=           0;                               // burst size should be less than 8, if not 0 set it to (FIFO_TXLVL-1)
const int             burst_rx=           (2-1);                           // burst size should be less than 8 (FIFO_RXLVL -1) defined in Function_define_WW_HFB.c
const int             transfer=           15;                              // [(MEM_BUFFER_SIZE/FIFO_LVL)-1]
// ........... JNIE .........................//
//JNIE
Uint32 led_default = 0x00000000;
Uint32 led_offset = 0x00005C00;
Uint32 led_off= 0x461C4000;             // 10000 offset
Uint32 led_flag;
float              tData[3];             // data to transfer to CPU1 from CPU2

// ............ IPC .........................//
Uint32          sizeofIPC      = sizeof (float);                                         // size of IPC sending data in bit, 32bit for floating


// Function declaration
void InitfanEPWM(unsigned int, int, float, float, float);
void EPWM_fan_duty_update(unsigned int, float, float);
void EPWM_dutyupdate(unsigned int,float);
void EPWM_phaseshiftupdate(unsigned int,float);
void StartEPWM(unsigned int);
void StopEPWM(unsigned int);
//void InitEPWM(unsigned int,int,int, int, float,float,int);
void InitUPDOWNEPWM(unsigned int,int,int,int, float ,float,unsigned int);
void InitEPWMsGpio(unsigned int);                                               // Configure the GPIOs' pull-resistors on ePWMs
void ConfigureADC(int, int , int , int ,int );
void ConfigureDAC(Uint16 dac_num);                                  // Initialize DAC (dac_num = A,B,C) (Bitfield)
void spi_fifo_init(unsigned int, int, int, int);                                             // Bitfield SPI FIFO initialization - the slave and master defined here, TX and RX INT trigger level
void dma_init(uint16_t, uint16_t,int, int, int);                                                  // Bitfield DMA initialization for transmitting SPI data
void GPIO_relay_longmax();
void GPIO_signalcomm_longmax();
void open_precharge_grid_relay();
void close_precharge_grid_relay();
void open_main_grid_relay();
void close_main_grid_relay();
void open_precharge_batt_relay();
void close_precharge_batt_relay();
void open_main_batt_relay();
void close_main_batt_relay();
void EPWM_peridupdate();
void InitspibGpio();
void InitspiaGpio();
void Initspi(unsigned int, int, int, int, int, int);
void StartTimer(void);
float StopTimer(void);
void gate_driver_DESAT_init(void);
void gate_driver_EN_init(void);
void gate_driver_disable(void);
void gate_driver_enable(void);
void gate_driver_RST_init(void);
void gate_driver_RDY_init(void);
void GPIO_temp_meas_select_init(void);
void gate_driver_RST_low(void);
void gate_driver_RST_high(void);
int max_temp(int, int, int);
#endif /* SHARED_VAR_LONGMAX_28379D_H_ */
