Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Baro/examples/ICM20789/ICM20789.cpp
Views: 1800
/*1minimal test program for ICM20789 baro with IMU on SPI and baro on I2C2*/34#include <AP_HAL/AP_HAL.h>5#include <AP_HAL/I2CDevice.h>6#include <AP_Baro/AP_Baro.h>7#include <AP_BoardConfig/AP_BoardConfig.h>8#include <utility>9#include <stdio.h>1011const AP_HAL::HAL& hal = AP_HAL::get_HAL();1213static AP_HAL::OwnPtr<AP_HAL::Device> i2c_dev;14static AP_HAL::OwnPtr<AP_HAL::Device> spi_dev;15static AP_HAL::OwnPtr<AP_HAL::Device> dev;1617// SPI registers18#define MPUREG_WHOAMI 0x7519#define MPUREG_USER_CTRL 0x6A20#define MPUREG_PWR_MGMT_1 0x6B2122#define MPUREG_INT_PIN_CFG 0x3723# define BIT_BYPASS_EN 0x0224# define BIT_INT_RD_CLEAR 0x10 // clear the interrupt when any read occurs25# define BIT_LATCH_INT_EN 0x20 // latch data ready pin2627#define BIT_USER_CTRL_I2C_MST_EN 0x20 // Enable MPU to act as the I2C Master to external slave sensors28#define BIT_PWR_MGMT_1_DEVICE_RESET 0x80 // reset entire device29#define BIT_USER_CTRL_I2C_IF_DIS 0x10 // Disable primary I2C interface and enable hal.spi->interface30#define BIT_PWR_MGMT_1_CLK_XGYRO 0x01 // PLL with X axis gyroscope reference31#define BIT_PWR_MGMT_1_CLK_ZGYRO 0x03 // PLL with Z axis gyroscope reference3233// baro commands34#define CMD_SOFT_RESET 0x805D35#define CMD_READ_ID 0xEFC83637void setup(void);38void loop(void);394041#ifdef HAL_INS_MPU60x0_NAME42static void spi_init()43{44// SPI reads have flag 0x80 set45spi_dev->set_read_flag(0x80);4647// run the SPI bus at low speed48spi_dev->set_speed(AP_HAL::Device::SPEED_LOW);4950uint8_t whoami = 0;51uint8_t v;5253spi_dev->write_register(0x6B, 0x01);54spi_dev->write_register(0x6B, 0x01);5556hal.scheduler->delay(1);57spi_dev->write_register(0x6A, 0x10);58spi_dev->write_register(0x6B, 0x41);5960hal.scheduler->delay(1);61spi_dev->write_register(0x6C, 0x3f);62spi_dev->write_register(0xF5, 0x00);63spi_dev->write_register(0x19, 0x09);64spi_dev->write_register(0xEA, 0x00);65spi_dev->write_register(0x6B, 0x01);6667hal.scheduler->delay(1);68spi_dev->write_register(0x6A, 0x10);69spi_dev->write_register(0x6B, 0x41);7071hal.scheduler->delay(1);72spi_dev->write_register(0x6B, 0x01);7374hal.scheduler->delay(1);75spi_dev->write_register(0x23, 0x00);76spi_dev->write_register(0x6B, 0x41);7778hal.scheduler->delay(1);79spi_dev->write_register(0x1D, 0xC0);80spi_dev->write_register(0x6B, 0x01);8182hal.scheduler->delay(1);83spi_dev->write_register(0x1A, 0xC0);84spi_dev->write_register(0x6B, 0x41);8586hal.scheduler->delay(1);87spi_dev->write_register(0x38, 0x01);8889spi_dev->read_registers(0x6A, &v, 1);90printf("reg 0x6A=0x%02x\n", v);91spi_dev->read_registers(0x6B, &v, 1);92printf("reg 0x6B=0x%02x\n", v);9394hal.scheduler->delay(1);95spi_dev->write_register(0x6A, 0x10);96spi_dev->write_register(0x6B, 0x41);9798hal.scheduler->delay(1);99spi_dev->write_register(0x6B, 0x01);100101hal.scheduler->delay(1);102spi_dev->write_register(0x23, 0x00);103spi_dev->write_register(0x6B, 0x41);104105hal.scheduler->delay(1);106spi_dev->write_register(0x6B, 0x41);107spi_dev->write_register(0x6C, 0x3f);108spi_dev->write_register(0x6B, 0x41);109110spi_dev->read_registers(0x6A, &v, 1);111printf("reg 0x6A=0x%02x\n", v);112spi_dev->write_register(0x6B, 0x01);113114hal.scheduler->delay(1);115spi_dev->write_register(0x6A, 0x10);116spi_dev->write_register(0x6B, 0x41);117118hal.scheduler->delay(1);119spi_dev->write_register(0x6B, 0x01);120121hal.scheduler->delay(1);122spi_dev->write_register(0x23, 0x00);123spi_dev->write_register(0x6B, 0x41);124125hal.scheduler->delay(1);126spi_dev->read_registers(0x6A, &v, 1);127printf("reg 0x6A=0x%02x\n", v);128spi_dev->write_register(0x6B, 0x01);129130hal.scheduler->delay(1);131spi_dev->write_register(0x6A, 0x10);132spi_dev->write_register(0x6B, 0x41);133134hal.scheduler->delay(1);135spi_dev->write_register(0x6B, 0x01);136137hal.scheduler->delay(1);138spi_dev->write_register(0x23, 0x00);139spi_dev->write_register(0x6B, 0x41);140141hal.scheduler->delay(1);142spi_dev->write_register(0x6B, 0x41);143spi_dev->write_register(0x6C, 0x3f);144spi_dev->write_register(0x6B, 0x41);145146spi_dev->read_registers(0x6A, &v, 1);147printf("reg 0x6A=0x%02x\n", v);148spi_dev->write_register(0x6B, 0x01);149150hal.scheduler->delay(1);151spi_dev->write_register(0x6A, 0x10);152spi_dev->write_register(0x6B, 0x41);153154hal.scheduler->delay(1);155spi_dev->write_register(0x6B, 0x01);156157hal.scheduler->delay(1);158spi_dev->write_register(0x23, 0x00);159spi_dev->write_register(0x6B, 0x41);160161// print the WHOAMI162spi_dev->read_registers(MPUREG_WHOAMI, &whoami, 1);163printf("20789 SPI whoami: 0x%02x\n", whoami);164165// wait for sensor to settle166hal.scheduler->delay(100);167168// dump registers169printf("ICM20789 registers\n");170#if 0171for (uint8_t reg = 0; reg<0x80; reg++) {172v=0;173spi_dev->read_registers(reg, &v, 1);174printf("%02x:%02x ", (unsigned)reg, (unsigned)v);175if ((reg+1) % 16 == 0) {176printf("\n");177}178}179printf("\n");180#endif181182spi_dev->get_semaphore()->give();183}184#endif185186#ifdef HAL_INS_MPU60x0_NAME187/*188send a 16 bit command to the baro189*/190static bool send_cmd16(uint16_t cmd)191{192uint8_t cmd_b[2] = { uint8_t(cmd >> 8), uint8_t(cmd & 0xFF) };193return i2c_dev->transfer(cmd_b, 2, nullptr, 0);194}195196/*197read baro calibration data198*/199static bool read_calibration_data(void)200{201// setup for OTP read202const uint8_t cmd[5] = { 0xC5, 0x95, 0x00, 0x66, 0x9C };203if (!i2c_dev->transfer(cmd, sizeof(cmd), nullptr, 0)) {204return false;205}206for (uint8_t i=0; i<4; i++) {207if (!send_cmd16(0xC7F7)) {208return false;209}210uint8_t d[3];211if (!i2c_dev->transfer(nullptr, 0, d, sizeof(d))) {212return false;213}214uint16_t c = int16_t((d[0]<<8) | d[1]);215printf("sensor_constants[%u]=%d\n", i, c);216}217return true;218}219220// initialise baro on i2c221static void i2c_init(void)222{223i2c_dev->get_semaphore()->take_blocking();224225if (send_cmd16(CMD_READ_ID)) {226printf("ICM20789: read ID ******\n");227uint8_t id[3] {};228if (!i2c_dev->transfer(nullptr, 0, id, 3)) {229printf("ICM20789: failed to read ID\n");230}231printf("ICM20789: ID %02x %02x %02x\n", id[0], id[1], id[2]);232} else {233printf("ICM20789 read ID failed\n");234}235236if (send_cmd16(CMD_SOFT_RESET)) {237printf("ICM20789: reset OK ************###########*********!!!!!!!!\n");238} else {239printf("ICM20789 baro reset failed\n");240}241hal.scheduler->delay(1);242243244read_calibration_data();245246i2c_dev->get_semaphore()->give();247248printf("checking baro\n");249dev->get_semaphore()->take_blocking();250251uint8_t regs[3] = { 0xC0, 0xC1, 0xC2 };252for (uint8_t i=0; i<ARRAY_SIZE(regs); i++) {253uint8_t v = 0;254dev->read_registers(regs[i], &v, 1);255printf("0x%02x : 0x%02x\n", regs[i], v);256}257dev->get_semaphore()->give();258}259#endif260261void setup()262{263printf("ICM20789 test\n");264265AP_BoardConfig{}.init();266267hal.scheduler->delay(1000);268269#ifdef HAL_INS_MPU60x0_NAME270spi_dev = std::move(hal.spi->get_device(HAL_INS_MPU60x0_NAME));271272spi_dev->get_semaphore()->take_blocking();273274i2c_dev = std::move(hal.i2c_mgr->get_device(1, 0x63));275dev = std::move(hal.i2c_mgr->get_device(1, 0x29));276277while (true) {278spi_init();279i2c_init();280hal.scheduler->delay(1000);281}282#else283while (true) {284printf("HAL_INS_MPU60x0_NAME not defined for this board\n");285hal.scheduler->delay(1000);286}287#endif288}289290291void loop()292{293}294295AP_HAL_MAIN();296297298