ICM20948 DMP代码详解(6)

2024-09-07 16:28
文章标签 代码 详解 dmp icm20948

本文主要是介绍ICM20948 DMP代码详解(6),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

接前一篇文章:ICM20948 DMP代码详解(5)

 

前一篇文章解析了EMP-App中的入口函数main()中重点关注的第1段代码,本回继续往下进行解析。为了便于理解和回顾,再次贴出main函数源码:

int main (void)
{int rc = 0;/* Hardware initialization */sysclk_init();board_init();sysclk_enable_peripheral_clock(ID_TC0);/* Configure Device - Host Interface */configure_console();#ifdef INV_MSG_ENABLE/* Setup message logging */INV_MSG_SETUP(INV_MSG_ENABLE, msg_printer);
#endifINV_MSG(INV_MSG_LEVEL_INFO, "##########################");INV_MSG(INV_MSG_LEVEL_INFO, "     ICM20948 example     ");INV_MSG(INV_MSG_LEVEL_INFO, "     Ver: %s", EMD_RELEASE_VERSION_STRING);INV_MSG(INV_MSG_LEVEL_INFO, "##########################");/* Initialize External Sensor Interrupt */ext_int_initialize(&ext_interrupt_handler);interface_initialize();/* Configure sysTick Timer */SysTick_Config(sysclk_get_cpu_hz() / MILLISECONDS_PER_SECOND);/** Initialize icm20948 serif structure*/struct inv_icm20948_serif icm20948_serif;icm20948_serif.context   = 0; /* no need */icm20948_serif.read_reg  = idd_io_hal_read_reg;icm20948_serif.write_reg = idd_io_hal_write_reg;icm20948_serif.max_read  = 1024*16; /* maximum number of bytes allowed per serial read */icm20948_serif.max_write = 1024*16; /* maximum number of bytes allowed per serial write */icm20948_serif.is_spi = interface_is_SPI();/** Reset icm20948 driver states*/inv_icm20948_reset_states(&icm_device, &icm20948_serif);inv_icm20948_register_aux_compass(&icm_device, INV_ICM20948_COMPASS_ID_AK09916, AK0991x_DEFAULT_I2C_ADDR);/** Setup the icm20948 device*/rc = icm20948_sensor_setup();/** Now that Icm20948 device was initialized, we can proceed with DMP image loading* This step is mandatory as DMP image are not store in non volatile memory*/rc += load_dmp3();check_rc(rc, "Error sensor_setup/DMP loading.");/** Initialize Dynamic protocol stuff*/DynProTransportUart_init(&transport, iddwrapper_transport_event_cb, 0);DynProtocol_init(&protocol, iddwrapper_protocol_event_cb, 0);InvScheduler_init(&scheduler);InvScheduler_initTask(&scheduler, &commandHandlerTask, "commandHandlerTask", CommandHandlerTaskMain, 0, INVSCHEDULER_TASK_PRIO_MIN, 1);InvScheduler_initTask(&scheduler, &blinkerLedTask, "blinkerLedTask", BlinkerLedTaskMain, 0, INVSCHEDULER_TASK_PRIO_MIN+1, 1000000/SCHEDULER_PERIOD);InvScheduler_startTask(&blinkerLedTask, 0);InvScheduler_startTask(&commandHandlerTask, 0);hw_timer_start(20);		// Start the timestamp timer at 20 Hz.while (1){InvScheduler_dispatchTasks(&scheduler);if (irq_from_device == 1) {inv_icm20948_poll_sensor(&icm_device, (void *)0, build_sensor_event_data);__disable_irq();irq_from_device = 0;__enable_irq();}}return 0;
}

接下来是第2段代码片段:

	/** Reset icm20948 driver states*/inv_icm20948_reset_states(&icm_device, &icm20948_serif);inv_icm20948_register_aux_compass(&icm_device, INV_ICM20948_COMPASS_ID_AK09916, AK0991x_DEFAULT_I2C_ADDR);

inv_icm20948_reset_states函数在EMD-Core\sources\Invn\Devices\Drivers\ICM20948\Icm20948.h中,属于内联函数,代码如下:

/** @brief Reset and initialize driver states*  @param[in] s             handle to driver states structure*/
static inline void inv_icm20948_reset_states(struct inv_icm20948 * s,const struct inv_icm20948_serif * serif)
{assert(icm20948_instance == 0);memset(s, 0, sizeof(*s));s->serif = *serif;icm20948_instance = s;
}

函数的第2个参数const struct inv_icm20948_serif * serif对应的实参就是上一回分析的、在main函数中新建并初始化的icm20948_serif。

0bbcf8d796a44e2893f6cf1575264090.png

而第1个参数对应的实参为&icm_device。icm_device是一个全局变量,在EMD-App\src\ICM20948\sensor.c中定义,代码如下:

/*
* Just a handy variable to handle the icm20948 object
*/
inv_icm20948_t icm_device;

inv_icm20948_t的定义在EMD-Core\sources\Invn\Devices\Drivers\ICM20948\Icm20948.h中,代码如下:

typedef struct inv_icm20948 {struct inv_icm20948_serif serif;/** @brief struct for the base_driver : this contains the Mems information */struct base_driver_t{unsigned char wake_state;chip_lp_ln_mode_icm20948_t chip_lp_ln_mode;unsigned char pwr_mgmt_1;unsigned char pwr_mgmt_2;unsigned char user_ctrl;unsigned char gyro_div;unsigned short secondary_div;short accel_div;unsigned char gyro_averaging;unsigned char accel_averaging;uint8_t gyro_fullscale; uint8_t accel_fullscale;uint8_t lp_en_support:1;uint8_t firmware_loaded:1;uint8_t serial_interface;uint8_t timebase_correction_pll;}base_state;/* secondary device support */struct inv_icm20948_secondary_states {struct inv_icm20948_secondary_reg {uint16_t addr;uint16_t reg;uint16_t ctrl;uint16_t d0;} slv_reg[4];unsigned char sSavedI2cOdr;/* compass support */uint8_t compass_sens[3];long final_matrix[9];const int16_t *st_upper;const int16_t *st_lower;int scale;uint8_t dmp_on;uint8_t secondary_resume_compass_state;uint8_t mode_reg_addr;int compass_chip_addr;int compass_slave_id;inv_icm20948_compass_state_t compass_state;} secondary_state;/* self test */uint8_t selftest_done;uint8_t offset_done;uint8_t gyro_st_data[3];uint8_t accel_st_data[3];/* mpu fifo control */struct fifo_info_t{int fifoError;unsigned char fifo_overflow;} fifo_info;/* interface mapping */unsigned long sStepCounterToBeSubtracted;unsigned long sOldSteps;/* data converter */long s_quat_chip_to_body[4];/* base driver */uint8_t sAllowLpEn;uint8_t s_compass_available;uint8_t s_proximity_available;/* base sensor ctrl*/unsigned short inv_dmp_odr_dividers[37];//INV_SENSOR_NUM_MAX /!\ if the size change unsigned short inv_dmp_odr_delays[37];//INV_SENSOR_NUM_MAX /!\ if the size changeunsigned short bac_on; // indicates if ANDROID_SENSOR_ACTIVITY_CLASSIFICATON is onunsigned short pickup;unsigned short bac_status;unsigned short b2s_status;unsigned short flip_pickup_status;unsigned short inv_sensor_control;unsigned short inv_sensor_control2;unsigned long inv_androidSensorsOn_mask[2] ;// Each bit corresponds to a sensor being onunsigned short inv_androidSensorsOdr_boundaries[51][2];//GENERAL_SENSORS_MAX /!\ if the size change unsigned char sGmrvIsOn; // indicates if GMRV was requested to be ON by end-user. Once this variable is set, it is either GRV or GMRV which is enabled internallyunsigned short lLastHwSmplrtDividerAcc;unsigned short lLastHwSmplrtDividerGyr;unsigned char sBatchMode;uint8_t header2_count;char mems_put_to_sleep;unsigned short smd_status;unsigned short ped_int_status;unsigned short bac_request;uint8_t go_back_lp_when_odr_low; // set to 1 when we forced a switch from LP to LN mode to be able to reach 1kHz ODR, so we will need to go back to LP mode ASAPunsigned short odr_acc_ms; // ODR in ms requested for ANDROID_SENSOR_ACCELEROMETER//unsigned short odr_acc_wom_ms; // ODR in ms requested for ANDROID_SENSOR_WOM when using ACCunsigned short odr_racc_ms; // ODR in ms requested for ANDROID_SENSOR_RAW_ACCELEROMETERunsigned short odr_gyr_ms; // ODR in ms requested for ANDROID_SENSOR_GYROSCOPE_UNCALIBRATEDunsigned short odr_rgyr_ms; // ODR in ms requested for ANDROID_SENSOR_RAW_GYROSCOPEint bias[9];// dmp bias [0-2]:acc,[3-5]:gyr,[6-8]:mag/* Icm20948Fifo usage */signed char mounting_matrix[9];signed char mounting_matrix_secondary_compass[9];long soft_iron_matrix[9];uint8_t skip_sample[INV_ICM20948_SENSOR_MAX+1];uint64_t timestamp[INV_ICM20948_SENSOR_MAX+1];uint8_t sFirstBatch[INV_ICM20948_SENSOR_MAX+1];sensor_type_icm20948_t sensorlist[INV_ICM20948_SENSOR_MAX+1];unsigned short saved_count;/* Icm20948Transport*/unsigned char reg;unsigned char lastBank;unsigned char lLastBankSelected;/* augmented sensors*/unsigned short sGravityOdrMs;unsigned short sGrvOdrMs;unsigned short sLinAccOdrMs;unsigned short sGravityWuOdrMs;unsigned short sGrvWuOdrMs;unsigned short sLinAccWuOdrMs;unsigned short sRvOdrMs;unsigned short sOriOdrMs;unsigned short sRvWuOdrMs;unsigned short sOriWuOdrMs;/* Icm20649Setup */short set_accuracy;int new_accuracy;
} inv_icm20948_t;

这是一个较为庞大的结构体,其中囊括了各种功能。而此结构体的第一个成员正是struct inv_icm20948_serif serif。

回到inv_icm20948_reset_states函数中:

/** @brief Reset and initialize driver states*  @param[in] s             handle to driver states structure*/
static inline void inv_icm20948_reset_states(struct inv_icm20948 * s,const struct inv_icm20948_serif * serif)
{assert(icm20948_instance == 0);memset(s, 0, sizeof(*s));s->serif = *serif;icm20948_instance = s;
}

inv_icm20948_reset_states函数的代码比较简单,先是将第1个参数的全部成员清零。这里,由于第1个参数对应的实参icm_device是全局变量,系统已经自动清零了,因此实际上这一句有没有都可以。如果有传入的实参为局部变量的情况,那么这一句就有用了。

接下来,就是将struct inv_icm20948对象的第一个成员struct inv_icm20948_serif serif赋值为main函数中新建的那个inv_icm20948_serif serif。注意这里是值传递,也就是两者并不指向同一段内存。

接下来将第1个参数赋给icm20948_instance。icm20948_instance也是一个全局变量,在EMD-Core\sources\Invn\Devices\Drivers\ICM20948\Icm20948Transport.c中,如下:

struct inv_icm20948 * icm20948_instance;

这里就和inv_icm20948_reset_states函数的第1行代码对应起来了。

	assert(icm20948_instance == 0);

调用inv_icm20948_reset_states函数的时候必须要求第1个参数对应的实参为空。由于struct inv_icm20948 * icm20948_instance为全局变量,因此系统会将其设置为0即空。

至此,main函数第2段代码片段的前一个函数inv_icm20948_reset_states就讲解完了。下一回讲解后一个函数inv_icm20948_register_aux_compass。

 

这篇关于ICM20948 DMP代码详解(6)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1145565

相关文章

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

java中反射(Reflection)机制举例详解

《java中反射(Reflection)机制举例详解》Java中的反射机制是指Java程序在运行期间可以获取到一个对象的全部信息,:本文主要介绍java中反射(Reflection)机制的相关资料... 目录一、什么是反射?二、反射的用途三、获取Class对象四、Class类型的对象使用场景1五、Class

golang 日志log与logrus示例详解

《golang日志log与logrus示例详解》log是Go语言标准库中一个简单的日志库,本文给大家介绍golang日志log与logrus示例详解,感兴趣的朋友一起看看吧... 目录一、Go 标准库 log 详解1. 功能特点2. 常用函数3. 示例代码4. 优势和局限二、第三方库 logrus 详解1.

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

一文详解如何从零构建Spring Boot Starter并实现整合

《一文详解如何从零构建SpringBootStarter并实现整合》SpringBoot是一个开源的Java基础框架,用于创建独立、生产级的基于Spring框架的应用程序,:本文主要介绍如何从... 目录一、Spring Boot Starter的核心价值二、Starter项目创建全流程2.1 项目初始化(