Silicon Labs / ICM20648

Dependents:   TBSense2_Sensor_Demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ICM20648.h Source File

ICM20648.h

Go to the documentation of this file.
00001 /***************************************************************************//**
00002  * @file ICM20648.h
00003  *******************************************************************************
00004  * @section License
00005  * <b>(C) Copyright 2017 Silicon Labs, http://www.silabs.com</b>
00006  *******************************************************************************
00007  *
00008  * SPDX-License-Identifier: Apache-2.0
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00011  * not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  * http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00018  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  *
00022  ******************************************************************************/
00023 
00024 #ifndef ICM20648_H
00025 #define ICM20648_H
00026 
00027 #include "mbed.h"
00028 
00029 /** ICM20648 class.
00030  *  Used for taking accelerometer and gyroscope measurements.
00031  *
00032  * Example:
00033  * @code
00034  * #include "mbed.h"
00035  * #include "ICM20648.h"
00036  *
00037  * //Create an ICM20648 object
00038  * ICM20648 sensor(PC4, PC5);
00039  *
00040  * int main()
00041  * {
00042  *     //Try to open the ICM20648
00043  *     if (sensor.open()) {
00044  *         printf("Device detected!\n");
00045  *
00046  *         while (1) {
00047  *             float acc_x, acc_y, acc_z;
00048  *             float gyr_x, gyr_y, gyr_z;
00049  *             
00050  *             sensor.measure();
00051  *
00052  *             sensor.get_accelerometer(&acc_x, &acc_y, &acc_z);
00053  *             sensor.get_gyroscope(&gyr_x, &gyr_y, &gyr_z);
00054  *
00055  *             //Print the current accelerometer measurement
00056  *             printf("acc: %.3f  %.3f  %.3f\n", acc_x, acc_y, acc_z);
00057  *             //Print the current gyroscope measurement
00058  *             printf("gyr: %.3f  %.3f  %.3f\n", gyr_x, gyr_y, gyr_z);
00059  *
00060  *             //Sleep for 0.5 seconds
00061  *             wait(0.5);
00062  *         }
00063  *     } else {
00064  *         error("Device not detected!\n");
00065  *     }
00066  * }
00067  * @endcode
00068  */
00069 class ICM20648
00070 {
00071 public:
00072 
00073     /** Create an ICM20648 object connected to the specified SPI pins
00074      *
00075      * @param mosi The SPI MOSI pin.
00076      * @param miso The SPI MISO pin.
00077      * @param sclk The SPI clock pin.
00078      * @param cs The SPI Chip Select pin.
00079      * @param irq The ICM20648 irq pin.
00080      */
00081     ICM20648(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName irq = NC);
00082 
00083     /**
00084     * ICM20648 destructor
00085     */
00086     ~ICM20648(void);
00087 
00088     /** Probe for the ICM20648 and try to initialize the sensor
00089      *
00090      * @returns
00091      *   'true' if the device exists on the bus,
00092      *   'false' if the device doesn't exist on the bus.
00093      */
00094     bool open();
00095 
00096     /** Perform a measurement
00097      *
00098      * @returns true if measurement was successful
00099      */
00100     bool measure();
00101 
00102     /** Do a measurement on the gyroscope
00103      *
00104      * @param[out] gyr_x Gyroscope measurement on X axis
00105      * @param[out] gyr_y Gyroscope measurement on Y axis
00106      * @param[out] gyr_z Gyroscope measurement on Z axis
00107      *
00108      * @returns true if measurement was successful
00109      */
00110     bool get_gyroscope(float *gyr_x, float *gyr_y, float *gyr_z);
00111 
00112     /** Do a measurement on the accelerometer
00113      *
00114      * @param[out] acc_x Accelerometer measurement on X axis
00115      * @param[out] acc_y Accelerometer measurement on Y axis
00116      * @param[out] acc_z Accelerometer measurement on Z axis
00117      *
00118      * @returns true if measurement was successful
00119      */
00120     bool get_accelerometer(float *acc_x, float *acc_y, float *acc_z);
00121 
00122     /** Do a measurement of the temperature sensor
00123      *
00124      * @param[out] temperature Measured temperature
00125      *
00126      * @returns true if measurement was successful
00127      */
00128     bool get_temperature(float *temperature);
00129 
00130 private:
00131     /* Private functions */
00132     void     read_register(uint16_t addr, int numBytes, uint8_t *data);
00133     void     write_register(uint16_t addr, uint8_t data);
00134     void     select_bank(uint8_t bank);
00135     uint32_t reset(void);
00136     uint32_t set_sample_rate(float sampleRate);
00137     float    set_gyro_sample_rate(float sampleRate);
00138     float    set_accel_sample_rate(float sampleRate);
00139     uint32_t set_gyro_bandwidth(uint8_t gyroBw);
00140     uint32_t set_accel_bandwidth(uint8_t accelBw);
00141     uint32_t read_accel_data(float *accel);
00142     uint32_t read_gyro_data(float *gyro);
00143     uint32_t get_accel_resolution(float *accelRes);
00144     uint32_t get_gyro_resolution(float *gyroRes);
00145     uint32_t set_accel_fullscale(uint8_t accelFs);
00146     uint32_t set_gyro_fullscale(uint8_t gyroFs);
00147     uint32_t enable_sleepmode(bool enable);
00148     uint32_t enable_cyclemode(bool enable);
00149     uint32_t enable_sensor(bool accel, bool gyro, bool temp);
00150     uint32_t enter_lowpowermode(bool enAccel, bool enGyro, bool enTemp);
00151     uint32_t enable_irq(bool dataReadyEnable, bool womEnable);
00152     uint32_t read_irqstatus(uint32_t *int_status);
00153     bool     is_data_ready(void);
00154     uint32_t enable_wake_on_motion(bool enable, uint8_t womThreshold, float sampleRate);
00155     uint32_t calibrate(float *accelBiasScaled, float *gyroBiasScaled);
00156     uint32_t calibrate_gyro(float *gyroBiasScaled);
00157     uint32_t read_temperature(float *temperature);
00158     uint32_t get_device_id(uint8_t *device_id);
00159 
00160     void irq_handler(void);
00161 
00162     /* Member variables */
00163     SPI m_SPI;
00164     DigitalOut m_CS;
00165     InterruptIn m_IRQ;
00166 };
00167 
00168 #endif