接前一篇文章:ICM20948 DMP代码详解(23)
上一回解析完了inv_icm20948_set_lowpower_or_highperformance函数,本回回到inv_icm20948_initialize_lower_driver函数中,继续往下解析。为了便于理解和回顾,再次贴出inv_icm20948_initialize_lower_driver函数源码,在EMD-Core\sources\Invn\Devices\Drivers\ICM20948\Icm20948DataBaseDriver.c中,如下:
/** Should be called once on power up. Loads DMP3, initializes internal variables needed
* for other lower driver functions.
*/
int inv_icm20948_initialize_lower_driver(struct inv_icm20948 * s, enum SMARTSENSOR_SERIAL_INTERFACE type,
const uint8_t *dmp3_image, uint32_t dmp3_image_size)
{
int result = 0;
static unsigned char data;
// set static variable
s->sAllowLpEn = 1;
s->s_compass_available = 0;
// ICM20948 do not support the proximity sensor for the moment.
// s_proximity_available variable is nerver changes
s->s_proximity_available = 0;
// Set varialbes to default values
memset(&s->base_state, 0, sizeof(s->base_state));
s->base_state.pwr_mgmt_1 = BIT_CLK_PLL;
s->base_state.pwr_mgmt_2 = BIT_PWR_ACCEL_STBY | BIT_PWR_GYRO_STBY | BIT_PWR_PRESSURE_STBY;
s->base_state.serial_interface = type;
result |= inv_icm20948_read_mems_reg(s, REG_USER_CTRL, 1, &s->base_state.user_ctrl);
result |= inv_icm20948_wakeup_mems(s);
result |= inv_icm20948_read_mems_reg(s, REG_WHO_AM_I, 1, &data);
/* secondary cycle mode should be set all the time */
data = BIT_I2C_MST_CYCLE|BIT_ACCEL_CYCLE|BIT_GYRO_CYCLE;
// Set default mode to low power mode
result |= inv_icm20948_set_lowpower_or_highperformance(s, 0);
// Disable Ivory DMP.
if(s->base_state.serial_interface == SERIAL_INTERFACE_SPI)
s->base_state.user_ctrl = BIT_I2C_IF_DIS;
else
s->base_state.user_ctrl = 0;
result |= inv_icm20948_write_single_mems_reg(s, REG_USER_CTRL, s->base_state.user_ctrl);
//Setup Ivory DMP.
result |= inv_icm20948_load_firmware(s, dmp3_image, dmp3_image_size);
if(result)
return result;
else
s->base_state.firmware_loaded = 1;
result |= inv_icm20948_set_dmp_address(s);
// Turn off all sensors on DMP by default.
//result |= dmp_set_data_output_control1(0); // FIXME in DMP, these should be off by default.
result |= dmp_icm20948_reset_control_registers(s);
// set FIFO watermark to 80% of actual FIFO size
result |= dmp_icm20948_set_FIFO_watermark(s, 800);
// Enable Interrupts.
data = 0x2;
result |= inv_icm20948_write_mems_reg(s, REG_INT_ENABLE, 1, &data); // Enable DMP Interrupt
data = 0x1;
result |= inv_icm20948_write_mems_reg(s, REG_INT_ENABLE_2, 1, &data); // Enable FIFO Overflow Interrupt
// TRACKING : To have accelerometers datas and the interrupt without gyro enables.
data = 0XE4;
result |= inv_icm20948_write_mems_reg(s, REG_SINGLE_FIFO_PRIORITY_SEL, 1, &data);
// Disable HW temp fix
inv_icm20948_read_mems_reg(s, REG_HW_FIX_DISABLE,1,&data);
data |= 0x08;
inv_icm20948_write_mems_reg(s, REG_HW_FIX_DISABLE,1,&data);
// Setup MEMs properties.
s->base_state.accel_averaging = 1; //Change this value if higher sensor sample avergaing is required.
s->base_state.gyro_averaging = 1; //Change this value if higher sensor sample avergaing is required.
inv_icm20948_set_gyro_divider(s, FIFO_DIVIDER); //Initial sampling rate 1125Hz/19+1 = 56Hz.
inv_icm20948_set_accel_divider(s, FIFO_DIVIDER); //Initial sampling rate 1125Hz/19+1 = 56Hz.
// Init the sample rate to 56 Hz for BAC,STEPC and B2S
dmp_icm20948_set_bac_rate(s, DMP_ALGO_FREQ_56);
dmp_icm20948_set_b2s_rate(s, DMP_ALGO_FREQ_56);
// FIFO Setup.
result |= inv_icm20948_write_single_mems_reg(s, REG_FIFO_CFG, BIT_SINGLE_FIFO_CFG); // FIFO Config. fixme do once? burst write?
result |= inv_icm20948_write_single_mems_reg(s, REG_FIFO_RST, 0x1f); // Reset all FIFOs.
result |= inv_icm20948_write_single_mems_reg(s, REG_FIFO_RST, 0x1e); // Keep all but Gyro FIFO in reset.
result |= inv_icm20948_write_single_mems_reg(s, REG_FIFO_EN, 0x0); // Slave FIFO turned off.
result |= inv_icm20948_write_single_mems_reg(s, REG_FIFO_EN_2, 0x0); // Hardware FIFO turned off.
s->base_state.lp_en_support = 1;
if(s->base_state.lp_en_support == 1)
inv_icm20948_set_chip_power_state(s, CHIP_LP_ENABLE, 1);
result |= inv_icm20948_sleep_mems(s);
return result;
}
当前来到以下代码片段:
// Disable Ivory DMP.
if(s->base_state.serial_interface == SERIAL_INTERFACE_SPI)
s->base_state.user_ctrl = BIT_I2C_IF_DIS;
else
s->base_state.user_ctrl = 0;
result |= inv_icm20948_write_single_mems_reg(s, REG_USER_CTRL, s->base_state.user_ctrl);
这段代码前文讲过(参见https://phmatthaus.blog.csdn.net/article/details/142164687),是在inv_icm20948_wakeup_mems函数中:
这里又来了一遍。不过inv_icm20948_wakeup_mems函数中的代码只是针对于USER_CTRL寄存器中BIT_I2C_IF_DIS这一位的,其它位并未改变;而此处则是把其它位都清零了。
再来回顾一下USER_CTRL寄存器的内容:
但是这样一来,之前在inv_icm20948_wakeup_mems函数中设置的DMP_EN位和FIFO_EN位就又清零了,不过之前倒也没有置位。
接下来就来到了以下代码片段:
//Setup Ivory DMP.
result |= inv_icm20948_load_firmware(s, dmp3_image, dmp3_image_size);
if(result)
return result;
else
s->base_state.firmware_loaded = 1;
inv_icm20948_load_firmware函数在EMD-Core\sources\Invn\Devices\Drivers\ICM20948\Icm20948Dmp3Driver.c中,代码如下:
/** Loads the dmp firmware for the icm20948 part.
* @param[in] dmp_image_sram Load DMP3 image from SRAM.
*/
int inv_icm20948_load_firmware(struct inv_icm20948 * s, const unsigned char *dmp3_image, unsigned int dmp3_image_size)
{
return inv_icm20948_firmware_load(s, dmp3_image, dmp3_image_size, DMP_LOAD_START);
}
inv_icm20948_firmware_load函数在EMD-Core\sources\Invn\Devices\Drivers\ICM20948\Icm20948LoadFirmware.c中,代码如下:
int inv_icm20948_firmware_load(struct inv_icm20948 *s, const unsigned char *data_start, unsigned short size_start, unsigned short load_addr)
{
int write_size;
int result;
unsigned short memaddr;
const unsigned char *data;
unsigned short size;
unsigned char data_cmp[INV_MAX_SERIAL_READ];
int flag = 0;
if(s->base_state.firmware_loaded)
return 0;
// Write DMP memory
data = data_start;
size = size_start;
memaddr = load_addr;
while (size > 0) {
write_size = min(size, INV_MAX_SERIAL_WRITE);
if ((memaddr & 0xff) + write_size > 0x100) {
// Moved across a bank
write_size = (memaddr & 0xff) + write_size - 0x100;
}
result = inv_icm20948_write_mems(s, memaddr, write_size, (unsigned char *)data);
if (result)
return result;
data += write_size;
size -= write_size;
memaddr += write_size;
}
// Verify DMP memory
data = data_start;
size = size_start;
memaddr = load_addr;
while (size > 0) {
write_size = min(size, INV_MAX_SERIAL_READ);
if ((memaddr & 0xff) + write_size > 0x100) {
// Moved across a bank
write_size = (memaddr & 0xff) + write_size - 0x100;
}
result = inv_icm20948_read_mems(s, memaddr, write_size, data_cmp);
if (result)
flag++; // Error, DMP not written correctly
if (memcmp(data_cmp, data, write_size))
return -1;
data += write_size;
size -= write_size;
memaddr += write_size;
}
#if defined(WIN32)
//if(!flag)
// inv_log("DMP Firmware was updated successfully..\r\n");
#endif
return 0;
}
Icm20948LoadFirmware.c文件只有这一个函数,功能很专一。inv_icm20948_firmware_load函数比较长,一段一段来看。
首先来到以下代码片段:
if(s->base_state.firmware_loaded)
return 0;
如果s->base_state.firmware_loaded已经设置过、即固件已经加载,则不再重复加载,直接返回0。
接下来根据传入的函数参数设置各变量,代码片段如下:
int inv_icm20948_firmware_load(struct inv_icm20948 *s, const unsigned char *data_start, unsigned short size_start, unsigned short load_addr)
{
……
data = data_start;
size = size_start;
memaddr = load_addr;
……
}
函数参数对应的实参是在inv_icm20948_load_firmware函数中传入的:
int inv_icm20948_load_firmware(struct inv_icm20948 * s, const unsigned char *dmp3_image, unsigned int dmp3_image_size)
{
return inv_icm20948_firmware_load(s, dmp3_image, dmp3_image_size, DMP_LOAD_START);
}
而inv_icm20948_load_firmware函数对应的实参又是在inv_icm20948_initialize_lower_driver函数中传入的:
result |= inv_icm20948_load_firmware(s, dmp3_image, dmp3_image_size);
还要再往上追……一直追到icm20948_sensor_setup函数中调用inv_icm20948_initialize函数的片段:
rc = inv_icm20948_initialize(&icm_device, dmp3_image, sizeof(dmp3_image));
可以看到,最终的实参也还就是dmp3_image。其在EMD-App\src\ICM20948\sensor.c中定义,代码如下:
static const uint8_t dmp3_image[] = {
#include "icm20948_img.dmp3a.h"
};
EMD-App\src\ICM20948\icm20948_img.dmp3a.h文件的内容如下(完整内容太长,容易引起网页打开响应慢,因此后改为只截取部分代码):
/* bank # 0 */
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x05, 0x00, 0xff,
0xff, 0xf7, 0x00, 0x05, 0x00, 0x05, 0x00, 0x05, 0x00, 0x05, 0x00, 0x05, 0x00, 0x05, 0x00, 0x05,
0x80, 0x00, 0x80, 0x00, 0x40, 0x00, 0x40, 0x00, 0x20, 0x00, 0x20, 0x00, 0x10, 0x00, 0x10, 0x00,
0x08, 0x00, 0x08, 0x00, 0x04, 0x00, 0x04, 0x00, 0x02, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00,
0x00, 0x80, 0x00, 0x80, 0x00, 0x40, 0x00, 0x40, 0x00, 0x20, 0x00, 0x20, 0x00, 0x10, 0x00, 0x10,
0x00, 0x08, 0x00, 0x08, 0x00, 0x04, 0x00, 0x04, 0x00, 0x02, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01,
/* bank # 1 */
0x00, 0x00, 0x03, 0x84, 0x00, 0x00, 0x9c, 0x40, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x36, 0x66, 0x66, 0x66, 0x00, 0x0f, 0x00, 0x00, 0x13, 0x5c, 0x28, 0xf6, 0x0c, 0xf5, 0xc2, 0x8f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xf8, 0x00, 0x38,
0x09, 0xed, 0xd1, 0xe8, 0x00, 0x00, 0x68, 0x00, 0x00, 0x01, 0xff, 0xc7, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x47, 0xae, 0x14, 0x3e, 0xb8, 0x51, 0xec, 0x00, 0x0f, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x0c, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x55, 0x55, 0x55, 0x0a, 0xaa, 0xaa, 0xaa,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe1, 0x00, 0x00, 0x00, 0x01, 0x00, 0x06, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0xd1, 0x59, 0x3f, 0xb7, 0x2e, 0xa7,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x8e, 0x17, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x20,
/* bank # 2 */
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x00, 0x00, 0x05, 0x21, 0xe9,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x3e, 0x03, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x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, 0xf0, 0xd9, 0xfa, 0xd8, 0xd8, 0xf0, 0xbc, 0xb0,
0x80, 0xbd, 0xb4, 0x90, 0xbe, 0xb8, 0xa0, 0xe0, 0xf0, 0xaf, 0xf2, 0x11, 0x3d, 0xf3, 0x15, 0x3d,
0xf2, 0xb2, 0x8f, 0xd0, 0xcd, 0xcf, 0xf3, 0xdf, 0xdf, 0xdf, 0xdf, 0xf1, 0xd4, 0xfd, 0x70, 0xd5,
0xfd, 0x70, 0xd6, 0xfd, 0x70, 0xd7, 0xfd, 0x70, 0xb6, 0x9f, 0x0c, 0x10, 0x18, 0xf5, 0x00, 0xb5,
0x96, 0xf5, 0x18, 0xbb, 0xaf, 0xd0, 0xb7, 0x9f, 0xe0, 0xf0, 0xd0, 0xf3, 0xcf, 0xf2, 0xcc, 0xd0,
0xf3, 0xcd, 0xf2, 0xca, 0xd0, 0xf3, 0xcb, 0xf2, 0xc8, 0xd0, 0xf3, 0xc9, 0xe0
这实际上就是DMP的固件二进制代码。
dmp3_image_size实际上就是sizeof(dmp3_image),即dmp3_image数组的长度(大小)。
现在就能够基本知道inv_icm20948_firmware_load函数的作用了,正如其名,是加载DMP固件到ICM20948的寄存器中。
具体的代码实现细节的解析,请看下回。
标签:ICM20948,icm20948,inv,0x00,DMP,write,result,24,size From: https://blog.csdn.net/phmatthaus/article/details/142197918