|
RCM — ESP32 Room Controller
WT32-ETH01 firmware
|
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 |
I2C-драйвер Maxim DS3231 RTC (адрес 0x68).
Использует общую шину i2c_bus (GPIO14/15).
Definition in file ds3231.c.
| #define DS3231_HOURS_12H (1U << 6) |
Definition at line 38 of file ds3231.c.
Referenced by ds3231_get_time().
| #define DS3231_I2C_ADDR 0x68 |
| #define DS3231_REG_SECONDS 0x00 |
Definition at line 23 of file ds3231.c.
Referenced by ds3231_get_time(), and ds3231_set_time().
| #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().
| #define DS3231_REG_TEMP_MSB 0x11 |
Definition at line 34 of file ds3231.c.
Referenced by ds3231_get_temperature().
| #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().
| #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().
|
static |
Definition at line 45 of file ds3231.c.
Referenced by ds3231_get_time().
|
static |
Definition at line 50 of file ds3231.c.
Referenced by ds3231_set_time().
| 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().
| esp_err_t ds3231_get_temperature | ( | float * | temp_c | ) |
Прочитать температуру с встроенного датчика DS3231.
| [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().
| esp_err_t ds3231_get_time | ( | ds3231_datetime_t * | dt | ) |
Прочитать текущие дату и время из DS3231.
Definition at line 128 of file ds3231.c.
References bcd_to_bin(), ds3231_datetime_t::date, DS3231_HOURS_12H, ds3231_read_regs(), DS3231_REG_SECONDS, 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.
Referenced by app_main().
| esp_err_t ds3231_init | ( | void | ) |
Добавляет DS3231 на общую I2C-шину и проверяет доступность.
Требует предварительного (или внутреннего) вызова i2c_bus_init(). Адрес: 0x68. SCL=GPIO14, SDA=GPIO15. При отсутствии чипа возвращает ошибку I2C (не вызывает abort) — вызывающий код может продолжить работу без RTC.
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().
| esp_err_t ds3231_is_oscillator_stopped | ( | bool * | stopped | ) |
Проверить, был ли остановлен осциллятор (потеря питания / батареи).
| [out] | stopped | true, если бит 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().
|
static |
Definition at line 69 of file ds3231.c.
References I2C_BUS_TIMEOUT_MS, s_dev, and TAG.
Referenced by ds3231_clear_osf(), ds3231_get_temperature(), ds3231_get_time(), ds3231_init(), and ds3231_is_oscillator_stopped().
| 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.
|
static |
Definition at line 75 of file ds3231.c.
References ds3231_datetime_t::date, DS3231_YEAR_BASE, ds3231_datetime_t::hour, ds3231_datetime_t::minute, ds3231_datetime_t::month, ds3231_datetime_t::second, TAG, ds3231_datetime_t::weekday, and ds3231_datetime_t::year.
Referenced by ds3231_set_time().
|
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().
|
static |
Definition at line 42 of file ds3231.c.
Referenced by ds3231_init(), ds3231_read_regs(), and ds3231_write_regs().
|
static |
Definition at line 43 of file ds3231.c.
Referenced by ds3231_clear_osf(), ds3231_get_temperature(), ds3231_get_time(), ds3231_init(), ds3231_is_oscillator_stopped(), and ds3231_set_time().
|
static |
Definition at line 17 of file ds3231.c.
Referenced by ds3231_clear_osf(), ds3231_get_temperature(), ds3231_get_time(), ds3231_init(), ds3231_is_oscillator_stopped(), ds3231_read_regs(), ds3231_set_time(), ds3231_validate_datetime(), and ds3231_write_regs().