Unix Timestamps
- A Unix Timestamp represents the total number of seconds that have elapsed since the Unix Epoch (00:00:00 UTC on January 1, 1970), excluding leap seconds.
- This value is typically stored in a 32-bit or 64-bit integer variable acting as a seconds counter.
- Since the counter is based on UTC, all time zones share the same counter value; local time is derived by applying a specific timezone offset.
UTC vs GMT
- GMT (Greenwich Mean Time): A solar-based time system relying on Earth's rotation, dividing a full rotation into 24 hours.
- UTC (Coordinated Universal Time): An atomic-based time standard. One second is defined by 9,192,631,770 oscillations of a Cesium-133 atom. If the atomic time drifts from Earth's rotation by more than 0.9 seconds, a leap second is added to UTC to maintain synchronization.
Time Conversion in C
The standard C library time.h provides functions to handle conversions between timestamps, structured time, and strings.
#include <time.h>
time_t get_current_time(time_t* timer); // Fetch system clock
struct tm* get_utc_time(const time_t* timer); // Convert timestamp to UTC struct
struct tm* get_local_time(const time_t* timer); // Convert timestamp to local time struct
time_t convert_to_timestamp(struct tm* time_ptr); // Convert local time struct to timestamp
char* timestamp_to_string(const time_t* timer); // Convert timestamp to default string
char* struct_to_string(const struct tm* time_ptr); // Convert struct to default string
size_t format_time_string(char* buffer, size_t max_size, const char* format, const struct tm* time_ptr);
// Convert struct to custom formatted string
Backup Registers (BKP)
- BKP (Backup Registers) are special memory locations designed to retain user data.
- They remain powered by the VBAT pin (1.8V–3.6V) when the main VDD supply (2.0V–3.6V) is cut. Data persists through system resets, power cycles, or Standby mode wake-ups.
- A tamper detection event on the TAMPER pin will automatically erase all backup data for security.
- The BKP domain also stores RTC calibration values and can output RTC signals (calibration clock, alarm pulse, or second pulse).
- Storage Capacity: 20 bytes (Low/Medium Density devices) or 84 bytes (High Density/Connectivity Line devices).
BKP Architecture
- The orange highlighted area in diagrams typically represents the Backup Domain.
- Power switching: When VDD is present, it powers the backup domain. When VDD fails, VBAT takes over automatically.
- Registers: Includes control, status, and RTC calibration registers. Each data register is 16-bit (2 bytes).
- Tamper Detection: A rising or falling edge on the TAMPER pin triggers a mass erase of the BKP data.
- Clock Output: The RTC can drive a clock signal out through the PC13 pin for external use.
// Reset all backup data
void BKP_ResetAll(void);
// Tamper configuration
void BKP_ConfigTamperLevel(uint16_t Level);
void BKP_EnableTamperDetection(FunctionalState State);
// Interrupts and Output
void BKP_EnableInterrupt(FunctionalState State);
void BKP_SelectRTCOutput(uint16_t Source);
void BKP_WriteCalibration(uint8_t Value);
// Data Access
void BKP_StoreData(uint16_t RegisterAddress, uint16_t Value);
uint16_t BKP_RetrieveData(uint16_t RegisterAddress);
// Status management
FlagStatus BKP_ReadFlag(void);
void BKP_ClearFlagBit(void);
ITStatus BKP_CheckInterrupt(void);
void BKP_ClearIRQ(void);
Real-Time Clock (RTC)
- The RTC is an independent timer peripheral providing calendar and clock functionality.
- Located in the Backup Domain, it retains time data through system resets and continues counting via VBAT when VDD is lost.
- It features a 32-bit programmable counter (suitable for storing the Unix Timestamp seconds) and a 20-bit prescaler for various clock sources.
- Clock Sources:
- HSE / 128 (e.g., 8MHz / 128)
- LSE (Low Speed External, typically 32.768 kHz)
- LSI (Low Speed Internal, typically 40 kHz)
RTC Interrupts
- RTC_Second: Triggers once per second.
- RTC_Overflow: Triggers when the 32-bit counter rolls over.
- RTC_Alarm: Triggers when the counter matches the alarm value.
RTC Configuration Notes
- To access BKP and RTC, you must first enable clocks and disable the backup domain write protection:
- Enable RCC_APB1ENR clocks for PWR and BKP.
- Set the DBP bit in PWR_CR to allow access.
- If the APB1 interface was previously disabled, wait for the RSF (Register Synchronized) bit in RTC_CRL to be set by hardware.
- Enter configuration mode by setting the CNF bit in RTC_CRL before writing to RTC_PRL, RTC_CNT, or RTC_ALR.
- Always insure the previous operation is complete. Check the RTOFF bit in RTC_CR; write only when its '1'.
// Interrupt and Config
void RTC_EnableInterrupt(uint16_t Source, FunctionalState State);
void RTC_StartConfig(void);
void RTC_StopConfig(void);
// Counter management
uint32_t RTC_ReadCounter(void);
void RTC_WriteCounter(uint32_t value);
void RTC_WritePrescaler(uint32_t prescaler_val);
void RTC_SetAlarmValue(uint32_t alarm_val);
uint32_t RTC_ReadDivider(void);
// Task synchronization
void RTC_WaitForLastTaskComplete(void);
void RTC_WaitForSync(void);
// Flags and IRQ
FlagStatus RTC_CheckFlag(uint16_t flag);
void RTC_ClearFlagBit(uint16_t flag);
ITStatus RTC_CheckIRQ(uint16_t source);
void RTC_ClearIRQBit(uint16_t source);