#ifndef __MOSBUS_H__
#define __MOSBUS_H__


#include "stdint.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "uart.h"


//ӻַ slave address
#define SlaveAddr			  0x01
#define Hardware1_StA		  0x400000
#define	Hardware2_End	      0xFFFFFF
//ԤõmodbusĴĵַ
#define REG_Address 		  0x0001
//Ĵеݱ費ֵͬĹ
#define Erase_FlashSector 	  		0x0001  //Flash, sectorsвEPCQ128һ256飬ÿsectorsһ65,536 bytes
#define Erase_FlashSector_CRC 		0x19CA
#define Get_App2_addr		 	  	0x0002  //APP2ĵַ
#define Get_App2_addr_CRC 			0x59CB
#define Write_Flash 	  	  		0x0003  //FlashдҪµĳ
#define Write_Flash_CRC 	  		0x980B
#define Start_APP1	    	  		0x0004  //תû1
#define Start_APP1_CRC  	  		0xD9C9
#define Start_APP2   	  	  		0x0005  //תû2
#define Start_APP2_CRC        		0x1809
#define Start_APP3      	  		0x0006  //תû3
#define Start_APP3_CRC        		0x5808

/* 01H ǿƵȦ(̵) */
/* 05H дǿƵȦ(̵) */
#define REG_D01		0x0101
#define REG_D02		0x0102
#define REG_D03		0x0103
#define REG_D04		0x0104
#define REG_DXX 	REG_D04

/* 02H ȡ״̬(ưť) */
#define REG_T01		0x0201
#define REG_T02		0x0202
#define REG_TXX		REG_T02

#define SectorErase 0x01
#define SubsectorErase 0x02


/* 03H ּĴ(ڲĴ) */
/* 06H дּĴ(ڲĴ) */
/* 10H дĴ(ڲĴ) */
#define REG_P01		0x0301		
#define REG_P02		0x0302	

/* 04H ȡĴ(ģź) */
#define REG_A01		0x0401
#define REG_AXX		REG_A01

/* RTU Ӧ */
#define RSP_OK				0		/* ɹ */
#define RSP_ERR_CMD			0x01	/* ֵ֧Ĺ */
#define RSP_ERR_REG_ADDR	0x02	/* Ĵַ */
#define RSP_ERR_VALUE		0x03	/* ֵ */
#define RSP_ERR_WRITE		0x04	/* дʧ */

#define H_RX_BUF_SIZE		64
#define H_TX_BUF_SIZE      	128

typedef struct
{
	uint8_t RxBuf[H_RX_BUF_SIZE];
	uint8_t RxCount;
	uint8_t RxStatus;
	uint8_t RxNewFlag;

	uint8_t RspCode;

	uint8_t TxBuf[H_TX_BUF_SIZE];
	uint8_t TxCount;
	
	uint16_t Reg01H;		/* ͵ļĴ׵ַ */
	uint16_t Reg02H;
	uint16_t Reg03H;		
	uint16_t Reg04H;

	uint8_t RegNum;			/* Ĵ */

	uint8_t fAck01H;		/* Ӧ־ 0 ʾִʧ 1ʾִгɹ */
	uint8_t fAck02H;
	uint8_t fAck03H;
	uint8_t fAck04H;
	uint8_t fAck05H;		
	uint8_t fAck06H;		
	uint8_t fAck10H;
	
}MODH_T;

typedef struct
{
	/* 03H 06H дּĴ */
	uint16_t P01;
	uint16_t P02;
	
	/* 02H дɢĴ */
	uint16_t T01;
	uint16_t T02;
	uint16_t T03;
	
	/* 04H ȡģĴ */
	uint16_t A01;
	
	/* 01H 05H дǿȦ */
	uint16_t D01;
	uint16_t D02;
	uint16_t D03;
	uint16_t D04;
	
}VAR_T;

void ModBus_ReceieveCMD(UART_HANDLE hUART0);
uint8_t MODH_ReadParam_01H(uint16_t _reg, uint16_t _num);
uint8_t MODH_ReadParam_02H(uint16_t _reg, uint16_t _num);
uint8_t MODH_ReadParam_03H(uint16_t _reg, uint16_t _num);
uint8_t MODH_ReadParam_04H(uint16_t _reg, uint16_t _num);
uint8_t MODH_WriteParam_05H(uint16_t _reg, uint16_t _value);
uint8_t MODH_WriteParam_06H(uint16_t _reg, uint16_t _value);
uint8_t MODH_WriteParam_10H(uint16_t _reg, uint8_t _num, uint8_t *_buf);
void MODH_Read_06H(UART_HANDLE hUART0);
void MODH_Read_05H(UART_HANDLE hUART0)
void MODH_AnalyzeApp(UART_HANDLE hUART0);

void delay(uint32_t t);
void jump_hardware1();
void jump_hardware2();
char CopyCocheData(uint8_t *dat, uint16_t len);


#endif
