/
bm8563.js
112 lines (104 loc) · 2.8 KB
/
bm8563.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (c) 2023 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
BM8563 -I2C Real-Time-Clock
https://datasheet.lcsc.com/lcsc/2207281130_GATEMODE-BM8563ESA_C269877.pdf
*/
const Register = Object.freeze({
CONTROL_STATUS1: 0x00,
CONTROL_STATUS2: 0x01,
SECONDS: 0x02,
CENTURY_BIT: 0b10000000,
});
class BM8563 {
#io;
#blockBuffer = new Uint8Array(7);
constructor(options) {
const { clock } = options;
const io = (this.#io = new clock.io({
hz: 400_000,
address: 0x51,
...clock,
}));
try {
// Status reset (twice)
io.writeUint8(Register.CONTROL_STATUS1, 0x00);
io.writeUint8(Register.CONTROL_STATUS1, 0x00);
// Status2 reset
io.writeUint8(Register.CONTROL_STATUS2, 0x00);
} catch (e) {
io.close();
throw e;
}
}
close() {
this.#io.close();
this.#io = undefined;
}
configure(options) {}
get configuration() {
return {};
}
get time() {
const io = this.#io;
const reg = this.#blockBuffer;
io.readBuffer(Register.SECONDS, reg);
const century = reg[5] & Register.CENTURY_BIT ? 100 : 0;
// yr, mo, day, hr, min, sec
return Date.UTC(
bcdToDec(reg[6]) + century + 1900,
bcdToDec(reg[5] & 0b00011111),
bcdToDec(reg[3]),
bcdToDec(reg[2] & 0x3f),
bcdToDec(reg[1]),
bcdToDec(reg[0] & 0x7f)
);
}
set time(v) {
let b = this.#blockBuffer;
let now = new Date(v);
let year = now.getUTCFullYear();
b[0] = decToBcd(now.getUTCSeconds());
b[1] = decToBcd(now.getUTCMinutes());
b[2] = decToBcd(now.getUTCHours());
b[3] = decToBcd(now.getUTCDate());
b[4] = decToBcd(now.getUTCDay());
b[5] = decToBcd(now.getUTCMonth());
if (year >= 100) {
b[5] |= Register.CENTURY_BIT;
}
b[6] = decToBcd(year % 100);
this.#io.writeBuffer(Register.SECONDS, b);
}
}
function decToBcd(d) {
let val = d | 0;
let v = (val / 10) | 0;
v *= 16;
v += val % 10;
return v;
}
function bcdToDec(b) {
let v = (b / 16) | 0;
v *= 10;
v += b % 16;
return v;
}
export default BM8563;