|
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
|
Types, constants and function prototypes of the STM32_DS3231 driver. More...
Go to the source code of this file.
Data Structures | |
| struct | DS3231_TIME_t |
| A calendar date and time, always 24-hour clock. More... | |
| struct | DS3231_t |
Macros | |
| #define | DS3231_I2C_ADDRESS (0x68U << 1) |
| #define | DS3231_DEFAULT_MAX_ATTEMPTS 3U |
| #define | DS3231_I2C_TIMEOUT_MS 100U |
| #define | DS3231_YEAR_MIN 2000U |
| #define | DS3231_YEAR_MAX 2099U |
| #define | DS3231_EPOCH_MIN 946684800U |
| #define | DS3231_EPOCH_MAX 4102444799U |
Enumerations | |
| enum | DS3231_Status_t { DS3231_OK = 0 , DS3231_ERR_PARAM , DS3231_ERR_NOT_FOUND , DS3231_ERR_INVALID_DATA , DS3231_ERR_BUS } |
Functions | |
| 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. | |
| DS3231_Status_t | ds3231GetTime (DS3231_t *dev, DS3231_TIME_t *time) |
| Read the current date and time. | |
| DS3231_Status_t | ds3231SetTime (DS3231_t *dev, const DS3231_TIME_t *time) |
| Set the date and time. | |
| DS3231_Status_t | ds3231GetEpoch (DS3231_t *dev, uint32_t *epochSeconds) |
| Read the current time as seconds since 1970-01-01 00:00:00. | |
| DS3231_Status_t | ds3231SetEpoch (DS3231_t *dev, uint32_t epochSeconds) |
| Set the time from seconds since 1970-01-01 00:00:00. | |
| DS3231_Status_t | ds3231IsOscillatorStopped (DS3231_t *dev, bool *stopped) |
| Find out whether the oscillator has stopped since the flag was last cleared. | |
| DS3231_Status_t | ds3231ClearOscillatorStopFlag (DS3231_t *dev) |
| Clear the oscillator stop flag. | |
| bool | ds3231IsTimeValid (const DS3231_TIME_t *time) |
| Check that a date and time are valid for the DS3231. | |
| 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). | |
| 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. | |
Types, constants and function prototypes of the STM32_DS3231 driver.
Every function is documented where it is defined, in DS3231.c.
Problems that are the caller's to handle are returned as a DS3231_Status_t instead: a bad argument, a chip that does not answer or does not look like a DS3231 in ds3231Init(), and time registers that hold an impossible date.
Definition in file DS3231.h.
| #define DS3231_DEFAULT_MAX_ATTEMPTS 3U |
Number of attempts used when ds3231Init() is given 0 for maxAttempts.
Definition at line 76 of file DS3231.h.
Referenced by ds3231Init().
| #define DS3231_EPOCH_MAX 4102444799U |
Seconds from 1970-01-01 00:00:00 to 2099-12-31 23:59:59: the highest epoch value the driver accepts.
Definition at line 91 of file DS3231.h.
Referenced by ds3231EpochToTime().
| #define DS3231_EPOCH_MIN 946684800U |
| #define DS3231_I2C_ADDRESS (0x68U << 1) |
8-bit (write) I2C address of the DS3231: the 7-bit address 0x68 shifted left, as the HAL expects.
Definition at line 73 of file DS3231.h.
Referenced by ds3231Init().
| #define DS3231_I2C_TIMEOUT_MS 100U |
Timeout, in milliseconds, of one HAL I2C transfer.
Definition at line 79 of file DS3231.h.
Referenced by ds3231Init().
| #define DS3231_YEAR_MAX 2099U |
Last year the driver accepts.
Definition at line 85 of file DS3231.h.
Referenced by ds3231IsTimeValid().
| #define DS3231_YEAR_MIN 2000U |
First year the driver accepts (the chip stores a two-digit year).
Definition at line 82 of file DS3231.h.
Referenced by ds3231GetTime(), ds3231IsTimeValid(), and ds3231SetTime().
| enum DS3231_Status_t |
Result of a driver function.
| DS3231_Status_t ds3231ClearOscillatorStopFlag | ( | DS3231_t * | dev | ) |
Clear the oscillator stop flag.
Reads the status register and writes it back with OSF cleared and the 32 kHz output setting kept. The two alarm flags are written as 1, which the datasheet says leaves them unchanged, so an alarm that fires between the read and the write is not lost. The read followed by the write is not atomic against other users of the same I2C bus (see the thread-safety note).
| dev | The driver handle, set up with ds3231Init(). |
dev is NULL. Definition at line 520 of file DS3231.c.
References DS3231_ERR_BUS, DS3231_ERR_PARAM, DS3231_OK, and ds3231ClearOscillatorStopFlag().
Referenced by ds3231ClearOscillatorStopFlag(), and setClockFromBuildTime().
| 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.
Pure calculation: it does not touch the chip.
| epochSeconds | Seconds since 1970-01-01 00:00:00, DS3231_EPOCH_MIN to DS3231_EPOCH_MAX. |
| time | Receives the date and time, with the weekday filled in. |
time is NULL or epochSeconds is outside the range the chip can hold (years 2000 to 2099). Definition at line 267 of file DS3231.c.
References DS3231_TIME_t::day, DS3231_EPOCH_MAX, DS3231_ERR_PARAM, DS3231_OK, ds3231EpochToTime(), DS3231_TIME_t::hour, DS3231_TIME_t::minute, DS3231_TIME_t::month, DS3231_TIME_t::second, DS3231_TIME_t::weekday, and DS3231_TIME_t::year.
Referenced by ds3231EpochToTime(), ds3231SetEpoch(), and setClockFromBuildTime().
| DS3231_Status_t ds3231GetEpoch | ( | DS3231_t * | dev, |
| uint32_t * | epochSeconds ) |
Read the current time as seconds since 1970-01-01 00:00:00.
| dev | The driver handle, set up with ds3231Init(). |
| epochSeconds | Receives the seconds since 1970-01-01 00:00:00 (the chip is treated as UTC). |
epochSeconds is NULL. Definition at line 444 of file DS3231.c.
References DS3231_ERR_PARAM, DS3231_OK, ds3231GetEpoch(), ds3231GetTime(), and ds3231TimeToEpoch().
Referenced by ds3231GetEpoch().
| DS3231_Status_t ds3231GetTime | ( | DS3231_t * | dev, |
| DS3231_TIME_t * | time ) |
Read the current date and time.
Reads the seven timekeeping registers in one burst, which the chip freezes into a buffer so the values cannot change halfway through. A clock set to 12-hour mode by other code is converted to 24-hour. The weekday is worked out from the date.
| dev | The driver handle, set up with ds3231Init(). |
| time | Receives the date and time. |
Definition at line 343 of file DS3231.c.
References DS3231_TIME_t::day, DS3231_ERR_BUS, DS3231_ERR_INVALID_DATA, DS3231_ERR_PARAM, DS3231_OK, DS3231_YEAR_MIN, ds3231GetTime(), ds3231IsTimeValid(), DS3231_TIME_t::hour, DS3231_TIME_t::minute, DS3231_TIME_t::month, DS3231_TIME_t::second, DS3231_TIME_t::weekday, and DS3231_TIME_t::year.
Referenced by ds3231GetEpoch(), ds3231GetTime(), and printTime().
| 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.
Call it after MX_I2Cx_Init(). It checks that something acknowledges the DS3231 address, then reads the status register and checks that bits 6:4, which always read 0 on a DS3231, are 0. It does not change anything on the chip.
| dev | The handle to fill in. Not used by the driver before this call. |
| hi2c | The HAL I2C handle the chip is on, for example &hi2c1. |
| maxAttempts | Attempts per transaction before Error_Handler() is called; 0 selects DS3231_DEFAULT_MAX_ATTEMPTS. |
dev or hi2c is NULL; DS3231_ERR_NOT_FOUND if nothing acknowledged the address (DS3231_t::lastStatus holds the HAL status) or the status register does not look like a DS3231's. Definition at line 302 of file DS3231.c.
References DS3231_DEFAULT_MAX_ATTEMPTS, DS3231_ERR_BUS, DS3231_ERR_NOT_FOUND, DS3231_ERR_PARAM, DS3231_I2C_ADDRESS, DS3231_I2C_TIMEOUT_MS, DS3231_OK, ds3231Init(), DS3231_t::hi2c, DS3231_t::lastStatus, and DS3231_t::maxAttempts.
Referenced by ds3231Init(), and main().
| DS3231_Status_t ds3231IsOscillatorStopped | ( | DS3231_t * | dev, |
| bool * | stopped ) |
Find out whether the oscillator has stopped since the flag was last cleared.
The oscillator stop flag (OSF) is set the first time power is applied, and whenever both VCC and the backup battery were too low to keep the oscillator running, so a set flag means the time may be wrong.
| dev | The driver handle, set up with ds3231Init(). |
| stopped | Receives true if the flag is set, false if it is clear. |
Definition at line 494 of file DS3231.c.
References DS3231_ERR_BUS, DS3231_ERR_PARAM, DS3231_OK, and ds3231IsOscillatorStopped().
Referenced by ds3231IsOscillatorStopped(), and reportOscillator().
| bool ds3231IsTimeValid | ( | const DS3231_TIME_t * | time | ) |
Check that a date and time are valid for the DS3231.
The weekday member is not checked: the driver works it out from the date.
| time | The date and time to check. |
time is NULL. Definition at line 216 of file DS3231.c.
References DS3231_TIME_t::day, DS3231_YEAR_MAX, DS3231_YEAR_MIN, ds3231IsTimeValid(), DS3231_TIME_t::hour, DS3231_TIME_t::minute, DS3231_TIME_t::month, DS3231_TIME_t::second, and DS3231_TIME_t::year.
Referenced by ds3231GetTime(), ds3231IsTimeValid(), ds3231SetTime(), ds3231TimeToEpoch(), and parseBuildTime().
| DS3231_Status_t ds3231SetEpoch | ( | DS3231_t * | dev, |
| uint32_t | epochSeconds ) |
Set the time from seconds since 1970-01-01 00:00:00.
The chip is treated as UTC: the value is stored as the calendar date and time it represents, with no time-zone offset.
| dev | The driver handle, set up with ds3231Init(). |
| epochSeconds | Seconds since 1970-01-01 00:00:00, DS3231_EPOCH_MIN to DS3231_EPOCH_MAX. |
epochSeconds is outside the range the chip can hold. Definition at line 471 of file DS3231.c.
References DS3231_OK, ds3231EpochToTime(), ds3231SetEpoch(), and ds3231SetTime().
Referenced by ds3231SetEpoch().
| DS3231_Status_t ds3231SetTime | ( | DS3231_t * | dev, |
| const DS3231_TIME_t * | time ) |
Set the date and time.
Writes the seven timekeeping registers in one burst, in 24-hour mode with the century bit clear, and writes the weekday worked out from the date. The oscillator stop flag is not touched: call ds3231ClearOscillatorStopFlag() afterwards if the clock had stopped.
| dev | The driver handle, set up with ds3231Init(). |
| time | The date and time to set; the weekday member is ignored. |
Definition at line 419 of file DS3231.c.
References DS3231_TIME_t::day, DS3231_ERR_PARAM, DS3231_YEAR_MIN, ds3231IsTimeValid(), ds3231SetTime(), DS3231_TIME_t::hour, DS3231_TIME_t::minute, DS3231_TIME_t::month, DS3231_TIME_t::second, and DS3231_TIME_t::year.
Referenced by ds3231SetEpoch(), ds3231SetTime(), and setClockFromBuildTime().
| 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).
Pure calculation: it does not touch the chip.
| time | The date and time; the weekday member is ignored. |
| epochSeconds | Receives the seconds since 1970-01-01 00:00:00. |
time is not valid (see ds3231IsTimeValid()). Definition at line 243 of file DS3231.c.
References DS3231_TIME_t::day, DS3231_ERR_PARAM, DS3231_OK, ds3231IsTimeValid(), ds3231TimeToEpoch(), DS3231_TIME_t::hour, DS3231_TIME_t::minute, DS3231_TIME_t::month, DS3231_TIME_t::second, and DS3231_TIME_t::year.
Referenced by ds3231GetEpoch(), ds3231TimeToEpoch(), and setClockFromBuildTime().