M5Unified
BMM150_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 "BMM150_Class.hpp"
7 #include <freertos/FreeRTOS.h>
8 #include <freertos/task.h>
9 
10 namespace m5
11 {
13  BMM150_Class::BMM150_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 BMM150_Class::begin(I2C_Class* i2c)
18  {
19  if (i2c)
20  {
21  _i2c = i2c;
22  }
23 
24  writeRegister8(0x4B, 0x83); // software reset + power on
25 
26  WhoAmI();
27 // printf("BMM150 : %02x\n", WhoAmI());
28  if (WhoAmI() != 0x32)
29  {
30  return imu_spec_none;
31  }
32 // printf("BMM150 found!\n");
33  writeRegister8(0x4C, 0x38); // normal mode / ODR 30Hz
34 
35  // writeRegister8(CMD_REG_ADDR, SOFT_RESET_CMD); // software reset.
36  // {
37  // int retry = 16;
38  // while (0 == readRegister8(PWR_CONF_ADDR) && --retry);
39  // }
40 
41  // writeRegister8(PWR_CONF_ADDR, 0x00); // Power save disabled.
42  // vTaskDelay(1);
43  // bool res = _upload_file(BMM150_config_file, 0, sizeof(BMM150_config_file));
44  // writeRegister8(INIT_CTRL_ADDR, 0x01);
45 
46  // writeRegister8(PWR_CTRL_ADDR, 0x0F); // temp en | ACC en | GYR en | AUX en
47 
48  // if (res) {
49  // int retry = 16;
50  // do { vTaskDelay(1);}
51  // while (0 == readRegister8(INTERNAL_STATUS_ADDR) && --retry);
52  // res = retry > 0;
53  // }
54  _init = true;
55  return imu_spec_mag;
56  }
57 
58  std::uint8_t BMM150_Class::WhoAmI(void)
59  {
60  return readRegister8(CHIP_ID_ADDR);
61  }
62 
63  IMU_Base::imu_spec_t BMM150_Class::getImuRawData(imu_raw_data_t* data) const
64  {
65  if (readRegister8(0x48) & 1)
66  {
67  std::int16_t buf[3];
68  if (readRegister(0x42, (std::uint8_t*)buf, 6))
69  {
70  data->mag.x = buf[0] >> 2;
71  data->mag.y = buf[1] >> 2;
72  data->mag.z = buf[2] & 0xFFFEu;
73  return imu_spec_mag;
74  }
75  }
76  return imu_spec_none;
77  }
78 
79  void BMM150_Class::getConvertParam(imu_convert_param_t* param) const
80  {
81  param->mag_res = 10.0f * 4912.0f / 32760.0f;
82  // param->temp_offset = 23.0f;
83  // param->temp_res = 1.0f / 512.0f;
84  }
85 }
86 #endif
BMM150_Class(std::uint8_t i2c_addr=DEFAULT_ADDRESS, std::uint32_t freq=400000, I2C_Class *i2c=&In_I2C)
virtual ~BMM150_Class()
Definition: M5Unified.cpp:48