177 lines
5.3 KiB
C
177 lines
5.3 KiB
C
|
/*************************************************************************************************
|
|||
|
**Filename: lis2dh.c
|
|||
|
**Version :
|
|||
|
**Author :
|
|||
|
**Date :
|
|||
|
**Description:
|
|||
|
**
|
|||
|
*************************************************************************************************/
|
|||
|
#include "lis2dh.h"
|
|||
|
#include "delay.h"
|
|||
|
#include "gpio.h"
|
|||
|
#include "i2c.h"
|
|||
|
#include "mcu_bsp.h"
|
|||
|
#include "DebugLog.h"
|
|||
|
|
|||
|
/*********************************************************************
|
|||
|
* Global Variables (declared in header file with 'extern')
|
|||
|
*/
|
|||
|
stmdev_ctx_t dev_ctx1;
|
|||
|
/*********************************************************************
|
|||
|
* Static Functions ('static')
|
|||
|
*/
|
|||
|
static void platform1_delay(uint16_t ms);
|
|||
|
static int32_t platform1_write(uint8_t I2C_addr, uint8_t addr, uint8_t* value, uint16_t len);
|
|||
|
static int32_t platform1_read(uint8_t I2C_addr,uint8_t regAddress, uint8_t * data, uint16_t len);
|
|||
|
static int32_t lis2dh12_xyz_axis_enable_set(const stmdev_ctx_t *ctx, uint8_t val);
|
|||
|
|
|||
|
/*************************************************************************************************
|
|||
|
* Function Name:
|
|||
|
* Description : Lis2dh <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
* Arguments :
|
|||
|
* Return Value :
|
|||
|
*************************************************************************************************/
|
|||
|
void Lis2dh_Init(void)
|
|||
|
{
|
|||
|
uint8_t temp = 0;
|
|||
|
lis2dh12_fs_t fs;
|
|||
|
lis2dh12_ctrl_reg6_t reg6;
|
|||
|
|
|||
|
dev_ctx1.write_reg = platform1_write;
|
|||
|
dev_ctx1.read_reg = platform1_read;
|
|||
|
dev_ctx1.mdelay = platform1_delay;
|
|||
|
dev_ctx1.master_addr = LIS2DH_ADDRESS;
|
|||
|
|
|||
|
platform1_delay(100);
|
|||
|
|
|||
|
lis2dh12_device_id_get(&dev_ctx1, &temp);
|
|||
|
|
|||
|
if(temp == LIS2DH12_ID)
|
|||
|
{
|
|||
|
//dbg_printf("lis2dh exist\r\n");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// dbg_printf("lis2dh not exist\r\n");
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ, <20>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>ֵ
|
|||
|
// lis2dh12_boot_set(&dev_ctx1,PROPERTY_ENABLE);
|
|||
|
//
|
|||
|
// platform1_delay(100);
|
|||
|
//
|
|||
|
// lis2dh12_boot_set(&dev_ctx1,PROPERTY_DISABLE);
|
|||
|
|
|||
|
// lis2dh12_full_scale_get(&dev_ctx1,&fs);
|
|||
|
//
|
|||
|
// dbg_printf("fs = %d\r\n",fs);
|
|||
|
//
|
|||
|
// lis2dh12_full_scale_set(&dev_ctx1, LIS2DH12_4g);
|
|||
|
//
|
|||
|
// lis2dh12_full_scale_get(&dev_ctx1,&fs);
|
|||
|
//
|
|||
|
// dbg_printf("fs = %d\r\n",fs);
|
|||
|
|
|||
|
lis2dh12_full_scale_set(&dev_ctx1, LIS2DH12_2g); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
lis2dh12_data_rate_set(&dev_ctx1,LIS2DH12_ODR_100Hz);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
lis2dh12_act_threshold_set(&dev_ctx1, 0x10); //<2F><><EFBFBD>û/<2F>ǻ<C7BB><EEB6AF>ֵ
|
|||
|
|
|||
|
lis2dh12_act_timeout_set(&dev_ctx1, 0x10); //<2F><><EFBFBD>û/<2F>ǻʱ<EEB6AF>䳬ʱ
|
|||
|
|
|||
|
lis2dh12_operating_mode_set(&dev_ctx1, LIS2DH12_LP_8bit); //<2F><><EFBFBD>õ<C3B5><CDB9><EFBFBD>ģʽ
|
|||
|
|
|||
|
lis2dh12_block_data_update_set(&dev_ctx1,PROPERTY_ENABLE);//<2F><><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
reg6.i2_act = 1;
|
|||
|
reg6.int_polarity = 0;
|
|||
|
lis2dh12_pin_int2_config_set(&dev_ctx1,®6); //<2F><EFBFBD>¼<EFBFBD><C2BC>ź<EFBFBD>ӳ<EFBFBD>䵽 INT2 <20><><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
lis2dh12_xyz_axis_enable_set(&dev_ctx1,PROPERTY_ENABLE); //ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶȼ<D9B6>
|
|||
|
}
|
|||
|
/*************************************************************************************************
|
|||
|
* Function Name:
|
|||
|
* Description : Lis2dh<EFBFBD>״̬<EFBFBD>ж<EFBFBD>
|
|||
|
* Arguments :
|
|||
|
* Return Value :
|
|||
|
*************************************************************************************************/
|
|||
|
void Lis2dh_Act_State_Judge(void)
|
|||
|
{
|
|||
|
if((GPIO_Pin_Read(U32BIT(LIS2D_INT2)) & U32BIT(LIS2D_INT2))!= 0)
|
|||
|
{
|
|||
|
dbg_printf("Act_State\r\n");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
dbg_printf("Inact_State\r\n");
|
|||
|
}
|
|||
|
}
|
|||
|
/*************************************************************************************************
|
|||
|
* Function Name:
|
|||
|
* Description : ʹ<EFBFBD><EFBFBD>XYZ<EFBFBD>ļ<EFBFBD><EFBFBD>ٶȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* Arguments :
|
|||
|
* Return Value :
|
|||
|
*************************************************************************************************/
|
|||
|
static int32_t lis2dh12_xyz_axis_enable_set(const stmdev_ctx_t *ctx, uint8_t val)
|
|||
|
{
|
|||
|
lis2dh12_ctrl_reg1_t ctrl_reg1;
|
|||
|
int32_t ret;
|
|||
|
|
|||
|
ret = lis2dh12_read_reg(ctx, LIS2DH12_CTRL_REG1, (uint8_t *)&ctrl_reg1, 1);
|
|||
|
|
|||
|
if (ret == 0) {
|
|||
|
ctrl_reg1.xen = val;
|
|||
|
ctrl_reg1.yen = val;
|
|||
|
ctrl_reg1.zen = val;
|
|||
|
ret = lis2dh12_write_reg(ctx, LIS2DH12_CTRL_REG1, (uint8_t *)&ctrl_reg1, 1);
|
|||
|
}
|
|||
|
|
|||
|
return ret;
|
|||
|
}
|
|||
|
/*************************************************************************************************
|
|||
|
* Function Name:
|
|||
|
* Description : <EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>д
|
|||
|
* Arguments :
|
|||
|
* Return Value :
|
|||
|
*************************************************************************************************/
|
|||
|
static int32_t platform1_write(uint8_t I2C_addr, uint8_t addr, uint8_t* value, uint16_t len)
|
|||
|
{
|
|||
|
ErrorStatus bret = SUCCESS;
|
|||
|
|
|||
|
bret = i2c_1_write(I2C_addr, 1, addr, value, len);
|
|||
|
|
|||
|
return (int32_t)(!bret);
|
|||
|
}
|
|||
|
|
|||
|
/*************************************************************************************************
|
|||
|
* Function Name:
|
|||
|
* Description : <EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* Arguments :
|
|||
|
* Return Value :
|
|||
|
*************************************************************************************************/
|
|||
|
static int32_t platform1_read(uint8_t I2C_addr,uint8_t regAddress, uint8_t * data, uint16_t len)
|
|||
|
{
|
|||
|
ErrorStatus bret = SUCCESS;
|
|||
|
|
|||
|
bret = i2c_1_read(I2C_addr, 1, regAddress, data, len);
|
|||
|
|
|||
|
return (int32_t)(!bret);
|
|||
|
}
|
|||
|
|
|||
|
/*************************************************************************************************
|
|||
|
* Function Name:
|
|||
|
* Description : <EFBFBD><EFBFBD>ʱ
|
|||
|
* Arguments :
|
|||
|
* Return Value :
|
|||
|
*************************************************************************************************/
|
|||
|
static void platform1_delay(uint16_t ms)
|
|||
|
{
|
|||
|
delay_ms(ms);
|
|||
|
}
|
|||
|
|
|||
|
/*************************************************************************************************/
|
|||
|
|
|||
|
|
|||
|
|