/*
******************************************************************************
* @file asm330lhh_multi_read_fifo_simple.c
* @author Sensors Software Solution Team
* @brief This file show the simplest way to get data from sensor FIFO.
*
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 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
*
******************************************************************************
*/
/*
* This example was developed using the following STMicroelectronics
* evaluation boards:
*
* - STEVAL_MKI109V3 + STEVAL-MKI195V1
* - NUCLEO_F411RE + STEVAL-MKI195V1
* - DISCOVERY_SPC584B + STEVAL-MKI195V1
*
* and STM32CubeMX tool with STM32CubeF4 MCU Package
*
* Used interfaces:
*
* STEVAL_MKI109V3 - Host side: USB (Virtual COM)
* - Sensor side: SPI(Default) / I2C(supported)
*
* NUCLEO_STM32F411RE - Host side: UART(COM) to USB bridge
* - Sensor side: I2C(Default) / SPI(supported)
*
* DISCOVERY_SPC584B - Host side: UART(COM) to USB bridge
* - Sensor side: I2C(Default) / SPI(supported)
*
* If you need to run this example on a different hardware platform a
* modification of the functions: `platform_write`, `platform_read`,
* `tx_com` and 'platform_init' is required.
*
*/
/* STMicroelectronics evaluation boards definition
*
* Please uncomment ONLY the evaluation boards in use.
* If a different hardware is used please comment all
* following target board and redefine yours.
*/
//#define STEVAL_MKI109V3 /* little endian */
//#define NUCLEO_F411RE /* little endian */
//#define SPC584B_DIS /* big endian */
/* ATTENTION: By default the driver is little endian. If you need switch
* to big endian please see "Endianness definitions" in the
* header file of the driver (_reg.h).
*/
#if defined(STEVAL_MKI109V3)
/* MKI109V3: Define communication interface */
#define SENSOR_BUS hspi2
/* MKI109V3: Vdd and Vddio power supply values */
#define PWM_3V3 915
#elif defined(NUCLEO_F411RE)
/* NUCLEO_F411RE: Define communication interface */
#define SENSOR_BUS hi2c1
#elif defined(SPC584B_DIS)
/* DISCOVERY_SPC584B: Define communication interface */
#define SENSOR_BUS I2CD1
#endif
/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include <stdio.h>
#include "asm330lhh_reg.h"
#if defined(NUCLEO_F411RE)
#include "stm32f4xx_hal.h"
#include "usart.h"
#include "gpio.h"
#include "i2c.h"
#elif defined(STEVAL_MKI109V3)
#include "stm32f4xx_hal.h"
#include "usbd_cdc_if.h"
#include "gpio.h"
#include "spi.h"
#include "tim.h"
#elif defined(SPC584B_DIS)
#include "components.h"
#endif
/* Private macro -------------------------------------------------------------*/
#define BOOT_TIME 10 //ms
typedef union {
int16_t i16bit[3];
uint8_t u8bit[6];
} axis3bit16_t;
/* Private variables ---------------------------------------------------------*/
static axis3bit16_t data_raw_acceleration;
static axis3bit16_t data_raw_angular_rate;
static axis3bit16_t dummy;
static float acceleration_mg[3];
static float angular_rate_mdps[3];
static uint8_t whoamI, rst;
static uint8_t tx_buffer[1000];
/* Extern variables ----------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*
* WARNING:
* Functions declare in this section are defined at the end of this file
* and are strictly related to the hardware platform used.
*
*/
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 tx_com( uint8_t *tx_buffer, uint16_t len );
static void platform_delay(uint32_t ms);
static void platform_init(void);
/* Main Example --------------------------------------------------------------*/
void asm330lhh_fifo_read(void)
{
stmdev_ctx_t dev_ctx;
/* Uncomment to configure INT 1 */
//asm330lhh_pin_int1_route_t int1_route;
/* Uncomment to configure INT 2 */
//asm330lhh_pin_int2_route_t int2_route;
/* Initialize mems driver interface */
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.mdelay = platform_delay;
dev_ctx.handle = &SENSOR_BUS;
/* Init test platform */
platform_init();
/* Wait sensor boot time */
platform_delay(BOOT_TIME);
/* Check device ID */
asm330lhh_device_id_get(&dev_ctx, &whoamI);
if (whoamI != ASM330LHH_ID)
while (1);
/* Restore default configuration */
asm330lhh_reset_set(&dev_ctx, PROPERTY_ENABLE);
do {
asm330lhh_reset_get(&dev_ctx, &rst);
} while (rst);
/* Start device configuration. */
asm330lhh_device_conf_set(&dev_ctx, PROPERTY_ENABLE);
/* Enable Block Data Update */
asm330lhh_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
/* Set full scale */
asm330lhh_xl_full_scale_set(&dev_ctx, ASM330LHH_2g);
asm330lhh_gy_full_scale_set(&dev_ctx, ASM330LHH_2000dps);
/* Set FIFO watermark (number of unread sensor data TAG + 6 bytes
* stored in FIFO) to 10 samples
*/
asm330lhh_fifo_watermark_set(&dev_ctx, 10);
/* Set FIFO batch XL/Gyro ODR to 12.5Hz */
asm330lhh_fifo_xl_batch_set(&dev_ctx, ASM330LHH_XL_BATCHED_AT_12Hz5);
asm330lhh_fifo_gy_batch_set(&dev_ctx, ASM330LHH_GY_BATCHED_AT_12Hz5);
/* Set FIFO mode to Stream mode (aka Continuous Mode) */
asm330lhh_fifo_mode_set(&dev_ctx, ASM330LHH_STREAM_MODE);
/* Enable drdy 75 μs pulse: uncomment if interrupt must be pulsed */
//asm330lhh_data_ready_mode_set(&dev_ctx, ASM330LHH_DRDY_PULSED);
/* Uncomment if interrupt generation on Free Fall INT1 pin */
//asm330lhh_pin_int1_route_get(&dev_ctx, &int1_route);
//int1_route.reg.int1_ctrl.int1_fifo_th = PROPERTY_ENABLE;
//asm330lhh_pin_int1_route_set(&dev_ctx, &int1_route);
/* Uncomment if interrupt generation on Free Fall INT2 pin */
//asm330lhh_pin_int2_route_get(&dev_ctx, &int2_route);
//int2_route.reg.int2_ctrl.int2_fifo_th = PROPERTY_ENABLE;
//asm330lhh_pin_int2_route_set(&dev_ctx, &int2_route);
/* Set Output Data Rate */
asm330lhh_xl_data_rate_set(&dev_ctx, ASM330LHH_XL_ODR_12Hz5);
asm330lhh_gy_data_rate_set(&dev_ctx, ASM330LHH_GY_ODR_12Hz5);
/* Wait samples. */
while (1) {
asm330lhh_fifo_tag_t reg_tag;
uint8_t wmflag = 0;
uint16_t num = 0;
/* Read watermark flag */
asm330lhh_fifo_wtm_flag_get(&dev_ctx, &wmflag);
if (wmflag > 0) {
/* Read number of samples in FIFO */
asm330lhh_fifo_data_level_get(&dev_ctx, &num);
while (num--) {
/* Read FIFO tag */
asm330lhh_fifo_sensor_tag_get(&dev_ctx, ®_tag);
switch (reg_tag) {
case ASM330LHH_XL_NC_TAG:
memset(data_raw_acceleration.u8bit, 0x00, 3 * sizeof(int16_t));
asm330lhh_fifo_out_raw_get(&dev_ctx, data_raw_acceleration.u8bit);
acceleration_mg[0] =
asm330lhh_from_fs2g_to_mg(data_raw_acceleration.i16bit[0]);
acceleration_mg[1] =
asm330lhh_from_fs2g_to_mg(data_raw_acceleration.i16bit[1]);
acceleration_mg[2] =
asm330lhh_from_fs2g_to_mg(data_raw_acceleration.i16bit[2]);
sprintf((char *)tx_buffer,
"Acceleration [mg]:%4.2f\t%4.2f\t%4.2f\r\n",
acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);
tx_com(tx_buffer, strlen((char const *)tx_buffer));
break;
case ASM330LHH_GYRO_NC_TAG:
memset(dat
asm330lhh-STdC嵌入式程序示例
需积分: 5 201 浏览量
2024-03-12
17:55:46
上传
评论
收藏 18KB ZIP 举报
xylon47
- 粉丝: 0
- 资源: 5
最新资源
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB 的霍夫曼变换答题卡识别源码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab+bp神经网络的神经网络汉字识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈