/*******************************************************************************
|
* File Name : Lis3dhApp.c
|
* Description :
|
* Created on : 2021Äê5ÔÂ30ÈÕ
|
* Author : www.hido-studio.com
|
*******************************************************************************/
|
|
/*******************************************************************************
|
* Include Files *
|
*******************************************************************************/
|
#include "Lis3dhApp.h"
|
#include "lis3dh_reg.h"
|
#include "HIDO_Debug.h"
|
#include "RTC.h"
|
#include "SPI.h"
|
#include "string.h"
|
#include "Device.h"
|
#include "LCD.h"
|
|
/*******************************************************************************
|
* Macro *
|
*******************************************************************************/
|
|
/*******************************************************************************
|
* Type Definition *
|
*******************************************************************************/
|
typedef union{
|
int16_t i16bit[3];
|
uint8_t u8bit[6];
|
} axis3bit16_t;
|
|
/*******************************************************************************
|
* Local Variable *
|
*******************************************************************************/
|
static stmdev_ctx_t dev_ctx;
|
static HIDO_BOOL l_bWakeUp = HIDO_FALSE;
|
static HIDO_UINT32 l_u32MoveTS = 0;
|
|
/*******************************************************************************
|
* Local Function Declaration *
|
*******************************************************************************/
|
|
/*******************************************************************************
|
* Local Function *
|
*******************************************************************************/
|
/*******************************************************************************
|
* Function Name :
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021Äê5ÔÂ30ÈÕ
|
*******************************************************************************/
|
static HIDO_INT32 platform_init(HIDO_VOID)
|
{
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
|
SPI_Enable(SPI_ID_3D);
|
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Function Name :
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021Äê5ÔÂ30ÈÕ
|
*******************************************************************************/
|
static HIDO_INT32 platform_write(HIDO_VOID *handle, HIDO_UINT8 reg, HIDO_UINT8 *bufp, HIDO_UINT16 len)
|
{
|
HIDO_UINT16 i = 0;
|
|
for(i = 0; i < len; i++)
|
{
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
|
SPI_Write(SPI_ID_3D, ®, 1);
|
SPI_Write(SPI_ID_3D, &bufp[i], 1);
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
|
reg++;
|
}
|
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Function Name :
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021Äê5ÔÂ30ÈÕ
|
*******************************************************************************/
|
static HIDO_INT32 platform_read(HIDO_VOID *handle, HIDO_UINT8 reg, HIDO_UINT8 *bufp,
|
HIDO_UINT16 len)
|
{
|
HIDO_UINT16 i = 0;
|
|
reg |= 0x80;
|
for(i = 0; i < len; i++)
|
{
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
|
SPI_Write(SPI_ID_3D, ®, 1);
|
SPI_ReadWrite(SPI_ID_3D, &bufp[i], HIDO_NULL, 1);
|
reg++;
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
|
}
|
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Global Function *
|
*******************************************************************************/
|
/*******************************************************************************
|
* Function Name : Lis3dhApp_ISR
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021Äê5ÔÂ30ÈÕ
|
*******************************************************************************/
|
HIDO_VOID Lis3dhApp_ISR(HIDO_UINT16 _u16Pin)
|
{
|
if((HIDO_NULL == dev_ctx.read_reg) || (HIDO_NULL == dev_ctx.write_reg))
|
{
|
return;
|
}
|
|
l_bWakeUp = HIDO_TRUE;
|
}
|
|
/*******************************************************************************
|
* Function Name : Lis3dhApp_IsWakeUp
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021Äê5ÔÂ30ÈÕ
|
*******************************************************************************/
|
HIDO_BOOL Lis3dhApp_IsWakeUp(HIDO_VOID)
|
{
|
HIDO_BOOL bWakeUp = l_bWakeUp;
|
l_bWakeUp = HIDO_FALSE;
|
|
return bWakeUp;
|
}
|
|
/*******************************************************************************
|
* Function Name : Lis3dhApp_GetMoveState
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021Äê5ÔÂ30ÈÕ
|
*******************************************************************************/
|
HIDO_UINT8 Lis3dhApp_GetMoveState(HIDO_VOID)
|
{
|
if((RTC_GetSeconds() - l_u32MoveTS) < Device_GetSettings()->m_stManDownAlarm.m_u16MotionlessTime)
|
{
|
return 1;
|
}
|
|
return 0;
|
}
|
|
/*******************************************************************************
|
* Function Name : Lis3dhApp_ClearMoveState
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021Äê5ÔÂ30ÈÕ
|
*******************************************************************************/
|
HIDO_INT32 Lis3dhApp_ClearMoveState(HIDO_VOID)
|
{
|
l_u32MoveTS = RTC_GetSeconds();
|
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Function Name : Lis3dhApp_Init
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021Äê5ÔÂ30ÈÕ
|
*******************************************************************************/
|
HIDO_INT32 Lis3dhApp_Poll(HIDO_VOID)
|
{
|
lis3dh_int1_src_t src;
|
|
lis3dh_int1_gen_source_get(&dev_ctx, &src);
|
|
if (1 == src.yh)
|
{
|
LCD_SetFlip(HIDO_FALSE);
|
}
|
|
if (1 == src.xl)
|
{
|
LCD_SetFlip(HIDO_TRUE);
|
}
|
|
lis3dh_reg_t reg;
|
HIDO_FLOAT acc_x, acc_y, acc_z, acc_g;
|
axis3bit16_t data_raw_acceleration;
|
|
/* Read output only if new value available */
|
lis3dh_xl_data_ready_get(&dev_ctx, ®.byte);
|
if (reg.byte)
|
{
|
/* Read accelerometer data */
|
memset(data_raw_acceleration.u8bit, 0x00, 3 * sizeof(int16_t));
|
lis3dh_acceleration_raw_get(&dev_ctx, data_raw_acceleration.u8bit);
|
acc_x = lis3dh_from_fs2_hr_to_mg(data_raw_acceleration.i16bit[0]);
|
acc_y = lis3dh_from_fs2_hr_to_mg(data_raw_acceleration.i16bit[1]);
|
acc_z = lis3dh_from_fs2_hr_to_mg(data_raw_acceleration.i16bit[2]);
|
|
HIDO_FLOAT fDis = 50;
|
|
if(Device_GetSettings()->m_stManDownAlarm.m_u8Sensitivity != 0)
|
{
|
fDis = 80;
|
}
|
|
if (fabs(acc_x) > fDis || fabs(acc_y) > fDis || fabs(acc_z) > fDis)
|
{
|
// HIDO_Debug("acc_x=%.2f acc_y=%.2f acc_z=%.2f\r\n", acc_x, acc_y, acc_z);
|
l_u32MoveTS = RTC_GetSeconds();
|
}
|
}
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Function Name : Lis3dhApp_ClearINT
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021Äê5ÔÂ30ÈÕ
|
*******************************************************************************/
|
HIDO_INT32 Lis3dhApp_ClearINT(HIDO_VOID)
|
{
|
lis3dh_int1_src_t src;
|
|
/*
|
* Read INT pin 1 in polling mode
|
* or read src status register
|
*/
|
lis3dh_int1_gen_source_get(&dev_ctx, &src);
|
while (src.xh || src.yh || src.zh)
|
{
|
lis3dh_int1_gen_source_get(&dev_ctx, &src);
|
}
|
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Function Name : Lis3dhApp_Init
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021Äê5ÔÂ30ÈÕ
|
*******************************************************************************/
|
HIDO_INT32 Lis3dhApp_Init(HIDO_VOID)
|
{
|
/*
|
* Initialize mems driver interface
|
*/
|
|
HIDO_UINT8 whoamI = 0;
|
|
lis3dh_int1_cfg_t int1_cfg;
|
lis3dh_ctrl_reg3_t ctrl_reg3;
|
HIDO_UINT8 dummy;
|
|
dev_ctx.write_reg = platform_write;
|
dev_ctx.read_reg = platform_read;
|
dev_ctx.handle = HIDO_NULL;
|
|
/*
|
* Initialize platform specific hardware
|
*/
|
platform_init();
|
|
/*
|
* Check device ID
|
*/
|
lis3dh_device_id_get(&dev_ctx, &whoamI);
|
if (whoamI != LIS3DH_ID)
|
{
|
return HIDO_ERR;
|
}
|
|
lis3dh_full_scale_set(&dev_ctx, LIS3DH_2g);
|
lis3dh_int1_gen_threshold_set(&dev_ctx, 20);
|
lis3dh_operating_mode_set(&dev_ctx, LIS3DH_HR_12bit);
|
lis3dh_data_rate_set(&dev_ctx, LIS3DH_ODR_100Hz);
|
|
int1_cfg.xlie = PROPERTY_ENABLE;
|
int1_cfg.xhie = PROPERTY_ENABLE;
|
int1_cfg.ylie = PROPERTY_ENABLE;
|
|
int1_cfg.yhie = PROPERTY_ENABLE;
|
int1_cfg.zlie = PROPERTY_ENABLE;
|
int1_cfg.zhie = PROPERTY_ENABLE;
|
|
int1_cfg._6d = PROPERTY_ENABLE;
|
int1_cfg.aoi = PROPERTY_ENABLE;
|
lis3dh_int1_gen_conf_set(&dev_ctx, &int1_cfg);
|
|
Lis3dhApp_ISR(0);
|
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
|
|
return HIDO_OK;
|
}
|