foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
rtc.c File Reference

Implementation of the RTC driver. More...

#include "rtc.h"
#include "database.h"
#include "diag.h"
#include "foxmath.h"
#include "fstd_types.h"
#include "ftask.h"
#include "i2c.h"
#include <stdint.h>
#include <time.h>
Include dependency graph for rtc.c:

Go to the source code of this file.

Data Structures

struct  RTC_SYSTEM_TIMER_EPOCH_s
 

Functions

static RTC_TIME_DATA_s RTC_ReadTime (void)
 Reads the time from the RTC IC. More...
 
static void RTC_WriteTime (RTC_TIME_DATA_s rtcTime)
 Write the time to the RTC IC. More...
 
static void RTC_SetOverCanMessage (void)
 Check if a CAN message was received to set the RTC time. More...
 
static void RTC_AdjustTime (void)
 Adjust RTC system timer with time from RTC IC. More...
 
static void RTC_CheckBatteryLowVoltageAlert (void)
 Read bit for battery voltage low flag. More...
 
static void RTC_SetSystemTimeEpochFormat (time_t timeEpochFormat, uint16_t milliseconds)
 Set the RTC system timer value. More...
 
static RTC_SYSTEM_TIMER_EPOCH_s RTC_GetSystemTimeEpochFormat (void)
 Get the RTC system timer value. More...
 
static struct tm RTC_rtcFormatToTmFormat (RTC_TIME_DATA_s timeRtcFormat)
 Convert time from RTC_TIME_DATA_s to struct tm format. More...
 
static RTC_TIME_DATA_s RTC_tmFormatToRtcFormat (struct tm timeTmFormat)
 Convert time from struct tm to RTC_TIME_DATA_s format. More...
 
void RTC_Trigger (void)
 trigger function for the RTC driver. More...
 
STD_RETURN_TYPE_e RTC_Initialize (void)
 initialization of the RTC IC. More...
 
void RTC_InitializeSystemTimeWithRtc (void)
 initialize system time with RTC time. More...
 
void RTC_IncrementSystemTime (void)
 increment the RTC system timer. More...
 
void RTC_SetSystemTimeRtcFormat (RTC_TIME_DATA_s timeRtcFormat)
 set the RTC system timer. More...
 
RTC_TIME_DATA_s RTC_GetSystemTimeRtcFormat (void)
 get the RTC system timer. More...
 

Variables

static uint8_t rtc_i2cWriteBuffer [RTC_MAX_I2C_TRANSACTION_SIZE_IN_BYTES] = {0}
 
static uint8_t rtc_i2cReadBuffer [RTC_MAX_I2C_TRANSACTION_SIZE_IN_BYTES] = {0}
 
static RTC_SYSTEM_TIMER_EPOCH_s rtc_SystemTime = {0}
 

Detailed Description

Implementation of the RTC driver.

SPDX-License-Identifier: BSD-3-Clause

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

We kindly request you to use one or more of the following phrases to refer to foxBMS in your hardware, software, documentation or advertising materials:

  • ″This product uses parts of foxBMS®″
  • ″This product includes parts of foxBMS®″
  • ″This product is derived from foxBMS®″
Author
foxBMS Team
Date
2021-02-22 (date of creation)
Updated
2023-10-12 (date of last update)
Version
v1.6.0
Prefix
RTC

Uses the NXP PCF2131 RTC over I2C

Definition in file rtc.c.

Function Documentation

◆ RTC_AdjustTime()

static void RTC_AdjustTime ( void  )
static

Adjust RTC system timer with time from RTC IC.

The time is read from the RTC IC and compared to the RTC system timer. If the difference is greater than the limit, the RTC system timer is adjusted.

Definition at line 213 of file rtc.c.

Here is the call graph for this function:

◆ RTC_CheckBatteryLowVoltageAlert()

static void RTC_CheckBatteryLowVoltageAlert ( void  )
static

Read bit for battery voltage low flag.

Definition at line 242 of file rtc.c.

Here is the call graph for this function:

◆ RTC_GetSystemTimeEpochFormat()

static RTC_SYSTEM_TIMER_EPOCH_s RTC_GetSystemTimeEpochFormat ( void  )
static

Get the RTC system timer value.

This function gets the timer value directly. It is called by RTC_GetSystemTimeRtcFormat() which returns a RTC_TIME_DATA_s parameter.

Returns
RTC system timer value in seconds since epoch format

Definition at line 301 of file rtc.c.

Here is the call graph for this function:

◆ RTC_GetSystemTimeRtcFormat()

RTC_TIME_DATA_s RTC_GetSystemTimeRtcFormat ( void  )

get the RTC system timer.

This function is used to get the RTC system timer value.

Returns
value of the RTC system timer

Definition at line 607 of file rtc.c.

Here is the call graph for this function:

◆ RTC_IncrementSystemTime()

void RTC_IncrementSystemTime ( void  )

increment the RTC system timer.

This function must be called every millisecond and so must be called in the 1ms task.

Definition at line 586 of file rtc.c.

◆ RTC_Initialize()

STD_RETURN_TYPE_e RTC_Initialize ( void  )

initialization of the RTC IC.

Returns
STD_OK if initialization is finished, STD_NOT_OK otherwise

Definition at line 441 of file rtc.c.

Here is the call graph for this function:

◆ RTC_InitializeSystemTimeWithRtc()

void RTC_InitializeSystemTimeWithRtc ( void  )

initialize system time with RTC time.

This function is called once at the startup of the system. It reads the time from the RTC IC over I2C and sets the RTC system timer with this value. It must be called once the 1ms task is already running, otherwise the RTC system timer will not be incremented and there will be a discrepancy between the RTC IC time and the RTC system timer.

Definition at line 577 of file rtc.c.

Here is the call graph for this function:

◆ RTC_ReadTime()

static RTC_TIME_DATA_s RTC_ReadTime ( void  )
static

Reads the time from the RTC IC.

This function reads the time from the RTC IC over I2C.

Returns
RTC time read from the RTC IC

Definition at line 349 of file rtc.c.

Here is the call graph for this function:

◆ RTC_rtcFormatToTmFormat()

static struct tm RTC_rtcFormatToTmFormat ( RTC_TIME_DATA_s  timeRtcFormat)
static

Convert time from RTC_TIME_DATA_s to struct tm format.

Parameters
timeRtcFormattime in RTC_TIME_DATA_s format
Returns
time in struct tm format

Definition at line 301 of file rtc.c.

◆ RTC_SetOverCanMessage()

static void RTC_SetOverCanMessage ( void  )
static

Check if a CAN message was received to set the RTC time.

When a CAN message to set the RTC time is received, it is written in the dedicated queue. This function check if an element is present in the queue. If yes, the time is first written to the RTC IC over I2C, then to the RTC system timer.

Definition at line 195 of file rtc.c.

Here is the call graph for this function:

◆ RTC_SetSystemTimeEpochFormat()

static void RTC_SetSystemTimeEpochFormat ( time_t  timeEpochFormat,
uint16_t  milliseconds 
)
static

Set the RTC system timer value.

This function sets the timer value directly. It is called by RTC_SetSystemTimeRtcFormat() which takes a RTC_TIME_DATA_s parameter as input.

Parameters
timeEpochFormatRTC system timer value to set, main value in seconds
millisecondsRTC system timer value to set, secondary value in milliseconds

Definition at line 285 of file rtc.c.

Here is the call graph for this function:

◆ RTC_SetSystemTimeRtcFormat()

void RTC_SetSystemTimeRtcFormat ( RTC_TIME_DATA_s  timeRtcFormat)

set the RTC system timer.

This function is used to set the RTC system timer with RTC time data. It is called at startup by RTC_InitializeSystemTimeWithRtc().

Parameters
timeRtcFormattime data to set the RTC system timer

Definition at line 595 of file rtc.c.

Here is the call graph for this function:

◆ RTC_tmFormatToRtcFormat()

static RTC_TIME_DATA_s RTC_tmFormatToRtcFormat ( struct tm  timeTmFormat)
static

Convert time from struct tm to RTC_TIME_DATA_s format.

Parameters
timeTmFormattime in struct tm format
Returns
time in RTC_TIME_DATA_s format

Definition at line 335 of file rtc.c.

◆ RTC_Trigger()

void RTC_Trigger ( void  )

trigger function for the RTC driver.

This function uses RTC_SetOverCanMessage() to check in the RTC queue for requests to set the RTC time. If a request was made, the time of the RTC IC is set over I2C and the RTC system timer is set.

Definition at line 414 of file rtc.c.

Here is the call graph for this function:

◆ RTC_WriteTime()

static void RTC_WriteTime ( RTC_TIME_DATA_s  rtcTime)
static

Write the time to the RTC IC.

This function writes the time to the RTC IC over I2C.

Parameters
rtcTimeRTC time to write to the IC

Definition at line 121 of file rtc.c.

Here is the call graph for this function:

Variable Documentation

◆ rtc_i2cReadBuffer

uint8_t rtc_i2cReadBuffer[RTC_MAX_I2C_TRANSACTION_SIZE_IN_BYTES] = {0}
static

I2C buffer for read transactions with RTC

Definition at line 85 of file rtc.c.

◆ rtc_i2cWriteBuffer

uint8_t rtc_i2cWriteBuffer[RTC_MAX_I2C_TRANSACTION_SIZE_IN_BYTES] = {0}
static

I2C buffer for write transactions with RTC

Definition at line 83 of file rtc.c.

◆ rtc_SystemTime

RTC_SYSTEM_TIMER_EPOCH_s rtc_SystemTime = {0}
static

Variable containing the RTC system time

Definition at line 90 of file rtc.c.