#ifndef __APP_PROTOCOL__
#define __APP_PROTOCOL__

#include "stdbool.h"
#include "hc32_ll_fcg.h"
#include "string.h"


#define APP_A_ADDR   	 (0x8000)
#define BOOT_INFO_ADDR (0x3c000)

#define DATA_LENTH (512)

#define FRAME_HEAD (0X5A)
#define DATA_HEAD  (0X5A)
#define FRAME_TAIL (0XA5)

#define MPU_ADDR   (0X10)
#define MCU_ADDR   (0X20)

#define MASTER_CMD      (0X50)
#define SLAVE_CMD_ACK   (0X5C)
#define SLAVE_CMD_NACK  (0X5F)

#define MASTER_CTR      (0X60)
#define SLAVE_CTR_ACK   (0X6C)
#define SLAVE_CTR_NACK  (0X6F)

#define SPI_BUF_LEN									(512)

/* PWM控制路数 PWM_NUM * 3 */
#define PWM_NUM_CONFIG (2)
/*
*帧协议控制
*/
#define FRAME_MAX_LENGTH (256)
#pragma pack(1)
/* 固定开头帧 */
typedef struct
{
	uint8_t frame_head;
	uint16_t data_lenth;
	uint8_t source_addr;
	uint8_t destination_addr;
	uint8_t	cmd;
	uint8_t	cmd_type;
}frame_pack_t;

/* 固定帧各字节顺序*/
enum
{
	FRAME_HEAD_INDEX = 0,
	FRAME_LEN_INDEX,
	FRAME_SOUADDR_INDEX = 3,
	FRAME_DETADDR_INDEX,
	FRAME_CMD_INDEX,
	FRAME_CMD_TYPE_INDEX,
};
/* SLAVE_CMD_ACK 0X50 */

#define TEMP_NUMBER 15
/***********-- --- --0X01 查询-- --- --***********/
/*设备状态*/
typedef struct
{
	uint8_t work_status;
	
	uint8_t temperatures_number;
	int8_t  temperatures_data[TEMP_NUMBER];
	
	uint8_t	backup_voltage;
	uint8_t	backup_temperature;
	
	uint8_t high_driver_number;
	uint8_t high_driver_status;//最大8路 bit
	
	uint8_t low_driver_number;
	uint8_t low_driver_status; //最大8路 bit	
	
	uint8_t last_wake_up_source;
	
	uint8_t location_status;
}cmd_05_01_device_status_t;

/***********-- --- --0X03 查询-- --- --***********/
/*通讯数据*/
typedef struct
{
	uint16_t x_data;
	uint16_t y_data;
	uint16_t z_data;
	
	uint8_t  can0_data_status;
	uint8_t  can0_fault_status;
	uint16_t can0_data_lenth;
	uint8_t  can0_data[16];

	uint8_t  can1_data_status;
	uint8_t  can1_fault_status;
	uint16_t can1_data_lenth;
	uint8_t  can1_data[16];
	
	uint8_t  rs485_data_status;
	uint16_t rs485_data_lenth;
	uint8_t  rs485_data[128];	
}communication_data_t;



typedef struct
{
	uint16_t default_init; 
	uint16_t is_ota_flag;  
	uint16_t is_can_write_flag;  
	uint32_t destination_address; 
}sys_paratemer_info_t;


/*数据包基础结构*/
typedef struct
{
	uint8_t frame_head;
	uint8_t source_addr;
	uint8_t destination_addr;
	uint8_t data_head;
	uint8_t	cmd;
	uint8_t	cmd_type;
	uint16_t data_lenth;
	int8_t *data;
	uint16_t crc;
	uint8_t frame_tail;
}pack_t;





/*定位信息*/
typedef struct 
{
	uint8_t locate_mark;	//֨定位标志
	uint8_t satellite_num;//卫星数量
	uint8_t direction[2];	//方位
	uint8_t speed[2];			//̙速度
	uint8_t altitude[2];	//海拔
	uint8_t latitude[4];	//纬度
	uint8_t longitude[4];	//经度
	uint8_t accmileage[4];// 
} gps_info_t;


/***********控制****************/
/*输出控制*/
typedef struct
{
	uint8_t output_number;//最大8路
	uint8_t output_control;//bit
	
  uint16_t pwm_number;
	uint8_t pwm_data[PWM_NUM_CONFIG][3];//PWM_NUM_CONFIG路PWM 频率2bytes,占空比1byte	
}output_control_t;


/*休眠控制*/
typedef struct
{
  uint8_t  control_type;
	uint16_t sleep_time; //s
}sleep_control_t;


/*FLASH控制*/
typedef struct
{
  uint8_t   data_type;
	uint16_t  write_data_count;
	uint16_t  write_data_lenth;
	uint8_t   write_data[512];
	uint16_t  crc;
}flash_write_t;


/*通讯控制*/
typedef struct
{
  uint8_t   can_send_data_type;
	uint32_t  can_send_data_lenth;
	uint8_t   can_send_data[13];
	
  uint8_t   rs485_send_data_type;
	uint32_t  rs485_send_data_lenth;
	uint8_t   rs485_send_data[512];
}communication_control_t;



void mcu_to_mpu_task(void *arg);
#pragma pack()
//inline unsigned short crc16_modbus(unsigned char *pdata, int len);



#endif