#ifndef __MODBUS_H__
#define __MODBUS_H__

#ifdef __cplusplus
 extern "C" {
#endif

#include "stm32f4xx.h"

#define MODBUS_FUNCTION_CODE_READ_INPUT   (0x04)
#define MODBUS_FUNCTION_CODE_WRITE_SINGLE (0x06)
#define MODBUS_FUNCTION_CODE_WRITE_MULTI  (0x10)

#define MODBUS_ERROR_CODE_FUNCTION (0x01)
#define MODBUS_ERROR_CODE_ADDR     (0x02)
#define MODBUS_ERROR_CODE_VALUE    (0x03)
#define MODBUS_ERROR_CODE_EXECUTE  (0x04)

#define MODBUS_ADDR_RO_S (0x0000)
#define MODBUS_ADDR_RO_P (0x0700)
#define MODBUS_ADDR_RW_S (0x1000)
#define MODBUS_ADDR_RW_P (0x1050)

struct crc16_result_s {
	uint8_t crch;
	uint8_t crcl;
};

void crc16_8005_calc(uint8_t * p, uint16_t len, struct crc16_result_s *ret);
void modbus_init(void);
uint16_t modbus_data_big2little16(uint8_t *buff);
uint32_t modbus_data_big2little32(uint8_t *buff);
uint16_t modbus_error_process(uint8_t *pr, uint8_t *pt, uint8_t err_type);
uint16_t modbus_heat_onoff(uint16_t data, uint8_t *pr, uint8_t *pt);
uint16_t modbus_func_read_input(uint8_t *pr, uint8_t *pt, uint16_t len);
uint16_t modbus_process_zero_record(uint8_t ch, uint16_t data, uint8_t *pr, uint8_t *pt);
uint16_t modbus_process_span_record(uint8_t ch, uint32_t data, uint8_t *pr, uint8_t *pt);
uint16_t modbus_process_cali_active(uint8_t ch, uint16_t data, uint8_t *pr, uint8_t *pt);
uint16_t modbus_func_write_single(uint8_t *pr, uint8_t *pt, uint16_t len);
uint16_t modbus_func_write_multi(uint8_t *pr, uint8_t *pt, uint16_t len);
void modbus_loop(void);

#ifdef __cplusplus
}
#endif

#endif /* __MODBUS_H__ */
