#ifndef _RTC_H_
#define _RTC_H_
//----------------------------------------------------------------------------
//
//
//
//----------------------------------------------------------------------------


//---Includes-----------------------------------------------------------------


#include <stdint.h>


//---Forward declarations-----------------------------------------------------


#if defined (MCU_PLATFORM_STM32)
   //#define RTC_HANDLE_TYPE    RTC_HandleTypeDef
   #include <HALWrapper/stm32_rtc.h>
   #include <time.h>           // time_t
#else
   #define RTC_HandleTypeDef    void
   #include <time.h>                   // time_t
   #include <stdint.h>                 // int8_t
#endif

#if !defined(STM32F1)
   #define SUBSECOND_SUPPORTED
#endif

#if ! defined CONFIG_BIWAK_RTC_IS_UTC
   #define CONFIG_BIWAK_RTC_IS_UTC     1
#endif


//---Declaration--------------------------------------------------------------


class CRtc
{
    private:
         RTC_HandleTypeDef* m_pHandler;

    public:
        int status;
        int error;

        CRtc();
        ~CRtc();
        int init();
        void enableLsiLse();
        void getTime( int &hour, int &min, int &sec) const;
        void getTime( int &hour, int &min, int &sec, int &msec) const;
        void getDate( int &year, int &month, int &day) const;


        //static uint32_t getTimeStampSubSecond( void );
        uint32_t getMSec( void );
        uint32_t getSec( void );
        static uint32_t getSec( uint8_t hour, uint8_t min, uint8_t sec);
        static uint32_t getMSec( uint8_t hour, uint8_t min, uint8_t sec, uint8_t subsec );
        //static uint32_t getSubSecond( void );

        int setTime( int8_t hour, int8_t min, int8_t sec);
        void setRelativeTime(int relHour, int relMin, int relSec);
        int setDate( int16_t year, int8_t month, int8_t day);

        bool isValid();
        //bool value();
        uint32_t waitSecondSwitch();
        #if defined ( STM32F103xx )
        void syncDateRead();
        void syncDateWrite() const;
        #endif
        void setAlarm();
        void interrupt();
        
        RTC_HandleTypeDef* getHandler()
        {
           return(m_pHandler);
        }
        time_t getSecondsSinceEpoch() const;
#if 0
        void setEpoch(time_t epoch, int year, int month, int day,
                      int hour, int minute, int second);
#endif
};

// For syscalls
extern CRtc *libcBiwakRtc;

extern "C"
{
   struct tm* bLocaltime (const time_t *_timer);
   struct tm* bGmtime (const time_t *__timer);
}


//---fin-----------------------------------------------------------------------
#endif
