当前位置: 首页 > article >正文

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);
#endif
 
	INV_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 change
	unsigned short bac_on; // indicates if ANDROID_SENSOR_ACTIVITY_CLASSIFICATON is on
	unsigned 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 on
	unsigned 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 internally
	unsigned 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 ASAP
	unsigned 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 ACC
	unsigned short odr_racc_ms; // ODR in ms requested for ANDROID_SENSOR_RAW_ACCELEROMETER
	unsigned short odr_gyr_ms; // ODR in ms requested for ANDROID_SENSOR_GYROSCOPE_UNCALIBRATED
	unsigned short odr_rgyr_ms; // ODR in ms requested for ANDROID_SENSOR_RAW_GYROSCOPE
	int 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。

 


http://www.kler.cn/a/293829.html

相关文章:

  • QQ 小程序已发布,但无法被搜索的解决方案
  • vivo 游戏中心包体积优化方案与实践
  • 【JVM】关于JVM的内部原理你到底了解多少(八股文面经知识点)
  • 智能电视/盒子的应用管理——通过ADB工具优化体验
  • 使用@react-three/fiber,@mkkellogg/gaussian-splats-3d加载.splat,.ply,.ksplat文件
  • vue3 pdf base64转成文件流打开
  • vim常用快捷键问答之二
  • CentOS 7 上安装 NVIDIA Docker
  • ​​NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南002
  • 设计模式-行为型模式-状态模式
  • 如何在 Ubuntu 24.04 上安装 MariaDB ?
  • 怎么使用matplotlib绘制一个从-2π到2π的sin(x)的折线图-学习篇
  • 深度学习基础--卷积的变种
  • 江协科技stm32————11-5 硬件SPI读写W25Q64
  • zookeeper初识
  • Optuna发布 4.0 重大更新:多目标TPESampler自动化超参数优化速度提升显著
  • Docker | Win10 安装
  • vscode spring boot项目编辑yaml不自动提示补全如何解决
  • Ubuntu 基于sox音频处理工具shell脚本批量处理音频文件
  • C++ | Leetcode C++题解之第387题字符串中的第一个唯一字符
  • 问:你知道IO和NIO有哪些区别不?
  • 基于golang实现简单的文件监控
  • 【Python百日进阶-Web开发-音频】Day705 - 音频加载 librosa.load / librosa.stream
  • 《系统架构设计师教程(第2版)》第17章-通信系统架构设计理论与实践-02-广域网网络架构
  • HTML和HTML5有什么区别
  • 畅捷通如何远程访问