使用STMems_Standard_C_drivers库
程序参考了STM32CubeExpansion MEMS-XT1的LSM6DSL_FreeFallDetection
- void lsm6dsl_free_fall_detection(void)
- {
- /*
- * Initialize mems driver interface
- */
- dev_ctx.write_reg = platform_write;
- dev_ctx.read_reg = platform_read;
- dev_ctx.handle = &hi2c1;
-
- /*
- * Check device ID
- */
- whoamI = 0;
- lsm6dsl_device_id_get(&dev_ctx, &whoamI);
- if ( whoamI != LSM6DSL_ID )
- while(1); /*manage here device not found */
- /*
- * Restore default configuration
- */
- lsm6dsl_reset_set(&dev_ctx, PROPERTY_ENABLE);
- do {
- lsm6dsl_reset_get(&dev_ctx, &rst);
- } while (rst);
- /*
- * Enable Block Data Update
- */
- lsm6dsl_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
- /*
- * Set Output Data Rate
- */
- lsm6dsl_xl_data_rate_set(&dev_ctx, LSM6DSL_XL_ODR_416Hz);
- /*
- * Set full scale
- */
- lsm6dsl_xl_full_scale_set(&dev_ctx, LSM6DSL_2g);
- lsm6dsl_wkup_dur_set(&dev_ctx,0);
- lsm6dsl_timestamp_res_set(&dev_ctx,LSM6DSL_LSB_6ms4);
- lsm6dsl_act_sleep_dur_set(&dev_ctx,0);
- lsm6dsl_ff_dur_set(&dev_ctx,0);
- lsm6dsl_ff_threshold_set(&dev_ctx,LSM6DSL_FF_TSH_312mg);
- /*
- * 中断
- */
- lsm6dsl_int1_route_t lsm6dsl_int1_route;
- lsm6dsl_int1_route.int1_ff = 1;
- lsm6dsl_pin_int1_route_set(&dev_ctx,lsm6dsl_int1_route);
- lsm6dsl_all_sources_t lsm6dsl_all_sources;
- while(1)
- {
- if(int1_flag == 1)
- {
- int1_flag = 0;
- lsm6dsl_all_sources_get(&dev_ctx,&lsm6dsl_all_sources);
- if(lsm6dsl_all_sources.reg.wake_up_src.ff_ia == 1)
- {
-
- snprintf(data_out, MAX_BUF_SIZE, "free fall indication.\r\n");
- tx_com((uint8_t*)data_out, strlen(data_out));
- }
- }
- }
- }
工程文件