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

ICM20948 DMP代码详解(5)

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

 

上一回开始深入到代码,先从EMP-App中的入口函数main开头,该函数在EMD-App\src\ICM20948\main.c中,再次贴出其代码如下:

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;
}

上一回也提到,由于工程中的代码是适配TDK SAMG55开发板的(TDK SAMG55 Dev Kit)的,而笔者的目标平台是乐鑫的ESP32系列模组,因此属于系统初始化的相关内容(以下代码片段)可以略过。

	/* 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);

当然,这里的略过并不是说完全不关注,而只是不用深入其具体实现,但是仍然需要留意系统的相关时钟、外设(尤其是i2c)接口的初始化,以便后边对到ESP32(ESP-IDF)中。

从以下部分开始,才是需要重点关注的内容:

	/*
	* 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();

struct inv_icm20948_serif的定义在EMD-Core\sources\Invn\Devices\Drivers\ICM20948\Icm20948Serif.h中,如下:

/** @brief ICM20948 serial interface
 */
struct inv_icm20948_serif {
	void *     context;
	int      (*read_reg)(void * context, uint8_t reg, uint8_t * buf, uint32_t len);
	int      (*write_reg)(void * context, uint8_t reg, const uint8_t * buf, uint32_t len);
	uint32_t   max_read;
	uint32_t   max_write;
	inv_bool_t is_spi;
};

其中包含了6个成员:

  • void *context

可以理解成句柄。

  • int      (*read_reg)(void * context, uint8_t reg, uint8_t * buf, uint32_t len);

寄存器读回调函数。后文书实际用到的时候再深入讲解。

  • int      (*write_reg)(void * context, uint8_t reg, const uint8_t * buf, uint32_t len);

寄存器写回调函数。后文书实际用到的时候再深入讲解。

  • uint32_t   max_read;

最大能够读取的字节数。

  • uint32_t   max_write;

最大能够写入的字节数。

  • inv_bool_t is_spi;

是否为SPI接口。由于ICM20948可以支持I2C和SPI两种接口,因此以此标志进行区分。

e91538fb7d9d4756971f1fd5e97b01da.png

485d7edc71cc49b5a61e531cfae52a8f.png

回到main函数中。以上代码片段是在主函数中新建了一个struct inv_icm20948_serif 的对象icm20948_serif,然后对其进行初始化。

  • *context设置为0即NULL,不需要;
  • read_reg函数指针设置为idd_io_hal_read_reg;
  • write_reg函数指针设置为idd_io_hal_write_reg;
  • max_read设置为16 * 1024,即16K字节;
  • max_write也设置为16K字节;
  • is_spi设置为interface_is_SPI()的返回值。

interface_is_SPI函数在EMD-App\src\ICM20948\system.c中,代码如下:

inv_bool_t interface_is_SPI(void)
{
#if SERIF_TYPE_SPI
	return true;
#else
	return false;
#endif	
}

官方代码中是根据SERIF_TYPE_SPI宏来进行判断的。如果是自己的硬件,已经确定好使用I2C或SPI,直接返回false或true就好。

至此,main函数中的第一段所关注的代码就解析完了,余下代码的解析请看后篇。

 

 


http://www.kler.cn/news/294002.html

相关文章:

  • Vue eslint 语法检测问题
  • 水晶连连看 - 无限版软件操作说明书
  • 硬件工程师笔试面试知识器件篇——电感
  • [第三篇 运维与安全管理] ==> 第8章 数据库安全管理与审计
  • SpringCloud开发实战(三):集成Eureka注册中心
  • 算法训练营——day3长度最小子数组
  • 18055 主对角线上的元素之和
  • 超详细!!!electron-vite-vue开发桌面应用之应用更新版本提示(十三)
  • 数据集火焰检测 >> DataBall
  • 搭贝低代码平台在零售管理中的应用:推动企业快速数据化转型
  • Node.js应用的高效部署与运维:从流程自动化到精细化监控
  • Excel中.xls和.xlsx文件格式的区别,及C++操作Excel文件
  • 2024年全国大学生数学建模C题完整论文
  • SQL治理经验谈:索引覆盖
  • 数据结构(1)
  • LIN协议栈 AUTOSAR架构下 状态管理
  • Matplotlib通过axis()配置坐标轴数据详解
  • JavaEE(3)
  • 【debug】dpkg: error processing archive...Invalid cross-device link
  • pgrx在docker中问题无法解决
  • gitlab 启动/关闭/启用开机启动/禁用开机启动
  • 关于HTTP SESSION
  • 算法复盘——Leetcode hot100: 双指针算法
  • 软件测试基础总结+面试八股文
  • Vue2电商项目(二) Home模块的开发;(还需要补充js节流和防抖的回顾链接)
  • 数据结构(单向链表)
  • 软文发稿相比其他广告形式有哪些持续性优势?
  • 如何从硬盘恢复已删除/丢失的文件?硬盘恢复已删除的文件技巧
  • 如何录制黑神话悟空的游戏BGM导入iPhone手机制作铃声?
  • notepad下载安装使用以及高级使用技巧