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两种接口,因此以此标志进行区分。
回到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函数中的第一段所关注的代码就解析完了,余下代码的解析请看后篇。