forked from wanrenqi/PASII
142 lines
3.7 KiB
C
142 lines
3.7 KiB
C
|
/*****************************************************************
|
|||
|
;Project:
|
|||
|
;MCU:
|
|||
|
;Date:
|
|||
|
;File:
|
|||
|
;Function:
|
|||
|
******************************************************************/
|
|||
|
#include "lis2dw.h"
|
|||
|
#include "gpio.h"
|
|||
|
#include "i2c.h"
|
|||
|
#include "lis2dw12_reg.h"
|
|||
|
#include "delay.h"
|
|||
|
#include "DebugLog.h"
|
|||
|
|
|||
|
stmdev_ctx_t dev_ctx1;
|
|||
|
|
|||
|
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);
|
|||
|
|
|||
|
/*---------------------------------------------------------------*/
|
|||
|
//Lis2dw IO <20><>ʼ<EFBFBD><CABC>
|
|||
|
void Lis2dw_IO_Init(void)
|
|||
|
{
|
|||
|
//IIC IO<49><4F>ʼ<EFBFBD><CABC>
|
|||
|
i2c_Init(I2C1,U32BIT(I2C1_SCLK),U32BIT(I2C1_SDA));
|
|||
|
}
|
|||
|
|
|||
|
/*---------------------------------------------------------------*/
|
|||
|
//Lis2dw <20><>ʼ<EFBFBD><CABC>
|
|||
|
void Lis2dw_Init(void)
|
|||
|
{
|
|||
|
uint8_t temp = 0;
|
|||
|
lis2dw12_ctrl4_int1_pad_ctrl_t int_route;
|
|||
|
|
|||
|
dev_ctx1.write_reg = platform1_write;
|
|||
|
dev_ctx1.read_reg = platform1_read;
|
|||
|
dev_ctx1.mdelay = platform1_delay;
|
|||
|
dev_ctx1.master_addr = LIS2DW_ADDRESS;
|
|||
|
|
|||
|
Lis2dw_IO_Init();
|
|||
|
|
|||
|
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Id
|
|||
|
lis2dw12_device_id_get(&dev_ctx1, &temp);
|
|||
|
|
|||
|
if(temp == LIS2DW_ID)
|
|||
|
{
|
|||
|
dbg_printf("device exist\r\n");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
dbg_printf("device not exist\r\n");
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ, <20>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>ֵ
|
|||
|
lis2dw12_reset_set(&dev_ctx1, PROPERTY_ENABLE);
|
|||
|
|
|||
|
do {
|
|||
|
lis2dw12_reset_get(&dev_ctx1, &temp);
|
|||
|
} while (temp);
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̷<EFBFBD>Χ
|
|||
|
lis2dw12_full_scale_set(&dev_ctx1, LIS2DW12_2g);
|
|||
|
|
|||
|
//<2F><><EFBFBD>õ<EFBFBD>ͨ<EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
lis2dw12_filter_path_set(&dev_ctx1, LIS2DW12_LPF_ON_OUT);
|
|||
|
lis2dw12_filter_bandwidth_set(&dev_ctx1,LIS2DW12_ODR_DIV_4);
|
|||
|
|
|||
|
//<2F><><EFBFBD>õ<EFBFBD>Դģʽ
|
|||
|
lis2dw12_power_mode_set(&dev_ctx1, LIS2DW12_CONT_LOW_PWR_12bit);
|
|||
|
|
|||
|
//<2F><><EFBFBD>ü<EFBFBD><C3BC>ٶȼƵĻ<C6B5><C4BB>ѳ<EFBFBD><D1B3><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
lis2dw12_wkup_dur_set(&dev_ctx1,2);
|
|||
|
|
|||
|
//<2F><><EFBFBD>ý<EFBFBD><C3BD><EFBFBD>˯<EFBFBD><CBAF>ģʽǰ<CABD>ij<EFBFBD><C4B3><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
lis2dw12_act_sleep_dur_set(&dev_ctx1,2);
|
|||
|
|
|||
|
//<2F><><EFBFBD>û<EFBFBD><C3BB>Ѽ<EFBFBD><D1BC>ٶȼ<D9B6><C8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<C4BB><EEB6AF>ֵ
|
|||
|
lis2dw12_wkup_threshold_set(&dev_ctx1,2);
|
|||
|
|
|||
|
lis2dw12_wkup_feed_data_set(&dev_ctx1,LIS2DW12_HP_FEED);
|
|||
|
|
|||
|
//<2F><><EFBFBD>ü<EFBFBD><C3BC>ٶȼƼ<C8BC><C6BC><EFBFBD><EFBFBD><EFBFBD><EEB6AF><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9>ģʽ
|
|||
|
lis2dw12_act_mode_set(&dev_ctx1, LIS2DW12_DETECT_ACT_INACT);
|
|||
|
|
|||
|
//ʹ<>ܻ<DCBB><EEB6AF><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|||
|
lis2dw12_pin_int1_route_get(&dev_ctx1, &int_route);
|
|||
|
int_route.int1_wu = PROPERTY_ENABLE;
|
|||
|
lis2dw12_pin_int1_route_set(&dev_ctx1, &int_route);
|
|||
|
|
|||
|
//<2F><><EFBFBD>ü<EFBFBD><C3BC>ٶȼƵ<C8BC><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
lis2dw12_data_rate_set(&dev_ctx1, LIS2DW12_XL_ODR_200Hz);
|
|||
|
}
|
|||
|
|
|||
|
/*---------------------------------------------------------------*/
|
|||
|
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ھ<EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD>״̬
|
|||
|
void Act_State_Judge(void)
|
|||
|
{
|
|||
|
lis2dw12_all_sources_t all_source;
|
|||
|
/* Read status register */
|
|||
|
lis2dw12_all_sources_get(&dev_ctx1, &all_source);
|
|||
|
|
|||
|
/* Check if Activity/Inactivity events */
|
|||
|
if(all_source.wake_up_src.sleep_state_ia)
|
|||
|
{
|
|||
|
dbg_printf("Inactivity Detected\r\n");
|
|||
|
}
|
|||
|
|
|||
|
if(all_source.wake_up_src.wu_ia)
|
|||
|
{
|
|||
|
dbg_printf("Activity Detected\r\n");
|
|||
|
}
|
|||
|
}
|
|||
|
/*---------------------------------------------------------------*/
|
|||
|
//<2F>Ĵ<EFBFBD><C4B4><EFBFBD>д
|
|||
|
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);
|
|||
|
}
|
|||
|
|
|||
|
/*---------------------------------------------------------------*/
|
|||
|
//<2F>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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);
|
|||
|
}
|
|||
|
|
|||
|
/*---------------------------------------------------------------*/
|
|||
|
//<2F><>ʱ
|
|||
|
static void platform1_delay(uint16_t ms)
|
|||
|
{
|
|||
|
delay_ms(ms);
|
|||
|
}
|
|||
|
/*---------------------------------------------------------------*/
|