#ifndef DS1302_H_
#define DS1302_H_
#include <stddef.h>
#include <stdint.h>
// #include "config.h"
#include "time_clock.h"
#include "stm32f4xx.h"

/*
ds1302_get_time_s 获得BCD 并转时间戳  ds1302_read_bytes(&ds1302,DS1302_CMD_MASK|DS1302_CMD_REG_MASK,DS1302_REG_ADDR_ALL,time_bcd,8), 
时间戳转  time_clock_date ， time_clock_time_to_date(uint64_t time_s,struct time_clock_date *date,int8_t time_zone)
将time_clock_date 转时间戳 ，time_clock_date_to_time(struct time_clock_date *date,int8_t time_zone)
ds1302_set_time_s 将时间戳写入ds1302
获得时间字符串  uint32_t time_clock_get_time_str(char *buf,int8_t time_zone); 字符串写入buf, 返回字符串长度
*/

#define  Bit_SET     SET 
#define  Bit_RESET     RESET 
#define  GPIO_WriteBit HAL_GPIO_WritePin
#define  GPIO_ReadInputDataBit HAL_GPIO_ReadPin

#define DS1302_CMD_MASK (0x80)
#define DS1302_CMD_READ_MASK (0x01)
#define DS1302_CMD_WRITE_MASK (0x00)
#define DS1302_CMD_REG_MASK (0x00)
#define DS1302_CMD_RAM_MASK (0x40)

#define DS1302_REG_SECONDS_CH_MASK (0x80)
#define DS1302_REG_HOUR_12_N24_MASK (0x80)
#define DS1302_REG_HOUR_NAM_PM_MASK (0x20)
#define DS1302_REG_CONTROL_WP_MASK (0x80)

enum ds1302_reg_addr
{
    DS1302_REG_ADDR_SECONDS=0x00,
    DS1302_REG_ADDR_MINUTES=0x01,
    DS1302_REG_ADDR_HOUR=0x02,
    DS1302_REG_ADDR_DATE=0x03,
    DS1302_REG_ADDR_MONTH=0x04,
    DS1302_REG_ADDR_DAY=0x05,
    DS1302_REG_ADDR_YEAR=0x06,
    DS1302_REG_ADDR_CONTROL=0x07,
    DS1302_REG_ADDR_TRICKLE_CHARGER=0x08,
    DS1302_REG_ADDR_ALL=0x1f
};
enum ds1302_trickle_charger_tcs{
    DS1302_TRICKLE_CHARGER_TCS_OFF=0x00,
    DS1302_TRICKLE_CHARGER_TCS_ON=0xa0
};
enum ds1302_trickle_charger_ds{
    DS1302_TRICKLE_CHARGER_DS_1=0x04,
    DS1302_TRICKLE_CHARGER_DS_2=0x08
};
enum ds1302_trickle_charger_rs{
    DS1302_TRICKLE_CHARGER_RS_NONE=0x00,
    DS1302_TRICKLE_CHARGER_RS_2K=0x01,
    DS1302_TRICKLE_CHARGER_RS_4K=0x02,
    DS1302_TRICKLE_CHARGER_RS_8K=0x03
};
struct ds1302
{
    struct time_clock_time time_base;
    GPIO_TypeDef *gpio_ce;
    GPIO_TypeDef *gpio_sclk;
    GPIO_TypeDef *gpio_io;
    uint16_t pin_ce;
    uint16_t pin_sclk;
    uint16_t pin_io;
};
struct ds1302* ds1302_init(struct ds1302 *ds1302,GPIO_TypeDef *gpio_ce,GPIO_TypeDef *gpio_sclk,GPIO_TypeDef *gpio_io,uint16_t pin_ce,uint16_t pin_sclk,uint16_t pin_io);
int ds1302_write_bytes(struct ds1302 *ds1302,uint8_t cmd,uint8_t addr,const uint8_t *bytes,uint8_t num);
int ds1302_read_bytes(struct ds1302 *ds1302,uint8_t cmd,uint8_t addr,uint8_t *bytes,uint8_t num);
int ds1302_write_disable(struct ds1302 *ds1302);
int ds1302_write_enable(struct ds1302 *ds1302);
uint64_t ds1302_get_time_s(struct ds1302 *ds1302);
int ds1302_set_time_s(struct ds1302 *ds1302,uint64_t time_s);
int ds1302_read_ram(struct ds1302 *ds1302,uint8_t addr,uint8_t *bytes,uint8_t num);
int ds1302_write_ram(struct ds1302 *ds1302,uint8_t addr,uint8_t *bytes,uint8_t num);
int ds1302_set_charger(struct ds1302 *ds1302,enum ds1302_trickle_charger_tcs tcs,enum ds1302_trickle_charger_ds ds,enum ds1302_trickle_charger_rs rs);
#endif