status_t LSM303C::begin(InterfaceMode_t im, MAG_DO_t modr, MAG_FS_t mfs, MAG_BDU_t mbu, MAG_OMXY_t mxyodr, MAG_OMZ_t mzodr, MAG_MD_t mm, ACC_FS_t afs, ACC_BDU_t abu, uint8_t aea, ACC_ODR_t aodr) { uint8_t successes = 0; // Select I2C or SPI interfaceMode = im; enableIMU(); ////////// Initialize Magnetometer ////////// // Initialize magnetometer output data rate successes += MAG_SetODR(modr); // Initialize magnetic field full scale successes += MAG_SetFullScale(mfs); // Enabling block data updating successes += MAG_BlockDataUpdate(mbu); // Initialize magnetometer X/Y axes ouput data rate successes += MAG_XY_AxOperativeMode(mxyodr); // Initialize magnetometer Z axis performance mode successes += MAG_Z_AxOperativeMode(mzodr); // Initialize magnetometer run mode. successes += MAG_SetMode(mm); ////////// Initialize Accelerometer ////////// // Initialize acceleration full scale successes += ACC_SetFullScale(afs); // Enable block data updating successes += ACC_BlockDataUpdate(abu); // Enable X, Y, and Z accelerometer axes successes += ACC_EnableAxis(aea); // Initialize accelerometer output data rate successes += ACC_SetODR(aodr); return (successes == IMU_SUCCESS) ? IMU_SUCCESS : IMU_HW_ERROR; }
status_t LSM303C::begin(InterfaceMode_t im, MAG_DO_t modr, MAG_FS_t mfs, MAG_BDU_t mbu, MAG_OMXY_t mxyodr, MAG_OMZ_t mzodr, MAG_MD_t mm, ACC_FS_t afs, ACC_BDU_t abu, uint8_t aea, ACC_ODR_t aodr) { uint8_t successes = 0; // Select I2C or SPI interfaceMode = im; if (interfaceMode == MODE_SPI) { debug_println("Setting up SPI"); // Setup pins for SPI // CS & CLK must be outputs DDRxn = 1 bitSet(DIR_REG, CSBIT_MAG); bitSet(DIR_REG, CSBIT_XL); bitSet(DIR_REG, CLKBIT); // Deselect SPI chips bitSet(CSPORT_MAG, CSBIT_MAG); bitSet(CSPORT_XL, CSBIT_XL); // Clock polarity (CPOL) = 1 bitSet(CLKPORT, CLKBIT); // SPI Serial Interface Mode (SIM) bits must be set SPI_WriteByte(ACC, ACC_CTRL4, 0b111); SPI_WriteByte(MAG, MAG_CTRL_REG3, _BV(2)); } else { Wire.begin(); Wire.setClock(400000L); } ////////// Initialize Magnetometer ////////// // Initialize magnetometer output data rate successes += MAG_SetODR(modr); // Initialize magnetic field full scale successes += MAG_SetFullScale(mfs); // Enabling block data updating successes += MAG_BlockDataUpdate(mbu); // Initialize magnetometer X/Y axes ouput data rate successes += MAG_XY_AxOperativeMode(mxyodr); // Initialize magnetometer Z axis performance mode successes += MAG_Z_AxOperativeMode(mzodr); // Initialize magnetometer run mode. successes += MAG_SetMode(mm); ////////// Initialize Accelerometer ////////// // Initialize acceleration full scale successes += ACC_SetFullScale(afs); // Enable block data updating successes += ACC_BlockDataUpdate(abu); // Enable X, Y, and Z accelerometer axes successes += ACC_EnableAxis(aea); // Initialize accelerometer output data rate successes += ACC_SetODR(aodr); return (successes == IMU_SUCCESS) ? IMU_SUCCESS : IMU_HW_ERROR; }