ICM20948 DMP代码详解(5)

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

本文主要是介绍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);
#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;
}

上一回也提到,由于工程中的代码是适配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);
#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);

当然,这里的略过并不是说完全不关注,而只是不用深入其具体实现,但是仍然需要留意系统的相关时钟、外设(尤其是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_SPIreturn true;
#elsereturn false;
#endif	
}

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

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

 

 

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



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

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected