123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- /*
- * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
- *
- * SPDX-License-Identifier: GPL-2.0-only
- */
- #include <driver/i2c.h>
- #include <sys/time.h>
- #include <esp_err.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "bcd.h"
- #define DS3231_ADDRESS 0x68
- #define SECONDS_REGISTER_ADDRESS 0x00
- #define MINUTES_REGISTER_ADDRESS 0x01
- #define HOURS_REGISTER_ADDRESS 0x02
- #define DOW_REGISTER_ADDRESS 0x03
- #define DAY_REGISTER_ADDRESS 0x04
- #define MONTH_REGISTER_ADDRESS 0x05
- #define YEAR_REGISTER_ADDRESS 0x06
- esp_err_t ds3231_init(int sda_pin, int scl_pin) {
- int i2c_master_port = I2C_NUM_0;
- i2c_config_t conf;
- conf.mode = I2C_MODE_MASTER;
- conf.sda_io_num = sda_pin;
- conf.sda_pullup_en = 1;
- conf.scl_io_num = scl_pin;
- conf.scl_pullup_en = 1;
- ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode));
- ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &conf));
- return ESP_OK;
- }
- static esp_err_t read_value(uint8_t register_addr, uint8_t* value_read) {
- esp_err_t ret;
- i2c_cmd_handle_t cmd = i2c_cmd_link_create();
- i2c_master_start(cmd);
- i2c_master_write_byte(cmd, DS3231_ADDRESS << 1 | I2C_MASTER_WRITE, 0x01);
- i2c_master_write_byte(cmd, register_addr, 0x01);
- i2c_master_stop(cmd);
- ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
- i2c_cmd_link_delete(cmd);
-
- if(ret != ESP_OK) {
- return ret;
- }
-
- cmd = i2c_cmd_link_create();
- i2c_master_start(cmd);
- i2c_master_write_byte(cmd, 0x68 << 1 | I2C_MASTER_READ, 0x01);
- i2c_master_read(cmd, value_read, 1, 0x02);
- i2c_master_stop(cmd);
- i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
- i2c_cmd_link_delete(cmd);
-
- return ret;
-
- }
- static esp_err_t write_value(uint8_t register_addr, uint8_t write_value) {
- esp_err_t ret;
- i2c_cmd_handle_t cmd = i2c_cmd_link_create();
- i2c_master_start(cmd);
- i2c_master_write_byte(cmd, DS3231_ADDRESS << 1 | I2C_MASTER_WRITE, 0x01);
- i2c_master_write_byte(cmd, register_addr, 0x01);
- i2c_master_write_byte(cmd, write_value, 0x01);
- i2c_master_stop(cmd);
- ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
- i2c_cmd_link_delete(cmd);
-
- return ret;
-
- }
- static uint8_t convert_12h_to_24h(uint8_t value_read) {
- uint8_t cleaned_value = value_read & 0xBF;
- if (value_read & 0x10) {
- cleaned_value = cleaned_value & 0XDF;
- return bcd_first_digit_value_bin(cleaned_value)
- + 12 + bcd_last_digit_value_bin(cleaned_value);
- }
- return bcd_first_digit_value_bin(cleaned_value)
- + bcd_last_digit_value_bin(cleaned_value);
- }
- static esp_err_t read_hour_value(uint8_t* hour_value) {
- uint8_t value_read;
- esp_err_t ret = read_value(HOURS_REGISTER_ADDRESS, &value_read);
-
- if(ret != ESP_OK) {
- return ret;
- }
-
- if (value_read & 0x20) {
- *hour_value = convert_12h_to_24h(value_read);
- } else {
- *hour_value = bcd2bin(value_read);
- }
-
- return ret;
- }
- static esp_err_t read_month_value(uint8_t* month_value) {
- uint8_t value_read;
- esp_err_t ret = read_value(MONTH_REGISTER_ADDRESS, &value_read);
- if (ret == ESP_OK) {
- *month_value = bcd2bin(value_read & 0x7F);
- }
- return ret;
- }
- esp_err_t ds3231_read_date_time(struct tm* date_time) {
- esp_err_t ret;
- uint8_t seconds, minutes, hours, dow, day, month, year;
- ret = read_value(SECONDS_REGISTER_ADDRESS, &seconds);
- ret &= read_value(MINUTES_REGISTER_ADDRESS, &minutes);
- ret &= read_hour_value(&hours);
- ret &= read_value(DOW_REGISTER_ADDRESS, &dow);
- ret &= read_value(DAY_REGISTER_ADDRESS, &day);
- ret &= read_month_value(&month);
- ret &= read_value(YEAR_REGISTER_ADDRESS, &year);
-
- date_time->tm_sec = bcd2bin(seconds);
- date_time->tm_min = bcd2bin(minutes);
- date_time->tm_hour = hours;
- date_time->tm_wday = bcd2bin(dow);
- date_time->tm_mday = bcd2bin(day);
- date_time->tm_mon = month;
- date_time->tm_year = bcd2bin(year);
-
- return ret;
- }
- esp_err_t ds3231_write_date_time(struct tm date_time) {
- esp_err_t ret;
- ret = write_value(SECONDS_REGISTER_ADDRESS, bin2bcd(date_time.tm_sec));
- ret &= write_value(MINUTES_REGISTER_ADDRESS, bin2bcd(date_time.tm_min));
- ret &= write_value(HOURS_REGISTER_ADDRESS, bin2bcd(date_time.tm_hour));
- ret &= write_value(DOW_REGISTER_ADDRESS, bin2bcd(date_time.tm_wday));
- ret &= write_value(DAY_REGISTER_ADDRESS, bin2bcd(date_time.tm_mday));
- ret &= write_value(MONTH_REGISTER_ADDRESS, bin2bcd(date_time.tm_mon) | 0x80);
- ret &= write_value(YEAR_REGISTER_ADDRESS, bin2bcd(date_time.tm_year));
- ret &= write_value(YEAR_REGISTER_ADDRESS, bin2bcd(date_time.tm_year));
-
- return ret;
- }
|