📄 motion_sensor.c
字号:
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2005
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*****************************************************************************
*
* Filename:
* ---------
* motion_sensor.c
*
* Project:
* --------
* Maui_Software
*
* Description:
* ------------
* This Module is for motion sensor driver.
*
* Author:
* -------
* -------
*
*============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
****************************************************************************/
#if defined(MOTION_SENSOR_SUPPORT)
#include "kal_release.h"
#include "custom_config.h"
#include "gpio_sw.h"
#include "drv_comm.h"
#include "motion_sensor.h"
#include "motion_sensor_custom.h"
#include "motion_sensor_buff.h"
#include "gpt_sw.h"
#include "intrCtrl.h"
#include "eint.h"
/*Variable Declaration*/
MotionSensorStruct motion_sensor_data;
MotionSensorBufferStruct motion_sensor_buff;
MotionSensor_customize_function_struct *motion_sensor_custom_fp;
MotionSensor_custom_data_struct *motion_sensor_custom_dp;
kal_int16 acc_x_plus_g_adc=2000, acc_x_minus_g_adc=2000;
kal_int16 acc_y_plus_g_adc=2000, acc_y_minus_g_adc=2000;
kal_int16 acc_z_plus_g_adc=2000, acc_z_minus_g_adc=2000;
kal_bool acc_execute_cali=KAL_FALSE;
/*Motion main function*/
/*
* FUNCTION
* motion_sensor_start_cali
*
* DESCRIPTION
* This function is to start calibration
*
* CALLS
*
* PARAMETERS
* None
*
* RETURNS
* None
*/
void motion_sensor_start_cali(void)
{
acc_execute_cali=KAL_TRUE;
}
/*
* FUNCTION
* motion_sensor_cancel_cali
*
* DESCRIPTION
* This function is to cancel calibration
*
* CALLS
*
* PARAMETERS
* None
*
* RETURNS
* None
*/
void motion_sensor_cancel_cali(void)
{
/*user cancel calibration, don't calculate it*/
acc_execute_cali=KAL_FALSE;
/*use zerog as reference*/
acc_x_plus_g_adc=motion_sensor_data.cali.x_offset;
acc_x_minus_g_adc=motion_sensor_data.cali.x_offset;
acc_y_plus_g_adc=motion_sensor_data.cali.y_offset;
acc_y_minus_g_adc=motion_sensor_data.cali.y_offset;
acc_z_plus_g_adc=motion_sensor_data.cali.z_offset;
acc_z_minus_g_adc=motion_sensor_data.cali.z_offset;
}
/*
* FUNCTION
* motion_sensor_read_cali
*
* DESCRIPTION
* This function is to read calibration data
*
* CALLS
*
* PARAMETERS
* cali: calibration data
*
* RETURNS
* None
*/
void motion_sensor_read_cali(MotionSensorCaliStruct *cali)
{
*cali=motion_sensor_data.cali;
}
/*
* FUNCTION
* motion_sensor_set_cali
*
* DESCRIPTION
* This function is to configure calibration data
*
* CALLS
*
* PARAMETERS
* cali: calibration data
*
* RETURNS
* None
*/
void motion_sensor_set_cali(MotionSensorCaliStruct cali)
{
motion_sensor_data.cali=cali;
}
/*
* FUNCTION
* motion_sensor_complete_cali
*
* DESCRIPTION
* This function is to calculate calibration data
*
* CALLS
*
* PARAMETERS
* None
*
* RETURNS
* None
*/
//MotionSensorCaliStruct test_cali;
void motion_sensor_complete_cali(void)
{
acc_execute_cali=KAL_FALSE;
/*offset*/
motion_sensor_data.cali.x_offset=(acc_x_plus_g_adc+ acc_x_minus_g_adc)>>1;
motion_sensor_data.cali.y_offset=(acc_y_plus_g_adc+acc_y_minus_g_adc)>>1;
motion_sensor_data.cali.z_offset=(acc_z_plus_g_adc+ acc_z_minus_g_adc)>>1;
/*gain*/
motion_sensor_data.cali.x_gain=(acc_x_plus_g_adc-acc_x_minus_g_adc)>>1;
motion_sensor_data.cali.y_gain=(acc_y_plus_g_adc-acc_y_minus_g_adc)>>1;
motion_sensor_data.cali.z_gain=(acc_z_plus_g_adc-acc_z_minus_g_adc)>>1;
if(motion_sensor_data.cali.x_gain==0)
{
motion_sensor_data.cali.x_gain=0;
}
else
motion_sensor_data.cali.x_gain=(1000<<10)/(motion_sensor_data.cali.x_gain);
if(motion_sensor_data.cali.y_gain==0)
{
motion_sensor_data.cali.y_gain=0;
}
else
motion_sensor_data.cali.y_gain=(1000<<10)/(motion_sensor_data.cali.y_gain);
if(motion_sensor_data.cali.z_gain==0)
{
motion_sensor_data.cali.z_gain=0;
}
else
motion_sensor_data.cali.z_gain=(1000<<10)/(motion_sensor_data.cali.z_gain);
/*use zerog as reference*/
acc_x_plus_g_adc=motion_sensor_data.cali.x_offset;
acc_x_minus_g_adc=motion_sensor_data.cali.x_offset;
acc_y_plus_g_adc=motion_sensor_data.cali.y_offset;
acc_y_minus_g_adc=motion_sensor_data.cali.y_offset;
acc_z_plus_g_adc=motion_sensor_data.cali.z_offset;
acc_z_minus_g_adc=motion_sensor_data.cali.z_offset;
//motion_sensor_read_cali(&test_cali);
}
/*
* FUNCTION
* motion_sensor_check_cali
*
* DESCRIPTION
* This function is to check calibration data
*
* CALLS
*
* PARAMETERS
* x: ADC in X-axis
* y: ADC in Y-axis
* z: ADC in Z-axis
*
* RETURNS
* None
*/
void motion_sensor_check_cali(kal_int16 x, kal_int16 y, kal_int16 z)
{
if(acc_execute_cali==KAL_TRUE)
{
if(x>acc_x_plus_g_adc)
acc_x_plus_g_adc=x;
else if(x<acc_x_minus_g_adc)
acc_x_minus_g_adc =x;
if(y>acc_y_plus_g_adc)
acc_y_plus_g_adc=y;
else if(y<acc_y_minus_g_adc)
acc_y_minus_g_adc =y;
if(z>acc_z_plus_g_adc)
acc_z_plus_g_adc=z;
else if(z<acc_z_minus_g_adc)
acc_z_minus_g_adc =z;
}
}
/*
* FUNCTION
* motion_sensor_power
*
* DESCRIPTION
* This function is to enable or disable motion sensor module
*
* CALLS
*
* PARAMETERS
* enable: enable or disable
*
* RETURNS
* None
*/
kal_bool motion_sensor_power(kal_bool enable)
{
if(enable)
{
motion_sensor_custom_fp->ms_power_up();
}
else
{
motion_sensor_custom_fp->ms_power_down();
motion_sensor_sample(KAL_FALSE);
motion_sensor_reset();
}
return KAL_TRUE;
}
/*
* FUNCTION
* motion_sensor_conf_sample_period
*
* DESCRIPTION
* This function is to configure sample period
*
* CALLS
*
* PARAMETERS
* period: sample period
*
* RETURNS
* None
*/
void motion_sensor_conf_sample_period(kal_uint32 period)
{
motion_sensor_data.sample_period=(kal_uint8)period;
}
/*
* FUNCTION
* motion_sensor_eint_hisr
*
* DESCRIPTION
* This function is to handle low-g and high-g interrupt
*
* CALLS
*
* PARAMETERS
* None
*
* RETURNS
* None
*/
void motion_sensor_eint_hisr(void)
{
kal_uint32 savedMask;
kal_bool low_g, high_g;
if(motion_sensor_custom_dp->int_support==KAL_TRUE)
{
savedMask = SaveAndSetIRQMask();
motion_sensor_custom_fp->ms_read_int_status(&low_g, &high_g);
motion_sensor_custom_fp->ms_clear_int_status();
if(low_g!=KAL_FALSE&&motion_sensor_data.low_g_cb!=NULL)
motion_sensor_data.low_g_cb();
if(high_g!=KAL_FALSE&&motion_sensor_data.high_g_cb!=NULL)
motion_sensor_data.high_g_cb();
RestoreIRQMask(savedMask);
}
else /*interrupt should not be triggered*/
ASSERT(0);
}
/*
* FUNCTION
* motion_sensor_init
*
* DESCRIPTION
* This function is to initialize motion sensor driver
*
* CALLS
*
* PARAMETERS
* None
*
* RETURNS
* None
*/
void motion_sensor_init(void)
{
/*get customization functin data and function pointr*/
motion_sensor_custom_fp=ms_GetFunc();
motion_sensor_custom_fp->ms_custom_init();
motion_sensor_custom_dp=motion_sensor_custom_fp->ms_get_data();
//motion_sensor_cb_registration(acc_test_cb, NULL);
GPTI_GetHandle(&motion_sensor_data.gpt_handle);
motion_sensor_conf_sample_period(2);
motion_sensor_conf_filter(10);
/*interrupt*/
if(motion_sensor_custom_dp->int_support==KAL_TRUE)
{
extern kal_int32 EINT_SW_Debounce_Modify(kal_uint8 eintno, kal_uint8 debounce_time);
motion_sensor_custom_fp->ms_configure_low_g(1,100);
motion_sensor_custom_fp->ms_configure_high_g(1,1000);
EINT_SW_Debounce_Modify(motion_sensor_custom_dp->int_chan, 10);
EINT_Mask(motion_sensor_custom_dp->int_chan);
EINT_Registration(motion_sensor_custom_dp->int_chan, KAL_TRUE,
motion_sensor_custom_dp->int_level,motion_sensor_eint_hisr, KAL_TRUE);
}
/*calibration*/
motion_sensor_data.cali.x_offset=motion_sensor_custom_dp->x_0g_adc;
motion_sensor_data.cali.y_offset=motion_sensor_custom_dp->y_0g_adc;
motion_sensor_data.cali.z_offset=motion_sensor_custom_dp->z_0g_adc;
motion_sensor_data.cali.x_gain=
(motion_sensor_custom_dp->x_1g_adc-motion_sensor_custom_dp->x_minus1g_adc)>>1;
motion_sensor_data.cali.y_gain=
(motion_sensor_custom_dp->y_1g_adc-motion_sensor_custom_dp->y_minus1g_adc)>>1;
motion_sensor_data.cali.z_gain=
(motion_sensor_custom_dp->z_1g_adc-motion_sensor_custom_dp->z_minus1g_adc)>>1;
if(motion_sensor_data.cali.x_gain==0)
{
motion_sensor_data.cali.x_gain=0;
}
else
motion_sensor_data.cali.x_gain=(1000<<10)/(motion_sensor_data.cali.x_gain);
if(motion_sensor_data.cali.y_gain==0)
{
motion_sensor_data.cali.y_gain=0;
}
else
motion_sensor_data.cali.y_gain=(1000<<10)/(motion_sensor_data.cali.y_gain);
if(motion_sensor_data.cali.z_gain==0)
{
motion_sensor_data.cali.z_gain=0;
}
else
motion_sensor_data.cali.z_gain=(1000<<10)/(motion_sensor_data.cali.z_gain);
/*use zerog as reference*/
acc_x_plus_g_adc=motion_sensor_data.cali.x_offset;
acc_x_minus_g_adc=motion_sensor_data.cali.x_offset;
acc_y_plus_g_adc=motion_sensor_data.cali.y_offset;
acc_y_minus_g_adc=motion_sensor_data.cali.y_offset;
acc_z_plus_g_adc=motion_sensor_data.cali.z_offset;
acc_z_minus_g_adc=motion_sensor_data.cali.z_offset;
}
/*
* FUNCTION
* motion_sensor_flush_buff
*
* DESCRIPTION
* This function is to flush data buffer
*
* CALLS
*
* PARAMETERS
* None
*
* RETURNS
* None
*/
void motion_sensor_flush_buff(void)
{
motion_flush_data_buffer();
}
/*
* FUNCTION
* motion_sensor_conf_filter
*
* DESCRIPTION
* This function is to configure filter offset
*
* CALLS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -