H753_CPP_I2C_DS3231_RTC_01 1.0
STM32H753 DS3231 real-time clock over I2C, with an AT24C32 EEPROM marker that stops the clock being set twice, built on the STM32_DS3231 and STM32_AT24C32 libraries
Loading...
Searching...
No Matches
DS3231.h
Go to the documentation of this file.
1/*
2 * DS3231.h
3 *
4 * STM32 HAL driver for the Analog Devices (Maxim) DS3231 I2C real-time clock,
5 * as found on the common ZS-042 breakout module.
6 *
7 * Written from the DS3231 datasheet: I2C address, timekeeping registers
8 * 00h-06h, and the control/status register at 0Fh. It is an independent
9 * implementation, not a port of anyone else's library.
10 *
11 * Created on: Oct 7, 2026
12 * Author: johng
13 */
14
15#ifndef STM32_DS3231_DS3231_H_
16#define STM32_DS3231_DS3231_H_
17
18#include "main.h"
19#include <stdbool.h>
20#include <stdint.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
73#define DS3231_I2C_ADDRESS (0x68U << 1)
74
76#define DS3231_DEFAULT_MAX_ATTEMPTS 3U
77
79#define DS3231_I2C_TIMEOUT_MS 100U
80
82#define DS3231_YEAR_MIN 2000U
83
85#define DS3231_YEAR_MAX 2099U
86
88#define DS3231_EPOCH_MIN 946684800U
89
91#define DS3231_EPOCH_MAX 4102444799U
92
101
109typedef struct {
110 uint16_t year;
111 uint8_t month;
112 uint8_t day;
113 uint8_t weekday;
114 uint8_t hour;
115 uint8_t minute;
116 uint8_t second;
118
120typedef struct {
121 I2C_HandleTypeDef *hi2c;
122 uint8_t maxAttempts;
123 HAL_StatusTypeDef lastStatus;
124} DS3231_t;
125
126DS3231_Status_t ds3231Init(DS3231_t *dev, I2C_HandleTypeDef *hi2c, uint8_t maxAttempts);
129DS3231_Status_t ds3231GetEpoch(DS3231_t *dev, uint32_t *epochSeconds);
130DS3231_Status_t ds3231SetEpoch(DS3231_t *dev, uint32_t epochSeconds);
133
134bool ds3231IsTimeValid(const DS3231_TIME_t *time);
135DS3231_Status_t ds3231TimeToEpoch(const DS3231_TIME_t *time, uint32_t *epochSeconds);
136DS3231_Status_t ds3231EpochToTime(uint32_t epochSeconds, DS3231_TIME_t *time);
137
138#ifdef __cplusplus
139}
140#endif
141
142#endif /* STM32_DS3231_DS3231_H_ */
DS3231_Status_t ds3231GetEpoch(DS3231_t *dev, uint32_t *epochSeconds)
Read the current time as seconds since 1970-01-01 00:00:00.
Definition DS3231.c:444
DS3231_Status_t ds3231SetTime(DS3231_t *dev, const DS3231_TIME_t *time)
Set the date and time.
Definition DS3231.c:419
DS3231_Status_t ds3231TimeToEpoch(const DS3231_TIME_t *time, uint32_t *epochSeconds)
Convert a date and time to seconds since 1970-01-01 00:00:00 (UTC, no leap seconds).
Definition DS3231.c:243
DS3231_Status_t ds3231EpochToTime(uint32_t epochSeconds, DS3231_TIME_t *time)
Convert seconds since 1970-01-01 00:00:00 (UTC, no leap seconds) to a date and time.
Definition DS3231.c:267
DS3231_Status_t
Definition DS3231.h:94
@ DS3231_OK
Definition DS3231.h:95
@ DS3231_ERR_BUS
Definition DS3231.h:99
@ DS3231_ERR_NOT_FOUND
Definition DS3231.h:97
@ DS3231_ERR_PARAM
Definition DS3231.h:96
@ DS3231_ERR_INVALID_DATA
Definition DS3231.h:98
DS3231_Status_t ds3231GetTime(DS3231_t *dev, DS3231_TIME_t *time)
Read the current date and time.
Definition DS3231.c:343
bool ds3231IsTimeValid(const DS3231_TIME_t *time)
Check that a date and time are valid for the DS3231.
Definition DS3231.c:216
DS3231_Status_t ds3231SetEpoch(DS3231_t *dev, uint32_t epochSeconds)
Set the time from seconds since 1970-01-01 00:00:00.
Definition DS3231.c:471
DS3231_Status_t ds3231ClearOscillatorStopFlag(DS3231_t *dev)
Clear the oscillator stop flag.
Definition DS3231.c:520
DS3231_Status_t ds3231IsOscillatorStopped(DS3231_t *dev, bool *stopped)
Find out whether the oscillator has stopped since the flag was last cleared.
Definition DS3231.c:494
DS3231_Status_t ds3231Init(DS3231_t *dev, I2C_HandleTypeDef *hi2c, uint8_t maxAttempts)
Set up a driver handle and check that a DS3231 answers on the bus.
Definition DS3231.c:302
: Header for main.c file. This file contains the common defines of the application.
A calendar date and time, always 24-hour clock.
Definition DS3231.h:109
uint8_t second
Definition DS3231.h:116
uint8_t weekday
Definition DS3231.h:113
uint8_t month
Definition DS3231.h:111
uint8_t minute
Definition DS3231.h:115
uint8_t hour
Definition DS3231.h:114
uint16_t year
Definition DS3231.h:110
uint8_t day
Definition DS3231.h:112
HAL_StatusTypeDef lastStatus
Definition DS3231.h:123
uint8_t maxAttempts
Definition DS3231.h:122
I2C_HandleTypeDef * hi2c
Definition DS3231.h:121