#ifndef __HAL_PCA8565_H__
#define __HAL_PCA8565_H__

#include "hc32_ll_usart.h"
#include "hc32_ll_gpio.h"
#include "hc32_ll_fcg.h"
#include "hc32_ll_interrupts.h"
#include "hc32_ll_tmr0.h"
#include "ring_buf.h"
#include "hc32_ll_aos.h"



/*PCF8563的写地址*/
#define PCF8563_SLAVE_ADDRESS       			  				0xA2   	//读地址为写地址加1        

/*寄存器定义*/
#define PCF8563_ADDR_CONTROL_STATUS1                0x00  	//控制/状态寄存器1
//寄存器位定义
#define PCF8563_ADDR_CONTROL_STATUS1_TEST_DIS       0x7F  	//测试模式失能(and)默认
#define PCF8563_ADDR_CONTROL_STATUS1_TEST_EN        0x80  	//测试模式使能(or)
#define PCF8563_ADDR_CONTROL_STATUS1_STOP_DIS       0xDF  	//RTC source clock runs(and)默认
#define PCF8563_ADDR_CONTROL_STATUS1_STOP_EN        0x20  	//the RTC clock is stopped(or)
#define PCF8563_ADDR_CONTROL_STATUS1_POR_DIS        0xF7  	//Power-On Reset (POR) override facility is disabled(and)默认
#define PCF8563_ADDR_CONTROL_STATUS1_POR_EN         0x08  	//Power-On Reset (POR) override may be enabled(or)

/*寄存器定义*/
#define PCF8563_ADDR_CONTROL_STATUS2       			0x01  	//控制/状态寄存器2 
//寄存器位定义
#define PCF8563_ADDR_CONTROL_STATUS2_TI_TP_DIS      0xEF  	//INT is active when TF is active(and)默认
#define PCF8563_ADDR_CONTROL_STATUS2_TI_TP_EN       0x10  	//(or)
#define PCF8563_ADDR_CONTROL_STATUS2_AF_DIS         0xF7  	//read: alarm flag inactive/write: alarm flag is cleared(and)默认
#define PCF8563_ADDR_CONTROL_STATUS2_AF_EN          0x08  	//read: alarm flag active/write: alarm flag remains unchanged（or）
#define PCF8563_ADDR_CONTROL_STATUS2_TF_DIS         0xFB  	//read: timer flag inactive/write: timer flag is cleared(and)默认
#define PCF8563_ADDR_CONTROL_STATUS2_TF_EN          0x04  	//read: timer flag active/write: timer flag remains unchanged（or）
#define PCF8563_ADDR_CONTROL_STATUS2_AIE_DIS        0xFD  	//alarm interrupt disabled(and)默认
#define PCF8563_ADDR_CONTROL_STATUS2_AIE_EN         0x02  	//alarm interrupt enabled（or）
#define PCF8563_ADDR_CONTROL_STATUS2_TIE_DIS        0xFE  	//timer interrupt disabled(and)默认
#define PCF8563_ADDR_CONTROL_STATUS2_TIE_EN         0x01  	//timer interrupt enabled(or)

/*寄存器定义*/
#define PCF8563_ADDR_CLKOUT           	            0x0d  	//CLKOUT频率寄存器
//寄存器位定义
#define PCF8563_ADDR_CLKOUT_FE_DIS           	    0xEF  	//the CLKOUT output  output is high-impedance(and)
#define PCF8563_ADDR_CLKOUT_FE_EN           	    0x10  	//the CLKOUT output is activated(or)默认
#define PCF8563_ADDR_CLKOUT_FD_00           	    0xFC  	//frequency output 32.768 kHz(and)默认
//#define PCF8563_ADDR_CLKOUT_FD_01           	    0x01  	//frequency output 1.024 kHz
//#define PCF8563_ADDR_CLKOUT_FD_10           	    0x10  	//frequency output 32 Hz
#define PCF8563_ADDR_CLKOUT_FD_11           	    0x02  	//frequency output 1 Hz(or)

/*寄存器定义*/
#define PCF8563_ADDR_TIMER                      	0x0e  	//定时器控制寄存器
//寄存器位定义
#define PCF8563_ADDR_TIMER_TE_DIS             	    0x7F  	//timer is disabled(and)默认
#define PCF8563_ADDR_TIMER_TE_EN             	    0x80  	//timer is enabled(or)
#define PCF8563_ADDR_TIMER_TD_00           	        0xFC  	//timer source clock frequency 4.096 kHz(and)
//#define PCF8563_ADDR_TIMER_TD_01           	    0x01  	//timer source clock frequency 64 Hz
//#define PCF8563_ADDR_TIMER_TD_10           	    0x10  	//timer source clock frequency 1 Hz
#define PCF8563_ADDR_TIMER_TD_11           	        0x02  	//timer source clock frequency 1/64 Hz(or)

/*寄存器定义*/
#define PCF8563_ADDR_TIMER_VAL             			0x0f 	//定时器倒计数寄存器 
#define PCF8563_ADDR_YEAR                			0x08  	//年
#define PCF8563_ADDR_MON                			0x07  	//月和世纪
#define PCF8563_ADDR_WEEKDAY            			0x06  	//星期
#define PCF8563_ADDR_DATE              				0x05  	//日
#define PCF8563_ADDR_HOUR              				0x04  	//小时
#define PCF8563_ADDR_MIN                            0x03  	//分钟
#define PCF8563_ADDR_SEC                            0x02  	//秒和VL

//寄存器定义
#define PCF8563_ALARM_MIN                 			0x09  	//分钟报警
//寄存器位定义
#define PCF8563_ALARM_MIN_AE_M_DIS                  0x7F  	//minute alarm is enabled(and)
#define PCF8563_ALARM_MIN_AE_M_EN                   0x80  	//minute alarm is disabled(or)默认

//寄存器定义
#define PCF8563_ALARM_HOUR                 			0x09  	//小时报警
//寄存器位定义
#define PCF8563_ALARM_HOUR_AE_H_DIS                 0x7F  	//hour alarm is enabled(and)
#define PCF8563_ALARM_HOUR_AE_H_EN                  0x80  	//hour alarm is disabled(or)默认

//寄存器定义
#define PCF8563_ALARM_DATE                 			0x09  	//日报警
//寄存器位定义
#define PCF8563_ALARM_DATE_AE_H_DIS                 0x7F  	//date alarm is enabled(and)
#define PCF8563_ALARM_DATE_AE_H_EN                  0x80  	//date alarm is disabled(or)默认

//寄存器定义
#define PCF8563_ALARM_WEEKDAY                		0x09  	//星期报警
//寄存器位定义
#define PCF8563_ALARM_WEEKDAY_AE_H_DIS              0x7F  	//weekday alarm is enabled(and)
#define PCF8563_ALARM_WEEKDAY_AE_H_EN               0x80  	//weekday alarm is disabled(or)默认

/*计算平年闰年*/
#define ISLOOP(year) (((year) % 4 == 0 && (year) % 100 != 0) || ((year) %400 == 0)) ? 1 : 0

/*数据结构声明*/
typedef struct
{
	uint16_t  year;			//年	
	uint8_t   mon;			//月
	uint8_t   date;			//日
	uint8_t   hour;			//小时
	uint8_t   min;			//分
	uint8_t   sec;			//秒	   
	const char *week;	//星期
} rtc_time_st;          //RTC时间信息

/*变量和常量声明*/
extern rtc_time_st rtc_time;
extern const char week_day[7][10];
extern const int8_t months[12];

/*Pcf8563_Rtc底层驱动函数*/
unsigned char DecToBCD(unsigned char _dec);
unsigned char BCDToDec(unsigned char _BCD);
int Days_Count(rtc_time_st *start,rtc_time_st *stop);

/*Pcf8563_Rtc用户接口函数*/
void LogRtcTime(rtc_time_st *rtc_time);
void rtc_init(void);
void Get_Pcf8563_Rtc_Time(rtc_time_st *rtc_time);
void Set_Pcf8563_Rtc_Time(uint16_t year,uint8_t mon,uint8_t date,uint8_t hour,uint8_t min,uint8_t sec,uint8_t week);


void pca8565_task(void *argv);
 
#endif
