AP_InertialSensor_BMI088.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <utility>
  16. #include <AP_HAL/AP_HAL.h>
  17. #include <AP_Math/AP_Math.h>
  18. #include "AP_InertialSensor_BMI088.h"
  19. /*
  20. device registers, names follow datasheet conventions, with REGA_
  21. prefix for accel, and REGG_ prefix for gyro
  22. */
  23. #define REGA_CHIPID 0x00
  24. #define REGA_ERR_REG 0x02
  25. #define REGA_STATUS 0x03
  26. #define REGA_X_LSB 0x12
  27. #define REGA_INT_STATUS_1 0x1D
  28. #define REGA_TEMP_LSB 0x22
  29. #define REGA_TEMP_MSB 0x23
  30. #define REGA_CONF 0x40
  31. #define REGA_RANGE 0x41
  32. #define REGA_PWR_CONF 0x7C
  33. #define REGA_PWR_CTRL 0x7D
  34. #define REGA_SOFTRESET 0x7E
  35. #define REGA_FIFO_CONFIG0 0x48
  36. #define REGA_FIFO_CONFIG1 0x49
  37. #define REGA_FIFO_DOWNS 0x45
  38. #define REGA_FIFO_DATA 0x26
  39. #define REGA_FIFO_LEN0 0x24
  40. #define REGA_FIFO_LEN1 0x25
  41. #define REGG_CHIPID 0x00
  42. #define REGA_RATE_X_LSB 0x02
  43. #define REGG_INT_STATUS_1 0x0A
  44. #define REGG_INT_STATUS_2 0x0B
  45. #define REGG_INT_STATUS_3 0x0C
  46. #define REGG_FIFO_STATUS 0x0E
  47. #define REGG_RANGE 0x0F
  48. #define REGG_BW 0x10
  49. #define REGG_LPM1 0x11
  50. #define REGG_RATE_HBW 0x13
  51. #define REGG_BGW_SOFTRESET 0x14
  52. #define REGG_FIFO_CONFIG_1 0x3E
  53. #define REGG_FIFO_DATA 0x3F
  54. extern const AP_HAL::HAL& hal;
  55. AP_InertialSensor_BMI088::AP_InertialSensor_BMI088(AP_InertialSensor &imu,
  56. AP_HAL::OwnPtr<AP_HAL::Device> _dev_accel,
  57. AP_HAL::OwnPtr<AP_HAL::Device> _dev_gyro,
  58. enum Rotation _rotation)
  59. : AP_InertialSensor_Backend(imu)
  60. , dev_accel(std::move(_dev_accel))
  61. , dev_gyro(std::move(_dev_gyro))
  62. , rotation(_rotation)
  63. {
  64. }
  65. AP_InertialSensor_Backend *
  66. AP_InertialSensor_BMI088::probe(AP_InertialSensor &imu,
  67. AP_HAL::OwnPtr<AP_HAL::Device> dev_accel,
  68. AP_HAL::OwnPtr<AP_HAL::Device> dev_gyro,
  69. enum Rotation rotation)
  70. {
  71. if (!dev_accel || !dev_gyro) {
  72. return nullptr;
  73. }
  74. auto sensor = new AP_InertialSensor_BMI088(imu, std::move(dev_accel), std::move(dev_gyro), rotation);
  75. if (!sensor) {
  76. return nullptr;
  77. }
  78. if (!sensor->init()) {
  79. delete sensor;
  80. return nullptr;
  81. }
  82. return sensor;
  83. }
  84. void AP_InertialSensor_BMI088::start()
  85. {
  86. accel_instance = _imu.register_accel(1600, dev_accel->get_bus_id_devtype(DEVTYPE_INS_BMI088));
  87. gyro_instance = _imu.register_gyro(2000, dev_gyro->get_bus_id_devtype(DEVTYPE_INS_BMI088));
  88. // setup sensor rotations from probe()
  89. set_gyro_orientation(gyro_instance, rotation);
  90. set_accel_orientation(accel_instance, rotation);
  91. // setup callbacks
  92. dev_accel->register_periodic_callback(1000000UL / 1600,
  93. FUNCTOR_BIND_MEMBER(&AP_InertialSensor_BMI088::read_fifo_accel, void));
  94. dev_gyro->register_periodic_callback(1000000UL / 2000,
  95. FUNCTOR_BIND_MEMBER(&AP_InertialSensor_BMI088::read_fifo_gyro, void));
  96. }
  97. /*
  98. read from accelerometer registers, special SPI handling needed
  99. */
  100. bool AP_InertialSensor_BMI088::read_accel_registers(uint8_t reg, uint8_t *data, uint8_t len)
  101. {
  102. // when on I2C we just read normally
  103. if (dev_accel->bus_type() != AP_HAL::Device::BUS_TYPE_SPI) {
  104. return dev_accel->read_registers(reg, data, len);
  105. }
  106. // for SPI we need to discard the first returned byte. See
  107. // datasheet for explanation
  108. uint8_t b[len+2];
  109. b[0] = reg | 0x80;
  110. memset(&b[1], 0, len+1);
  111. if (!dev_accel->transfer(b, len+2, b, len+2)) {
  112. return false;
  113. }
  114. memcpy(data, &b[2], len);
  115. return true;
  116. }
  117. /*
  118. write to accel registers with retries. The SPI sensor may take
  119. several tries to correctly write a register
  120. */
  121. bool AP_InertialSensor_BMI088::write_accel_register(uint8_t reg, uint8_t v)
  122. {
  123. for (uint8_t i=0; i<8; i++) {
  124. dev_accel->write_register(reg, v);
  125. uint8_t v2 = 0;
  126. if (read_accel_registers(reg, &v2, 1) && v2 == v) {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. static const struct {
  133. uint8_t reg;
  134. uint8_t value;
  135. } accel_config[] = {
  136. { REGA_CONF, 0xAC },
  137. // setup 24g range
  138. { REGA_RANGE, 0x03 },
  139. // disable low-power mode
  140. { REGA_PWR_CONF, 0 },
  141. { REGA_PWR_CTRL, 0x04 },
  142. // setup FIFO for streaming X,Y,Z
  143. { REGA_FIFO_CONFIG0, 0x00 },
  144. { REGA_FIFO_CONFIG1, 0x50 },
  145. };
  146. bool AP_InertialSensor_BMI088::setup_accel_config(void)
  147. {
  148. if (done_accel_config) {
  149. return true;
  150. }
  151. accel_config_count++;
  152. for (uint8_t i=0; i<ARRAY_SIZE(accel_config); i++) {
  153. uint8_t v;
  154. if (!read_accel_registers(accel_config[i].reg, &v, 1)) {
  155. return false;
  156. }
  157. if (v == accel_config[i].value) {
  158. continue;
  159. }
  160. if (!write_accel_register(accel_config[i].reg, accel_config[i].value)) {
  161. return false;
  162. }
  163. }
  164. done_accel_config = true;
  165. hal.console->printf("BMI088: accel config OK (%u tries)\n", (unsigned)accel_config_count);
  166. return true;
  167. }
  168. /*
  169. probe and initialise accelerometer
  170. */
  171. bool AP_InertialSensor_BMI088::accel_init()
  172. {
  173. WITH_SEMAPHORE(dev_accel->get_semaphore());
  174. uint8_t v;
  175. // dummy ready on accel ChipID to init accel (see section 3 of datasheet)
  176. read_accel_registers(REGA_CHIPID, &v, 1);
  177. if (!read_accel_registers(REGA_CHIPID, &v, 1) || v != 0x1E) {
  178. return false;
  179. }
  180. if (!setup_accel_config()) {
  181. hal.console->printf("BMI088: delaying accel config\n");
  182. }
  183. hal.console->printf("BMI088: found accel\n");
  184. return true;
  185. }
  186. /*
  187. probe and initialise gyro
  188. */
  189. bool AP_InertialSensor_BMI088::gyro_init()
  190. {
  191. WITH_SEMAPHORE(dev_gyro->get_semaphore());
  192. uint8_t v;
  193. if (!dev_gyro->read_registers(REGG_CHIPID, &v, 1) || v != 0x0F) {
  194. return false;
  195. }
  196. if (!dev_gyro->write_register(REGG_BGW_SOFTRESET, 0xB6)) {
  197. return false;
  198. }
  199. hal.scheduler->delay(10);
  200. dev_gyro->setup_checked_registers(5, 20);
  201. // setup 2000dps range
  202. if (!dev_gyro->write_register(REGG_RANGE, 0x00, true)) {
  203. return false;
  204. }
  205. // setup filter bandwidth 230Hz, no decimation
  206. if (!dev_gyro->write_register(REGG_BW, 0x81, true)) {
  207. return false;
  208. }
  209. // disable low-power mode
  210. if (!dev_gyro->write_register(REGG_LPM1, 0, true)) {
  211. return false;
  212. }
  213. // setup for filtered data
  214. if (!dev_gyro->write_register(REGG_RATE_HBW, 0x00, true)) {
  215. return false;
  216. }
  217. // setup FIFO for streaming X,Y,Z
  218. if (!dev_gyro->write_register(REGG_FIFO_CONFIG_1, 0x80, true)) {
  219. return false;
  220. }
  221. hal.console->printf("BMI088: found gyro\n");
  222. return true;
  223. }
  224. bool AP_InertialSensor_BMI088::init()
  225. {
  226. dev_accel->set_read_flag(0x80);
  227. dev_gyro->set_read_flag(0x80);
  228. return accel_init() && gyro_init();
  229. }
  230. /*
  231. read accel fifo
  232. */
  233. void AP_InertialSensor_BMI088::read_fifo_accel(void)
  234. {
  235. if (!setup_accel_config()) {
  236. return;
  237. }
  238. uint8_t len[2];
  239. if (!read_accel_registers(REGA_FIFO_LEN0, len, 2)) {
  240. _inc_accel_error_count(accel_instance);
  241. return;
  242. }
  243. uint16_t fifo_length = len[0] + (len[1]<<8);
  244. if (fifo_length & 0x8000) {
  245. // empty
  246. return;
  247. }
  248. // don't read more than 8 frames at a time
  249. if (fifo_length > 8*7) {
  250. fifo_length = 8*7;
  251. }
  252. if (fifo_length == 0) {
  253. return;
  254. }
  255. uint8_t data[fifo_length];
  256. if (!read_accel_registers(REGA_FIFO_DATA, data, fifo_length)) {
  257. _inc_accel_error_count(accel_instance);
  258. return;
  259. }
  260. // assume configured for 24g range
  261. const float scale = (1.0/32768.0) * GRAVITY_MSS * 24.0;
  262. const uint8_t *p = &data[0];
  263. while (fifo_length >= 7) {
  264. /*
  265. the fifo frames are variable length, with the frame type in the first byte
  266. */
  267. uint8_t frame_len = 2;
  268. switch (p[0] & 0xFC) {
  269. case 0x84: {
  270. // accel frame
  271. frame_len = 7;
  272. const uint8_t *d = p+1;
  273. int16_t xyz[3] {
  274. int16_t(uint16_t(d[0] | (d[1]<<8))),
  275. int16_t(uint16_t(d[2] | (d[3]<<8))),
  276. int16_t(uint16_t(d[4] | (d[5]<<8)))};
  277. Vector3f accel(xyz[0], xyz[1], xyz[2]);
  278. accel *= scale;
  279. _rotate_and_correct_accel(accel_instance, accel);
  280. _notify_new_accel_raw_sample(accel_instance, accel);
  281. break;
  282. }
  283. case 0x40:
  284. // skip frame
  285. frame_len = 2;
  286. break;
  287. case 0x44:
  288. // sensortime frame
  289. frame_len = 4;
  290. break;
  291. case 0x48:
  292. // fifo config frame
  293. frame_len = 2;
  294. break;
  295. case 0x50:
  296. // sample drop frame
  297. frame_len = 2;
  298. break;
  299. }
  300. p += frame_len;
  301. fifo_length -= frame_len;
  302. }
  303. if (temperature_counter++ == 100) {
  304. temperature_counter = 0;
  305. uint8_t tbuf[2];
  306. if (!read_accel_registers(REGA_TEMP_LSB, tbuf, 2)) {
  307. _inc_accel_error_count(accel_instance);
  308. } else {
  309. uint16_t temp_uint11 = (tbuf[0]<<3) | (tbuf[1]>>5);
  310. int16_t temp_int11 = temp_uint11>1023?temp_uint11-2048:temp_uint11;
  311. float temp_degc = temp_int11 * 0.125f + 23;
  312. _publish_temperature(accel_instance, temp_degc);
  313. }
  314. }
  315. }
  316. /*
  317. read gyro fifo
  318. */
  319. void AP_InertialSensor_BMI088::read_fifo_gyro(void)
  320. {
  321. uint8_t num_frames;
  322. if (!dev_gyro->read_registers(REGG_FIFO_STATUS, &num_frames, 1)) {
  323. _inc_gyro_error_count(gyro_instance);
  324. return;
  325. }
  326. num_frames &= 0x7F;
  327. // don't read more than 8 frames at a time
  328. if (num_frames > 8) {
  329. num_frames = 8;
  330. }
  331. if (num_frames == 0) {
  332. return;
  333. }
  334. uint8_t data[6*num_frames];
  335. if (!dev_gyro->read_registers(REGG_FIFO_DATA, data, num_frames*6)) {
  336. _inc_gyro_error_count(gyro_instance);
  337. return;
  338. }
  339. // data is 16 bits with 2000dps range
  340. const float scale = radians(2000.0f) / 32767.0f;
  341. for (uint8_t i = 0; i < num_frames; i++) {
  342. const uint8_t *d = &data[i*6];
  343. int16_t xyz[3] {
  344. int16_t(uint16_t(d[0] | d[1]<<8)),
  345. int16_t(uint16_t(d[2] | d[3]<<8)),
  346. int16_t(uint16_t(d[4] | d[5]<<8)) };
  347. Vector3f gyro(xyz[0], xyz[1], xyz[2]);
  348. gyro *= scale;
  349. _rotate_and_correct_gyro(gyro_instance, gyro);
  350. _notify_new_gyro_raw_sample(gyro_instance, gyro);
  351. }
  352. if (!dev_gyro->check_next_register()) {
  353. _inc_gyro_error_count(gyro_instance);
  354. }
  355. }
  356. bool AP_InertialSensor_BMI088::update()
  357. {
  358. update_accel(accel_instance);
  359. update_gyro(gyro_instance);
  360. return true;
  361. }