KETCube
Macros | Functions | Variables
ketCube_rtc.c File Reference

This file contains implementation of the ketCube RTC driver. More...

#include <math.h>
#include <time.h>
#include "hw.h"
#include "low_power.h"
#include "systime.h"
#include "ketCube_terminal.h"
#include "ketCube_mcu.h"

Macros

#define MIN_ALARM_DELAY   3 /* in ticks */
 
#define N_PREDIV_S   10
 
#define PREDIV_S   ((1<<N_PREDIV_S)-1)
 
#define PREDIV_A   (1<<(15-N_PREDIV_S))-1
 
#define RTC_ALARMSUBSECONDMASK   (N_PREDIV_S<<RTC_ALRMASSR_MASKSS_Pos)
 
#define USEC_NUMBER   1000000
 
#define MSEC_NUMBER   (USEC_NUMBER/1000)
 
#define RTC_ALARM_TIME_BASE   (USEC_NUMBER>>N_PREDIV_S)
 
#define COMMON_FACTOR   3
 
#define CONV_NUMER   (MSEC_NUMBER>>COMMON_FACTOR)
 
#define CONV_DENOM   (1<<(N_PREDIV_S-COMMON_FACTOR))
 
#define DAYS_IN_LEAP_YEAR   ( ( uint32_t ) 366U )
 
#define DAYS_IN_YEAR   ( ( uint32_t ) 365U )
 
#define SECONDS_IN_1DAY   ( ( uint32_t )86400U )
 
#define SECONDS_IN_1HOUR   ( ( uint32_t ) 3600U )
 
#define SECONDS_IN_1MINUTE   ( ( uint32_t ) 60U )
 
#define MINUTES_IN_1HOUR   ( ( uint32_t ) 60U )
 
#define HOURS_IN_1DAY   ( ( uint32_t ) 24U )
 
#define DAYS_IN_MONTH_CORRECTION_NORM   ((uint32_t) 0x99AAA0 )
 
#define DAYS_IN_MONTH_CORRECTION_LEAP   ((uint32_t) 0x445550 )
 
#define DIVC(X, N)   ( ( (X) + (N) -1 ) / (N) )
 

Functions

uint32_t ketCube_RTC_GetTimerElapsedTime (void)
 Get the RTC timer elapsed time since the last Alarm was set. More...
 
uint32_t ketCube_RTC_SetTimerContext (void)
 set Time Reference set also the RTC_DateStruct and RTC_TimeStruct More...
 
static void ketCube_RTC_SetConfig (void)
 Configures the RTC timer. More...
 
static void ketCube_RTC_SetAlarmConfig (void)
 configure alarm at init More...
 
static void ketCube_RTC_StartWakeUpAlarm (uint32_t timeoutValue)
 start wake up alarm More...
 
static uint64_t ketCube_RTC_GetCalendarValue (RTC_DateTypeDef *RTC_DateStruct, RTC_TimeTypeDef *RTC_TimeStruct)
 get current time from calendar in ticks More...
 
ketCube_cfg_DrvError_t ketCube_RTC_Init (void)
 Initializes the RTC timer. More...
 
void ketCube_RTC_setMcuWakeUpTime (void)
 calculates the wake up time between wake up and mcu start More...
 
int16_t ketCube_RTC_getMcuWakeUpTime (void)
 
uint32_t ketCube_RTC_GetMinimumTimeout (void)
 returns the wake up time in ticks More...
 
uint32_t ketCube_RTC_ms2Tick (TimerTime_t timeMicroSec)
 converts time in ms to time in ticks More...
 
TimerTime_t ketCube_RTC_Tick2ms (uint32_t tick)
 converts time in ticks to time in ms More...
 
void ketCube_RTC_SetAlarm (uint32_t timeout)
 Set the alarm. More...
 
uint32_t ketCube_RTC_GetTimerValue (void)
 Get the RTC timer value. More...
 
void ketCube_RTC_StopAlarm (void)
 Stop the Alarm. More...
 
void ketCube_RTC_IrqHandler (void)
 RTC IRQ Handler on the RTC Alarm. More...
 
void ketCube_RTC_DelayMs (uint32_t delay)
 a delay of delay ms by polling RTC More...
 
uint32_t HAL_GetTick (void)
 HAL_GetTick RTC-based replacement. More...
 
uint32_t ketCube_RTC_GetTimerContext (void)
 Get the RTC timer Reference. More...
 
uint32_t ketCube_RTC_GetCalendarTime (uint16_t *mSeconds)
 Get system time. More...
 
uint32_t ketCube_RTC_GetSysTime (void)
 Get system time. More...
 
void ketCube_RTC_BKUPWrite (uint32_t Data0, uint32_t Data1)
 
void ketCube_RTC_BKUPRead (uint32_t *Data0, uint32_t *Data1)
 
TimerTime_t RtcTempCompensation (TimerTime_t period, float temperature)
 
void HAL_RTC_AlarmAEventCallback (RTC_HandleTypeDef *hrtc)
 Alarm A callback. More...
 
void ketCube_RTC_AlarmAEventExec (void)
 Alarm A callback. More...
 
void HAL_RTC_MspInit (RTC_HandleTypeDef *hrtc)
 RTC MSP Initialization This function configures the hardware resources used in this example: More...
 
void HAL_RTC_MspDeInit (RTC_HandleTypeDef *hrtc)
 RTC MSP De-Initialization This function freeze the hardware resources used in this example: More...
 
HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)
 This function configures the source of the time base. More...
 
void HAL_Delay (__IO uint32_t delay)
 This function redefines systick-based HAL_Delay. More...
 

Variables

static volatile bool initialized = FALSE
 
static bool McuWakeUpTimeInitialized = false
 compensates MCU wakeup time More...
 
static int16_t McuWakeUpTimeCal = 0
 compensates MCU wakeup time More...
 
static const uint8_t DaysInMonth [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
 
static const uint8_t DaysInMonthLeapYear [] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
 
static RTC_HandleTypeDef RtcHandle ={0}
 
static RTC_AlarmTypeDef RTC_AlarmStructure
 
static RtcTimerContext_t RtcTimerContext
 
static volatile bool executeAlarmOutOfISR = FALSE
 

Detailed Description

This file contains implementation of the ketCube RTC driver.

This file contains definitions for the ketCube RTC driver.

Author
Jan Belohoubek
Version
alpha
Date
2019-12-10
Note
This code is based on Semtech and STM RTC driver implementation. See the original file Semtech licenses in LICENSE_SEMTECH. The ST Ultimate Liberty license is dennoted SLA0044. You may not use this file except in compliance with the License. You may obtain a copy of the License at: www.st.com/SLA0044
Attention

© Copyright (c) 2013, SEMTECH S.A. All rights reserved.

© Copyright (c) 2018 STMicroelectronics. All rights reserved.

© Copyright (c) 2019 University of West Bohemia in Pilsen All rights reserved.

Developed by: The SmartCampus Team Department of Technologies and Measurement www.smartcampus.cz | www.zcu.cz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal with the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.

Macro Definition Documentation

◆ COMMON_FACTOR

#define COMMON_FACTOR   3

◆ CONV_DENOM

#define CONV_DENOM   (1<<(N_PREDIV_S-COMMON_FACTOR))

◆ CONV_NUMER

#define CONV_NUMER   (MSEC_NUMBER>>COMMON_FACTOR)

◆ DAYS_IN_LEAP_YEAR

#define DAYS_IN_LEAP_YEAR   ( ( uint32_t ) 366U )

◆ DAYS_IN_MONTH_CORRECTION_LEAP

#define DAYS_IN_MONTH_CORRECTION_LEAP   ((uint32_t) 0x445550 )

◆ DAYS_IN_MONTH_CORRECTION_NORM

#define DAYS_IN_MONTH_CORRECTION_NORM   ((uint32_t) 0x99AAA0 )

◆ DAYS_IN_YEAR

#define DAYS_IN_YEAR   ( ( uint32_t ) 365U )

◆ DIVC

#define DIVC (   X,
 
)    ( ( (X) + (N) -1 ) / (N) )

◆ HOURS_IN_1DAY

#define HOURS_IN_1DAY   ( ( uint32_t ) 24U )

◆ MIN_ALARM_DELAY

#define MIN_ALARM_DELAY   3 /* in ticks */

◆ MINUTES_IN_1HOUR

#define MINUTES_IN_1HOUR   ( ( uint32_t ) 60U )

◆ MSEC_NUMBER

#define MSEC_NUMBER   (USEC_NUMBER/1000)

◆ N_PREDIV_S

#define N_PREDIV_S   10

◆ PREDIV_A

#define PREDIV_A   (1<<(15-N_PREDIV_S))-1

◆ PREDIV_S

#define PREDIV_S   ((1<<N_PREDIV_S)-1)

◆ RTC_ALARM_TIME_BASE

#define RTC_ALARM_TIME_BASE   (USEC_NUMBER>>N_PREDIV_S)

◆ RTC_ALARMSUBSECONDMASK

#define RTC_ALARMSUBSECONDMASK   (N_PREDIV_S<<RTC_ALRMASSR_MASKSS_Pos)

◆ SECONDS_IN_1DAY

#define SECONDS_IN_1DAY   ( ( uint32_t )86400U )

◆ SECONDS_IN_1HOUR

#define SECONDS_IN_1HOUR   ( ( uint32_t ) 3600U )

◆ SECONDS_IN_1MINUTE

#define SECONDS_IN_1MINUTE   ( ( uint32_t ) 60U )

◆ USEC_NUMBER

#define USEC_NUMBER   1000000

Function Documentation

◆ HAL_Delay()

void HAL_Delay ( __IO uint32_t  delay)

This function redefines systick-based HAL_Delay.

This function provides delay in ms

Parameters
Delayspecifies the delay time length, in milliseconds.
Return values
None

◆ HAL_InitTick()

HAL_StatusTypeDef HAL_InitTick ( uint32_t  TickPriority)

This function configures the source of the time base.

KETCube uses RTC, so sysTick is not enabled

Parameters
TickPriorityTick interrupt priority.
Return values
HALstatus

◆ HAL_RTC_AlarmAEventCallback()

void HAL_RTC_AlarmAEventCallback ( RTC_HandleTypeDef *  hrtc)

Alarm A callback.

Parameters
hrtcRTC handle
Return values
None

◆ HAL_RTC_MspDeInit()

void HAL_RTC_MspDeInit ( RTC_HandleTypeDef *  hrtc)

RTC MSP De-Initialization This function freeze the hardware resources used in this example:

  • Disable the Peripheral's clock
    Parameters
    hrtcRTC handle pointer
    Return values
    None

◆ HAL_RTC_MspInit()

void HAL_RTC_MspInit ( RTC_HandleTypeDef *  hrtc)

RTC MSP Initialization This function configures the hardware resources used in this example:

  • Peripheral's clock enable
    Parameters
    hrtcRTC handle pointer
    Note
    Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select the RTC clock source; in this case the Backup domain will be reset in
    order to modify the RTC Clock source, as consequence RTC registers (including the backup registers) and RCC_CSR register are set to their reset values.
    Return values
    None

◆ ketCube_RTC_GetCalendarValue()

static uint64_t ketCube_RTC_GetCalendarValue ( RTC_DateTypeDef *  RTC_DateStruct,
RTC_TimeTypeDef *  RTC_TimeStruct 
)
static

get current time from calendar in ticks

Parameters
pointerto RTC_DateStruct
pointerto RTC_TimeStruct
Return values
timein ticks

◆ ketCube_RTC_SetAlarmConfig()

static void ketCube_RTC_SetAlarmConfig ( void  )
static

configure alarm at init

Parameters
none
Return values
none

◆ ketCube_RTC_SetConfig()

static void ketCube_RTC_SetConfig ( void  )
static

Configures the RTC timer.

Note
The timer is based on the RTC
Parameters
none
Return values
none

◆ ketCube_RTC_StartWakeUpAlarm()

static void ketCube_RTC_StartWakeUpAlarm ( uint32_t  timeoutValue)
static

start wake up alarm

Note
alarm in RtcTimerContext.Rtc_Time + timeoutValue
Parameters
timeoutValuein ticks
Return values
none

◆ RtcTempCompensation()

TimerTime_t RtcTempCompensation ( TimerTime_t  period,
float  temperature 
)

Variable Documentation

◆ DaysInMonth

const uint8_t DaysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
static

Number of days in each month on a normal year

◆ DaysInMonthLeapYear

const uint8_t DaysInMonthLeapYear[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
static

Number of days in each month on a leap year

◆ executeAlarmOutOfISR

volatile bool executeAlarmOutOfISR = FALSE
static

◆ initialized

volatile bool initialized = FALSE
static

◆ McuWakeUpTimeCal

int16_t McuWakeUpTimeCal = 0
static

compensates MCU wakeup time

◆ McuWakeUpTimeInitialized

bool McuWakeUpTimeInitialized = false
static

compensates MCU wakeup time

◆ RTC_AlarmStructure

RTC_AlarmTypeDef RTC_AlarmStructure
static

◆ RtcHandle

RTC_HandleTypeDef RtcHandle ={0}
static

◆ RtcTimerContext

RtcTimerContext_t RtcTimerContext
static

Keep the value of the RTC timer when the RTC alarm is set Set with the ketCube_RTC_SetTimerContext function Value is kept as a Reference to calculate alarm