summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/mpu9150/ak8975.cxx
blob: 7330eb3717e04cbcf431bf98da471a8cafe43543 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <unistd.h>
#include <iostream>
#include <string>
#include <stdexcept>

#include "ak8975.h"

using namespace upm;
using namespace std;


AK8975::AK8975(int bus, uint8_t address):
  m_i2c(bus)
{
  m_addr = address;
  m_xCoeff = 0.0;
  m_yCoeff = 0.0;
  m_zCoeff = 0.0;

  mraa::Result rv;
  if ( (rv = m_i2c.address(m_addr)) != mraa::SUCCESS)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": I2c.address() failed");
      return;
    }
}

AK8975::~AK8975()
{
}

bool AK8975::init()
{
  // we put the device in 'fuse mode', and then read the compensation
  // coefficients and store them.

  // first, set power down mode

  if (!setMode(CNTL_PWRDWN))
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": Unable to set PWRDWN mode");
      return false;
    }

  if (!setMode(CNTL_FUSE_ACCESS))
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": Unable to set FUSE mode");
      return false;
    }

  // Read each byte and store
  m_xCoeff = (float)m_i2c.readReg(REG_ASAX);
  m_yCoeff = (float)m_i2c.readReg(REG_ASAY);
  m_zCoeff = (float)m_i2c.readReg(REG_ASAZ);

  // now, place back in power down mode
  if (!setMode(CNTL_PWRDWN))
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": Unable to set reset PWRDWN mode");
      return false;
    }

  return true;
}

bool AK8975::setMode(CNTL_MODES_T mode)
{
  mraa::Result rv;
  if ((rv = m_i2c.writeReg(REG_CNTL, mode)) != mraa::SUCCESS)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": I2c.writeReg() failed");
      return false;
    } 

  // sleep at least 100us for for mode transition to complete
  usleep(150);

  return true;
}

bool AK8975::isReady()
{
  uint8_t rdy = m_i2c.readReg(REG_ST1);
  
  if (rdy & ST1_DRDY)
    return true;

  return false;
}

bool AK8975::waitforDeviceReady()
{
  const int maxRetries = 20;

  int retries = 0;

  while (retries < maxRetries)
    {
      if (isReady())
        return true;

      usleep(5000);
      retries++;
    }
  
  throw std::runtime_error(std::string(__FUNCTION__) +
                           ": timeout waiting for device to become ready");

  return false;
}

bool AK8975::update(bool selfTest)
{
  // this flag (selfTest) is used so that we can read values without
  // specifically taking a measurement.  For example, selfTest will
  // pass true to this method so that the test results aren't
  // overwritten by a measurement.
  if (!selfTest)
    {
      // First set measurement mode (take a measurement)
      if (!setMode(CNTL_MEASURE))
        {
          throw std::runtime_error(std::string(__FUNCTION__) +
                                   ": Unable to set MEASURE mode");
          return false;
        }
    }

  if (!waitforDeviceReady())
    return false;

  // hope it worked.  Now read out the values and store them (uncompensated)
  uint8_t data[6];
  m_i2c.readBytesReg(REG_HXL, data, 6);

  int16_t x, y, z;
  x = ( (data[1] << 8) | data[0] );
  y = ( (data[3] << 8) | data[2] );
  z = ( (data[5] << 8) | data[4] );

  m_xData = float(x);
  m_yData = float(y);
  m_zData = float(z);

  return true;
}

bool AK8975::selfTest()
{
  mraa::Result rv;

  // set power down first
  if (!setMode(CNTL_PWRDWN))
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": Unable to set PWRDWN mode");
      return false;
    }

  // enable self test bit
  if ((rv = m_i2c.writeReg(REG_ASTC, ASTC_SELF)) != mraa::SUCCESS)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": failed to enable self test");
      return false;
    } 
  
  // now set self test mode
  if (!setMode(CNTL_SELFTEST))
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": Unable to set SELFTEST mode");
      return false;
    }
  
  // now update current data (without enabling a measurement)
  update(true);
  
  // Now, reset self test register
  uint8_t reg = m_i2c.readReg(REG_ASTC);
  reg &= ~ASTC_SELF;
  if ((rv = m_i2c.writeReg(REG_ASTC, reg)) != mraa::SUCCESS)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": failed to disable self test");
      return false;
    } 

  // after self-test measurement, device transitions to power down mode
  return true;
}

float AK8975::adjustValue(float value, float adj)
{
  // apply the proper compensation to value.  This equation is taken
  // from the AK8975 datasheet, section 8.3.11

  return ( value * ((((adj - 128.0) * 0.5) / 128.0) + 1.0) );
} 

void AK8975::getMagnetometer(float *x, float *y, float *z)
{
  if (x)
    *x = adjustValue(m_xData, m_xCoeff);
  if (y)
    *y = adjustValue(m_yData, m_yCoeff);
  if (z)
    *z = adjustValue(m_zData, m_zCoeff);
}