
#ifndef SOFT_POWERAMP_H
#define SOFT_POWERAMP_H

#include "stdint.h"
#include "stdbool.h"
#include "common.h"
///////////////////

#define POWERAMP_CRC8_KEY (0x2A)
/*
Number Item Data Index Remark
1 电池总电压 Data[0-1] unsigned int, 16bits， mV,Range: 0 – 60000mV
2 充放电电流 Data[2-3] signed int， 16bits， 10mA
                Range: – 300000 to 80000mA
                Positive for charging
                Negative for discharging.
3 Max allowable charging current
                Data[4-5] signed int， 16bits， 10mA
                Range: – 300000 to 80000mA
                Positive for charging
                Negative for discharging.
                Remark:
                If communicating with UAV, this value
                will be 0.
4 最大电芯温度 Data[6-7] signed int， 16bits， 0.1 Degree C, range: -40 to 125 degree C
5 最小电芯温度 Data[8-9] signed int， 16bits， 0.1 Degree C, range: -40 to 125 degree C
6 相对充电状态 Data[10-11] unsigned int， 16bits， 0.1% 0 to 1000 (0 to 1000‰)
7 绝对充电状态 Data[12-13] unsigned int， 16bits， 0.1% 0 to 1000 (0 to 1000‰)
8 剩余容量 Data[14-15] unsigned int， 16bits， mAH 0 mA to 20000 mAH
9 最大电芯压差 Data[16-17] unsigned int， 16bits， mV, 0mV to 1000mV
10 Cell 1 电压 Data[18-19] unsigned int， 16bits， mV 0 to 4500mV
11 Cell 2 电压 Data[20-21] unsigned int， 16bits， mV 0 to 4500mV
12 Cell 3 电压 Data[22-23] unsigned int， 16bits， mV 0 to 4500mV
13 Cell 4 电压 Data[24-25] unsigned int， 16bits， mV 0 to 4500mV
14 Cell 5 电压 Data[26-27] unsigned int， 16bits， mV 0 to 4500mV
15 Cell 6 电压 Data[28-29] unsigned int， 16bits， mV 0 to 4500mV
16 Cell 7 电压 Data[30-31] unsigned int， 16bits， mV 0 to 4500mV
17 Cell 8 电压 Data[32-33] unsigned int， 16bits， mV 0 to 4500mV
18 Cell 9 电压 Data[34-35] unsigned int， 16bits， mV 0 to 4500mV
19 Cell 10 电压 Data[36-37] unsigned int， 16bits， mV 0 to 4500mV
20 Cell 11 电压 Data[38-39] unsigned int， 16bits， mV 0 to 4500mV
21 Cell 12 电压 Data[40-41] unsigned int， 16bits， mV 0 to 4500mV
22 Cell 13 电压 Data[42-43] unsigned int， 16bits， mV 0 to 4500mV
23 Warning level Data[44-45] Unsigned char, 8 bits,
                            Value:
                            0: no warning
                            1: level 1 warning, UAV shall return
                            2: level 2 warning, UAV shall land
24 系统状态 Data[46-47] unsigned int, 16 bits
                        Permanent Fault Alarm:
                        Bit0: 1:电芯压差大, 0: 正常
                        Bit1: 1:循环寿命结束， 0: 正常
                        Bit2: 1:电芯温度高， 0: 正常
                        Bit3: 1:其他失效， 0: 正常
                        Cell Fault Alarm：
                        Bit4: 1： 电芯压差较大, 0: 正常
                        Discharging Alarm:
                        Bit5: 1:电池放电过温, 0: 正常
                        Bit6: 1:电池放电低温, 0: 正常
                        Bit7, Bit8: 放电低压告警
                        value: 0: 正常
                        value: 1: level 1, 电芯电压 3.4V 或
                        SOC 30%, See Spec for details.
                        value: 2: level 2, 电芯电压 3.0V 或
                        SOC 20%, See Spec for details.
                        value: 3: level 3, 电芯电压 2.8V 或
                        SOC 10%, See Spec for details.
                        Bit9: 1:放电过流， 0: 正常
                        Charging Alarm:
                        Bit10: 1:充电过温， 0: 正常
                        Bit11: 1:充电低温， 0: 正常
                        Bit12: 1:充电过压， 0: 正常
                        Bit13: 1:充电过流， 0: 正常
                        Bit14: 1:充电满， 0: 正常
                        Remark:
                        For charger: when BMS encounters
                        the Charging Alarm or Cell fault
                        alarm, BMS will turn off the charging
                        MOSFET to stop charging.For UAV: when BMS encounters the
                        Discharging Alarm or Cell fault alarm
                        (UAV not in operation), BMS will turn
                        off the discharging MOSFET.
*/
#pragma pack(1)
typedef struct
{
    short amp_data[24];  //
}_POWERAMP_INF;
#pragma pack()
extern _POWERAMP_INF poweramp_inf;




/*
Number Item Data Index Remark
1 软件版本号 Data[0-1] unsigned char, 2 bytes.
            Data[0]: major version
            Data[1]: minor version
            e.g.: v02.60, Data [0]=2, Data[1]=60
2 硬件版本号 Data[2-3] unsigned char, 2 bytes.
            Data[0]: major version
            Data[1]: minor version
            e.g.: v02.60, Data [0]=2, Data[1]=60
3 通信协议版本号 Data[4-5] unsigned char, 2 bytes.
            Data[0]: major version
            Data[1]: minor version
            e.g.: v02.60, Data [0]=2, Data[1]=60
*/
#pragma pack(1)
typedef struct
{
    unsigned char amp_ver[6];  //
}_POWERAMP_VER;
#pragma pack()
extern _POWERAMP_VER poweramp_ver;





/*
Number Item Data Index Remark
1 BMS 设计容量 Data[0-1] unsigned int， 16bits， 0.1AH Max value 6553.5 AH
2 电池串数 Data[2] unsigned char, 8bits, unit: 1 Value: 13
3 电池健康度 Data[3-4] unsigned int， 16bits， 0.1% Range: 0 to 1000 (0 to 1000‰)
4 电池循环次数 Data[5-6] unsigned int, 16bits, unit: 1 time
5 电池过充次数 Data[7-8] unsigned int, 16bits, unit: 1 time
6 电池过放次数 Data[9-10] unsigned int, 16bits, unit: 1 time
7 电池过温次数 Data[11-12] unsigned int, 16bits, unit: 1 time
8 电池过流次数 Data[13-14] unsigned int, 16bits, unit: 1 time
9 单体电池满充电压 Data[15-16] unsigned int, 16bits, mV
10 单体电池过放电压 Data[17-18] unsigned int, 16bits, mV
11 电池编号 Data[19-25] 7 bytes,
                        Data [19-20]: ‘J’’M’
                        Data [21]: year, 19-50, offset 2000,
                        2019-2050
                        Data [22]: month, 0-12
                        Data [23]: day,0-31max
                        Data [24-25]: unsigned int, production
                        line SN
12 年 Data[26] unsigned char, 8 bits, offset 2000 E.g.: 19+2000 = 2019
13 月 Data[27] unsigned char, 8 bitsRange: 0 - 12
14 日 Data[28] unsigned char, 8 bitsRange: 0 – 31 max
15 时 Data[29] unsigned char, 8 bitsRange: 0 - 23
16 分 Data[20] unsigned char, 8 bitsRange: 0 - 59
17 秒 Data[31] unsigned char, 8 bitsRang
*/

#pragma pack(1)
typedef struct
{
    uint16_t amp_cap; //容量
    uint8_t amp_cellnum; //串数
    uint16_t amp_soh; //健康度
    uint16_t amp_cycle; //循环次数
    uint16_t amp_chg_count; //
    uint16_t amp_dsg_count;
    uint16_t amp_otp_count;
    uint16_t amp_ocp_count;
    uint16_t amp_fullchg_cellvolt;
    uint16_t amp_uvd_cellvolt;
    uint8_t amp_sn[14]; //编号
    uint8_t amp_year;
    uint8_t amp_month;
    uint8_t amp_day;
    uint8_t amp_hour;
    uint8_t amp_min;
    uint8_t amp_sencond;
}_POWERAMP_SYS;
#pragma pack()
extern _POWERAMP_SYS poweramp_sys;

extern Connect_check Poweramp_Link;
void PowerAmpCanRecvHookFunction(uint32_t cellCanID, uint8_t* data, uint8_t len);

void send_msg_to_poweramp(void);
void check_poweramp_link(void);


extern uint32_t time_poweramp_lastrecv;

#endif
