硬件平台STM32C8T6+IIS3DWB三轴加速度传感器
- List item
这个代码实现了:
- SPI接口的硬件控制
- 手动NSS片选管理
- 传感器初始化配置
- 加速度和温度的轮询采集
- 通过USB CDC输出格式化数据
代码可以完美运行,USB速率可以达到12M,量程可达±16g,采样速率最高26.6K。
参考原理图:
采用CubeMX生成HAL库,STM32C8T6的引脚配置情况如下,使用SPI1,CS引脚可以根据需求随意设置,USB引脚是固定的。
/* main.c */
#include "main.h"
#include "iis3dwb_reg.h"
SPI_HandleTypeDef hspi1;
extern USBD_HandleTypeDef hUsbDeviceFS;
/* 传感器上下文 */
stmdev_ctx_t dev_ctx;
/* 私有函数声明 */
static void SPI1_Init(void);
static void GPIO_Init(void);
static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len);
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len);
static void Error_Handler(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
GPIO_Init();
SPI1_Init();
MX_USB_DEVICE_Init();
/* 初始化传感器接口 */
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.handle = &hspi1;
/* 等待传感器启动 */
HAL_Delay(100);
/* 验证设备ID */
uint8_t whoamI;
iis3dwb_device_id_get(&dev_ctx, &whoamI);
if (whoamI != IIS3DWB_ID) {
Error_Handler();
}
/* 传感器配置 */
iis3dwb_reset_set(&dev_ctx, PROPERTY_ENABLE);
HAL_Delay(10);
iis3dwb_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
iis3dwb_xl_data_rate_set(&dev_ctx, IIS3DWB_XL_ODR_26k7Hz);
iis3dwb_xl_full_scale_set(&dev_ctx, IIS3DWB_2g);
iis3dwb_xl_filt_path_on_out_set(&dev_ctx, IIS3DWB_LP_ODR_DIV_100);
int16_t data_raw_acc[3];
int16_t data_raw_temp;
float acc_mg[3];
float temp_degC;
uint8_t status;
while (1)
{
/* 轮询加速度数据 */
iis3dwb_xl_flag_data_ready_get(&dev_ctx, &status);
if (status) {
iis3dwb_acceleration_raw_get(&dev_ctx, data_raw_acc);
acc_mg[0] = iis3dwb_from_fs2g_to_mg(data_raw_acc[0]);
acc_mg[1] = iis3dwb_from_fs2g_to_mg(data_raw_acc[1]);
acc_mg[2] = iis3dwb_from_fs2g_to_mg(data_raw_acc[2]);
char buf[100];
int len = snprintf(buf, sizeof(buf), "Acc: X=%.2f mg, Y=%.2f mg, Z=%.2f mg\r\n",
acc_mg[0], acc_mg[1], acc_mg[2]);
CDC_Transmit_FS((uint8_t*)buf, len);
}
/* 轮询温度数据 */
iis3dwb_temp_flag_data_ready_get(&dev_ctx, &status);
if (status) {
iis3dwb_temperature_raw_get(&dev_ctx, &data_raw_temp);
temp_degC = iis3dwb_from_lsb_to_celsius(data_raw_temp);
char buf[50];
int len = snprintf(buf, sizeof(buf), "Temp: %.2f C\r\n", temp_degC);
CDC_Transmit_FS((uint8_t*)buf, len);
}
HAL_Delay(10);
}
}
/* SPI读写实现 */
static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
/* 添加寄存器写标志 */
reg |= 0x00; // 写操作不需要修改寄存器地址
HAL_SPI_Transmit(handle, ®, 1, 100);
HAL_SPI_Transmit(handle, (uint8_t*)bufp, len, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
return 0;
}
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
/* 添加寄存器读标志 */
reg |= 0x80; // 设置读位
HAL_SPI_Transmit(handle, ®, 1, 100);
HAL_SPI_Receive(handle, bufp, len, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
return 0;
}
/* SPI初始化 */
static void SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK) {
Error_Handler();
}
}
/* GPIO初始化 */
static void GPIO_Init(void)
{
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {
0};
/* NSS引脚配置 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
/* SPI引脚配置 */
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
static void Error_Handler(void)
{
while(1) {
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
HAL_Delay(100);
}
}
/* 系统时钟配置和USB初始化需要根据实际工程配置 */
下面是iis3dwb_reg.c和iis3dwb_reg.h文件,是官方的文件,比较重要。
/**
******************************************************************************
* @file iis3dwb_reg.c
* @author Sensors Software Solution Team
* @brief IIS3DWB driver file
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
#include "iis3dwb_reg.h"
#include <string.h>
/**
* @defgroup IIS3DWB
* @brief This file provides a set of functions needed to drive the
* iis3dwb enhanced inertial module.
* @{
*
*/
/**
* @defgroup IIS3DWB_Interfaces_Functions
* @brief This section provide a set of functions used to read and
* write a generic register of the device.
* MANDATORY: return 0 -> no Error.
* @{
*
*/
/**
* @brief Read generic device register
*
* @param ctx read / write interface definitions(ptr)
* @param reg register to read
* @param data pointer to buffer that store the data read(ptr)
* @param len number of consecutive register to read
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t __weak iis3dwb_read_reg(const stmdev_ctx_t *ctx, uint8_t reg,
uint8_t *data,
uint16_t len)
{
if (ctx == NULL) return -1;
return ctx->read_reg(ctx->handle, reg, data, len);
}
/**
* @brief Write generic device register
*
* @param ctx read / write interface definitions(ptr)
* @param reg register to write
* @param data pointer to data to write in register reg(ptr)
* @param len number of consecutive register to write
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t __weak iis3dwb_write_reg(const stmdev_ctx_t *ctx, uint8_t reg,
uint8_t *data,
uint16_t len)
{
if (ctx == NULL) return -1;
return ctx->write_reg(ctx->handle, reg, data, len);
}
/**
* @}
*
*/
/**
* @defgroup Private functions
* @brief Section collect all the utility functions needed by APIs.
* @{
*
*/
static void bytecpy(uint8_t *target, const uint8_t *source)
{
if ((target != NULL) && (source != NULL))
{
*target = *source;
}
}
/**
* @}
*
*/
/**
* @defgroup IIS3DWB_Sensitivity
* @brief These functions convert raw-data into engineering units.
* @{
*
*/
float_t iis3dwb_from_fs2g_to_mg(int16_t lsb)
{
return ((float_t)lsb * 0.061f);
}
float_t iis3dwb_from_fs4g_to_mg(int16_t lsb)
{
return ((float_t)lsb * 0.122f);
}
float_t iis3dwb_from_fs8g_to_mg(int16_t lsb)
{
return ((float_t)lsb * 0.244f);
}
float_t iis3dwb_from_fs16g_to_mg(int16_t lsb)
{
return ((float_t)lsb * 0.488f);
}
float_t iis3dwb_from_lsb_to_celsius(int16_t lsb)
{
return (((float_t)lsb / 256.0f) + 25.0f);
}
float_t iis3dwb_from_lsb_to_nsec(int32_t lsb)
{
return ((float_t)lsb * 25000.0f);
}
/**
* @}
*
*/
/**
* @defgroup LSM9DS1_Data_generation
* @brief This section groups all the functions concerning data
* generation
* @{
*
*/
/**
* @brief Accelerometer full-scale selection[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of fs_xl in reg CTRL1_XL
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_full_scale_set(const stmdev_ctx_t *ctx,
iis3dwb_fs_xl_t val)
{
iis3dwb_ctrl1_xl_t ctrl1_xl;
int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL1_XL, (uint8_t *)&ctrl1_xl, 1);
if (ret == 0)
{
ctrl1_xl.fs_xl = (uint8_t)val;
ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL1_XL,
(uint8_t *)&ctrl1_xl, 1);
}
return ret;
}
/**
* @brief Accelerometer full-scale selection.[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Get the values of fs_xl in reg CTRL1_XL
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_full_scale_get(const stmdev_ctx_t *ctx,
iis3dwb_fs_xl_t *val)
{
iis3dwb_ctrl1_xl_t ctrl1_xl;
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL1_XL, (uint8_t *)&ctrl1_xl, 1);
switch (ctrl1_xl.fs_xl)
{
case IIS3DWB_2g:
*val = IIS3DWB_2g;
break;
case IIS3DWB_16g:
*val = IIS3DWB_16g;
break;
case IIS3DWB_4g:
*val = IIS3DWB_4g;
break;
case IIS3DWB_8g:
*val = IIS3DWB_8g;
break;
default:
*val = IIS3DWB_2g;
break;
}
return ret;
}
/**
* @brief Accelerometer UI data rate selection.[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of xl_en in reg CTRL1_XL
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_data_rate_set(const stmdev_ctx_t *ctx,
iis3dwb_odr_xl_t val)
{
iis3dwb_ctrl1_xl_t ctrl1_xl;
int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL1_XL, (uint8_t *)&ctrl1_xl, 1);
if (ret == 0)
{
ctrl1_xl.xl_en = (uint8_t)val;
ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL1_XL,
(uint8_t *)&ctrl1_xl, 1);
}
return ret;
}
/**
* @brief Accelerometer UI data rate selection.[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Get the values of odr_xl in reg CTRL1_XL
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_data_rate_get(const stmdev_ctx_t *ctx,
iis3dwb_odr_xl_t *val)
{
iis3dwb_ctrl1_xl_t ctrl1_xl;
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL1_XL, (uint8_t *)&ctrl1_xl, 1);
switch (ctrl1_xl.xl_en)
{
case IIS3DWB_XL_ODR_OFF:
*val = IIS3DWB_XL_ODR_OFF;
break;
case IIS3DWB_XL_ODR_26k7Hz:
*val = IIS3DWB_XL_ODR_26k7Hz;
break;
default:
*val = IIS3DWB_XL_ODR_OFF;
break;
}
return ret;
}
/**
* @brief Block data update.[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of bdu in reg CTRL3_C
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_block_data_update_set(const stmdev_ctx_t *ctx, uint8_t val)
{
iis3dwb_ctrl3_c_t ctrl3_c;
int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
if (ret == 0)
{
ctrl3_c.bdu = (uint8_t)val;
ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
}
return ret;
}
/**
* @brief Block data update.[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of bdu in reg CTRL3_C
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_block_data_update_get(const stmdev_ctx_t *ctx, uint8_t *val)
{
iis3dwb_ctrl3_c_t ctrl3_c;
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
*val = ctrl3_c.bdu;
return ret;
}
/**
* @brief Weight of XL user offset bits of registers X_OFS_USR (73h),
* Y_OFS_USR (74h), Z_OFS_USR (75h).[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of usr_off_w in reg CTRL6_C
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_offset_weight_set(const stmdev_ctx_t *ctx,
iis3dwb_usr_off_w_t val)
{
iis3dwb_ctrl6_c_t ctrl6_c;
int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
if (ret == 0)
{
ctrl6_c.usr_off_w = (uint8_t)val;
ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
}
return ret;
}
/**
* @brief Weight of XL user offset bits of registers X_OFS_USR (73h),
* Y_OFS_USR (74h), Z_OFS_USR (75h).[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Get the values of usr_off_w in reg CTRL6_C
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_offset_weight_get(const stmdev_ctx_t *ctx,
iis3dwb_usr_off_w_t *val)
{
iis3dwb_ctrl6_c_t ctrl6_c;
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
switch (ctrl6_c.usr_off_w)
{
case IIS3DWB_LSb_1mg:
*val = IIS3DWB_LSb_1mg;
break;
case IIS3DWB_LSb_16mg:
*val = IIS3DWB_LSb_16mg;
break;
default:
*val = IIS3DWB_LSb_1mg;
break;
}
return ret;
}
/**
* @brief select accelerometer axis.[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of xl_axis_sel in reg CTRL6_C and
* the values of _1ax_to_3regout in reg CTRL4_C
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_axis_selection_set(const stmdev_ctx_t *ctx,
iis3dwb_xl_axis_sel_t val)
{
iis3dwb_ctrl4_c_t ctrl4_c;
iis3dwb_ctrl6_c_t ctrl6_c;
int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
if (ret == 0)
{
ctrl4_c._1ax_to_3regout = ((uint8_t)val & 0x10U) >> 4;
ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
}
if (ret == 0)
{
ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
}
if (ret == 0)
{
ctrl6_c.xl_axis_sel = (uint8_t)val;
ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
}
return ret;
}
/**
* @brief select accelerometer axis.[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Get the values of xl_axis_sel in reg CTRL6_C and
* the values of _1ax_to_3regout in reg CTRL4_C.(ptr)
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_axis_selection_get(const stmdev_ctx_t *ctx,
iis3dwb_xl_axis_sel_t *val)
{
iis3dwb_ctrl4_c_t ctrl4_c;
iis3dwb_ctrl6_c_t ctrl6_c;
*val = IIS3DWB_ENABLE_ALL;
int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
if (ret != 0) {
return ret; }
ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
if (ret != 0) {
return ret; }
switch ((ctrl4_c._1ax_to_3regout << 4) + ctrl6_c.xl_axis_sel)
{
case IIS3DWB_ENABLE_ALL:
*val = IIS3DWB_ENABLE_ALL;
break;
case IIS3DWB_ONLY_X_ON_ONE_OUT_REG:
*val = IIS3DWB_ONLY_X_ON_ONE_OUT_REG;
break;
case IIS3DWB_ONLY_Y_ON_ONE_OUT_REG:
*val = IIS3DWB_ONLY_Y_ON_ONE_OUT_REG;
break;
case IIS3DWB_ONLY_Z_ON_ONE_OUT_REG:
*val = IIS3DWB_ONLY_Z_ON_ONE_OUT_REG;
break;
case IIS3DWB_ONLY_X_ON_ALL_OUT_REG:
*val = IIS3DWB_ONLY_X_ON_ALL_OUT_REG;
break;
case IIS3DWB_ONLY_Y_ON_ALL_OUT_REG:
*val = IIS3DWB_ONLY_Y_ON_ALL_OUT_REG;
break;
case IIS3DWB_ONLY_Z_ON_ALL_OUT_REG:
*val = IIS3DWB_ONLY_Z_ON_ALL_OUT_REG;
break;
default:
*val = IIS3DWB_ENABLE_ALL;
break;
}
return ret;
}
/**
* @brief Read all the interrupt flag of the device.[get]
* @param ctx Read / write interface definitions.(ptr)
* @param val Get registers ALL_INT_SRC; WAKE_UP_SRC;
* TAP_SRC; D6D_SRC; STATUS_REG;
* EMB_FUNC_STATUS; FSM_STATUS_A/B
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_all_sources_get(const stmdev_ctx_t *ctx,
iis3dwb_all_sources_t *val)
{
int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_ALL_INT_SRC,
(uint8_t *)&val->all_int_src, 1);
if (ret == 0)
{
ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_SRC,
(uint8_t *)&val->wake_up_src, 1);
}
if (ret == 0)
{
ret = iis3dwb_read_reg(ctx, IIS3DWB_STATUS_REG,
(uint8_t *)&val->status_reg, 1);
}
return ret;
}
/**
* @brief The STATUS_REG register is read by the primary interface.[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Get register STATUS_REG
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_status_reg_get(const stmdev_ctx_t *ctx,
iis3dwb_status_reg_t *val)
{
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_STATUS_REG, (uint8_t *) val, 1);
return ret;
}
/**
* @brief Accelerometer new data available.[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of xlda in reg STATUS_REG
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_flag_data_ready_get(const stmdev_ctx_t *ctx,
uint8_t *val)
{
iis3dwb_status_reg_t status_reg;
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_STATUS_REG,
(uint8_t *)&status_reg, 1);
*val = status_reg.xlda;
return ret;
}
/**
* @brief Temperature new data available.[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of tda in reg STATUS_REG
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_temp_flag_data_ready_get(const stmdev_ctx_t *ctx,
uint8_t *val)
{
iis3dwb_status_reg_t status_reg;
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_STATUS_REG,
(uint8_t *)&status_reg, 1);
*val = status_reg.tda;
return ret;
}
/**
* @brief Enables the accelerometer user offset correction block, can be enabled
* by setting the USR_OFF_ON_OUT bit of the CTRL7_C register.[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of USR_OFF_ON_OUT in reg CTRL7_C
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_usr_offset_block_set(const stmdev_ctx_t *ctx, uint8_t val)
{
iis3dwb_ctrl7_c_t ctrl7_c;
int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL7_C, (uint8_t *)&ctrl7_c, 1);
if (ret == 0)
{
ctrl7_c.usr_off_on_out = val;
ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL7_C, (uint8_t *)&ctrl7_c, 1);
}
return ret;
}
/**
* @brief Enables the accelerometer user offset correction block, can be enabled
* by setting the USR_OFF_ON_OUT bit of the CTRL7_C register.[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of USR_OFF_ON_OUT in reg CTRL7_C
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_usr_offset_block_get(const stmdev_ctx_t *ctx, uint8_t *val)
{
iis3dwb_ctrl7_c_t ctrl7_c;
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL7_C, (uint8_t *)&ctrl7_c, 1);
*val = ctrl7_c.usr_off_on_out;
return ret;
}
/**
* @brief Accelerometer X-axis user offset correction expressed in two’s
* complement, weight depends on USR_OFF_W in CTRL6_C (15h).
* The value must be in the range [-127 127].[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @param buff Buffer that contains data to write
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_usr_offset_x_set(const stmdev_ctx_t *ctx, uint8_t *buff)
{
const int32_t ret = iis3dwb_write_reg(ctx, IIS3DWB_X_OFS_USR, buff, 1);
return ret;
}
/**
* @brief Accelerometer X-axis user offset correction expressed in two’s
* complement, weight depends on USR_OFF_W in CTRL6_C (15h).
* The value must be in the range [-127 127].[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param buff Buffer that stores data read
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_usr_offset_x_get(const stmdev_ctx_t *ctx, uint8_t *buff)
{
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_X_OFS_USR, buff, 1);
return ret;
}
/**
* @brief Accelerometer Y-axis user offset correction expressed in two’s
* complement, weight depends on USR_OFF_W in CTRL6_C (15h).
* The value must be in the range [-127 127].[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @param buff Buffer that contains data to write
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_usr_offset_y_set(const stmdev_ctx_t *ctx, uint8_t *buff)
{
const int32_t ret = iis3dwb_write_reg(ctx, IIS3DWB_Y_OFS_USR, buff, 1);
return ret;
}
/**
* @brief Accelerometer Y-axis user offset correction expressed in two’s
* complement, weight depends on USR_OFF_W in CTRL6_C (15h).
* The value must be in the range [-127 127].[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param buff Buffer that stores data read
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_usr_offset_y_get(const stmdev_ctx_t *ctx, uint8_t *buff)
{
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_Y_OFS_USR, buff, 1);
return ret;
}
/**
* @brief Accelerometer Z-axis user offset correction expressed in two’s
* complement, weight depends on USR_OFF_W in CTRL6_C (15h).
* The value must be in the range [-127 127].[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @param buff Buffer that contains data to write
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_usr_offset_z_set(const stmdev_ctx_t *ctx, uint8_t *buff)
{
const int32_t ret = iis3dwb_write_reg(ctx, IIS3DWB_Z_OFS_USR, buff, 1);
return ret;
}
/**
* @brief Accelerometer X-axis user offset correction expressed in two’s
* complement, weight depends on USR_OFF_W in CTRL6_C (15h).
* The value must be in the range [-127 127].[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param buff Buffer that stores data read
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_xl_usr_offset_z_get(const stmdev_ctx_t *ctx, uint8_t *buff)
{
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_Z_OFS_USR, buff, 1);
return ret;
}
/**
* @}
*
*/
/**
* @defgroup IIS3DWB_Timestamp
* @brief This section groups all the functions that manage the
* timestamp generation.
* @{
*
*/
/**
* @brief Reset timestamp counter.[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_timestamp_rst(const stmdev_ctx_t *ctx)
{
uint8_t rst_val = 0xAA;
return iis3dwb_write_reg(ctx, IIS3DWB_TIMESTAMP2, &rst_val, 1);
}
/**
* @brief Enables timestamp counter.[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of timestamp_en in reg CTRL10_C
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_timestamp_set(const stmdev_ctx_t *ctx, uint8_t val)
{
iis3dwb_ctrl10_c_t ctrl10_c;
int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL10_C, (uint8_t *)&ctrl10_c, 1);
if (ret == 0)
{
ctrl10_c.timestamp_en = (uint8_t)val;
ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL10_C,
(uint8_t *)&ctrl10_c, 1);
}
return ret;
}
/**
* @brief Enables timestamp counter.[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of timestamp_en in reg CTRL10_C
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_timestamp_get(const stmdev_ctx_t *ctx, uint8_t *val)
{
iis3dwb_ctrl10_c_t ctrl10_c;
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL10_C, (uint8_t *)&ctrl10_c, 1);
*val = ctrl10_c.timestamp_en;
return ret;
}
/**
* @brief Timestamp first data output register (r).
* The value is expressed as a 32-bit word and the bit resolution
* is 25 μs.[get]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Buffer that stores data read
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_timestamp_raw_get(const stmdev_ctx_t *ctx, uint32_t *val)
{
uint8_t buff[4];
const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_TIMESTAMP0, buff, 4);
*val = buff[3];
*val = (*val * 256U) + buff[2];
*val = (*val * 256U) + buff[1];
*val = (*val * 256U) + buff[0];
return ret;
}
/**
* @}
*
*/
/**
* @defgroup IIS3DWB_Data output
* @brief This section groups all the data output functions.
* @{
*
*/
/**
* @brief Circular burst-mode (rounding) read of the output registers.[set]
*
* @param ctx Read / write interface definitions.(ptr)
* @param val Change the values of rounding in reg CTRL5_C
* @retval Interface status (MANDATORY: return 0 -> no Error).
*
*/
int32_t iis3dwb_rounding_mode_set(const