DIY Logging Volt/Ampmeter
rtc.h File Reference

RTC functionality for high-precision logging voltage/current meter. More...

#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  RTC_dateTime_t
 

Macros

#define JULIAN_DATE_BASE   2440588
 

Functions

void RTC_Init (void)
 Function to initialize the Real Time Clock. More...
 
void readFromRTC (void)
 Function to update the RTC data in the struct. More...
 
void writeToRTC (void)
 Function to update the RTC values according to the data in the struct. More...
 

Variables

struct RTC_dateTime_t rtc
 

Detailed Description

RTC functionality for high-precision logging voltage/current meter.

Version
1.0
Author
Brecht Van Eeckhoudt

Versions

  • v1.0: Initial version.

License

Copyright (C) 2021 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.

Definition in file rtc.h.

Macro Definition Documentation

◆ JULIAN_DATE_BASE

#define JULIAN_DATE_BASE   2440588

Unix epoch time in Julian calendar (UnixTime = 00:00:00 01.01.1970 => JDN = 2440588)

Definition at line 47 of file rtc.h.

Function Documentation

◆ readFromRTC()

void readFromRTC ( void  )

Function to update the RTC data in the struct.

Definition at line 309 of file rtc.c.

310 {
311  RTCcounter_to_dateTime(LL_RTC_TIME_Get(RTC));
312 }

◆ RTC_Init()

void RTC_Init ( void  )

Function to initialize the Real Time Clock.

Definition at line 97 of file rtc.c.

98 {
99  /* Enable the PWR Clock and access to the backup domain */
100  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
101  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_BKP);
102  LL_PWR_EnableBkUpAccess();
103 
104  /* Enable LSE only if disabled.*/
105  if (LL_RCC_LSE_IsReady() != 1)
106  {
107  LL_RCC_ForceBackupDomainReset();
108  LL_RCC_ReleaseBackupDomainReset();
109  LL_RCC_LSE_Enable();
110 
112 
113  while (LL_RCC_LSE_IsReady() != 1)
114  {
116 
117  if (Timeout == 0) while(1); /* LSE activation error */
118  }
119  }
120 
121  /* Set LSE as the clock source for the RTC if not already the case */
122  if (LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE)
123  {
124  LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
125  }
126 
127  /* Enable RTC peripheral Clock */
128  LL_RCC_EnableRTC();
129 
130 
131  /* Disable RTC registers write protection */
132  LL_RTC_DisableWriteProtection(RTC);
133 
134  /* Enter in initialization mode */
135  if (LL_RTC_EnterInitMode(RTC) != 0)
136  {
137  while(1); /* Initialization Error */
138  }
139 
140  /* Configure RTC prescaler, set Asynch Prediv value according to source clock */
141  LL_RTC_SetAsynchPrescaler(RTC, RTC_ASYNCH_PREDIV);
142 
143  /* RTC_Alarm Interrupt Configuration: EXTI configuration */
144  LL_EXTI_EnableIT_0_31(LL_EXTI_LINE_17);
145  LL_EXTI_EnableRisingTrig_0_31(LL_EXTI_LINE_17);
146 
147  /* Configure the NVIC for RTC Alarm */
150 
151  /* Enable Second Interrupt */
152  LL_RTC_EnableIT_SEC(RTC);
153 
154  /* Exit of initialization mode */
155  LL_RTC_ExitInitMode(RTC);
156 
157  /* Enable RTC registers write protection */
158  LL_RTC_EnableWriteProtection(RTC);
159 
160 
161  /* Signal logic so the hour/min/second fields have to be converted to chars after RTC initialization (~boot) */
162  rtc.firstUpdate = 1;
163 }

◆ writeToRTC()

void writeToRTC ( void  )

Function to update the RTC values according to the data in the struct.

Definition at line 319 of file rtc.c.

320 {
321  /* Disable RTC registers write protection */
322  LL_RTC_DisableWriteProtection(RTC);
323 
324  /* Enter in initialization mode */
325  if (LL_RTC_EnterInitMode(RTC) != 0)
326  {
327  while(1); /* Initialization Error */
328  }
329 
330  /* Set time and date according to struct values, which have been converted to onee uint32_t value */
331  LL_RTC_TIME_SetCounter(RTC, dateTime_to_RTCcounter());
332 
333  /* Clear RSF flag */
334  LL_RTC_ClearFlag_RS(RTC);
335 
337 
338  /* Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are synchronized with RTC APB clock */
339  while(LL_RTC_IsActiveFlag_RS(RTC) != 1)
340  {
342 
343  if (Timeout == 0) while(1); /* Initialization Error */
344  }
345 
346  /* Exit of initialization mode */
347  if (LL_RTC_ExitInitMode(RTC) != 0)
348  {
349  while(1); /* Initialization Error */
350  }
351 
352  /* Enable RTC registers write protection */
353  LL_RTC_EnableWriteProtection(RTC);
354 }

Variable Documentation

◆ rtc

struct RTC_dateTime_t rtc
Timeout
static uint32_t Timeout
Definition: rtc.c:85
RTC
#define RTC
Definition: stm32f103xb.h:651
LL_SYSTICK_IsActiveCounterFlag
__STATIC_INLINE uint32_t LL_SYSTICK_IsActiveCounterFlag(void)
This function checks if the Systick counter flag is active or not.
Definition: stm32f1xx_ll_cortex.h:231
RTC_IRQn
@ RTC_IRQn
Definition: stm32f103xb.h:86
RTC_TIMEOUT_MS
#define RTC_TIMEOUT_MS
Definition: rtc.c:77
rtc
struct RTC_dateTime_t rtc
dateTime_to_RTCcounter
static uint32_t dateTime_to_RTCcounter(void)
Function to convert the date-time values in the struct to a corresponding RTC counter value.
Definition: rtc.c:255
RTCcounter_to_dateTime
static void RTCcounter_to_dateTime(uint32_t counter)
Function to convert the RTC counter value to date-time values in the struct.
Definition: rtc.c:177
LSE_TIMEOUT_VALUE
#define LSE_TIMEOUT_VALUE
Definition: stm32_hal_legacy.h:3300
NVIC_SetPriority
#define NVIC_SetPriority
Definition: core_armv8mbl.h:1197
NVIC_EnableIRQ
#define NVIC_EnableIRQ
Definition: core_armv8mbl.h:1190
RTC_ASYNCH_PREDIV
#define RTC_ASYNCH_PREDIV
Definition: rtc.c:80
RTC_dateTime_t::firstUpdate
uint8_t firstUpdate
Definition: rtc.h:52