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 初始化
|
|
* 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");
|
|
}
|
|
|
|
//软件复位, 寄存器到默认配值
|
|
// 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); //满量程设置
|
|
|
|
lis2dh12_data_rate_set(&dev_ctx1,LIS2DH12_ODR_100Hz);//设置输出数据率
|
|
|
|
lis2dh12_act_threshold_set(&dev_ctx1, 0x10); //设置活动/非活动阈值
|
|
|
|
lis2dh12_act_timeout_set(&dev_ctx1, 0x10); //设置活动/非活动时间超时
|
|
|
|
lis2dh12_operating_mode_set(&dev_ctx1, LIS2DH12_LP_8bit); //设置低功耗模式
|
|
|
|
lis2dh12_block_data_update_set(&dev_ctx1,PROPERTY_ENABLE);//输出寄存器更新时锁定
|
|
|
|
reg6.i2_act = 1;
|
|
reg6.int_polarity = 0;
|
|
lis2dh12_pin_int2_config_set(&dev_ctx1,®6); //活动事件信号映射到 INT2 引脚
|
|
|
|
lis2dh12_xyz_axis_enable_set(&dev_ctx1,PROPERTY_ENABLE); //使能三轴加速度计
|
|
}
|
|
/*************************************************************************************************
|
|
* Function Name:
|
|
* Description : Lis2dh活动状态判断
|
|
* 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 : 使能XYZ的加速度传感器
|
|
* 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 : 寄存器写
|
|
* 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 : 寄存器读
|
|
* 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 : 延时
|
|
* Arguments :
|
|
* Return Value :
|
|
*************************************************************************************************/
|
|
static void platform1_delay(uint16_t ms)
|
|
{
|
|
delay_ms(ms);
|
|
}
|
|
|
|
/*************************************************************************************************/
|
|
|
|
|
|
|