#ifndef HW_CONFIG_H_
#define HW_CONFIG_H_

#include "main.h"

#define UART_RX_BUF_MAX 	512//272
#define UART_TX_BUF_MAX 	512//128

#define USB_CDC_BUF_MAX 	272

typedef	struct uart_def_8bit
{
	unsigned char rx_buf[UART_RX_BUF_MAX];
	unsigned short rx_len;
	unsigned short rx_index;
	unsigned short rx_complete;
	unsigned short rsv;
	
	unsigned char tx_buf[UART_TX_BUF_MAX];
	unsigned short tx_len;
	unsigned short tx_index;
	unsigned short tx_none_buf;
	unsigned short tx_complete;
	unsigned short tx_complete_delay_flag;
	unsigned short tx_complete_delay_tick;
}uart_info_8bit_struct;

typedef	struct
{
	unsigned char rx_buf[USB_CDC_BUF_MAX];
	unsigned int rx_len;
	unsigned int rx_complete;
	
	unsigned char tx_buf[USB_CDC_BUF_MAX];
	unsigned int tx_len;
	unsigned int tx_complete;
	unsigned int tx_complete_delay_flag;
	unsigned int tx_complete_delay_tick;
}T_usb_cdc_data_struct;

typedef union{
	unsigned char D8[256];
	unsigned int D32[64];
}TdataType;

typedef union{
    uint32_t val_32[3];
    uint8_t val_8[12];
}uid_union;

/* Exported types ------------------------------------------------------------*/
typedef enum {PASSED = 0, FAILED = !PASSED} TestStatus_t;
/* Exported constants --------------------------------------------------------*/
#define SDRAM_BANK_ADDR                 ((uint32_t)0xC0000000)

/* #define SDRAM_MEMORY_WIDTH            FMC_SDRAM_MEM_BUS_WIDTH_8  */
/* #define SDRAM_MEMORY_WIDTH            FMC_SDRAM_MEM_BUS_WIDTH_16 */
#define SDRAM_MEMORY_WIDTH               FMC_SDRAM_MEM_BUS_WIDTH_32

#define SDCLOCK_PERIOD                   FMC_SDRAM_CLOCK_PERIOD_2
/* #define SDCLOCK_PERIOD                FMC_SDRAM_CLOCK_PERIOD_3 */

#define SDRAM_TIMEOUT                    ((uint32_t)0xFFFF)

//刷新频率计数器(以SDCLK频率计数),计算方法:
//COUNT=SDRAM刷新周期/行数-20=SDRAM刷新周期(us)*SDCLK频率(Mhz)/行数
//我们使用的SDRAM刷新周期为64ms,SDCLK=240/3=80Mhz,行数为8192(2^13).
//所以,COUNT=64*1000*80/8192-20=677
#define REFRESH_COUNT                    ((uint32_t)625)   /* SDRAM refresh counter */
   
#define SDRAM_MODEREG_BURST_LENGTH_1             ((uint16_t)0x0000)
#define SDRAM_MODEREG_BURST_LENGTH_2             ((uint16_t)0x0001)
#define SDRAM_MODEREG_BURST_LENGTH_4             ((uint16_t)0x0002)
#define SDRAM_MODEREG_BURST_LENGTH_8             ((uint16_t)0x0004)
#define SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL      ((uint16_t)0x0000)
#define SDRAM_MODEREG_BURST_TYPE_INTERLEAVED     ((uint16_t)0x0008)
#define SDRAM_MODEREG_CAS_LATENCY_2              ((uint16_t)0x0020)
#define SDRAM_MODEREG_CAS_LATENCY_3              ((uint16_t)0x0030)
#define SDRAM_MODEREG_OPERATING_MODE_STANDARD    ((uint16_t)0x0000)
#define SDRAM_MODEREG_WRITEBURST_MODE_PROGRAMMED ((uint16_t)0x0000)
#define SDRAM_MODEREG_WRITEBURST_MODE_SINGLE     ((uint16_t)0x0200)

extern uid_union uid_value;

extern unsigned int usb_is_host;

extern uart_info_8bit_struct uart_info_8bit[8];
extern T_usb_cdc_data_struct usb_cdc_data;

extern void uart_tx_trigger(unsigned int uart_ord);
extern void uart_tx_load_data(unsigned int uart_ord, unsigned char * p_data, unsigned int len);
extern unsigned int get_uart_tx_complete(unsigned int uart_ord);
extern unsigned int get_uart_tx_complete_delay_flag(unsigned int uart_ord);
extern void uart_tx_complete_delay(void);
extern void uart_tx_over(unsigned int uart_ord, unsigned char * p_data, unsigned int len);
extern void uart_load_txbuf(unsigned int uart_ord, unsigned char * p_data, unsigned int len);

extern void SDRAM_Initialization_Sequence(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *Command);
extern void HW_init(void);

extern unsigned int check_sum8(unsigned char* p_data,unsigned int data_length);

extern unsigned int check_open_password(unsigned int check_value);
extern unsigned int check_open_password_isenable(void);
extern unsigned int check_auth_function_isenable(void);
extern void set_open_password_isover(void);
extern void set_auth_function_able(unsigned int able);

extern void usb_host_to_device(void);
extern void usb_device_to_host(void);

extern unsigned int  Crc32Cal(unsigned int * SourceData, unsigned int Length);
extern void Crc32Reset(void);
extern void Crc32Feed(unsigned int aVal);
extern unsigned int  Crc32GetResult();


#endif
