ds3231.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only
  5. */
  6. #include <driver/i2c.h>
  7. #include <sys/time.h>
  8. #include <esp_err.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "bcd.h"
  12. #define DS3231_ADDRESS 0x68
  13. #define SECONDS_REGISTER_ADDRESS 0x00
  14. #define MINUTES_REGISTER_ADDRESS 0x01
  15. #define HOURS_REGISTER_ADDRESS 0x02
  16. #define DOW_REGISTER_ADDRESS 0x03
  17. #define DAY_REGISTER_ADDRESS 0x04
  18. #define MONTH_REGISTER_ADDRESS 0x05
  19. #define YEAR_REGISTER_ADDRESS 0x06
  20. esp_err_t ds3231_init(int sda_pin, int scl_pin) {
  21. int i2c_master_port = I2C_NUM_0;
  22. i2c_config_t conf;
  23. conf.mode = I2C_MODE_MASTER;
  24. conf.sda_io_num = sda_pin;
  25. conf.sda_pullup_en = 1;
  26. conf.scl_io_num = scl_pin;
  27. conf.scl_pullup_en = 1;
  28. ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode));
  29. ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &conf));
  30. return ESP_OK;
  31. }
  32. static esp_err_t read_value(uint8_t register_addr, uint8_t* value_read) {
  33. esp_err_t ret;
  34. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  35. i2c_master_start(cmd);
  36. i2c_master_write_byte(cmd, DS3231_ADDRESS << 1 | I2C_MASTER_WRITE, 0x01);
  37. i2c_master_write_byte(cmd, register_addr, 0x01);
  38. i2c_master_stop(cmd);
  39. ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
  40. i2c_cmd_link_delete(cmd);
  41. if(ret != ESP_OK) {
  42. return ret;
  43. }
  44. cmd = i2c_cmd_link_create();
  45. i2c_master_start(cmd);
  46. i2c_master_write_byte(cmd, 0x68 << 1 | I2C_MASTER_READ, 0x01);
  47. i2c_master_read(cmd, value_read, 1, 0x02);
  48. i2c_master_stop(cmd);
  49. i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
  50. i2c_cmd_link_delete(cmd);
  51. return ret;
  52. }
  53. static esp_err_t write_value(uint8_t register_addr, uint8_t write_value) {
  54. esp_err_t ret;
  55. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  56. i2c_master_start(cmd);
  57. i2c_master_write_byte(cmd, DS3231_ADDRESS << 1 | I2C_MASTER_WRITE, 0x01);
  58. i2c_master_write_byte(cmd, register_addr, 0x01);
  59. i2c_master_write_byte(cmd, write_value, 0x01);
  60. i2c_master_stop(cmd);
  61. ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
  62. i2c_cmd_link_delete(cmd);
  63. return ret;
  64. }
  65. static uint8_t convert_12h_to_24h(uint8_t value_read) {
  66. uint8_t cleaned_value = value_read & 0xBF;
  67. if (value_read & 0x10) {
  68. cleaned_value = cleaned_value & 0XDF;
  69. return bcd_first_digit_value_bin(cleaned_value)
  70. + 12 + bcd_last_digit_value_bin(cleaned_value);
  71. }
  72. return bcd_first_digit_value_bin(cleaned_value)
  73. + bcd_last_digit_value_bin(cleaned_value);
  74. }
  75. static esp_err_t read_hour_value(uint8_t* hour_value) {
  76. uint8_t value_read;
  77. esp_err_t ret = read_value(HOURS_REGISTER_ADDRESS, &value_read);
  78. if(ret != ESP_OK) {
  79. return ret;
  80. }
  81. if (value_read & 0x20) {
  82. *hour_value = convert_12h_to_24h(value_read);
  83. } else {
  84. *hour_value = bcd2bin(value_read);
  85. }
  86. return ret;
  87. }
  88. static esp_err_t read_month_value(uint8_t* month_value) {
  89. uint8_t value_read;
  90. esp_err_t ret = read_value(MONTH_REGISTER_ADDRESS, &value_read);
  91. if (ret == ESP_OK) {
  92. *month_value = bcd2bin(value_read & 0x7F);
  93. }
  94. return ret;
  95. }
  96. esp_err_t ds3231_read_date_time(struct tm* date_time) {
  97. esp_err_t ret;
  98. uint8_t seconds, minutes, hours, dow, day, month, year;
  99. ret = read_value(SECONDS_REGISTER_ADDRESS, &seconds);
  100. ret &= read_value(MINUTES_REGISTER_ADDRESS, &minutes);
  101. ret &= read_hour_value(&hours);
  102. ret &= read_value(DOW_REGISTER_ADDRESS, &dow);
  103. ret &= read_value(DAY_REGISTER_ADDRESS, &day);
  104. ret &= read_month_value(&month);
  105. ret &= read_value(YEAR_REGISTER_ADDRESS, &year);
  106. date_time->tm_sec = bcd2bin(seconds);
  107. date_time->tm_min = bcd2bin(minutes);
  108. date_time->tm_hour = hours;
  109. date_time->tm_wday = bcd2bin(dow);
  110. date_time->tm_mday = bcd2bin(day);
  111. date_time->tm_mon = month;
  112. date_time->tm_year = bcd2bin(year);
  113. return ret;
  114. }
  115. esp_err_t ds3231_write_date_time(struct tm date_time) {
  116. esp_err_t ret;
  117. ret = write_value(SECONDS_REGISTER_ADDRESS, bin2bcd(date_time.tm_sec));
  118. ret &= write_value(MINUTES_REGISTER_ADDRESS, bin2bcd(date_time.tm_min));
  119. ret &= write_value(HOURS_REGISTER_ADDRESS, bin2bcd(date_time.tm_hour));
  120. ret &= write_value(DOW_REGISTER_ADDRESS, bin2bcd(date_time.tm_wday));
  121. ret &= write_value(DAY_REGISTER_ADDRESS, bin2bcd(date_time.tm_mday));
  122. ret &= write_value(MONTH_REGISTER_ADDRESS, bin2bcd(date_time.tm_mon) | 0x80);
  123. ret &= write_value(YEAR_REGISTER_ADDRESS, bin2bcd(date_time.tm_year));
  124. ret &= write_value(YEAR_REGISTER_ADDRESS, bin2bcd(date_time.tm_year));
  125. return ret;
  126. }