V1.0
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/*!
|
||||
* \file rtc.h
|
||||
*
|
||||
* \brief Target rtc implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __RTC_H__
|
||||
#define __RTC_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
|
||||
#include "xc6xxx.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
extern volatile uint32_t Utc_TotalSec;
|
||||
// Test val
|
||||
extern uint8_t Sec_Flag;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
void rtc_sec_handler(void);
|
||||
void RTC_Config( void );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __RTC_H__ */
|
||||
@@ -0,0 +1,86 @@
|
||||
/*!
|
||||
* \file time_conv.h
|
||||
*
|
||||
* \brief Target time convert implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __TIME_CONV_H__
|
||||
#define __TIME_CONV_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "rtc.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
#define YEAR_BASE 2000U
|
||||
#define TIME_ZONE (8)
|
||||
|
||||
#define M_JAN 1U
|
||||
#define M_FEB 2U
|
||||
#define M_MAR 3U
|
||||
#define M_APR 4U
|
||||
#define M_MAY 5U
|
||||
#define M_JUN 6U
|
||||
#define M_JUL 7U
|
||||
#define M_AUG 8U
|
||||
#define M_SEP 9U
|
||||
#define M_OCT 10U
|
||||
#define M_NOV 11U
|
||||
#define M_DEC 12U
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Typedef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
typedef struct tm Utc_Date_t;
|
||||
typedef struct
|
||||
{
|
||||
uint8_t year;
|
||||
uint8_t month;
|
||||
uint8_t day;
|
||||
uint8_t hour;
|
||||
uint8_t min;
|
||||
uint8_t sec;
|
||||
uint8_t week;
|
||||
} DateTime_t;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
DateTime_t UTC_SecToDate( uint32_t utc_sec );
|
||||
DateTime_t UTC_DateToZoneDate(int8_t zone_num, uint16_t year_base, uint8_t u_year, uint8_t u_month,
|
||||
uint8_t u_day, uint8_t u_hour, uint8_t u_min, uint8_t u_sec, uint8_t u_week);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TIME_CONV_H__ */
|
||||
@@ -0,0 +1,103 @@
|
||||
/*!
|
||||
* \file rtc.c
|
||||
*
|
||||
* \brief Target rtc implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "rtc.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------
|
||||
-----------------------------------------*/
|
||||
#define MANUF_UTC_SEC 1680516516 // rtc start time
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Constant
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Local Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
volatile uint32_t Utc_TotalSec = MANUF_UTC_SEC;
|
||||
// Test val
|
||||
uint8_t Sec_Flag = false;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief RTC handler func
|
||||
* @param xinc_drv_rtc_int_type_t - type
|
||||
* @retval void
|
||||
*/
|
||||
uint8_t Sec_Callback(void)
|
||||
{
|
||||
Utc_TotalSec += 1;
|
||||
Sec_Flag = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RTC config func
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void RTC_Config(void)
|
||||
{
|
||||
// Initialize RTC instance
|
||||
RTC_InitTypeDef rtc_cfg = {0};
|
||||
NVIC_DisableIRQ(RTC_IRQn);
|
||||
|
||||
// rtc_cfg.Date.Sec = 0;
|
||||
// rtc_cfg.Date.Min = 0;
|
||||
// rtc_cfg.Date.Hour = 18;
|
||||
// rtc_cfg.Date.Day = 93;
|
||||
// rtc_cfg.Date.Week = 1;
|
||||
rtc_cfg.Date.Sec = 0;
|
||||
rtc_cfg.Date.Min = 45;
|
||||
rtc_cfg.Date.Hour = 19;
|
||||
rtc_cfg.Date.Day = 340;
|
||||
rtc_cfg.Date.Week = 2;
|
||||
|
||||
rtc_cfg.DateLimit.SecLimit = 60;
|
||||
rtc_cfg.DateLimit.MinLimit = 60;
|
||||
rtc_cfg.DateLimit.HourLimit = 24;
|
||||
|
||||
rtc_cfg.MatchTimeEnable = false;
|
||||
|
||||
rtc_cfg.SecIT_Enable = true;
|
||||
rtc_cfg.MinIT_Enable = false;
|
||||
rtc_cfg.HourIT_Enable = false;
|
||||
rtc_cfg.DayIT_Enable = false;
|
||||
|
||||
// Rtc enable
|
||||
RTC_Init(XC_RTC, &rtc_cfg);
|
||||
RTC_Start(XC_RTC);
|
||||
NVIC_EnableIRQ(RTC_IRQn);
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
/*!
|
||||
* \file time_conv.c
|
||||
*
|
||||
* \brief Target time convert implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "time_conv.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief UTC second convert to date and time.
|
||||
* @details
|
||||
* @param[in] uint32_t - utc_sec
|
||||
* @param[out] void
|
||||
* @retval DateTime_t
|
||||
* @retval void
|
||||
* @par identifier
|
||||
* reserve
|
||||
* @par other
|
||||
* void
|
||||
* @par change log
|
||||
* Alex created on 2022-09-09
|
||||
*/
|
||||
DateTime_t UTC_SecToDate(uint32_t utc_sec)
|
||||
{
|
||||
Utc_Date_t tmp;
|
||||
DateTime_t ret;
|
||||
|
||||
tmp = *localtime(&utc_sec);
|
||||
|
||||
ret.year = tmp.tm_year;
|
||||
ret.month = tmp.tm_mon;
|
||||
ret.day = tmp.tm_mday;
|
||||
ret.hour = tmp.tm_hour;
|
||||
ret.min = tmp.tm_min;
|
||||
ret.sec = tmp.tm_sec;
|
||||
ret.week = tmp.tm_wday;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert the date in the UTC time zone to the date in the
|
||||
* corresponding time zone.
|
||||
* @details
|
||||
* @param[in] int8_t - zone_num
|
||||
* uint16_t - year_base
|
||||
* uint8_t - u_year, u_month, u_day, u_hour, u_min, u_sec, u_week
|
||||
* @param[out] void
|
||||
* @retval DateTime_t
|
||||
* @retval void
|
||||
* @par identifier
|
||||
* reserve
|
||||
* @par other
|
||||
* void
|
||||
* @par change log
|
||||
* Alex created on 2022-09-09
|
||||
*/
|
||||
DateTime_t UTC_DateToZoneDate(int8_t zone_num, uint16_t year_base,
|
||||
uint8_t u_year, uint8_t u_month, uint8_t u_day,
|
||||
uint8_t u_hour, uint8_t u_min, uint8_t u_sec,
|
||||
uint8_t u_week)
|
||||
{
|
||||
DateTime_t zonedate;
|
||||
int t_year = 0;
|
||||
int lastday = 0; // last day of the month
|
||||
int lastmon_lastday = 0; // last day of last month
|
||||
int8_t t_hour = 0;
|
||||
|
||||
t_year = u_year - 100 + year_base;
|
||||
zonedate.month = u_month + 1;
|
||||
zonedate.day = u_day;
|
||||
zonedate.hour = u_hour;
|
||||
zonedate.min = u_min;
|
||||
zonedate.sec = u_sec;
|
||||
zonedate.week = u_week;
|
||||
|
||||
// Determine days by month
|
||||
if (M_JAN == zonedate.month || M_MAR == zonedate.month ||
|
||||
M_MAY == zonedate.month || M_JUL == zonedate.month ||
|
||||
M_AUG == zonedate.month || M_OCT == zonedate.month ||
|
||||
M_DEC == zonedate.month) {
|
||||
lastday = 31;
|
||||
if (M_MAR == zonedate.month) {
|
||||
// leap year determination
|
||||
if ((t_year % 400 == 0) || (t_year % 4 == 0 && t_year % 100 != 0))
|
||||
lastmon_lastday = 29;
|
||||
else
|
||||
lastmon_lastday = 28;
|
||||
}
|
||||
if (M_AUG == zonedate.month)
|
||||
lastmon_lastday = 31;
|
||||
} else if (M_APR == zonedate.month || M_JUN == zonedate.month ||
|
||||
M_SEP == zonedate.month || M_NOV == zonedate.month) {
|
||||
lastday = 30;
|
||||
lastmon_lastday = 31;
|
||||
} else {
|
||||
lastmon_lastday = 31;
|
||||
// leap year determination
|
||||
if ((t_year % 400 == 0) || (t_year % 4 == 0 && t_year % 100 != 0))
|
||||
lastday = 29;
|
||||
else
|
||||
lastday = 28;
|
||||
}
|
||||
|
||||
t_hour = (int8_t)zonedate.hour + zone_num;
|
||||
if (t_hour >= 24) {
|
||||
zonedate.hour = (uint8_t)t_hour - 24;
|
||||
zonedate.day += 1;
|
||||
if (zonedate.day > lastday) {
|
||||
zonedate.day -= lastday;
|
||||
zonedate.month += 1;
|
||||
if (zonedate.month > M_DEC) {
|
||||
zonedate.month -= M_DEC;
|
||||
t_year += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (zonedate.week < 6)
|
||||
zonedate.week += 1;
|
||||
else
|
||||
zonedate.week = 0;
|
||||
} else if (t_hour < 0) {
|
||||
zonedate.hour = (uint8_t)(t_hour + 24);
|
||||
zonedate.day -= 1;
|
||||
if (zonedate.day == 0) {
|
||||
zonedate.day = lastmon_lastday;
|
||||
zonedate.month -= 1;
|
||||
if (zonedate.month == 0) {
|
||||
zonedate.month = M_DEC;
|
||||
t_year -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
if ((zonedate.week > 0) && (zonedate.week <= 6))
|
||||
zonedate.week -= 1;
|
||||
else
|
||||
zonedate.week = 6;
|
||||
} else
|
||||
zonedate.hour = t_hour;
|
||||
|
||||
zonedate.year = t_year - year_base;
|
||||
|
||||
return zonedate;
|
||||
}
|
||||
Reference in New Issue
Block a user