1/* 2 $License: 3 Copyright 2011 InvenSense, Inc. 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 $ 17 */ 18 19/****************************************************************************** 20 * 21 * $Id: ml_mputest.c 5641 2011-06-14 02:10:02Z mcaramello $ 22 * 23 *****************************************************************************/ 24 25/** 26 * @defgroup MPU_SELF_TEST 27 * @brief C wrapper to integrate the MPU Self Test wrapper in MPL. 28 * Provides ML name compliant naming and an additional API that 29 * automates the suspension of normal MPL operations, runs the test, 30 * and resume. 31 * 32 * @{ 33 * @file ml_mputest.c 34 * @brief C wrapper to integrate the MPU Self Test wrapper in MPL. 35 * The main logic of the test and APIs can be found in mputest.c 36 */ 37 38#include <stdio.h> 39#include <time.h> 40#include <string.h> 41#include <math.h> 42#include <stdlib.h> 43 44#include "ml_mputest.h" 45 46#include "mlmath.h" 47#include "mlinclude.h" 48#include "ml.h" 49#include "mlstates.h" 50#include "mldl.h" 51#include "mldl_cfg.h" 52 53#ifdef __cplusplus 54extern "C" { 55#endif 56 57/* 58 Globals 59*/ 60extern struct mldl_cfg *mputestCfgPtr; 61extern signed char g_z_sign; 62 63/* 64 Prototypes 65*/ 66extern inv_error_t inv_factory_calibrate(void *mlsl_handle, 67 uint_fast8_t provide_result); 68 69/** 70 * @brief An MPL wrapper for the main MPU Self Test API inv_factory_calibrate(). 71 * See inv_factory_calibrate() function for more details. 72 * 73 * @pre inv_dmp_open() <b>must</b> have been called to populate the mldl_cfg 74 * data structure. 75 * On Windows, SetupPlatform() is also a madatory pre condition to 76 * ensure the accelerometer is properly configured before running the 77 * test. 78 * 79 * @param mlsl_handle 80 * serial interface handle to allow serial communication with the 81 * device, both gyro and accelerometer. 82 * @param provide_result 83 * If 1: 84 * perform and analyze the offset, drive frequency, and noise 85 * calculation and compare it against set thresholds. Report 86 * also the final result using a bit-mask like error code as 87 * described in the inv_test_gyro_xxxx() functions. 88 * When 0: 89 * skip the noise and drive frequency calculation and pass/fail 90 * assessment. It simply calculates the gyro and accel biases. 91 * NOTE: for MPU6050 devices, this parameter is currently 92 * ignored. 93 * 94 * @return INV_SUCCESS or first non-zero error code otherwise. 95 */ 96inv_error_t inv_self_test_factory_calibrate(void *mlsl_handle, 97 unsigned char provide_result) 98{ 99 INVENSENSE_FUNC_START; 100 inv_error_t firstError = INV_SUCCESS; 101 inv_error_t result; 102 unsigned char initState = inv_get_state(); 103 104 if (initState < INV_STATE_DMP_OPENED) { 105 MPL_LOGE("Self Test cannot run before inv_dmp_open()\n"); 106 return INV_ERROR_SM_IMPROPER_STATE; 107 } 108 109 /* obtain a pointer to the 'struct mldl_cfg' data structure. */ 110 mputestCfgPtr = inv_get_dl_config(); 111 112 if(initState == INV_STATE_DMP_STARTED) { 113 result = inv_dmp_stop(); 114 ERROR_CHECK_FIRST(firstError, result); 115 } 116 117 result = inv_factory_calibrate(mlsl_handle, provide_result); 118 ERROR_CHECK_FIRST(firstError, result); 119 120 if(initState == INV_STATE_DMP_STARTED) { 121 result = inv_dmp_start(); 122 ERROR_CHECK_FIRST(firstError, result); 123 } 124 125 return firstError; 126} 127 128/** 129 * @brief Runs the MPU test at MPL runtime. 130 * If the DMP is operating, stops the DMP temporarely, 131 * runs the MPU Self Test, and re-starts the DMP. 132 * 133 * @return INV_SUCCESS or a non-zero error code otherwise. 134 */ 135inv_error_t inv_self_test_run(void) 136{ 137#ifdef CONFIG_MPU_SENSORS_MPU3050 138 return inv_self_test_factory_calibrate(inv_get_serial_handle(), TRUE); 139#else 140 return inv_self_test_factory_calibrate(inv_get_serial_handle(), FALSE); 141#endif 142} 143 144/** 145 * @brief Runs the MPU test for bias correction only at MPL runtime. 146 * If the DMP is operating, stops the DMP temporarely, 147 * runs the bias calculation routines, and re-starts the DMP. 148 * 149 * @return INV_SUCCESS or a non-zero error code otherwise. 150 */ 151inv_error_t inv_self_test_bias_only(void) 152{ 153 return inv_self_test_factory_calibrate(inv_get_serial_handle(), FALSE); 154} 155 156/** 157 * @brief Set the orientation of the acceleroemter Z axis as it will be 158 * expected when running the MPU Self Test. 159 * Specifies the orientation of the accelerometer Z axis : Z axis 160 * pointing upwards or downwards. 161 * @param z_sign 162 * The sign of the accelerometer Z axis; valid values are +1 and 163 * -1 for +Z and -Z respectively. Any other value will cause the 164 * setting to be ignored and an error code to be returned. 165 * @return INV_SUCCESS or a non-zero error code. 166 */ 167inv_error_t inv_self_test_set_accel_z_orient(signed char z_sign) 168{ 169 if (z_sign != +1 && z_sign != -1) { 170 return INV_ERROR_INVALID_PARAMETER; 171 } 172 g_z_sign = z_sign; 173 return INV_SUCCESS; 174} 175 176 177#ifdef __cplusplus 178} 179#endif 180 181/** 182 * @} 183 */ 184 185