接前一篇文章:ICM20948 DMP代码详解(6)
上一回讲解了EMP-App中的入口函数main()中重点关注的第2段代码的前一个函数inv_icm20948_reset_states,本回讲解后一个函数inv_icm20948_register_aux_compass。
为了便于理解和回顾,再次贴出第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_register_aux_compass函数在EMD-Core\sources\Invn\Devices\Drivers\ICM20948\Icm20948AuxCompassAkm.c中,代码如下:
void inv_icm20948_register_aux_compass(struct inv_icm20948 * s,
enum inv_icm20948_compass_id compass_id, uint8_t compass_i2c_addr)
{
switch(compass_id) {
case INV_ICM20948_COMPASS_ID_AK09911:
s->secondary_state.compass_slave_id = HW_AK09911;
s->secondary_state.compass_chip_addr = compass_i2c_addr;
s->secondary_state.compass_state = INV_ICM20948_COMPASS_INITED;
/* initialise mounting matrix of compass to identity akm9911 */
s->mounting_matrix_secondary_compass[0] = -1 ;
s->mounting_matrix_secondary_compass[4] = -1;
s->mounting_matrix_secondary_compass[8] = 1;
break;
case INV_ICM20948_COMPASS_ID_AK09912:
s->secondary_state.compass_slave_id = HW_AK09912;
s->secondary_state.compass_chip_addr = compass_i2c_addr;
s->secondary_state.compass_state = INV_ICM20948_COMPASS_INITED;
/* initialise mounting matrix of compass to identity akm9912 */
s->mounting_matrix_secondary_compass[0] = 1 ;
s->mounting_matrix_secondary_compass[4] = 1;
s->mounting_matrix_secondary_compass[8] = 1;
break;
case INV_ICM20948_COMPASS_ID_AK08963:
s->secondary_state.compass_slave_id = HW_AK8963;
s->secondary_state.compass_chip_addr = compass_i2c_addr;
s->secondary_state.compass_state = INV_ICM20948_COMPASS_INITED;
/* initialise mounting matrix of compass to identity akm8963 */
s->mounting_matrix_secondary_compass[0] = 1;
s->mounting_matrix_secondary_compass[4] = 1;
s->mounting_matrix_secondary_compass[8] = 1;
break;
case INV_ICM20948_COMPASS_ID_AK09916:
s->secondary_state.compass_slave_id = HW_AK09916;
s->secondary_state.compass_chip_addr = compass_i2c_addr;
s->secondary_state.compass_state = INV_ICM20948_COMPASS_INITED;
/* initialise mounting matrix of compass to identity akm9916 */
s->mounting_matrix_secondary_compass[0] = 1 ;
s->mounting_matrix_secondary_compass[4] = -1;
s->mounting_matrix_secondary_compass[8] = -1;
break;
default:
s->secondary_state.compass_slave_id = 0;
s->secondary_state.compass_chip_addr = 0;
s->secondary_state.compass_state = INV_ICM20948_COMPASS_RESET;
}
}
inv_icm20948_register_aux_compass函数的第1个参数struct inv_icm20948 * s上一回已经讲解过了,其对应的实参为EMD-App\src\ICM20948\sensor.c中的全局变量inv_icm20948_t icm_device。
inv_icm20948_register_aux_compass函数的第2个参数enum inv_icm20948_compass_id compass_id是一个枚举类型的变量,enum inv_icm20948_compass_id的定义在EMD-Core\sources\Invn\Devices\Drivers\ICM20948\Icm20948AuxCompassAkm.h中,代码如下:
/** @brief Supported auxiliary compass identifer
*/
enum inv_icm20948_compass_id {
INV_ICM20948_COMPASS_ID_NONE = 0, /**< no compass */
INV_ICM20948_COMPASS_ID_AK09911, /**< AKM AK09911 */
INV_ICM20948_COMPASS_ID_AK09912, /**< AKM AK09912 */
INV_ICM20948_COMPASS_ID_AK09916, /**< AKM AK09916 */
INV_ICM20948_COMPASS_ID_AK08963, /**< AKM AK08963 */
};
调用inv_icm20948_register_aux_compass函数时,第2个参数对应的实参为INV_ICM20948_COMPASS_ID_AK09916,也就是使用ICM20948片上自带的磁力计芯片AK09916。这一部分参见芯片手册(DS-000189-ICM-20948-v1.3.pdf)中的说明:
当然,使用上边枚举值中包含的其它值也是可以的,不过那就需要外挂磁力计芯片了。
inv_icm20948_register_aux_compass函数的第3个参数uint8_t compass_i2c_addr对应的实参为AK0991x_DEFAULT_I2C_ADDR,其在EMD-App\src\ICM20948\system.h中定义,如下:
#define AK0991x_DEFAULT_I2C_ADDR 0x0C /* The default I2C address for AK0991x Magnetometers */
#define AK0991x_SECONDARY_I2C_ADDR 0x0E /* The secondary I2C address for AK0991x Magnetometers */
这个0x0C以及0x0E是哪里来的?这个要看ICM20948的芯片资料是找不到的,需要看AK09916的芯片资料才可以。
至于0x0E,笔者也没有找到,也许是上边其它那几个AK传感器中会有这个地址吧。
至此,inv_icm20948_register_aux_compass函数的各个参数就解析完了,下一回解析函数的具体代码。