From 3bcf1dcd96cf41eb66ff1bf9d852632eef025ca0 Mon Sep 17 00:00:00 2001 From: cfif Date: Fri, 20 Dec 2024 14:34:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=B0=D0=BD=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20RTC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Log.c => Old/Log._c | 2 +- Log.h => Old/Log._h | 6 +- RTC_Config.c => Old/RTC_Config._c | 4 +- RTC_Config.h => Old/RTC_Config._h | 6 +- RTC_n32g45x.c => Old/RTC_n32g45x._c | 4 +- RTC_n32g45x.h => Old/RTC_n32g45x._h | 6 +- Rtc_Nation.c | 210 ++++++++++++++++++++++++++++ Rtc_Nation.h | 27 ++++ 8 files changed, 251 insertions(+), 14 deletions(-) rename Log.c => Old/Log._c (98%) rename Log.h => Old/Log._h (91%) rename RTC_Config.c => Old/RTC_Config._c (99%) rename RTC_Config.h => Old/RTC_Config._h (90%) rename RTC_n32g45x.c => Old/RTC_n32g45x._c (98%) rename RTC_n32g45x.h => Old/RTC_n32g45x._h (61%) create mode 100644 Rtc_Nation.c create mode 100644 Rtc_Nation.h diff --git a/Log.c b/Old/Log._c similarity index 98% rename from Log.c rename to Old/Log._c index 7084902..b4eed49 100644 --- a/Log.c +++ b/Old/Log._c @@ -2,7 +2,7 @@ // Created by ilya on 13.02.23. // -#include "Log.h" +#include "Log._h" #if LOG_ENABLE #include "n32g45x.h" diff --git a/Log.h b/Old/Log._h similarity index 91% rename from Log.h rename to Old/Log._h index 0e992c7..f84f06b 100644 --- a/Log.h +++ b/Old/Log._h @@ -2,8 +2,8 @@ // Created by ilya on 13.02.23. // -#ifndef UVEOS_ON_NATION_LOG_H -#define UVEOS_ON_NATION_LOG_H +#ifndef UVEOS_ON_NATION_LOG__H +#define UVEOS_ON_NATION_LOG__H #ifdef __cplusplus extern "C" { #endif @@ -71,4 +71,4 @@ void log_init(void); #endif -#endif //UVEOS_ON_NATION_LOG_H +#endif //UVEOS_ON_NATION_LOG__H diff --git a/RTC_Config.c b/Old/RTC_Config._c similarity index 99% rename from RTC_Config.c rename to Old/RTC_Config._c index abc3236..5974a5c 100644 --- a/RTC_Config.c +++ b/Old/RTC_Config._c @@ -2,10 +2,10 @@ // Created by ilya on 13.02.23. // -#include "RTC_Config.h" +#include "RTC_Config._h" #include #include -#include "Log.h" +#include "Log._h" #include "n32g45x_rtc.h" diff --git a/RTC_Config.h b/Old/RTC_Config._h similarity index 90% rename from RTC_Config.h rename to Old/RTC_Config._h index b8b387f..6c5470d 100644 --- a/RTC_Config.h +++ b/Old/RTC_Config._h @@ -2,8 +2,8 @@ // Created by ilya on 13.02.23. // -#ifndef UVEOS_ON_NATION_RTC_CONFIG_H -#define UVEOS_ON_NATION_RTC_CONFIG_H +#ifndef UVEOS_ON_NATION_RTC_CONFIG__H +#define UVEOS_ON_NATION_RTC_CONFIG__H #ifdef __cplusplus extern "C" { #endif @@ -40,4 +40,4 @@ extern void RTC_TimeStampShow(void); } #endif -#endif //UVEOS_ON_NATION_RTC_CONFIG_H +#endif //UVEOS_ON_NATION_RTC_CONFIG__H diff --git a/RTC_n32g45x.c b/Old/RTC_n32g45x._c similarity index 98% rename from RTC_n32g45x.c rename to Old/RTC_n32g45x._c index fb55c36..f7e7f00 100644 --- a/RTC_n32g45x.c +++ b/Old/RTC_n32g45x._c @@ -2,12 +2,12 @@ // Created by ilya on 10.02.23. // -#include "RTC_n32g45x.h" +#include "RTC_n32g45x._h" #include #include #include #include "n32g45x_rtc.h" -#include "RTC_Config.h" +#include "RTC_Config._h" void vRtcInit(tRtc_n32g45x *env) { RTC_DateAndTimeDefaultVale(); diff --git a/RTC_n32g45x.h b/Old/RTC_n32g45x._h similarity index 61% rename from RTC_n32g45x.h rename to Old/RTC_n32g45x._h index 2932c34..66f51f5 100644 --- a/RTC_n32g45x.h +++ b/Old/RTC_n32g45x._h @@ -2,8 +2,8 @@ // Created by ilya on 10.02.23. // -#ifndef UVEOS_ON_NATION_RTC_N32G45X_H -#define UVEOS_ON_NATION_RTC_N32G45X_H +#ifndef UVEOS_ON_NATION_RTC_N32G45X__H +#define UVEOS_ON_NATION_RTC_N32G45X__H #include "Rtc.h" #include "cmsis_os2.h" @@ -14,4 +14,4 @@ void vRtcInit(tRtc_n32g45x *env); tRtcIO vRtcGetIo(tRtc_n32g45x *env); -#endif //UVEOS_ON_NATION_RTC_N32G45X_H +#endif //UVEOS_ON_NATION_RTC_N32G45X__H diff --git a/Rtc_Nation.c b/Rtc_Nation.c new file mode 100644 index 0000000..4c1097d --- /dev/null +++ b/Rtc_Nation.c @@ -0,0 +1,210 @@ +// +// Created by cfif on 06.11.2023. +// +#include "Rtc_Nation.h" +#include "n32g45x_rtc.h" +#include +#include "string.h" + +void RTC_CLKSourceConfig(RTC_CLK_SRC_TYPE Clk_Src_Type, bool Is_First_Cfg_RCC, bool Is_Rst_Bkp) { + + uint32_t SynchPrediv; + uint32_t AsynchPrediv; + + RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_PWR | RCC_APB1_PERIPH_BKP, ENABLE); + RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO, ENABLE); + + PWR_BackupAccessEnable(ENABLE); + + if (true == Is_Rst_Bkp) { + BKP_DeInit(); + } + + RCC_EnableRtcClk(DISABLE); + if (RTC_CLK_SRC_TYPE_HSE128 == Clk_Src_Type) { + if (true == Is_First_Cfg_RCC) { + + RCC_EnableLsi(DISABLE); + RCC_ConfigHse(RCC_HSE_ENABLE); + while (RCC_WaitHseStable() == ERROR) { + } + RCC_ConfigRtcClk(RCC_RTCCLK_SRC_HSE_DIV128); + } else { + RCC_EnableLsi(DISABLE); + RCC_ConfigRtcClk(RCC_RTCCLK_SRC_HSE_DIV128); + + RCC_ConfigHse(RCC_HSE_ENABLE); + while (RCC_WaitHseStable() == ERROR) { + } + } + SynchPrediv = 0x1E8; + AsynchPrediv = 0x7F; + } else if (RTC_CLK_SRC_TYPE_LSE == Clk_Src_Type) { + if (true == Is_First_Cfg_RCC) { + + RCC_EnableLsi(DISABLE); + RCC_ConfigLse(RCC_LSE_ENABLE); + while (RCC_GetFlagStatus(RCC_FLAG_LSERD) == RESET) { + } + RCC_ConfigRtcClk(RCC_RTCCLK_SRC_LSE); + } else { + + RCC_EnableLsi(DISABLE); + RCC_ConfigRtcClk(RCC_RTCCLK_SRC_LSE); + + RCC_ConfigLse(RCC_LSE_ENABLE); + while (RCC_GetFlagStatus(RCC_FLAG_LSERD) == RESET) { + } + } + SynchPrediv = 0xFF; + AsynchPrediv = 0x7F; + } else if (RTC_CLK_SRC_TYPE_LSI == Clk_Src_Type) { + if (true == Is_First_Cfg_RCC) { + + RCC_EnableLsi(ENABLE); + while (RCC_GetFlagStatus(RCC_FLAG_LSIRD) == RESET) { + } + RCC_ConfigRtcClk(RCC_RTCCLK_SRC_LSI); + } else { + RCC_ConfigRtcClk(RCC_RTCCLK_SRC_LSI); + + RCC_EnableLsi(ENABLE); + while (RCC_GetFlagStatus(RCC_FLAG_LSIRD) == RESET) { + } + } + SynchPrediv = 0x136; + AsynchPrediv = 0x7F; + } else { + } + + RCC_EnableRtcClk(ENABLE); + RTC_WaitForSynchro(); + + RTC_InitType RTC_InitStructure; + + RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv; + RTC_InitStructure.RTC_SynchPrediv = SynchPrediv; + RTC_InitStructure.RTC_HourFormat = RTC_24HOUR_FORMAT; + + RTC_Init(&RTC_InitStructure); + +} + +ErrorStatus RTC_DateRegulate(void) { + RTC_DateType RTC_DateStructure; + RTC_DateStructure.WeekDay = 4; + RTC_DateStructure.Date = 11; + RTC_DateStructure.Month = 12; + RTC_DateStructure.Year = 20; + + if (RTC_SetDate(RTC_FORMAT_BIN, &RTC_DateStructure) == ERROR) + return ERROR; + + return SUCCESS; +} + +ErrorStatus RTC_TimeRegulate(void) { + RTC_TimeType RTC_TimeStructure; + RTC_TimeStructure.H12 = RTC_AM_H12; + RTC_TimeStructure.Hours = 1; + RTC_TimeStructure.Minutes = 2; + RTC_TimeStructure.Seconds = 3; + + if (RTC_ConfigTime(RTC_FORMAT_BIN, &RTC_TimeStructure) == ERROR) + return ERROR; + + return SUCCESS; +} + +void vRtc_Init(TRtcNational *env) { + + RTC_CLKSourceConfig(RTC_CLK_SRC_TYPE_LSE, true, true); + + RTC_DateRegulate(); + RTC_TimeRegulate(); + + RTC_ConfigCalibOutput(RTC_CALIB_OUTPUT_1HZ); + + RTC_ConfigOutputType(RTC_OUTPUT_PUSHPULL); + + RTC_EnableCalibOutput(ENABLE); +} + +void impRtcSet(TRtcNational *env, time_t *timestamp) { + struct tm *rtc_Date_Time; + rtc_Date_Time = localtime(timestamp); + RTC_DateType date; + date.Year = rtc_Date_Time->tm_year - 2000; + date.Month = rtc_Date_Time->tm_mon; + date.Date = rtc_Date_Time->tm_mday; + RTC_TimeType time; + time.Hours = rtc_Date_Time->tm_hour; + time.Minutes = rtc_Date_Time->tm_min; + time.Seconds = rtc_Date_Time->tm_sec; + + RTC_SetDate(RTC_FORMAT_BIN, &date); + + RTC_ConfigTime(RTC_FORMAT_BIN, &time); + +} + +void impRtcSetTM(TRtcNational *env, struct tm *timestampTM) { + RTC_DateType date; + date.Year = timestampTM->tm_year; + date.Month = timestampTM->tm_mon - 1; + date.Date = timestampTM->tm_mday; + RTC_TimeType time; + time.Hours = timestampTM->tm_hour; + time.Minutes = timestampTM->tm_min; + time.Seconds = timestampTM->tm_sec; + + RTC_SetDate(RTC_FORMAT_BIN, &date); + + RTC_ConfigTime(RTC_FORMAT_BIN, &time); + +} + +void impRtcGet(TRtcNational *env, time_t *timestamp) { + RTC_DateType time; + RTC_GetDate(RTC_FORMAT_BIN, &time); + RTC_TimeType time2; + RTC_GetTime(RTC_FORMAT_BIN, &time2); + struct tm rtc_Date_Time; + memset(&rtc_Date_Time, 0, sizeof(rtc_Date_Time)); + time.Year = time.Year + 12; + rtc_Date_Time.tm_year = time.Year + 100; + rtc_Date_Time.tm_mon = time.Month; + rtc_Date_Time.tm_mday = time.Date; + rtc_Date_Time.tm_hour = time2.Hours; + rtc_Date_Time.tm_min = time2.Minutes; + rtc_Date_Time.tm_sec = time2.Seconds; + + *timestamp = mktime(&rtc_Date_Time); +} + +void impRtcGetTM(TRtcNational *env, struct tm *timestampTM) { + RTC_DateType time; + RTC_GetDate(RTC_FORMAT_BIN, &time); + RTC_TimeType time2; + RTC_GetTime(RTC_FORMAT_BIN, &time2); + memset(timestampTM, 0, sizeof(struct tm)); + + timestampTM->tm_year = time.Year + 90; + timestampTM->tm_mon = time.Month; + timestampTM->tm_mday = time.Date; + timestampTM->tm_hour = time2.Hours; + timestampTM->tm_min = time2.Minutes; + timestampTM->tm_sec = time2.Seconds; + +} + +tRtcIO RtcGet_Io(TRtcNational *env) { + tRtcIO io = { + .env = env, + .get = (RtcIOTransaction) impRtcGet, + .set = (RtcIOTransaction) impRtcSet, + .getTM = (RtcIOTransactionTM) impRtcGetTM, + .setTM = (RtcIOTransactionTM) impRtcSetTM + }; + return io; +} \ No newline at end of file diff --git a/Rtc_Nation.h b/Rtc_Nation.h new file mode 100644 index 0000000..dbff5c2 --- /dev/null +++ b/Rtc_Nation.h @@ -0,0 +1,27 @@ +// +// Created by cfif on 06.11.2023. +// + +#ifndef EMERGCALLDEV_RTC_NATION_H +#define EMERGCALLDEV_RTC_NATION_H + +#include "RtcIO.h" + +typedef enum { + RTC_CLK_SRC_TYPE_HSE128 = 1, + RTC_CLK_SRC_TYPE_LSE = 2, + RTC_CLK_SRC_TYPE_LSI = 3 +} RTC_CLK_SRC_TYPE; + +typedef struct { +} TRtcNational; + +void vRtc_Init(TRtcNational *env); +tRtcIO RtcGet_Io(TRtcNational *env); + +void impRtcSet(TRtcNational *env, time_t *timestamp); +void impRtcSetTM(TRtcNational *env, struct tm *timestampTM); +void impRtcGet(TRtcNational *env, time_t *timestamp); +void impRtcGetTM(TRtcNational *env, struct tm *timestampTM); + +#endif //EMERGCALLDEV_RTC_NATION_H