/*
 * Spi_Comm.h
 *
 *  Created on: 2020Äê10ÔÂ14ÈÕ
 *      Author: zhou_l4
 */

#ifndef APP_SPICOMM_SPI_COMM_H_
#define APP_SPICOMM_SPI_COMM_H_


typedef uint8_t (*SPI_MODUL_REGISTER_FUN)(uint16_t, void*, uint32_t );

#define TBOX_SPI_PROTOCOL_VERSION        (01)
#define TBOX_SPI_PROTOCOL_LARGE_PACKET_HEADER_FIRST  (0x0D)
#define TBOX_SPI_PROTOCOL_LARGE_PACKET_HEADER_LAST   (0x01)
#define TBOX_SPI_PROTOCOL_LARGE_HEADER_LEN (2+1+1+1+1+1)
#define TBOX_SPI_PROTOCOL_LARGE_DATA_MIN_LEN    (TBOX_SPI_PROTOCOL_LARGE_HEADER_LEN+1) // + CKS

#define TBOX_SPI_PROTOCOL_SMALL_PACKET_HEADER_FIRST  (0x0D)
#define TBOX_SPI_PROTOCOL_SMALL_PACKET_HEADER_LAST   (0x05)
#define TBOX_SPI_PROTOCOL_SMALL_HEADER_LEN (2+1+1+1+1)
#define TBOX_SPI_PROTOCOL_SMALL_DATA_MIN_LEN    (TBOX_SPI_PROTOCOL_SMALL_HEADER_LEN+1) //(TBOX_SPI_PROTOCOL_SMALL_HEADER_LEN+1) // + CKS
#define TBOX_SPI_SMALL_PACKAGE_LENGTH	(255)
#define TBOX_SPI_RECV_DATA_LENGTH	(TBOX_SPI_SMALL_PACKAGE_LENGTH - TBOX_SPI_PROTOCOL_SMALL_DATA_MIN_LEN)

#define TBOX_SPI_COMM_MAX_SIZE  (1024)
#define TBOX_TIMER_MAX      (10)
#define TBOX_SPI_CACHE_MAX    (10)
#define TBOX_SPI_RECV_COUNT	(2)
#define TBOX_SPI_ACK_COUNT	(10)
#define SPI_COMM_TRANSER_SIZE (512)
#define SPI_COMM_ACK_SIZE (9)
#define TBOX_SPI_COMM_RETRY_COUNT (5)

#define TBOX_SPI_STATUS_STANDBY_OUT	(0)	//SPI APP is standby out mode
#define TBOX_SPI_STATUS_STANDBY_IN	(1)	//SPI APP is standby in mode
#define TBOX_MCU_UPDATE_FLAG		(1)	//MCU BOOTLOADER : 0 , APPLICATION : 1
#define TBOX_MCU_UPDATE_FLAG_SIZE	(1)	//SIZE OF TBOX_MCU_UPDATE_FLAG
#define TBOX_SPI_CYC_TIME_5				(5) //SPI CYC TIME 5ms
#define TBOX_SPI_CYC_TIME_10			(10) //SPI CYC TIME 10ms
#define TBOX_SPI_HANDSHAKE_CYC_TIME_50	(50) //SPI CYC TIME 10ms
#define TBOX_SPI_CONNECT_DIFF_CONUNT	(500)
#define TBOX_SPI_MPU_START_COUNT		(2000)
#define TBOX_SPI_DRIVER_ERROR_COUNT		(10)
#define TBOX_SPI_WAIT_PIN_CHANGE_COUNT	(10)
#pragma pack (push, 1)

#define TBOX_SPI_SUCESS			(1)
#define TBOX_SPI_FAILED			(0)


typedef struct {
	uint16_t	event_code;
	uint32_t  	data_size;
	void *      data_buff;
} SpiCommMsg_st;


// register module id
typedef enum
{
	SPI_MODULE_GENERAL_ID = 0,
	SPI_MODULE_POWER_ID,
	SPI_MODULE_SYSTEM_ID,
	SPI_MODULE_BLE_ID,
	SPI_MODULE_MODEM_ID,
	SPI_MODULE_CAN_ID,
	SPI_MODULE_DIAGNOSTIC_ID,
	SPI_MODULE_UPGRADE_ID,
	SPI_MODULE_RTC_ID,	//Add by xuhongbin for RTC APP Development@2019.08.07
	SPI_MODULE_XCALL_ID,
	SPI_MODULE_SENSOR_ID,
	SPI_MODULE_JTOOLS_ID,
	SPI_MODULE_EEPROM_ID,
	SPI_MODULE_SPAREBATTERY_ID,
	SPI_MODULE_DTC_ID,
	SPI_MODULE_IMU_ID,
	SPI_MODULE_SYS_MONITOR,//add for Mcu System Monitor
	SPI_MODULE_ID_MAX
}eSpiModulId;

/* Connect Status */
typedef enum
{
	SPI_CLOSED = 0,
	SPI_CONNECTING,
	SPI_ESTABLISHED,
}eSpiConnectStatus;

/* Check DTC Status */
typedef enum
{
	SPI_DTC_CHECK_STOP = 0,
	SPI_DTC_CHECK_START,
	SPI_DTC_CHECK_RESTART,
	SPI_DTC_CHECK_INVALIDE,
}eSpiDtcCheckFlag;

typedef struct
{
	uint8_t FBH;      //First byte of message header
	uint8_t LBH;      //Last byte of message header
	uint8_t VER;      //Version
	uint8_t AOM;      //Action of message
	uint8_t MSN;      //Message transmit number
	uint8_t LEN_H8;   //Data length H8 bit
	uint8_t LEN_L8;   //Data length L8 bit
	uint8_t TOP;      //Data Top
}tSpiLargePktHeader;

typedef struct
{
	uint8_t FBH;      //First byte of message header
	uint8_t LBH;      //Last byte of message header
	uint8_t TOC;      //type of command
	uint8_t IOC;      //index of command
	uint8_t SCT;	  //Secrete Flag add by renxs 2020-04-20
	uint8_t LEN;      //Data length
	uint8_t TOP;      //Data Top
}tSpiSmallPktHeader;

/*receive data */
typedef struct
{
    uint8_t msg_buf[TBOX_SPI_COMM_MAX_SIZE];
    uint32_t msg_len;
} SPI_CommData_t;

typedef struct
{
    uint8_t msg_buf[SPI_COMM_TRANSER_SIZE];
    uint32_t msg_len;
} SPI_TranferData_t;

typedef struct
{
	uint32_t key;
	uint8_t counter;
	uint32_t msg_len;
	uint8_t *msg_buf;
	uint8_t isBusy;
	uint8_t isSend;
}SPI_RetryData_t;

typedef struct
{
	uint32_t msg_len;
	uint8_t *msg_buf;
	uint8_t isBusy;

}SPI_RecvData_t;

typedef struct
{
	uint32_t key;
	uint8_t counter ;
}SPI_RetryCounter_t;

typedef struct
{
	eSpiModulId moduleId;
	SPI_MODUL_REGISTER_FUN registerFunc ;
}SPI_Register_t;

typedef enum
{
	SPI_SEND_CACHE = 0,
	SPI_SEND_IMMEDIATE,
	SPI_SEND_INVALIDE
}eSendType;

typedef enum
{
	SPI_NG = 0,
	SPI_OK = 1,
	SPI_OHTER = 2,
}eSpiReturnType;

typedef enum
{
	SPI_SEND = 0,
	SPI_ACK,
	SPI_RESEND,
	SPI_NACK,
	SPI_CKS_ERROR
}eAomType;

typedef enum
{
	SPI_CYC_INVALIDE = 0,
	SPI_CYC_NORMAL,
	SPI_CYC_IDLE,
}eSpiCycType;

typedef enum
{
	//General Recv ID=0x0000 ~0x00FF MPU->MCU
	EV_SPI_MCU_RECV_MPU_REQUEST_HAND_SHAKE_1 = 0x0001,	/* response hand shake step1 */
	EV_SPI_MCU_RECV_MPU_REQUEST_HAND_SHAKE_2 = 0x0002,	/* response hand shake step2 */
	EV_SPI_MCU_RECV_MPU_REQUEST_SECRET_KEY   = 0x0003,
	EV_SPI_MCU_SEND_MCU_STANDBY_OUT 		 = 0x0004,
	EV_SPI_MCU_SEND_MCU_STANDBY_IN			 = 0x0005,
	EV_SPI_MCU_SEND_MCU_SPI_DRIVER_INIT		 = 0x0006,

}eSpiRecvCmdId;

typedef enum
{
	//General Send ID=0x0100 ~0x01FF MCU->MPU
	EV_SPI_MCU_SEND_MPU_RESPOND_HAND_SHAKE_1 = 0x0101,	/* request hand shake step1 */
	EV_SPI_MCU_SEND_MPU_RESPOND_HAND_SHAKE_2 = 0x0102,	/* response hand shake step2 */
	EV_SPI_MCU_SEND_MPU_RESPOND_SECRET_KEY 	 = 0x0103,

}eSpiSendCmdId;


#endif /* APP_SPICOMM_SPI_COMM_H_ */
