Files
2026-06-06 16:49:48 +08:00

263 lines
8.1 KiB
C

/*!
* \file xc_drv_rtc.h
*
* \brief The header of xc_drv_rtc.c
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
#ifndef _XC_DRV_RTC_H_
#define _XC_DRV_RTC_H_
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "xc6xxx.h"
#include <stdbool.h>
#include <stdio.h>
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
#define RTC_ICR_DAE_ENABLE (1UL)
#define RTC_ICR_DAE_DISABLE (0UL)
#define RTC_ICR_HOE_ENABLE (1UL)
#define RTC_ICR_HOE_DISABLE (0UL)
#define RTC_ICR_MIE_ENABLE (1UL)
#define RTC_ICR_MIE_DISABLE (0UL)
#define RTC_ICR_SEE_ENABLE (1UL)
#define RTC_ICR_SEE_DISABLE (0UL)
#define RTC_ICR_T2E_ENABLE (1UL)
#define RTC_ICR_T2E_DISABLE (0UL)
#define RTC_ICR_T1E_ENABLE (1UL)
#define RTC_ICR_T1E_DISABLE (0UL)
#define RTC_ICR_T3E_ENABLE (1UL)
#define RTC_ICR_T3E_DISABLE (0UL)
#define RTC_ICR_MASK_ALL_ENABLE (1UL)
#define RTC_ICR_MASK_ALL_DISABLE (0UL)
#define RTC_ICR_CNTE_ENABLE (1UL)
#define RTC_ICR_CNTE_DISABLE (0UL)
#define RTC_AO_TIMER_CTL_AO_TIMER_CLR_SET (1UL)
#define RTC_AO_TIMER_CTL_AO_TIMER_CLR_RESET (0UL)
#define RTC_AO_TIMER_CTL_AO_TIMER_EN_ENABLE (1UL)
#define RTC_AO_TIMER_CTL_AO_TIMER_EN_DISABLE (0UL)
#define RTC_ICR_T1E_Msk (0x1UL << 5UL) /*!< Bit mask of T1E field. */
#define RTC_ICR_T2E_Msk (0x1UL << 4UL) /*!< Bit mask of T2E field. */
#define RTC_ICR_T3E_Msk (0x1UL << 6UL) /*!< Bit mask of T3E field. */
#define RTC_ICR_DaE_Msk (0x1UL << 0UL) /*!< Bit mask of DaE field. */
#define RTC_ICR_HoE_Msk (0x1UL << 1UL) /*!< Bit mask of HoE field. */
#define RTC_ICR_MiE_Msk (0x1UL << 2UL) /*!< Bit mask of MiE field. */
#define RTC_ICR_SeE_Msk (0x1UL << 3UL) /*!< Bit mask of SeE field. */
#define RTC_AO_ALL_INTR_AO_TIMER_INTR_Msk (0x1UL << 5UL) /*!< Bit mask of AO_TIMER_INTR field. */
/*------------------------------------------------------------------------------------
TypeDef
-------------------------------------------------------------------------------------*/
typedef enum
{
RTC_MATCH_SUNDAY = (0x01 << 0), /*Interrupt from TIME1 event. */
RTC_MATCH_MONDAY = (0x01 << 1), /*Interrupt from TIME2 event. */
RTC_MATCH_TUESDAY = (0x01 << 2), /*Interrupt from TIME3 event. */
RTC_MATCH_WEDNESDAY = (0x01 << 3), /*Interrupt from SEC event. */
RTC_MATCH_THURSDAY = (0x01 << 4), /*Interrupt from MIN event. */
RTC_MATCH_FRIDAY = (0x01 << 5), /*Interrupt from HOUR event. */
RTC_MATCH_SATURDAY = (0x01 << 6) /*Interrupt from DAY event. */
} RTC_MatchWeek_TypeDef_t;
typedef enum
{
RTC_MATCH_T1 = (0x01 << 5), /**< MATCH_TIME_1 ch. */
RTC_MATCH_T2 = (0x01 << 4), /**< MATCH_TIME_2 ch */
RTC_MATCH_T3 = (0x01 << 6), /**< MATCH_TIME_3 ch */
} RTC_MatchTimeCh_TypeDef_t;
typedef struct
{
uint8_t Sec;
uint8_t Min;
uint8_t Hour;
uint8_t Week;
uint16_t Day;
} Rtc_DateTypeDef_t;
typedef struct
{
uint8_t HourLimit;
uint8_t MinLimit;
uint8_t SecLimit;
} Rtc_DateLimitTypeDef_t;
typedef struct
{
uint8_t ch;
uint8_t Sec;
uint8_t Min;
uint8_t Hour;
uint8_t Week;
} Rtc_MatchTimeTypeDef_t;
typedef struct
{
Rtc_DateTypeDef_t Date;
Rtc_DateLimitTypeDef_t DateLimit;
Rtc_MatchTimeTypeDef_t MatchTime;
bool MatchTimeEnable;
bool SecIT_Enable;
bool MinIT_Enable;
bool HourIT_Enable;
bool DayIT_Enable;
} RTC_InitCfg_t;
typedef struct RTC_Intr_Cb_s
{
uint8_t (*AO_Timer_Callback)(uint8_t calib_flag);
uint8_t (*T1_CMR_Callback)(void);
uint8_t (*T2_CMR_Callback)(void);
uint8_t (*T3_CMR_Callback)(void);
uint8_t (*Sec_Callback)(void);
uint8_t (*Min_Callback)(void);
uint8_t (*Hour_Callback)(void);
uint8_t (*Day_Callback)(void);
} RTC_Intr_Cb_t;
extern Rtc_DateTypeDef_t Date;
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Inline Functions
-------------------------------------------------------------------------------------*/
static __inline void __rtc_dateget(Rtc_DateTypeDef_t *Date)
{
Date->Sec = rtc_ccvr__rtc_ccvr_sec__getf();
Date->Min = rtc_ccvr__rtc_ccvr_min__getf();
Date->Hour = rtc_ccvr__rtc_ccvr_hour__getf();
Date->Day = rtc_ccvr__rtc_ccvr_day__getf();
Date->Week = rtc_wvr__getf();
}
static __inline void rtc_datelimitSet(RTC_InitCfg_t *rtc_cfg)
{
rtc_hour_limit__setf(rtc_cfg->DateLimit.HourLimit);
rtc_minute_limit__setf(rtc_cfg->DateLimit.MinLimit);
rtc_second_limit__setf(rtc_cfg->DateLimit.SecLimit);
}
static __inline void rtc_dateSet(RTC_InitCfg_t *rtc_cfg)
{
rtc_clr__rtc_clr_day__setf(rtc_cfg->Date.Day);
rtc_clr__rtc_clr_hour__setf(rtc_cfg->Date.Hour);
rtc_clr__rtc_clr_min__setf(rtc_cfg->Date.Min);
rtc_clr__rtc_clr_sec__setf(rtc_cfg->Date.Sec);
rtc_wlr__setf(rtc_cfg->Date.Week);
}
static __inline void rtc_config_matchtimeit(RTC_InitCfg_t *rtc_cfg)
{
if (rtc_cfg->MatchTime.ch == RTC_MATCH_T1) {
if (rtc_cfg->MatchTimeEnable) {
rtc_icr__rtc_t1e__setf(1);
} else {
rtc_icr__rtc_t1e__setf(0);
}
} else if (rtc_cfg->MatchTime.ch == RTC_MATCH_T2) {
if (rtc_cfg->MatchTimeEnable) {
rtc_icr__rtc_t2e__setf(1);
} else {
rtc_icr__rtc_t2e__setf(0);
}
} else if (rtc_cfg->MatchTime.ch == RTC_MATCH_T3) {
if (rtc_cfg->MatchTimeEnable) {
rtc_icr__rtc_t3e__setf(1);
} else {
rtc_icr__rtc_t3e__setf(1);
}
}
}
static __inline void rtc_config_secit(RTC_InitCfg_t *rtc_cfg)
{
if (rtc_cfg->SecIT_Enable) {
rtc_icr__rtc__se_e__setf(1);
} else {
rtc_icr__rtc__se_e__setf(0);
}
}
static __inline void rtc_config_minit(RTC_InitCfg_t *rtc_cfg)
{
if (rtc_cfg->SecIT_Enable) {
rtc_icr__rtc__mi_e__setf(1);
} else {
rtc_icr__rtc__mi_e__setf(0);
}
}
static __inline void rtc_config_hourit(RTC_InitCfg_t *rtc_cfg)
{
if (rtc_cfg->SecIT_Enable) {
rtc_icr__rtc__ho_e__setf(1);
} else {
rtc_icr__rtc__ho_e__setf(0);
}
}
static __inline void rtc_config_dayit(RTC_InitCfg_t *rtc_cfg)
{
if (rtc_cfg->SecIT_Enable) {
rtc_icr__rtc__da_e__setf(1);
} else {
rtc_icr__rtc__da_e__setf(0);
}
}
/*------------------------------------------------------------------------------------
Exported Functions
-------------------------------------------------------------------------------------*/
void xc_rtc_init(RTC_InitCfg_t *rtc_cfg);
void xc_rtc_start(void);
void xc_rtc_stop(void);
void xc_rtc_date_get(Rtc_DateTypeDef_t *Date);
uint8_t AO_Timer_Callback(uint8_t calib_flag);
uint8_t T1_CMR_Callback(void);
uint8_t T2_CMR_Callback(void);
uint8_t T3_CMR_Callback(void);
uint8_t Sec_Callback(void);
uint8_t Min_Callback(void);
uint8_t Hour_Callback(void);
uint8_t Day_Callback(void);
void rtc_intr_callback(uint64_t intr_sta);
#endif // _XC_DRV_RTC_H_