/*!
*****************************************************************************
* @file: bsp_spi_ADXL355_driver.c
* @brief: ADXL355 accelerometer IC
* @version: $Revision$
* @date: $Date$
*-----------------------------------------------------------------------------
*
*****************************************************************************/
/***************************** Include Files **********************************/
#include "bsp_spi_ADXL355_driver.h"
#include "main.h"
#include "myLogger.h"
#include "spi.h"
/****************************** Global Data ***********************************/
int32_t volatile i32SensorX;
int32_t volatile i32SensorY;
int32_t volatile i32SensorZ;
int32_t volatile i32SensorT;
uint32_t volatile ui32SensorX;
uint32_t volatile ui32SensorY;
uint32_t volatile ui32SensorZ;
uint32_t volatile ui32SensorT;
/************************* Static functions *****************************/
/**
* @brief Select ADXL355 by resetting CHIP SELECT Pin
*
* @return none
**/
static void ADXL355_Select()
{
HAL_GPIO_WritePin(ADXL1_CS_GPIO_Port, ADXL1_CS_Pin, GPIO_PIN_RESET);
}
/**
* @brief Deselect ADXL355 by setting CHIP SELECT Pin
*
* @return none
**/
static void ADXL355_Unselect()
{
HAL_GPIO_WritePin(ADXL1_CS_GPIO_Port, ADXL1_CS_Pin, GPIO_PIN_SET);
}
/************************* Global scope functions *****************************/
uint32_t ADXL355_GetID(SPI_HandleTypeDef *hspi)
{
uint8_t device_ad = 0, device_mst = 0, partid = 0, revid = 0;
device_ad = adxl35x_readOneByte(hspi, ADXL355_DEVID_AD);
device_mst = adxl35x_readOneByte(hspi, ADXL355_DEVID_MST);
partid = adxl35x_readOneByte(hspi, ADXL355_PARTID);
revid = adxl35x_readOneByte(hspi, ADXL355_REVID);
return (uint32_t)(device_ad << 24) | (device_mst << 16) | (partid << 8) | revid;
}
uint8_t read = 0;
adxl35x_operation_e ADXL355_Interrupt(SPI_HandleTypeDef *hspi, uint8_t function)
{
adxl35x_writeOneByte(hspi, ADXL355_INT_MAP, function);
read = adxl35x_readOneByte(hspi, ADXL355_INT_MAP);
if(read != function)
{
return ADXL35X_SET_PARA_ERROR;
}
return ADXL35X_OK;
}
adxl35x_operation_e ADXL355_Reset(SPI_HandleTypeDef *hspi)
{
adxl35x_writeOneByte(hspi, ADXL355_RESET, 0x52);
return ADXL35X_OK;
}
adxl35x_operation_e ADXL355_SetFifoLimit(SPI_HandleTypeDef *hspi, uint8_t fifoLimit)
{
uint8_t read = 0;
adxl35x_writeOneByte(hspi, ADXL355_FIFO_SAMPLES, fifoLimit);
read = adxl35x_readOneByte(hspi,ADXL355_FIFO_SAMPLES);
if(read != fifoLimit)
{
return ADXL35X_SET_PARA_ERROR;
}
return ADXL35X_OK;
}
adxl35x_operation_e ADXL355_GetDataFromFifo(SPI_HandleTypeDef *hspi, int32_t *data,uint16_t groupLen)
{
uint8_t temp[3] = {0};
uint8_t i,j;
int32_t *pData = data;
for(j=0;j<groupLen;j++)
{
for(i=0;i<3;i++)
{
adxl35x_readMultiBytes(hspi, ADXL355_FIFO_DATA, temp, 3);
pData[i] = (((temp[0]<<16) | (temp[1]<<8) | temp[2]) >>4);// 数据部分(补码)
if(pData[i] & 0x80000)
pData[i] = pData[i] | 0xFFF00000; // 补全为标准的int32_t形式(int32_t存储补码)
}
pData += 3;
}
return ADXL35X_OK;
}
uint8_t ADXL355_GetStatus(SPI_HandleTypeDef *hspi)
{
return adxl35x_readOneByte(hspi, ADXL355_STATUS);
}
uint8_t ADXL355_GetFifoNums(SPI_HandleTypeDef *hspi)
{
return adxl35x_readOneByte(hspi,ADXL355_FIFO_ENTRIES);
}
uint8_t IT;
uint32_t getID;
void ADXL355_Init(SPI_HandleTypeDef *hspi, ADXL355_HandleTypeDef *ADXL355_t)
{
hspi->Instance->CR2 |= (1<<0); // 设置TSIZE
__HAL_SPI_ENABLE(hspi);
ADXL355_Reset(hspi);
ADXL355_SetRange(hspi, ADXL355_t->Range); // 设置加速度范围
ADXL355_SetFilter(hspi, ADXL355_t->HighPass, ADXL355_t->LowPass); // 设置滤波器
ADXL355_Interrupt(hspi, ADXL355_t->interrupt); // 设置中断引脚
ADXL355_SetFifoLimit(hspi, ADXL355_t->Samples_points); // 设置FIfo
ADXL355_Startup(hspi); // 进入测量模式
IT = adxl35x_readOneByte(hspi, ADXL355_RANGE);
getID = ADXL355_GetID(hspi);
}
/**
* @brief Turns ADXL355 measurement mode.
*
* @param SPI handle Structure
*
* @return none
*
**/
void ADXL355_Startup(SPI_HandleTypeDef *hspi)
{
uint8_t ui8temp;
ui8temp = adxl35x_readOneByte(hspi, ADXL355_POWER_CTL); /* Read POWER_CTL register, before modifying it */
ui8temp &= ~(0x01); /* Set measurement bit in POWER_CTL register */
adxl35x_writeOneByte(hspi, ADXL355_POWER_CTL, ui8temp); /* Write the new value to POWER_CTL register */
}
/**
* @brief Puts ADXL355 into standby mode.
*
* @param SPI handle Structure
*
* @return none
**/
void ADXL355_Standby(SPI_HandleTypeDef *hspi)
{
uint8_t ui8temp;
ui8temp = adxl35x_readOneByte(hspi, ADXL355_POWER_CTL); /* Read POWER_CTL register, before modifying it */
ui8temp |= 0x01; /* Clear measurement bit in POWER_CTL register */
adxl35x_writeOneByte(hspi, ADXL355_POWER_CTL, ui8temp); /* Write the new value to POWER_CTL register */
}
/**
* @brief Reads the accelerometer data.
*
* @param SPI handle Structure
*
* @return none
**/
void ADXL355_ReadData(SPI_HandleTypeDef *hspi)
{
uint8_t Tmp[2];
/* Receive raw acceleration datas from accelerometer */
ui32SensorX = adxl35x_readOneByte(hspi, ADXL355_XDATA3);
ui32SensorY = adxl35x_readOneByte(hspi, ADXL355_YDATA3);
ui32SensorZ = adxl35x_readOneByte(hspi, ADXL355_ZDATA3);
adxl35x_readMultiBytes(hspi, ADXL355_TEMP2, Tmp, SPI_READ_TWO_REG);
ui32SensorT = (Tmp[0] << 8) | Tmp[1];
/* Receive signed integer raw datas */
i32SensorX = ADXL355_AccDataConversion(ui32SensorX);
i32SensorY = ADXL355_AccDataConversion(ui32SensorY);
i32SensorZ = ADXL355_AccDataConversion(ui32SensorZ);
i32SensorT = ADXL355_AccDataConversion(ui32SensorT);
}
/**
* @brief Changes high pass and low pass filter
*
* @param SPI handle Structure
* @param High pass filter value
* @param Low pass filter value(ie: ADXL355_ODR_1000, ADXL355_ODR_2000, ADXL355_ODR_4000);
*
* @return none
*
**/
adxl35x_operation_e ADXL355_SetFilter(SPI_HandleTypeDef *hspi, uint8_t hpf, uint8_t lpf)
{
uint8_t read = 0;
uint8_t filter = 0;
filter = (hpf << 4) | lpf; /* Combine high pass and low pass filter values to send */
adxl35x_writeOneByte(hspi, ADXL355_FILTER, filter);/* Set filter values within FILTER register */
read = adxl35x_readOneByte(hspi, ADXL355_FILTER);
if(read != filter)
{
return ADXL35X_SET_PARA_ERROR;
}
return ADXL35X_OK;
}
/**
* @brief Changes ranges to 2g, 4g or 8g.
*
* @param SPI handle Structure
*
* @return none
**/
adxl35x_operation_e ADXL355_SetRange(SPI_HandleTypeDef *hspi, uint8_t range)
{
uint8_t read = 0;
adxl35x_writeOneByte(hspi, ADXL355_RANGE, range);
read = adxl35x_readOneByte(hspi, ADXL355_RANGE);
if((read & 0x03) != (range & 0x03))
{
return ADXL35X_SET_PARA_ERROR;
}
return ADXL35X_OK;
}
/**
* @brief Convert the two's complement data in X,Y,Z registers to signed integers
*
* @param Raw data from register
*
* @return int32_t converted signed integer data
**/
int32_t ADXL355_AccDataConversion(uint32_t ui32SensorData)
{
int32_t volatile i32Conversion = 0;
ui32SensorData = (ui32SensorData >> 4);
ui32SensorData = (ui32SensorData & 0x000FFFFF);
if ((ui32SensorData & 0x00080000) == 0x00080000)
{
i32Conversion = (ui32SensorData | 0xFFF00000);
}
else
{
i32Conversion = ui32SensorData;
}
return i32Conversion;
}
/* 内部读写函数实现 */
#if 1
static uint8_t acclerate_1_spi_writeReadByte(SPI_HandleTypeDef *hspi, uint8_t txByte)
{
uint8_t ret = 0;
if(hspi->State != HAL_SPI_STATE_READY)
{
return HAL_BUSY;
}
__HAL_LOCK(hspi);
hspi->Instance->CR1 |= (1<<9);//开启主传输
while(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXP)==RESET);
*((__IO uint8_t *)&hspi->Ins
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ADXL355.zip (6个子文件)
filter.c 2KB
ADXL355_APP.c 4KB
bsp_spi_ADXL355_driver.c 10KB
filter.h 284B
bsp_spi_ADXL355_driver.h 8KB
ADXL355_APP.h 182B
共 6 条
- 1
资源评论
千里马02
- 粉丝: 3404
- 资源: 54
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功