ds3231.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <esp_err.h>
  8. #include <time.h>
  9. #include "bcd.h"
  10. #define DS3231_ADDRESS 0x68
  11. #define CLOCK_SECONDS_REGISTER_ADDRESS 0x00
  12. #define CLOCK_MINUTES_REGISTER_ADDRESS 0x01
  13. #define CLOCK_HOURS_REGISTER_ADDRESS 0x02
  14. #define CLOCK_DOW_REGISTER_ADDRESS 0x03
  15. #define CLOCK_DAY_REGISTER_ADDRESS 0x04
  16. #define CLOCK_MONTH_REGISTER_ADDRESS 0x05
  17. #define CLOCK_YEAR_REGISTER_ADDRESS 0x06
  18. #define ALRM1_SECONDS_REGISTER_ADDRESS 0x07
  19. #define ALRM1_MINUTES_REGISTER_ADDRESS 0x08
  20. #define ALRM1_HOURS_REGISTER_ADDRESS 0x09
  21. #define ALRM1_DAY_DATE_REGISTER_ADDRESS 0x0A
  22. #define CONTROL_REGISTER 0x0E
  23. #define STATUS_REGISTER 0x0F
  24. esp_err_t ds3231_init(int sda_pin, int scl_pin) {
  25. int i2c_master_port = I2C_NUM_0;
  26. i2c_config_t conf;
  27. conf.mode = I2C_MODE_MASTER;
  28. conf.sda_io_num = sda_pin;
  29. conf.sda_pullup_en = 1;
  30. conf.scl_io_num = scl_pin;
  31. conf.scl_pullup_en = 1;
  32. ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode));
  33. ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &conf));
  34. return ESP_OK;
  35. }
  36. static esp_err_t read_value(uint8_t register_addr, uint8_t *value_read) {
  37. esp_err_t ret;
  38. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  39. i2c_master_start(cmd);
  40. i2c_master_write_byte(cmd, DS3231_ADDRESS << 1 | I2C_MASTER_WRITE, 0x01);
  41. i2c_master_write_byte(cmd, register_addr, 0x01);
  42. i2c_master_stop(cmd);
  43. ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
  44. i2c_cmd_link_delete(cmd);
  45. if (ret != ESP_OK) {
  46. return ret;
  47. }
  48. cmd = i2c_cmd_link_create();
  49. i2c_master_start(cmd);
  50. i2c_master_write_byte(cmd, 0x68 << 1 | I2C_MASTER_READ, 0x01);
  51. i2c_master_read(cmd, value_read, 1, 0x02);
  52. i2c_master_stop(cmd);
  53. i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
  54. i2c_cmd_link_delete(cmd);
  55. return ret;
  56. }
  57. static esp_err_t write_value(uint8_t register_addr, uint8_t write_value) {
  58. esp_err_t ret;
  59. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  60. i2c_master_start(cmd);
  61. i2c_master_write_byte(cmd, DS3231_ADDRESS << 1 | I2C_MASTER_WRITE, 0x01);
  62. i2c_master_write_byte(cmd, register_addr, 0x01);
  63. i2c_master_write_byte(cmd, write_value, 0x01);
  64. i2c_master_stop(cmd);
  65. ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
  66. i2c_cmd_link_delete(cmd);
  67. return ret;
  68. }
  69. static uint8_t convert_12h_to_24h(uint8_t value_read) {
  70. uint8_t cleaned_value = value_read & 0xBF;
  71. if (value_read & 0x10) {
  72. cleaned_value = cleaned_value & 0XDF;
  73. return bcd_first_digit_value_bin(cleaned_value)
  74. + 12 + bcd_last_digit_value_bin(cleaned_value);
  75. }
  76. return bcd_first_digit_value_bin(cleaned_value)
  77. + bcd_last_digit_value_bin(cleaned_value);
  78. }
  79. static esp_err_t read_hour_value(uint8_t *hour_value) {
  80. uint8_t value_read;
  81. esp_err_t ret = read_value(CLOCK_HOURS_REGISTER_ADDRESS, &value_read);
  82. if (ret != ESP_OK) {
  83. return ret;
  84. }
  85. if (value_read & 0x20) {
  86. *hour_value = convert_12h_to_24h(value_read);
  87. } else {
  88. *hour_value = bcd2bin(value_read);
  89. }
  90. return ret;
  91. }
  92. static esp_err_t read_month_value(uint8_t *month_value) {
  93. uint8_t value_read;
  94. esp_err_t ret = read_value(CLOCK_MONTH_REGISTER_ADDRESS, &value_read);
  95. if (ret == ESP_OK) {
  96. *month_value = bcd2bin(value_read & 0x7F);
  97. }
  98. return ret;
  99. }
  100. esp_err_t ds3231_read_date_time(struct tm *date_time) {
  101. esp_err_t ret;
  102. uint8_t seconds, minutes, hours, dow, day, month, year;
  103. ret = read_value(CLOCK_SECONDS_REGISTER_ADDRESS, &seconds);
  104. ret |= read_value(CLOCK_MINUTES_REGISTER_ADDRESS, &minutes);
  105. ret |= read_hour_value(&hours);
  106. ret |= read_value(CLOCK_DOW_REGISTER_ADDRESS, &dow);
  107. ret |= read_value(CLOCK_DAY_REGISTER_ADDRESS, &day);
  108. ret |= read_month_value(&month);
  109. ret |= read_value(CLOCK_YEAR_REGISTER_ADDRESS, &year);
  110. date_time->tm_sec = bcd2bin(seconds);
  111. date_time->tm_min = bcd2bin(minutes);
  112. date_time->tm_hour = hours;
  113. date_time->tm_wday = bcd2bin(dow);
  114. date_time->tm_mday = bcd2bin(day);
  115. date_time->tm_mon = month;
  116. date_time->tm_year = bcd2bin(year);
  117. return ret;
  118. }
  119. esp_err_t ds3231_write_date_time(struct tm date_time) {
  120. esp_err_t ret;
  121. ret = write_value(CLOCK_SECONDS_REGISTER_ADDRESS, bin2bcd(date_time.tm_sec));
  122. ret |= write_value(CLOCK_MINUTES_REGISTER_ADDRESS, bin2bcd(date_time.tm_min));
  123. ret |= write_value(CLOCK_HOURS_REGISTER_ADDRESS, bin2bcd(date_time.tm_hour));
  124. ret |= write_value(CLOCK_DOW_REGISTER_ADDRESS, bin2bcd(date_time.tm_wday));
  125. ret |= write_value(CLOCK_DAY_REGISTER_ADDRESS, bin2bcd(date_time.tm_mday));
  126. ret |= write_value(CLOCK_MONTH_REGISTER_ADDRESS, bin2bcd(date_time.tm_mon) | 0x80);
  127. ret |= write_value(CLOCK_YEAR_REGISTER_ADDRESS, bin2bcd(date_time.tm_year));
  128. return ret;
  129. }
  130. esp_err_t ds3231_set_alrm1(struct tm date_time, uint16_t mode) {
  131. esp_err_t ret;
  132. ret = write_value(ALRM1_SECONDS_REGISTER_ADDRESS, bin2bcd(date_time.tm_sec) | ((mode & 0x01) << 7));
  133. ret |= write_value(ALRM1_MINUTES_REGISTER_ADDRESS, bin2bcd(date_time.tm_min) | ((mode & 0x02) << 6));
  134. ret |= write_value(ALRM1_HOURS_REGISTER_ADDRESS, bin2bcd(date_time.tm_hour) | ((mode & 0x04) << 5));
  135. ret |= write_value(ALRM1_DAY_DATE_REGISTER_ADDRESS, bin2bcd(date_time.tm_mday) | ((mode & 0x08) << 4));
  136. return ret;
  137. }
  138. esp_err_t ds3231_enable_arlm1_interrupt() {
  139. return write_value(CONTROL_REGISTER, 0x1D);
  140. }
  141. esp_err_t ds3231_acknowledge_alrm1() {
  142. esp_err_t ret;
  143. uint8_t register_value;
  144. ret = read_value(STATUS_REGISTER, &register_value);
  145. register_value ^= 0x01;
  146. ret |= write_value(STATUS_REGISTER, register_value);
  147. return ret;
  148. }
  149. esp_err_t ds3231_disable_32khz_output() {
  150. esp_err_t ret;
  151. uint8_t register_value;
  152. ret = read_value(STATUS_REGISTER, &register_value);
  153. register_value ^= 0x08;
  154. ret |= write_value(STATUS_REGISTER, register_value);
  155. return ret;
  156. }