/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    rtc_if.h
  * @author  MCD Application Team
  * @brief  configuration of the rtc_if.c instances
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __RTC_IF_H__
#define __RTC_IF_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32_systime.h"

/* USER CODE BEGIN Includes */
#include <time.h>

/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
#define RTC_N_PREDIV_S 10
#define RTC_PREDIV_S ((1<<RTC_N_PREDIV_S)-1)
#define RTC_PREDIV_A ((1<<(15-RTC_N_PREDIV_S))-1)

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* External variables --------------------------------------------------------*/
/* USER CODE BEGIN EV */
/* USER CODE END EV */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
/**
  * @brief Timer driver definition
  */
typedef struct
{
    void				  (* InitTimer )( void );                  /*!< Initialisation of the low layer timer    */

    uint32_t              (* SetTimerContext)( void );             /*!< Set the timer context */
    uint32_t              (* GetTimerContext)( void );             /*!< Get the timer context */

    uint32_t              (* GetTimerElapsedTime)( void );         /*!< Get elapsed time */
    uint32_t              (* GetTimerValue)( void );               /*!< Get timer value */
    uint32_t              (* GetMinimumTimeout)( void );           /*!< Get Minimum timeout */

    uint32_t              (* ms2Tick)( uint32_t timeMicroSec );    /*!< convert ms to tick */
    uint32_t              (* Tick2ms)( uint32_t tick );            /*!< convert tick into ms */
} UTIL_TIMER_Driver_s;

/**
  * @brief Timer value on 32 bits
  */
typedef uint32_t UTIL_TIMER_Time_t;
/**
  *  @}
  */

/*!
 * @brief Init RTC hardware
 * @param none
 * @retval none
 */
void RTC_IF_Init(void);

/*!
 * @brief set timer Reference (TimerContext)
 * @param none
 * @retval  Timer Reference Value in  Ticks
 */
uint32_t RTC_IF_SetTimerContext(void);

/*!
 * @brief Get the RTC timer Reference
 * @param none
 * @retval Timer Value in  Ticks
 */
uint32_t RTC_IF_GetTimerContext(void);

/*!
 * @brief Get the timer elapsed time since timer Reference (TimerContext) was set
 * @param none
 * @retval RTC Elapsed time in ticks
 */
uint32_t RTC_IF_GetTimerElapsedTime(void);

/*!
 * @brief Get the timer value
 * @param none
 * @retval RTC Timer value in ticks
 */
uint32_t RTC_IF_GetTimerValue(void);

/*!
 * @brief Return the minimum timeout in ticks the RTC is able to handle
 * @param none
 * @retval minimum value for a timeout in ticks
 */
uint32_t RTC_IF_GetMinimumTimeout(void);

/*!
 * @brief converts time in ms to time in ticks
 * @param [IN] time in milliseconds
 * @retval returns time in timer ticks
 */
uint32_t RTC_IF_Convert_ms2Tick(uint32_t timeMilliSec);

/*!
 * @brief converts time in ticks to time in ms
 * @param [IN] time in timer ticks
 * @retval returns time in timer milliseconds
 */
uint32_t RTC_IF_Convert_Tick2ms(uint32_t tick);

/*!
 * @brief Get rtc time
 * @param [OUT] subSeconds in ticks
 * @retval returns time seconds
 */
uint32_t RTC_IF_GetTime(uint16_t *subSeconds);

/*!
 * @brief write seconds in backUp register
 * @Note Used to store seconds difference between RTC time and Unix time
 * @param [IN] time in seconds
 * @retval None
 */
void RTC_IF_BkUp_Write_Seconds(uint32_t Seconds);

/*!
 * @brief reads seconds from backUp register
 * @Note Used to store seconds difference between RTC time and Unix time
 * @param [IN] None
 * @retval Time in seconds
 */
uint32_t RTC_IF_BkUp_Read_Seconds(void);

/*!
 * @brief writes SubSeconds in backUp register
 * @Note Used to store SubSeconds difference between RTC time and Unix time
 * @param [IN] time in SubSeconds
 * @retval None
 */
void RTC_IF_BkUp_Write_SubSeconds(uint32_t SubSeconds);

/*!
 * @brief reads SubSeconds from backUp register
 * @Note Used to store SubSeconds difference between RTC time and Unix time
 * @param [IN] None
 * @retval Time in SubSeconds
 */
uint32_t RTC_IF_BkUp_Read_SubSeconds(void);

void TimestampNow(uint8_t *buff, uint16_t *size);

void RTC_calibration( int16_t cal );
int16_t RTC_get_calibration();
uint8_t get_weekday( RTC_DateTypeDef sDate );
uint8_t now( struct tm *tm );
void set_time( uint8_t dd, uint8_t mm, uint8_t yy, uint8_t hh, uint8_t min, uint8_t sec);
uint8_t get_alarmA_time( uint8_t *hr, uint8_t *min, uint8_t *sec );
void stop_alarmA(void);
void stop_alarmB(void);
int get_time(void);
uint32_t get_EpochTime(void);
void set_alarmA( struct tm* alarm_tm );
void set_alarmB( struct tm* alarm_tm );
void newDay();

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

#ifdef __cplusplus
}
#endif

#endif /* __RTC_IF_H__ */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
