RCM — ESP32 Room Controller
WT32-ETH01 firmware
Loading...
Searching...
No Matches
ds3231.c File Reference

I2C-драйвер Maxim DS3231 RTC (адрес 0x68). More...

#include "ds3231.h"
#include "driver/i2c_master.h"
#include "esp_check.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "i2c_bus.h"

Go to the source code of this file.

Macros

#define DS3231_I2C_ADDR   0x68
 7-битный адрес DS3231 на шине
 
#define DS3231_REG_SECONDS   0x00
 
#define DS3231_REG_MINUTES   0x01
 
#define DS3231_REG_HOURS   0x02
 
#define DS3231_REG_DAY   0x03
 
#define DS3231_REG_DATE   0x04
 
#define DS3231_REG_MONTH   0x05
 
#define DS3231_REG_YEAR   0x06
 
#define DS3231_REG_CONTROL   0x0E
 
#define DS3231_REG_STATUS   0x0F
 
#define DS3231_REG_TEMP_MSB   0x11
 
#define DS3231_REG_TEMP_LSB   0x12
 
#define DS3231_STATUS_OSF   (1U << 7)
 
#define DS3231_HOURS_12H   (1U << 6)
 
#define DS3231_YEAR_BASE   2000U
 

Functions

static uint8_t bcd_to_bin (uint8_t bcd)
 
static uint8_t bin_to_bcd (uint8_t bin)
 
static esp_err_t ds3231_write_regs (uint8_t reg, const uint8_t *data, size_t len)
 
static esp_err_t ds3231_read_regs (uint8_t reg, uint8_t *data, size_t len)
 
static esp_err_t ds3231_validate_datetime (const ds3231_datetime_t *dt)
 
esp_err_t ds3231_init (void)
 Добавляет DS3231 на общую I2C-шину и проверяет доступность.
 
esp_err_t ds3231_get_time (ds3231_datetime_t *dt)
 Прочитать текущие дату и время из DS3231.
 
esp_err_t ds3231_set_time (const ds3231_datetime_t *dt)
 Записать дату и время в DS3231 (24-часовой режим).
 
esp_err_t ds3231_get_temperature (float *temp_c)
 Прочитать температуру с встроенного датчика DS3231.
 
esp_err_t ds3231_is_oscillator_stopped (bool *stopped)
 Проверить, был ли остановлен осциллятор (потеря питания / батареи).
 
esp_err_t ds3231_clear_osf (void)
 Сбросить флаг OSF в Status-регистре.
 

Variables

static const char * TAG = "ds3231"
 
static i2c_master_dev_handle_t s_dev
 
static bool s_initialized
 

Detailed Description

I2C-драйвер Maxim DS3231 RTC (адрес 0x68).

Использует общую шину i2c_bus (GPIO14/15).

Definition in file ds3231.c.

Macro Definition Documentation

◆ DS3231_HOURS_12H

#define DS3231_HOURS_12H   (1U << 6)

Definition at line 38 of file ds3231.c.

Referenced by ds3231_get_time().

◆ DS3231_I2C_ADDR

#define DS3231_I2C_ADDR   0x68

7-битный адрес DS3231 на шине

Definition at line 20 of file ds3231.c.

Referenced by ds3231_init().

◆ DS3231_REG_CONTROL

#define DS3231_REG_CONTROL   0x0E

Definition at line 32 of file ds3231.c.

◆ DS3231_REG_DATE

#define DS3231_REG_DATE   0x04

Definition at line 27 of file ds3231.c.

◆ DS3231_REG_DAY

#define DS3231_REG_DAY   0x03

Definition at line 26 of file ds3231.c.

◆ DS3231_REG_HOURS

#define DS3231_REG_HOURS   0x02

Definition at line 25 of file ds3231.c.

◆ DS3231_REG_MINUTES

#define DS3231_REG_MINUTES   0x01

Definition at line 24 of file ds3231.c.

◆ DS3231_REG_MONTH

#define DS3231_REG_MONTH   0x05

Definition at line 28 of file ds3231.c.

◆ DS3231_REG_SECONDS

#define DS3231_REG_SECONDS   0x00

Definition at line 23 of file ds3231.c.

Referenced by ds3231_get_time(), and ds3231_set_time().

◆ DS3231_REG_STATUS

#define DS3231_REG_STATUS   0x0F

Definition at line 33 of file ds3231.c.

Referenced by ds3231_clear_osf(), ds3231_init(), and ds3231_is_oscillator_stopped().

◆ DS3231_REG_TEMP_LSB

#define DS3231_REG_TEMP_LSB   0x12

Definition at line 35 of file ds3231.c.

◆ DS3231_REG_TEMP_MSB

#define DS3231_REG_TEMP_MSB   0x11

Definition at line 34 of file ds3231.c.

Referenced by ds3231_get_temperature().

◆ DS3231_REG_YEAR

#define DS3231_REG_YEAR   0x06

Definition at line 29 of file ds3231.c.

◆ DS3231_STATUS_OSF

#define DS3231_STATUS_OSF   (1U << 7)

Definition at line 37 of file ds3231.c.

Referenced by ds3231_clear_osf(), ds3231_init(), and ds3231_is_oscillator_stopped().

◆ DS3231_YEAR_BASE

#define DS3231_YEAR_BASE   2000U

Definition at line 40 of file ds3231.c.

Referenced by ds3231_get_time(), ds3231_set_time(), and ds3231_validate_datetime().

Function Documentation

◆ bcd_to_bin()

static uint8_t bcd_to_bin ( uint8_t bcd)
static

Definition at line 45 of file ds3231.c.

Referenced by ds3231_get_time().

◆ bin_to_bcd()

static uint8_t bin_to_bcd ( uint8_t bin)
static

Definition at line 50 of file ds3231.c.

Referenced by ds3231_set_time().

◆ ds3231_clear_osf()

esp_err_t ds3231_clear_osf ( void )

Сбросить флаг OSF в Status-регистре.

Definition at line 214 of file ds3231.c.

References ds3231_read_regs(), DS3231_REG_STATUS, DS3231_STATUS_OSF, ds3231_write_regs(), s_initialized, and TAG.

Referenced by ds3231_set_time().

◆ ds3231_get_temperature()

esp_err_t ds3231_get_temperature ( float * temp_c)

Прочитать температуру с встроенного датчика DS3231.

Parameters
[out]temp_cТемпература в °C (разрешение 0.25 °C).

Definition at line 184 of file ds3231.c.

References ds3231_read_regs(), DS3231_REG_TEMP_MSB, s_initialized, and TAG.

Referenced by app_main().

◆ ds3231_get_time()

◆ ds3231_init()

esp_err_t ds3231_init ( void )

Добавляет DS3231 на общую I2C-шину и проверяет доступность.

Требует предварительного (или внутреннего) вызова i2c_bus_init(). Адрес: 0x68. SCL=GPIO14, SDA=GPIO15. При отсутствии чипа возвращает ошибку I2C (не вызывает abort) — вызывающий код может продолжить работу без RTC.

Returns
ESP_OK при успехе; иначе код ошибки ESP-IDF / I2C.

Definition at line 89 of file ds3231.c.

References DS3231_I2C_ADDR, ds3231_read_regs(), DS3231_REG_STATUS, DS3231_STATUS_OSF, i2c_bus_add_device(), i2c_bus_init(), s_dev, s_initialized, and TAG.

Referenced by app_main().

◆ ds3231_is_oscillator_stopped()

esp_err_t ds3231_is_oscillator_stopped ( bool * stopped)

Проверить, был ли остановлен осциллятор (потеря питания / батареи).

Parameters
[out]stoppedtrue, если бит OSF установлен (время могло «поплыть»).

Definition at line 201 of file ds3231.c.

References ds3231_read_regs(), DS3231_REG_STATUS, DS3231_STATUS_OSF, s_initialized, and TAG.

Referenced by app_main().

◆ ds3231_read_regs()

static esp_err_t ds3231_read_regs ( uint8_t reg,
uint8_t * data,
size_t len )
static

◆ ds3231_set_time()

esp_err_t ds3231_set_time ( const ds3231_datetime_t * dt)

Записать дату и время в DS3231 (24-часовой режим).

Сбрасывает флаг OSF (Oscillator Stop Flag) в регистре Status после записи.

Definition at line 162 of file ds3231.c.

References bin_to_bcd(), ds3231_datetime_t::date, ds3231_clear_osf(), DS3231_REG_SECONDS, ds3231_validate_datetime(), ds3231_write_regs(), DS3231_YEAR_BASE, ds3231_datetime_t::hour, ds3231_datetime_t::minute, ds3231_datetime_t::month, s_initialized, ds3231_datetime_t::second, TAG, ds3231_datetime_t::weekday, and ds3231_datetime_t::year.

◆ ds3231_validate_datetime()

◆ ds3231_write_regs()

static esp_err_t ds3231_write_regs ( uint8_t reg,
const uint8_t * data,
size_t len )
static

Definition at line 55 of file ds3231.c.

References I2C_BUS_TIMEOUT_MS, s_dev, and TAG.

Referenced by ds3231_clear_osf(), and ds3231_set_time().

Variable Documentation

◆ s_dev

i2c_master_dev_handle_t s_dev
static

Definition at line 42 of file ds3231.c.

Referenced by ds3231_init(), ds3231_read_regs(), and ds3231_write_regs().

◆ s_initialized

bool s_initialized
static

◆ TAG