|
| 1 | +#include "RTC_RX8130CE.h" |
| 2 | +#include "RTClib.h" |
| 3 | + |
| 4 | + |
| 5 | +bool RTC_RX8130CE::stop(bool stop) { |
| 6 | + write_register(0x1E, stop ? 0x040 : 0x00); |
| 7 | + return true; |
| 8 | +} |
| 9 | + |
| 10 | +bool RTC_RX8130CE::begin(TwoWire *wire) { |
| 11 | + if (i2c_dev) { |
| 12 | + delete i2c_dev; |
| 13 | + } |
| 14 | + |
| 15 | + i2c_dev = new Adafruit_I2CDevice(this->_addr, wire); |
| 16 | + if (!i2c_dev->begin()) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + |
| 20 | + /* |
| 21 | + * Digital offset register: |
| 22 | + * [7] DET: 0 -> disabled |
| 23 | + * [6:0] L7-L1: 0 -> no offset |
| 24 | + */ |
| 25 | + write_register(0x30, 0x00); |
| 26 | + |
| 27 | + /* |
| 28 | + * Extension Register register: |
| 29 | + * [7:6] FSEL: 0 -> 0 |
| 30 | + * [5] USEL: 0 -> 0 |
| 31 | + * [4] TE: 0 -> |
| 32 | + * [3] WADA: 0 -> 0 |
| 33 | + * [2-0] TSEL: 0 -> 0 |
| 34 | + */ |
| 35 | + write_register(0x1C, 0x00); |
| 36 | + |
| 37 | + /* |
| 38 | + * Flag Register register: |
| 39 | + * [7] VBLF: 0 -> 0 |
| 40 | + * [6] 0: 0 -> |
| 41 | + * [5] UF: 0 -> |
| 42 | + * [4] TF: 0 -> |
| 43 | + * [3] AF: 0 -> 0 |
| 44 | + * [2] RSF: 0 -> 0 |
| 45 | + * [1] VLF: 0 -> 0 |
| 46 | + * [0] VBFF: 0 -> 0 |
| 47 | + */ |
| 48 | + write_register(0x1D, 0x00); |
| 49 | + |
| 50 | + /* |
| 51 | + * Control Register0 register: |
| 52 | + * [7] TEST: 0 -> 0 |
| 53 | + * [6] STOP: 0 -> |
| 54 | + * [5] UIE: 0 -> |
| 55 | + * [4] TIE: 0 -> |
| 56 | + * [3] AIE: 0 -> 0 |
| 57 | + * [2] TSTP: 0 -> 0 |
| 58 | + * [1] TBKON: 0 -> 0 |
| 59 | + * [0] TBKE: 0 -> 0 |
| 60 | + */ |
| 61 | + write_register(0x1E, 0x00); |
| 62 | + |
| 63 | + /* |
| 64 | + * Control Register1 register: |
| 65 | + * [7-6] SMPTSEL: 0 -> 0 |
| 66 | + * [5] CHGEN: 0 -> |
| 67 | + * [4] INIEN: 0 -> |
| 68 | + * [3] 0: 0 -> |
| 69 | + * [2] RSVSEL: 0 -> 0 |
| 70 | + * [1-0] BFVSEL: 0 -> 0 |
| 71 | + */ |
| 72 | + write_register(0x1F, 0x00); |
| 73 | + |
| 74 | + this->stop(false); // clear STOP bit |
| 75 | + |
| 76 | + /* |
| 77 | + * Function register: |
| 78 | + * [7] 100TH: 0 -> disabled |
| 79 | + * [6:5] Periodic interrupt: 0 -> no periodic interrupt |
| 80 | + * [4] RTCM: 0 -> real-time clock mode |
| 81 | + * [3] STOPM: 0 -> RTC stop is controlled by STOP bit only |
| 82 | + * [2:0] Clock output frequency: 000 (Default value) |
| 83 | + */ |
| 84 | + write_register(0x28, 0x00); |
| 85 | + |
| 86 | + // Battery switch register |
| 87 | + write_register(0x26, 0x00); // enable battery switch feature |
| 88 | + |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | +bool RTC_RX8130CE::setTime(struct tm *t) { |
| 93 | + uint8_t buf[8]; |
| 94 | + buf[0] = 0x10; |
| 95 | + buf[1] = bin2bcd(t->tm_sec) & 0x7F; |
| 96 | + buf[2] = bin2bcd(t->tm_min) & 0x7F; |
| 97 | + buf[3] = bin2bcd(t->tm_hour) & 0x3F; |
| 98 | + buf[4] = bin2bcd(t->tm_wday) & 0x07; |
| 99 | + buf[5] = bin2bcd(t->tm_mday) & 0x3F; |
| 100 | + buf[6] = bin2bcd(t->tm_mon + 1) & 0x1F; |
| 101 | + buf[7] = bin2bcd((t->tm_year - 100)); |
| 102 | + |
| 103 | + this->stop(true); |
| 104 | + i2c_dev->write(buf, sizeof(buf)); |
| 105 | + this->stop(false); |
| 106 | + |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +void RTC_RX8130CE::adjust(DateTime dt) { |
| 111 | + struct tm *atv; |
| 112 | + time_t utime; |
| 113 | + |
| 114 | + utime = (time_t)dt.unixtime(); |
| 115 | + atv = gmtime(&utime); |
| 116 | + |
| 117 | + this->setTime(atv); |
| 118 | +} |
| 119 | + |
| 120 | +DateTime RTC_RX8130CE::now() { |
| 121 | + struct tm atv; |
| 122 | + this->getTime(&atv); |
| 123 | + |
| 124 | + return DateTime((uint32_t)mktime(&atv)); |
| 125 | +} |
| 126 | + |
| 127 | +uint32_t RTC_RX8130CE::unixtime() { |
| 128 | + struct tm atv; |
| 129 | + this->getTime(&atv); |
| 130 | + |
| 131 | + return (uint32_t)mktime(&atv); |
| 132 | +} |
| 133 | + |
| 134 | +bool RTC_RX8130CE::getTime(struct tm *t) { |
| 135 | + uint8_t buff[7]; |
| 136 | + |
| 137 | + buff[0] = 0x10; |
| 138 | + |
| 139 | + i2c_dev->write_then_read(buff, 1, buff, 7); |
| 140 | + |
| 141 | + t->tm_sec = bcd2bin(buff[0] & 0x7F); |
| 142 | + t->tm_min = bcd2bin(buff[1] & 0x7F); |
| 143 | + t->tm_hour = bcd2bin(buff[2] & 0x3F); |
| 144 | + t->tm_wday = bcd2bin(buff[3] & 0x07); |
| 145 | + t->tm_mday = bcd2bin(buff[4] & 0x3F); |
| 146 | + t->tm_mon = bcd2bin(buff[5] & 0x1F) - 1; |
| 147 | + t->tm_year = bcd2bin(buff[6]) + 100; |
| 148 | + |
| 149 | + return true; |
| 150 | +} |
| 151 | + |
| 152 | +bool RTC_RX8130CE::writeRAM(uint8_t address, uint8_t value) { |
| 153 | + return this->writeRAM(address, &value, 1); |
| 154 | +} |
| 155 | + |
| 156 | +size_t RTC_RX8130CE::writeRAM(uint8_t address, uint8_t *value, size_t len) { |
| 157 | + uint8_t buf[len + 1]; |
| 158 | + |
| 159 | + if (address > 3) { |
| 160 | + return 0; |
| 161 | + } |
| 162 | + |
| 163 | + if ((address + len) > 3) { |
| 164 | + len = 3 - address; |
| 165 | + } |
| 166 | + |
| 167 | + buf[0] = 0x20 + address; |
| 168 | + |
| 169 | + for (int i = 1; i <= len + 1; i++) { |
| 170 | + buf[i] = value[i - 1]; |
| 171 | + } |
| 172 | + |
| 173 | + i2c_dev->write(buf, len + 1); |
| 174 | + |
| 175 | + return len; |
| 176 | +} |
| 177 | + |
| 178 | +bool RTC_RX8130CE::readRAM(uint8_t address, uint8_t *value, size_t len) { |
| 179 | + uint8_t real_address = 0x20 + address; |
| 180 | + |
| 181 | + if (address > 3) { // Oversize of 64-bytes RAM |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + if ((address + len) > 3) { // Data size over RAM size |
| 186 | + len = 3 - address; |
| 187 | + } |
| 188 | + |
| 189 | + i2c_dev->write_then_read(&real_address, 1, value, len); |
| 190 | + return true; |
| 191 | +} |
| 192 | + |
| 193 | +uint8_t RTC_RX8130CE::readRAM(uint8_t address) { |
| 194 | + uint8_t value = 0xFF; |
| 195 | + this->readRAM(address, &value, 1); |
| 196 | + return value; |
| 197 | +} |
0 commit comments