-
Notifications
You must be signed in to change notification settings - Fork 0
/
MicroMag.cpp
158 lines (132 loc) · 3.38 KB
/
MicroMag.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
#include <MicroMag.h>
MicroMag::MicroMag(uint8_t ssPin, uint8_t drdyPin, uint8_t resetPin,
uint8_t axes)
: _axes(axes), _ssPin(ssPin), _drdyPin(drdyPin), _resetPin(resetPin)
{
;
}
MicroMag::MicroMag(const MicroMag& mm) \
: _axes(mm._axes), _ssPin(mm._ssPin), _drdyPin(mm._drdyPin), _resetPin(mm._resetPin)
{
;
}
uint8_t MicroMag::begin(void) const
{
pinMode(_ssPin, OUTPUT);
if (_drdyPin != 0xff)
pinMode(_drdyPin, INPUT);
pinMode(_resetPin, OUTPUT);
digitalWrite(_ssPin, HIGH);
digitalWrite(_resetPin, LOW);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV32);
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
// Make one reading to switch device into low power mode
int16_t tmp;
return read(0, MM_PERIOD_32, tmp);
}
uint8_t MicroMag::convert(uint8_t axis, uint8_t period) const
{
if (axis > _axes)
return errorBadAxis;
if (period > MM_PERIOD_4096)
return errorBadPeriod;
uint8_t cmd = 0;
cmd |= (axis + 1);
cmd |= (period << 4);
// Select the device
digitalWrite(_ssPin, LOW);
// Reset the device
digitalWrite(_resetPin, HIGH);
delayMicroseconds(1);
digitalWrite(_resetPin, LOW);
// Send the command byte
SPI.transfer(cmd);
// De-select the device
digitalWrite(_ssPin, HIGH);
return errorNoError;
}
int16_t MicroMag::getResult(void) const
{
// Select the device
digitalWrite(_ssPin, LOW);
// Read 2 bytes
int16_t result = SPI.transfer(0);
result <<= 8;
result |= SPI.transfer(0);
// De-select the device
digitalWrite(_ssPin, HIGH);
return result;
}
uint8_t MicroMag::read(uint8_t axis, uint8_t period, int16_t& result,
uint16_t timeout_us) const
{
int8_t ret = convert(axis, period);
if (ret != errorNoError)
return ret;
// Wait for ready signal
if (timeout_us == 0)
// Set a default timeout which is approriate for the selected
// period. See data sheet for details. Values used are 1us larger
// to account for +/-1 jitter.
switch (period) {
case MM_PERIOD_32:
timeout_us = 501;
break;
case MM_PERIOD_64:
timeout_us = 1001;
break;
case MM_PERIOD_128:
timeout_us = 2001;
break;
case MM_PERIOD_256:
timeout_us = 4001;
break;
case MM_PERIOD_512:
timeout_us = 7501;
break;
case MM_PERIOD_1024:
timeout_us = 15001;
break;
case MM_PERIOD_2048:
timeout_us = 35501;
break;
case MM_PERIOD_4096:
timeout_us = 60001;
break;
default:
return errorBadPeriod;
}
if (_drdyPin == 0xff)
// Cannot monitor if device is ready just so wait
delayMicroseconds(timeout_us);
else {
// Wait until device reports it is ready, or timeout is reached
unsigned long t = micros();
while (!digitalRead(_drdyPin)) {
if (micros() - t > timeout_us)
return errorTimeout;
}
}
result = getResult();
return errorNoError;
}
uint8_t MicroMag::readHighPrec(uint8_t axis, int32_t& result,
uint16_t timeout_us) const
{
int8_t r;
int16_t dLow, dHigh;
if ((r = read(axis, MM_PERIOD_2048, dLow, timeout_us)) != 0)
return r;
if ((r = read(axis, MM_PERIOD_4096, dHigh, timeout_us)) != 0)
return r;
if (dLow <= -16000 || dLow >= 16000) {
// Assume high precision version has overflowed
result = dLow * 2;
result += (dHigh % 2); // try to keep extra precision
}
else
result = dHigh;
return errorNoError;
}