-
Notifications
You must be signed in to change notification settings - Fork 67
/
AS5600.cpp
175 lines (123 loc) · 3.52 KB
/
AS5600.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "./AS5600.h"
AS5600::AS5600(uint8_t address) : _address(address) {};
AS5600::~AS5600() {};
void AS5600::init(TwoWire* wire){
_wire = wire;
_wire->begin();
if (!closeTransactions) {
setAngleRegister();
}
};
void AS5600::setAngleRegister() {
_wire->beginTransmission(_address);
if (useHysteresis)
_wire->write(AS5600_REG_ANGLE);
else
_wire->write(AS5600_REG_ANGLE_RAW);
_wire->endTransmission(false);
}
uint16_t AS5600::angle() {
uint16_t result = 0;
if (closeTransactions) {
setAngleRegister();
}
_wire->requestFrom(_address, (uint8_t)2, (uint8_t)closeTransactions);
result = (_wire->read()&0x0F)<<8;
result |= _wire->read();
return result;
};
uint16_t AS5600::readRawAngle() {
return readRegister(AS5600_REG_ANGLE_RAW, 2) & 0x0FFF;
};
uint16_t AS5600::readAngle() {
return readRegister(AS5600_REG_ANGLE, 2) & 0x0FFF;
};
uint16_t AS5600::readMagnitude() {
return readRegister(AS5600_REG_MAGNITUDE, 2) & 0x0FFF;
};
AS5600Status AS5600::readStatus() {
AS5600Status result;
result.reg = (uint8_t)readRegister(AS5600_REG_STATUS, 1); // TODO: shift bits around
return result;
};
uint8_t AS5600::readAGC() {
return (uint8_t)readRegister(AS5600_REG_AGC, 1);
};
AS5600Conf AS5600::readConf() {
AS5600Conf result;
result.reg = readRegister(AS5600_REG_CONF, 2);
return result;
};
uint16_t AS5600::readMang() {
return readRegister(AS5600_REG_MANG, 2) & 0x0FFF;
};
uint16_t AS5600::readMPos() {
return readRegister(AS5600_REG_MPOS, 2) & 0x0FFF;
};
uint16_t AS5600::readZPos() {
return readRegister(AS5600_REG_ZPOS, 2) & 0x0FFF;
};
uint8_t AS5600::readZMCO() {
return (readRegister(AS5600_REG_ZMCO, 1)&0x03);
};
uint8_t AS5600::readI2CAddr() {
return (readRegister(AS5600_REG_I2CADDR, 1)>>1);
};
// set registers
void AS5600::setConf(AS5600Conf value) {
// TODO: read before write
writeRegister(AS5600_REG_CONF, value.reg);
};
void AS5600::setMang(uint16_t value) {
// TODO: read before write
writeRegister(AS5600_REG_MANG, value);
};
void AS5600::setMPos(uint16_t value) {
// TODO: read before write
writeRegister(AS5600_REG_MPOS, value);
};
void AS5600::setZPos(uint16_t value) {
// TODO: read before write
writeRegister(AS5600_REG_ZPOS, value);
};
void AS5600::setI2CAddr(uint8_t value) {
uint8_t val = (uint8_t)readRegister(AS5600_REG_I2CADDR, 1);
val = (value<<1) | (val&0x01);
writeRegister(AS5600_REG_I2CADDR, val);
};
void AS5600::setI2CUpdt(uint8_t value) {
uint8_t val = (uint8_t)readRegister(AS5600_REG_I2CUPDT, 1);
val = (value<<1) | (val&0x01);
writeRegister(AS5600_REG_I2CUPDT, val);
};
void AS5600::burnSettings(){
writeRegister(AS5600_REG_BURN, 0x40, 1);
}
uint16_t AS5600::readRegister(uint8_t reg, uint8_t len){
uint16_t result = 0;
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->endTransmission(false);
_wire->requestFrom(_address, len, (uint8_t)closeTransactions);
result = _wire->read();
if (len == 2) {
result <<= 8;
result |= _wire->read();
}
if (!closeTransactions) {
setAngleRegister();
}
return result;
};
void AS5600::writeRegister(uint8_t reg, uint16_t val, uint8_t len){
_wire->beginTransmission(_address);
_wire->write(reg);
if (len == 2) {
_wire->write(val>>8);
}
_wire->write(val&0xFF);
_wire->endTransmission(closeTransactions);
if (!closeTransactions) {
setAngleRegister();
}
};