uint16_t AP_InertialSensor_MPU6000::_init_sensor( Sample_rate sample_rate ) { if (_initialised) return _mpu6000_product_id; _initialised = true; _spi = hal.spi->device(AP_HAL::SPIDevice_MPU6000); _spi_sem = _spi->get_semaphore(); /* Pin 70 defined especially to hook up PE6 to the hal.gpio abstraction. (It is not a valid pin under Arduino.) */ _drdy_pin = hal.gpio->channel(70); hal.scheduler->suspend_timer_procs(); uint8_t tries = 0; do { bool success = _hardware_init(sample_rate); if (success) { hal.scheduler->delay(5+2); if (!_spi_sem->take(100)) { hal.scheduler->panic(PSTR("MPU6000: Unable to get semaphore")); } if (_data_ready()) { _spi_sem->give(); break; } else { hal.console->println_P( PSTR("MPU6000 startup failed: no data ready")); } _spi_sem->give(); } if (tries++ > 5) { hal.scheduler->panic(PSTR("PANIC: failed to boot MPU6000 5 times")); } } while (1); hal.scheduler->resume_timer_procs(); /* read the first lot of data. * _read_data_transaction requires the spi semaphore to be taken by * its caller. */ _last_sample_time_micros = hal.scheduler->micros(); hal.scheduler->delay(10); if (_spi_sem->take(100)) { _read_data_transaction(); _spi_sem->give(); } // start the timer process to read samples hal.scheduler->register_timer_process(AP_HAL_MEMBERPROC(&AP_InertialSensor_MPU6000::_poll_data)); #if MPU6000_DEBUG _dump_registers(); #endif return _mpu6000_product_id; }
/* initialise the sensor */ bool AP_InertialSensor_MPU9250::_init_sensor(void) { _spi = hal.spi->device(AP_HAL::SPIDevice_MPU9250); _spi_sem = _spi->get_semaphore(); // we need to suspend timers to prevent other SPI drivers grabbing // the bus while we do the long initialisation hal.scheduler->suspend_timer_procs(); uint8_t whoami = _register_read(MPUREG_WHOAMI); if (whoami != 0x71) { // TODO: we should probably accept multiple chip // revisions. This is the one on the PXF hal.console->printf("MPU9250: unexpected WHOAMI 0x%x\n", (unsigned)whoami); return false; } uint8_t tries = 0; do { bool success = _hardware_init(); if (success) { hal.scheduler->delay(10); if (!_spi_sem->take(100)) { hal.console->printf("MPU9250: Unable to get semaphore"); return false; } uint8_t status = _register_read(MPUREG_INT_STATUS); if ((status & BIT_RAW_RDY_INT) != 0) { _spi_sem->give(); break; } _spi_sem->give(); } if (tries++ > 5) { return false; } } while (1); hal.scheduler->resume_timer_procs(); _gyro_instance = _imu.register_gyro(); _accel_instance = _imu.register_accel(); _product_id = AP_PRODUCT_ID_MPU9250; // start the timer process to read samples hal.scheduler->register_timer_process(AP_HAL_MEMBERPROC(&AP_InertialSensor_MPU9250::_poll_data)); #if MPU9250_DEBUG _dump_registers(); #endif return true; }
/* initialise the sensor */ bool AP_InertialSensor_MPU6000::_init_sensor(void) { _spi = hal.spi->device(AP_HAL::SPIDevice_MPU6000); _spi_sem = _spi->get_semaphore(); #ifdef MPU6000_DRDY_PIN _drdy_pin = hal.gpio->channel(MPU6000_DRDY_PIN); _drdy_pin->mode(HAL_GPIO_INPUT); #endif hal.scheduler->suspend_timer_procs(); uint8_t tries = 0; do { bool success = _hardware_init(); if (success) { hal.scheduler->delay(5+2); if (!_spi_sem->take(100)) { return false; } if (_data_ready()) { _spi_sem->give(); break; } else { return false; } _spi_sem->give(); } if (tries++ > 5) { hal.console->print_P(PSTR("failed to boot MPU6000 5 times")); return false; } } while (1); // grab the used instances _gyro_instance = _imu.register_gyro(); _accel_instance = _imu.register_accel(); hal.scheduler->resume_timer_procs(); // start the timer process to read samples hal.scheduler->register_timer_process(AP_HAL_MEMBERPROC(&AP_InertialSensor_MPU6000::_poll_data)); #if MPU6000_DEBUG _dump_registers(); #endif return true; }
bool AP_Compass_AK8963::init() { _healthy[0] = true; _field[0].x = 0.0f; _field[0].y = 0.0f; _field[0].z = 0.0f; hal.scheduler->suspend_timer_procs(); if (!_backend->sem_take_blocking()) { error("_spi_sem->take failed\n"); return false; } if (!_backend_init()) { _backend->sem_give(); return false; } _register_write(AK8963_CNTL2, AK8963_RESET); /* Reset AK8963 */ hal.scheduler->delay(1000); int id_mismatch_count; uint8_t deviceid; for (id_mismatch_count = 0; id_mismatch_count < 5; id_mismatch_count++) { _register_read(AK8963_WIA, 0x01, &deviceid); /* Read AK8963's id */ if (deviceid == AK8963_Device_ID) { break; } error("trying to read AK8963's ID once more...\n"); _backend_reset(); hal.scheduler->delay(100); _dump_registers(); } if (id_mismatch_count == 5) { _initialised = false; hal.console->printf("WRONG AK8963 DEVICE ID: 0x%x\n", (unsigned)deviceid); hal.scheduler->panic(PSTR("AK8963: bad DEVICE ID")); } _calibrate(); _initialised = true; #if AK8963_SELFTEST if (_self_test()) { _initialised = true; } else { _initialised = false; } #endif /* Register value to continuous measurement */ _register_write(AK8963_CNTL1, AK8963_CONTINUOUS_MODE2 | _magnetometer_adc_resolution); _backend->sem_give(); hal.scheduler->resume_timer_procs(); hal.scheduler->register_timer_process( AP_HAL_MEMBERPROC(&AP_Compass_AK8963::_update)); _start_conversion(); _initialised = true; return _initialised; }
uint16_t AP_InertialSensor_L3GD20::_init_sensor( Sample_rate sample_rate ) { if (_initialised) return _L3GD20_product_id; _initialised = true; _spi = hal.spi->device(AP_HAL::SPIDevice_L3GD20); _spi_sem = _spi->get_semaphore(); #ifdef L3GD20_DRDY_PIN _drdy_pin = hal.gpio->channel(L3GD20_DRDY_PIN); _drdy_pin->mode(HAL_GPIO_INPUT); #endif hal.scheduler->suspend_timer_procs(); // Test WHOAMI uint8_t whoami = _register_read(ADDR_WHO_AM_I); if (whoami != WHO_I_AM) { // TODO: we should probably accept multiple chip // revisions. This is the one on the PXF hal.console->printf("L3GD20: unexpected WHOAMI 0x%x\n", (unsigned)whoami); hal.scheduler->panic(PSTR("L3GD20: bad WHOAMI")); } uint8_t tries = 0; do { bool success = _hardware_init(sample_rate); if (success) { hal.scheduler->delay(5+2); if (!_spi_sem->take(100)) { hal.scheduler->panic(PSTR("L3GD20: Unable to get semaphore")); } if (_data_ready()) { _spi_sem->give(); break; } else { hal.console->println_P( PSTR("L3GD20 startup failed: no data ready")); } _spi_sem->give(); } if (tries++ > 5) { hal.scheduler->panic(PSTR("PANIC: failed to boot L3GD20 5 times")); } } while (1); hal.scheduler->resume_timer_procs(); /* read the first lot of data. * _read_data_transaction requires the spi semaphore to be taken by * its caller. */ _last_sample_time_micros = hal.scheduler->micros(); hal.scheduler->delay(10); if (_spi_sem->take(100)) { _read_data_transaction(); _spi_sem->give(); } // start the timer process to read samples hal.scheduler->register_timer_process(AP_HAL_MEMBERPROC(&AP_InertialSensor_L3GD20::_poll_data)); #if L3GD20_DEBUG _dump_registers(); #endif return _L3GD20_product_id; }
uint16_t AP_InertialSensor_LSM9DS0::_init_sensor( AP_InertialSensor::Sample_rate sample_rate) { if (_initialised) return _lsm9ds0_product_id; _initialised = true; _spi = hal.spi->device(AP_HAL::SPIDevice_LSM9DS0_AM); _spi_sem = _spi->get_semaphore(); _drdy_pin_a = hal.gpio->channel(BBB_P8_8); _drdy_pin_m = hal.gpio->channel(BBB_P8_10); _drdy_pin_g = hal.gpio->channel(BBB_P8_34); // For some reason configuring the pins as an inputs make the driver fail // _drdy_pin_a->mode(GPIO_IN); // _drdy_pin_m->mode(GPIO_IN); // _drdy_pin_g->mode(GPIO_IN); hal.scheduler->suspend_timer_procs(); uint8_t tries = 0; do { bool success = _hardware_init(sample_rate); if (success) { hal.scheduler->delay(5+2); if (!_spi_sem->take(100)) { hal.scheduler->panic(PSTR("LSM9DS0: Unable to get semaphore")); } if (_data_ready()) { _spi_sem->give(); break; } else { hal.console->println_P( PSTR("LSM9DS0 startup failed: no data ready")); } _spi_sem->give(); } if (tries++ > 5) { hal.scheduler->panic(PSTR("PANIC: failed to boot LSM9DS0 5 times")); } } while (1); hal.scheduler->resume_timer_procs(); /* read the first lot of data. * _read_data_transaction requires the spi semaphore to be taken by * its caller. */ _last_sample_time_micros = hal.scheduler->micros(); hal.scheduler->delay(10); if (_spi_sem->take(100)) { _read_data_transaction_g(); _read_data_transaction_xm(); _spi_sem->give(); } // start the timer process to read samples hal.scheduler->register_timer_process(AP_HAL_MEMBERPROC(&AP_InertialSensor_LSM9DS0::_poll_data)); #if LSM9DS0_DEBUG _dump_registers(); #endif return _lsm9ds0_product_id; }
/* initialise the sensor */ uint16_t AP_InertialSensor_MPU9250::_init_sensor( Sample_rate sample_rate ) { if (_initialised) return _mpu9250_product_id; _initialised = true; _spi = hal.spi->device(AP_HAL::SPIDevice_MPU9250); _spi_sem = _spi->get_semaphore(); // we need to suspend timers to prevent other SPI drivers grabbing // the bus while we do the long initialisation hal.scheduler->suspend_timer_procs(); uint8_t whoami = _register_read(MPUREG_WHOAMI); if (whoami != 0x71) { // TODO: we should probably accept multiple chip // revisions. This is the one on the PXF hal.console->printf("MPU9250: unexpected WHOAMI 0x%x\n", (unsigned)whoami); hal.scheduler->panic("MPU9250: bad WHOAMI"); } uint8_t tries = 0; do { bool success = _hardware_init(sample_rate); if (success) { hal.scheduler->delay(10); if (!_spi_sem->take(100)) { hal.scheduler->panic(PSTR("MPU9250: Unable to get semaphore")); } uint8_t status = _register_read(MPUREG_INT_STATUS); if ((status & BIT_RAW_RDY_INT) != 0) { _spi_sem->give(); break; } else { hal.console->println_P( PSTR("MPU9250 startup failed: no data ready")); } _spi_sem->give(); } if (tries++ > 5) { hal.scheduler->panic(PSTR("PANIC: failed to boot MPU9250 5 times")); } } while (1); hal.scheduler->resume_timer_procs(); /* read the first lot of data. * _read_data_transaction requires the spi semaphore to be taken by * its caller. */ hal.scheduler->delay(10); if (_spi_sem->take(100)) { _read_data_transaction(); _spi_sem->give(); } // start the timer process to read samples hal.scheduler->register_timer_process(AP_HAL_MEMBERPROC(&AP_InertialSensor_MPU9250::_poll_data)); #if MPU9250_DEBUG _dump_registers(); #endif return _mpu9250_product_id; }
bool AP_InertialSensor_MPU6000::hardware_init(Sample_rate sample_rate) { if (!_spi_sem->take(100)) { hal.scheduler->panic(PSTR("MPU6000: Unable to get semaphore")); } // Chip reset uint8_t tries; for (tries = 0; tries<5; tries++) { register_write(MPUREG_PWR_MGMT_1, BIT_PWR_MGMT_1_DEVICE_RESET); hal.scheduler->delay(100); // Wake up device and select GyroZ clock. Note that the // MPU6000 starts up in sleep mode, and it can take some time // for it to come out of sleep register_write(MPUREG_PWR_MGMT_1, BIT_PWR_MGMT_1_CLK_ZGYRO); hal.scheduler->delay(5); // check it has woken up if (_register_read(MPUREG_PWR_MGMT_1) == BIT_PWR_MGMT_1_CLK_ZGYRO) { break; } #if MPU6000_DEBUG _dump_registers(); #endif } if (tries == 5) { hal.console->println_P(PSTR("Failed to boot MPU6000 5 times")); _spi_sem->give(); return false; } register_write(MPUREG_PWR_MGMT_2, 0x00); // only used for wake-up in accelerometer only low power mode hal.scheduler->delay(1); // Disable I2C bus (recommended on datasheet) register_write(MPUREG_USER_CTRL, BIT_USER_CTRL_I2C_IF_DIS); hal.scheduler->delay(1); uint8_t default_filter; // sample rate and filtering // to minimise the effects of aliasing we choose a filter // that is less than half of the sample rate switch (sample_rate) { case RATE_50HZ: // this is used for plane and rover, where noise resistance is // more important than update rate. Tests on an aerobatic plane // show that 10Hz is fine, and makes it very noise resistant default_filter = BITS_DLPF_CFG_10HZ; _sample_shift = 2; break; case RATE_100HZ: default_filter = BITS_DLPF_CFG_20HZ; _sample_shift = 1; break; case RATE_200HZ: default: default_filter = BITS_DLPF_CFG_20HZ; _sample_shift = 0; break; } _set_filter_register(_mpu6000_filter, default_filter); // set sample rate to 200Hz, and use _sample_divider to give // the requested rate to the application register_write(MPUREG_SMPLRT_DIV, MPUREG_SMPLRT_200HZ); hal.scheduler->delay(1); register_write(MPUREG_GYRO_CONFIG, BITS_GYRO_FS_2000DPS); // Gyro scale 2000º/s hal.scheduler->delay(1); // read the product ID rev c has 1/2 the sensitivity of rev d _mpu6000_product_id = _register_read(MPUREG_PRODUCT_ID); //Serial.printf("Product_ID= 0x%x\n", (unsigned) _mpu6000_product_id); if ((_mpu6000_product_id == MPU6000ES_REV_C4) || (_mpu6000_product_id == MPU6000ES_REV_C5) || (_mpu6000_product_id == MPU6000_REV_C4) || (_mpu6000_product_id == MPU6000_REV_C5)) { // Accel scale 8g (4096 LSB/g) // Rev C has different scaling than rev D register_write(MPUREG_ACCEL_CONFIG,1<<3); } else { // Accel scale 8g (4096 LSB/g) register_write(MPUREG_ACCEL_CONFIG,2<<3); } hal.scheduler->delay(1); // configure interrupt to fire when new data arrives register_write(MPUREG_INT_ENABLE, BIT_RAW_RDY_EN); hal.scheduler->delay(1); // clear interrupt on any read, and hold the data ready pin high // until we clear the interrupt register_write(MPUREG_INT_PIN_CFG, BIT_INT_RD_CLEAR | BIT_LATCH_INT_EN); hal.scheduler->delay(1); _spi_sem->give(); return true; }