#ifndef __RS485_H__
#define __RS485_H__

#ifdef __cplusplus
 extern "C" {
#endif

#include "stm32f4xx.h"

#define RS485_PORT    (GPIOA)
#define RS485_PIN_DIR (GPIO_Pin_8)
#define RS485_PIN_TX  (GPIO_Pin_9)
#define RS485_PIN_RX  (GPIO_Pin_10)

#define RS485_PARITY_NONE (0)
#define RS485_PARITY_ODD  (1)
#define RS485_PARITY_EVEN (2)

#define RS485_DEFAULT_BAUDRATE (19200)
#define RS485_DEFAULT_PARITY   (RS485_PARITY_NONE)
#define RS485_DEFAULT_STOPBITS (1)
#define RS485_DEFAULT_ADDR     (0x01)

#define RS485_BUFF_TX_LEN (1024)
#define RS485_BUFF_RX_LEN (1024)

/* reference for bootloader, partition.h */
#define ADDR_CONFIG (0x08080000)
#define SIZE_CONFIG (128 * 1024)

struct file_config_s {
    uint32_t id;
    uint32_t baudrate;
    uint8_t parity;
    uint8_t stopbits;
    uint8_t addr;
    uint8_t resv;
    uint32_t checksum;
};

struct rs485_data_s {
    uint32_t baudrate;
    uint8_t parity;
    uint8_t stopbits;
    uint8_t addr;
    uint16_t rx_len;
    uint16_t tx_len;
    uint8_t *buff_rx;
    uint8_t *buff_tx;
    uint32_t frame_cnt;
};

extern struct rs485_data_s rs485_data;

void rs485_init(void);
void rs485_timer_restart(void);
void rs485_timer_stop(void);
void rs485_mode_tx(void);
void rs485_mode_rx(void);
void rs485_rx_start(void);
void rs485_rx_stop(void);
void rs485_send_data(uint16_t len);

#ifdef __cplusplus
}
#endif

#endif /* __RS485_H__ */
