Переписан модуль RTC

This commit is contained in:
cfif 2024-12-20 14:34:53 +03:00
parent b0fc463b06
commit 3bcf1dcd96
8 changed files with 251 additions and 14 deletions

View File

@ -2,7 +2,7 @@
// Created by ilya on 13.02.23.
//
#include "Log.h"
#include "Log._h"
#if LOG_ENABLE
#include "n32g45x.h"

View File

@ -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

View File

@ -2,10 +2,10 @@
// Created by ilya on 13.02.23.
//
#include "RTC_Config.h"
#include "RTC_Config._h"
#include <stdio.h>
#include <stdint.h>
#include "Log.h"
#include "Log._h"
#include "n32g45x_rtc.h"

View File

@ -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

View File

@ -2,12 +2,12 @@
// Created by ilya on 10.02.23.
//
#include "RTC_n32g45x.h"
#include "RTC_n32g45x._h"
#include <time.h>
#include <string.h>
#include <SystemDelayInterface.h>
#include "n32g45x_rtc.h"
#include "RTC_Config.h"
#include "RTC_Config._h"
void vRtcInit(tRtc_n32g45x *env) {
RTC_DateAndTimeDefaultVale();

View File

@ -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

210
Rtc_Nation.c Normal file
View File

@ -0,0 +1,210 @@
//
// Created by cfif on 06.11.2023.
//
#include "Rtc_Nation.h"
#include "n32g45x_rtc.h"
#include <time.h>
#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;
}

27
Rtc_Nation.h Normal file
View File

@ -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