-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCP79410.cpp
84 lines (75 loc) · 1.76 KB
/
MCP79410.cpp
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
#include <Wire.h>
#include "MCP79410.h"
#include "utility.h"
extern int init_status;
size_t readRTC(uint8_t address, uint8_t *buffer, size_t size)
{
if (init_status & STATUS_ERTC) {
return 0;
}
Wire.beginTransmission(MCP79410_ADDR_RTC);
Wire.write(address);
Wire.endTransmission(false);
Wire.requestFrom(MCP79410_ADDR_RTC, size);
return Wire.readBytes(buffer, size);
}
uint8_t writeRTC(uint8_t address, uint8_t *buffer, size_t size)
{
if (init_status & STATUS_ERTC) {
return 0;
}
Wire.beginTransmission(MCP79410_ADDR_RTC);
Wire.write(address);
Wire.write(buffer, size);
return Wire.endTransmission();
}
int readRTC_trim()
{
uint8_t data;
readRTC(MCP79410_OSCTRIM, &data, 1);
int trim = data & 0x7F;
if (~data & 0x80) {
trim = -trim;
}
return trim;
}
void writeRTC_trim(int trim)
{
uint8_t data;
if (trim < 0) {
data = -trim & 0x7F;
} else {
data = (trim & 0x7F) | 0x80;
}
writeRTC(MCP79410_OSCTRIM, &data, 1);
}
void trimRTC(int temp)
{
// measured with 10p caps @ 26.5°C
// compensated to 25°C
const int base_error = 26;
int temp_error = clock_error(temp);
writeRTC_trim(base_error - temp_error);
}
size_t readEEPROM(uint8_t address, uint8_t *buffer, size_t size)
{
if (init_status & STATUS_ERTC) {
return 0;
}
Wire.beginTransmission(MCP79410_ADDR_EEPROM);
Wire.write(address);
Wire.endTransmission(false);
Wire.requestFrom(MCP79410_ADDR_EEPROM, size);
return Wire.readBytes(buffer, size);
}
// writes only 1 byte or whole page (8 bytes)
uint8_t writeEEPROM(uint8_t address, uint8_t *buffer, size_t size)
{
if (init_status & STATUS_ERTC) {
return 0;
}
Wire.beginTransmission(MCP79410_ADDR_EEPROM);
Wire.write(address);
Wire.write(buffer, size);
return Wire.endTransmission();
}