#ifndef __RTC_H
#define __RTC_H


#include "at32f403a_407.h"


#define TM_RTC_LEAP_YEAR(year) 			((((year) % 4 == 0) && ((year) % 100 != 0)) || ((year) % 400 == 0))



typedef struct
{
    __IO uint8_t  seconds;      /*!< Seconds parameter, from 00 to 59 */
    __IO uint16_t subseconds;   /*!< Subsecond downcounter. When it reaches zero, it's reload value is the same as
                                     @ref RTC_SYNC_PREDIV, so in our case 0x3FF = 1023, 1024 steps in one second */
	__IO uint8_t  minutes;      /*!< Minutes parameter, from 00 to 59 */
	__IO uint8_t  hours;        /*!< Hours parameter, 24Hour mode, 00 to 23 */
	__IO uint8_t  day;          /*!< Day in a week, from 1 to 7 */
	__IO uint8_t  date;         /*!< Date in a month, 1 to 31 */
	__IO uint8_t  month;        /*!< Month in a year, 1 to 12 */
	__IO uint8_t  year;         /*!< Year parameter, 00 to 99, 00 is 2000 and 99 is 2099 */
	__IO uint32_t unix;         /*!< Seconds from 01.01.1970 00:00:00 */
  
} TM_RTC_t;


// RTC Result enumeration
typedef enum {
	TM_RTC_Result_Ok,           /*!< Everything OK */
	TM_RTC_Result_Error         /*!< An error occurred */
} TM_RTC_Result_t;



// RTC date and time format
typedef enum {
	TM_RTC_Format_BIN = 0x00, /*!< RTC data in binary format */
	TM_RTC_Format_BCD         /*!< RTC data in binary-coded decimal format */
} TM_RTC_Format_t;



//
uint8_t TM_RTC_Init(void);

//
void TM_RTC_SetDataTimeUnix(uint32_t unixTime);

//
TM_RTC_Result_t TM_RTC_SetDateTime(TM_RTC_t* data);

//
TM_RTC_Result_t TM_RTC_SetDateTimeString(char* str);

//
void TM_RTC_GetDateTime(TM_RTC_t* data, TM_RTC_Format_t format);

//
uint32_t TM_RTC_GetUnixTimeStamp(TM_RTC_t* data);

//
void TM_RTC_GetDateTimeFromUnix(TM_RTC_t* data, uint32_t unix);

//
void TM_RTC_PrintTime(void);

//
uint32_t RTC_GetUnixTime(void);

//
void rtc_subtim_init(void);

//
uint64_t rtc_get_ms(void);

//
uint32_t rtc_foo(void);

//
void rtc_set_in_ms(uint64_t ms);

#endif
