/*
 * charger_gbt.h
 *
 *  Created on: Apr 18, 2024
 *      Author: colorbass
 */

#ifndef INC_CHARGER_GBT_H_
#define INC_CHARGER_GBT_H_
#include "main.h"
#include "connector.h"

#define GBT_CST_NO_REASON             0x0000F0F0 // Без причины
#define GBT_CST_CONDITION_REACHED     0x0100F0F0 // Заряд завершен
#define GBT_CST_SUSPENDS_ARTIFICIALLY 0x0400F0F0 // Искуственная остановка
#define GBT_CST_FAULT_SUSPENSION      0x1000F0F0 // Приостановка из за неисправности
#define GBT_CST_BMS_ACTIVELY_SUSPENDS 0x4000F0F0 // завершение по инициативе BMS
#define GBT_CST_CONNECTOR_OVER_TEMP   0x0001F0F0 // Перегрев коннектора
#define GBT_CST_INT_PART_OVER_TEMP    0x0010F0F0 // Перегрев внутренних частей
#define GBT_CST_CANNOT_SUPPLY_REQ_ELQ 0x0040F0F0 // Неполучится обеспечить столько энергии
#define GBT_CST_SUDDENSTOP            0x0000F1F0 // Внезапная остановка
#define GBT_CST_OTHERFALUT            0x0000F4F0 // Прочая ошибка
#define GBT_CST_CURRENT_MISMATCH      0x0000F0F1 // Неправильный ток
#define GBT_CST_ABNORMALVOLTAGEERROR  0x0000F0F4 // Ненормальное напряжение


typedef enum{
	GBT_CC_UNKNOWN,
	GBT_CC_12V,
	GBT_CC_6V,
	GBT_CC_4V,
	GBT_CC_2V,

}gbtCcState_t;

typedef enum{
	GBT_DISABLED = 0x10,
//	GBT_S0_UNCONNECTED = 0x10, //СС1 12V/6V СС2 12V
//	GBT_S1_CONNECTED = 0x11,   //СС1 4V СС2 4V (6V)
//	GBT_S2_LOCKED = 0x12,      //
	GBT_S3_STARTED = 0x13,     // 12V AUX
	GBT_S31_WAIT_BHM = 0x14,		// testing isolation, send CHM receive BHM
	GBT_S4_ISOTEST = 0x15,		// testing isolation, send CHM receive BHM
	GBT_S5_BAT_INFO = 0x16, 	// identifying BMS, send CRM receive BRM (long)
	GBT_S6_BAT_STAT = 0x17,	// send CRM(AA), receive BCP (long)
	GBT_S7_BMS_WAIT = 0x18,	// wait for BMS, send CTS+CML receive BRO(00), next BRO(AA)
	GBT_S8_INIT_CHARGER = 0x19,// starting power modules, send CRO(00)
	GBT_S9_WAIT_BCL = 0x20,	// waiting for BCL (requested voltage), send CRO(00)
	GBT_S10_CHARGING = 0x21,	// charging, contactor ON, send CCS, receiving BCL+BCS+BSM
	GBT_STOP = 0x22,			// normal stop
	GBT_STOP_CSD = 0x23,			// normal stop
	GBT_ERROR = 0x24,			// Error
	GBT_COMPLETE = 0x25,

}gbtState_t;

typedef enum __attribute__((packed)){
	GBT_ERR_OKAY = 0,
	GBT_ERR_INSULATION = 1,

}GBT_Error_t;

#pragma pack(push, 1)

typedef struct {
	uint16_t maxOutputVoltage;

}GBT_BHM_t;

typedef struct {
	uint8_t bmsIdentified;
	uint32_t chargerNumber;
	char chargerLocation[3];
}GBT_CRM_t;

typedef struct {
	uint16_t maxOutputVoltage;
	uint16_t minOutputVoltage;
	uint16_t maxOutputCurrent;
	uint16_t minOutputCurrent;

}GBT_CML_t;

typedef struct{
	uint8_t version[3]; 		//GB/T version
	uint8_t batteryType;		//battery type
	uint16_t batteryCapacity; 	// 0.1Ah/bit
	uint16_t batteryVoltage;  	// 0.1V/bit
	uint8_t batteryVendor[4]; 	// Battery vendor (ASCII string)
	uint32_t batterySN;			// int
	uint8_t batteryManuY;		// year (offset 1985)
	uint8_t batteryManuM;		// month
	uint8_t batteryManuD;		// day
	uint32_t batteryCycleCount:24; //uint24_t
	uint8_t ownAuto;			// 0 = lizing, 1 = own auto
	uint8_t rsvd0;
	uint8_t EVIN[17];			//EVIN
	uint8_t EV_SW_VER[8];

}GBT_BRM_t;

typedef struct {
	uint16_t maxCellVoltage; 		// 0.01v/bit
	uint16_t maxChargingCurrent; 	// 0.1A/bit
	uint16_t totalEnergy; 			// 0.1kWh
	uint16_t maxChargingVoltage; 	// 0.1V/ bit
	uint8_t  maxTemp; 				// 1C/bit, -50C offset
	uint16_t SOC; 					// 0.1%/bit , 0..100%
	uint16_t measVoltage; 			// 0.1V/bit
}GBT_BCP_t;

typedef struct {
	uint16_t requestedVoltage; 		// 0.1V/bit
	uint16_t requestedCurrent; 		// 0.1A/bit
	uint8_t chargingMode; 			// 0x01 - CV, 0x02 - CC

}GBT_BCL_t;

typedef struct {
	uint16_t measuredChargingVoltage;
	uint16_t measuredChargingCurrent;
	uint16_t highestVoltageOfBatteryCell;
	//uint8_t  groupNrOfCellWithHighestVoltage;
	uint8_t currentChargeState;
	uint16_t estimatedRemainingChargingTime;

}GBT_BCS_t;

typedef struct {

	uint8_t singleBatteryHighestVoltageSerNo;// SINGLE_BATTERY_HIGHEST_VOLTAGE_SER_NO
	uint8_t batteryHighestTemp; //BATTERY_HIGHEST_TEMP
	uint8_t batteryHighestTempSerNo;//BATTERY_HIGHEST_TEMP_SER_NO
	uint8_t batteryLowestTemp;//BATTERY_LOWEST_TEMP
	uint8_t batteryLowestTempSerNo;//BATTERY_LOWEST_TEMP_SER_NO
	uint8_t batteryCellVoltageState;//BATTERY_CELL_VOLTAGE_STATE
	uint8_t batteryStatus;//BATTERY_STATUS[bit-mask];

}GBT_BSM_t;

typedef struct {

	uint16_t outputVoltage;
	uint16_t outputCurrent;
	uint16_t chargingTime;
	uint16_t chargingPermissible;

}GBT_CCS_t;

typedef struct {
	uint16_t chargingTime;
	uint16_t outputEnergy;
	uint32_t chargerNumber;
}GBT_CSD_t;


typedef struct {
	uint8_t enablePSU;
	uint16_t requestedVoltage; 		// 0.1V/bit
	uint16_t requestedCurrent; 		// 0.1A/bit
	uint8_t chargingMode; 			// 0x01 - CV, 0x02 - CC
	uint8_t chargingPercentage; 			//
	uint16_t chargingRemainingTimeMin; 			//
	uint16_t chargingElapsedTimeMin; 			//
	uint8_t chargingElapsedTimeSec; 			//

	CONN_State_t connectorState;


}GBT_EDCAN_Output_t;

typedef struct {
	uint8_t PSU_Status;
	uint16_t measuredVoltage; 		// 0.1V/bit
	uint16_t measuredCurrent; 		// 0.1A/bit

	CONN_Control_t chargeControl;
	GBT_Error_t chargingError; // 0 if okay

}GBT_EDCAN_Input_t;


/*	500 - Power Supply
TX
* PSU_ENABLE
* BMS_STATUS
* BMS_ERRORS
* PSU_REQUESTED_VOLTAGE[2]
* PSU_REQUESTED_CURRENT[2]
* BMS_CHARGE_PERCENTAGE
// BMS_MIN_CURRENT
* CHARGE_REMAINING_TIME_MIN
* CHARGE_REMAINING_TIME_SEC
* CHARGE_ELAPSED_TIME[1]

RX
* MEASURED_VOLTAGE[2]
* MEASURED_CURRENT[2]
// MAX_VOLTAGE[2]
// MAX_CURRENT[2]
// MAX_POWER[2]
* PSU_STATUS
*
*/

#pragma pack(pop)

extern gbtState_t GBT_State;

extern ADC_HandleTypeDef hadc1;

extern GBT_BRM_t GBT_EVInfo;
extern GBT_BCP_t GBT_BATStat;
extern GBT_BCS_t GBT_ChargingStatus;
extern GBT_BCL_t GBT_ReqPower;
extern GBT_CML_t GBT_MaxLoad;
extern GBT_CCS_t GBT_ChargerCurrentStatus;
extern GBT_CRM_t GBT_ChargerInfo;
extern GBT_BSM_t GBT_BatteryStatus;
extern GBT_CSD_t GBT_ChargerStop;

extern uint8_t GBT_BRO;

extern uint8_t GBT_Charger_Enable;

void GBT_Init();
void GBT_Start();
void GBT_Reset();
void GBT_Stop(uint32_t causecode);
void GBT_ForceStop();
void GBT_ChargerTask();
void GBT_Error(uint32_t errorcode);

//float GBT_CC_GetAdc();

//uint8_t GBT_CC_GetState();

void GBT_SwitchState(gbtState_t state);
void GBT_Delay(uint32_t delay);
uint32_t GBT_StateTick();
void J_SendPacket(uint32_t PGN, uint8_t pri, uint8_t DLC, uint8_t *data);

void GBT_SendCCS();
void GBT_SendCST(uint32_t Cause);
void GBT_SendCRO(uint8_t state);
void GBT_SendCML();
void GBT_SendCTS();
void GBT_SendCHM();
void GBT_SendCRM(uint8_t state);
void GBT_SendCSD();
void GBT_SendCEM(uint32_t ErrorCode);


#endif /* INC_CHARGER_GBT_H_ */
