Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.1 KiB

  1. /*
  2. ds3231 support module
  3. Copyright (C) 2018 by Pavel Chauzov <poulch at mail dot ru>
  4. */
  5. #pragma once
  6. #include <TimeLib.h>
  7. #define DS3231ADDR 0x68
  8. #define _bcdToDec(val) ((uint8_t) ((val / 16 * 10) + (val % 16)))
  9. #define _decToBcd(val) ((uint8_t) ((val / 10 * 16) + (val % 10)))
  10. time_t rtcGetTime() {
  11. uint8_t data[7];
  12. tmElements_t tm;
  13. i2c_write_uint8(DS3231ADDR,0);
  14. i2c_read_buffer(DS3231ADDR, data, 7);
  15. tm.Second = _bcdToDec(data[0]);
  16. tm.Minute = _bcdToDec(data[1]);
  17. tm.Hour = _bcdToDec(data[2]);
  18. tm.Wday = _bcdToDec(data[3]);
  19. tm.Day = _bcdToDec(data[4]);
  20. tm.Month = _bcdToDec(data[5]);
  21. tm.Year = y2kYearToTm(_bcdToDec(data[6]));
  22. return makeTime(tm);
  23. }
  24. uint8_t rtcSetTime(time_t nt) {
  25. uint8_t data[8];
  26. tmElements_t ct;
  27. breakTime(nt, ct);
  28. data[0] = 0;
  29. data[1] = _decToBcd(ct.Second);
  30. data[2] = _decToBcd(ct.Minute);
  31. data[3] = _decToBcd(ct.Hour);
  32. data[4] = _decToBcd(ct.Wday);
  33. data[5] = _decToBcd(ct.Day);
  34. data[6] = _decToBcd(ct.Month);
  35. data[7] = _decToBcd(tmYearToY2k(ct.Year));
  36. uint8_t s = i2c_write_buffer(DS3231ADDR, data, 8);
  37. return s;
  38. }