M5Unified
AK8963_Class.cpp
Go to the documentation of this file.
1 // Copyright (c) M5Stack. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 
4 #if defined (ESP_PLATFORM)
5 
6 #include "AK8963_Class.hpp"
7 #include <freertos/FreeRTOS.h>
8 #include <freertos/task.h>
9 
10 namespace m5
11 {
13  AK8963_Class::AK8963_Class(std::uint8_t i2c_addr, std::uint32_t freq, I2C_Class* i2c)
14  : IMU_Base ( i2c_addr, freq, i2c )
15  {}
16 
17  IMU_Base::imu_spec_t AK8963_Class::begin(I2C_Class* i2c)
18  {
19  if (i2c)
20  {
21  _i2c = i2c;
22  }
23 
24  uint8_t rawData[3]; // x/y/z gyro calibration data stored here
25  writeRegister8(CTRL_CMD_ADDR, POWER_DOWN_CMD); // Power down magnetometer
26  vTaskDelay(10);
27  writeRegister8(CTRL_CMD_ADDR, FUSE_ROM_CMD); // Enter Fuse ROM access mode
28  vTaskDelay(10);
29  readRegister(ASAX_ADDR, rawData, 3); // Read the x-, y-, and z-axis calibration values
30 
31  writeRegister8(CTRL_CMD_ADDR, POWER_DOWN_CMD); // Power down magnetometer
32  vTaskDelay(10);
33  // Configure the magnetometer for continuous read and highest resolution
34  // set Mscale bit 4 to 1 (0) to enable 16 (14) bit resolution in CNTL
35  // register, and enable continuous mode data acquisition Mmode (bits [3:0]),
36  // 0010 for 8 Hz and 0110 for 100 Hz sample rates
37  writeRegister8(CTRL_CMD_ADDR, 1 << 4 | 0x06); // Power down magnetometer
38 
39  WhoAmI();
40 // printf("AK8963 : %02x\n", WhoAmI());
41  if (WhoAmI() == 0x48)
42  {
43  _init = true;
44  return imu_spec_mag;
45  }
46  return imu_spec_none;
47  }
48 
49  std::uint8_t AK8963_Class::WhoAmI(void)
50  {
51  return readRegister8(WHO_AM_I_ADDR);
52  }
53 
54  IMU_Base::imu_spec_t AK8963_Class::getImuRawData(imu_raw_data_t* data) const
55  {
56  union
57  {
58  std::uint8_t buf8[10];
59  std::int16_t buf16[5];
60  };
61  bool res = readRegister(STATUS1_ADDR, &buf8[1], 8) && buf8[1] && (0 == (buf8[8] & 0x08));
62  if (res)
63  {
64  data->mag.x = buf16[1];
65  data->mag.y = buf16[2];
66  data->mag.z = buf16[3];
67  return imu_spec_mag;
68  }
69  return imu_spec_none;
70  }
71 
72  void AK8963_Class::getConvertParam(imu_convert_param_t* param) const
73  { // Proper scale to return milliGauss
74  param->mag_res = 10.0f * 4912.0f / 32760.0f;
75  }
76 }
77 
78 #endif
AK8963_Class(std::uint8_t i2c_addr=DEFAULT_ADDRESS, std::uint32_t freq=400000, I2C_Class *i2c=&In_I2C)
virtual ~AK8963_Class()
Definition: M5Unified.cpp:48