本文主要是介绍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);
#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;
}
接下来是第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。
而第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 changeunsigned short bac_on; // indicates if ANDROID_SENSOR_ACTIVITY_CLASSIFICATON is onunsigned 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 onunsigned 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 internallyunsigned 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 ASAPunsigned 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 ACCunsigned short odr_racc_ms; // ODR in ms requested for ANDROID_SENSOR_RAW_ACCELEROMETERunsigned short odr_gyr_ms; // ODR in ms requested for ANDROID_SENSOR_GYROSCOPE_UNCALIBRATEDunsigned short odr_rgyr_ms; // ODR in ms requested for ANDROID_SENSOR_RAW_GYROSCOPEint 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。
这篇关于ICM20948 DMP代码详解(6)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!