在线时间72 小时
UID2088849
注册时间2015-8-11
NXP金币16
TA的每日心情 | 开心 2020-7-22 16:18 |
---|
签到天数: 77 天 连续签到: 1 天 [LV.6]常住居民II
高级会员

- 积分
- 816
- 最后登录
- 2023-12-22
|
以前用过K64的硬件I2C来读取MPU6050,现在需要用KL26的I2C来读取另外一款六轴传感器ICM20608(跟MPU6050基本一样),但是现在死活调不通I2C。用的是官方SDK2.2版本的库,各位大佬帮忙看一下,下面是相关代码。
- //i2c回调函数
- static void i2c_master_callback(I2C_Type *base, i2c_master_handle_t *handle, status_t status, void *userData)
- {
- /* Signal transfer success when received success status. */
- if (status == kStatus_Success)
- {
- completionFlag = true;
- }
- /* Signal transfer success when received success status. */
- if ((status == kStatus_I2C_Nak) || (status == kStatus_I2C_Addr_Nak))
- {
- nakFlag = true;
- }
- }
- //i2c写寄存器
- static bool I2C_WriteReg(I2C_Type *base, uint8_t device_addr, uint8_t reg_addr, uint8_t value)
- {
- i2c_master_transfer_t masterXfer;
- memset(&masterXfer, 0, sizeof(masterXfer));
- masterXfer.slaveAddress = device_addr;
- masterXfer.direction = kI2C_Write;
- masterXfer.subaddress = reg_addr;
- masterXfer.subaddressSize = 1;
- masterXfer.data = &value;
- masterXfer.dataSize = 1;
- masterXfer.flags = kI2C_TransferDefaultFlag;
- /* direction=write : start+device_write;cmdbuff;xBuff; */
- /* direction=recive : start+device_write;cmdbuff;repeatStart+device_read;xBuff; */
- I2C_MasterTransferNonBlocking(I2C_BASEADDR, &g_m_handle, &masterXfer);
- /* wait for transfer completed. */
- while ((!nakFlag) && (!completionFlag))
- {
- }
- nakFlag = false;
- if (completionFlag == true)
- {
- completionFlag = false;
- return true;
- }
- else
- {
- return false;
- }
- }
- //i2c读寄存器
- static bool I2C_ReadRegs(I2C_Type *base, uint8_t device_addr, uint8_t reg_addr, uint8_t *rxBuff, uint32_t rxSize)
- {
- i2c_master_transfer_t masterXfer;
- memset(&masterXfer, 0, sizeof(masterXfer));
- masterXfer.slaveAddress = device_addr;
- masterXfer.direction = kI2C_Read;
- masterXfer.subaddress = reg_addr;
- masterXfer.subaddressSize = 1;
- masterXfer.data = rxBuff;
- masterXfer.dataSize = rxSize;
- masterXfer.flags = kI2C_TransferDefaultFlag;
- /* direction=write : start+device_write;cmdbuff;xBuff; */
- /* direction=recive : start+device_write;cmdbuff;repeatStart+device_read;xBuff; */
- I2C_MasterTransferNonBlocking(I2C_BASEADDR, &g_m_handle, &masterXfer);
- /* wait for transfer completed. */
- while ((!nakFlag) && (!completionFlag))
- {
- }
- nakFlag = false;
- if (completionFlag == true)
- {
- completionFlag = false;
- return true;
- }
- else
- {
- return false;
- }
- }
- //i2c引脚复用设置
- void I2C_ConfigurePins(void)
- {
- PORT_SetPinMux(PORTB, 0U, kPORT_MuxAlt2); /* PORTB0 (pin 35) is configured as I2C0_SCL */
- PORTB->PCR[0] = ((PORTB->PCR[0] &
- (~(PORT_PCR_PS_MASK | PORT_PCR_PE_MASK | PORT_PCR_ISF_MASK))) /* Mask bits to zero which are setting */
- | PORT_PCR_PS(0x01u) /* Pull Select: Internal pullup resistor is enabled on the corresponding pin, if the corresponding PE field is set. */
- | PORT_PCR_PE(0x01U) /* Pull Enable: Internal pullup or pulldown resistor is enabled on the corresponding pin, if the pin is configured as a digital input. */
- );
- PORT_SetPinMux(PORTB, 1U, kPORT_MuxAlt2); /* PORTB1 (pin 36) is configured as I2C0_SDA */
- PORTB->PCR[1] = ((PORTB->PCR[1] &
- (~(PORT_PCR_PS_MASK | PORT_PCR_PE_MASK | PORT_PCR_ISF_MASK))) /* Mask bits to zero which are setting */
- | PORT_PCR_PS(0x01u) /* Pull Select: Internal pullup resistor is enabled on the corresponding pin, if the corresponding PE field is set. */
- | PORT_PCR_PE(0x01u) /* Pull Enable: Internal pullup or pulldown resistor is enabled on the corresponding pin, if the pin is configured as a digital input. */
- );
- int icm_init(void)
- {
- unsigned char data[6];
-
- I2C_ConfigurePins();
- I2C_MasterTransferCreateHandle(I2C_BASEADDR, &g_m_handle, i2c_master_callback, NULL);
- i2c_master_config_t masterConfig;
- /*
- * masterConfig.baudRate_Bps = 100000U;
- * masterConfig.enableStopHold = false;
- * masterConfig.glitchFilterWidth = 0U;
- * masterConfig.enableMaster = true;
- */
- I2C_MasterGetDefaultConfig(&masterConfig);
- I2C_MasterInit(I2C_BASEADDR, &masterConfig, CLOCK_GetFreq(I2C0_CLK_SRC));
-
- /* Reset device. */
- data[0] = BIT_RESET;
- if(I2C_WriteReg(I2C_BASEADDR,st.hw->addr,st.reg->pwr_mgmt_1,data[0]))
- return -1;
- delay_ms(100);
- /* Wake up chip. */
- data[0] = 0x00;
-
- if (I2C_WriteReg(I2C_BASEADDR,st.hw->addr, st.reg->pwr_mgmt_1, data[0]))
- return -1;
- /* ICM20608 has no DMP therefore no need to share memory */
- data[0] = BIT_FIFO_SIZE_4096;
-
- if (I2C_WriteReg(I2C_BASEADDR,st.hw->addr, st.reg->accel_cfg2, data[0]))
- return -1;
-
- /* Set to invalid values to ensure no I2C writes are skipped. */
- st.chip_cfg.sensors = 0xFF;
- st.chip_cfg.gyro_fsr = 0xFF;
- st.chip_cfg.accel_fsr = 0xFF;
- st.chip_cfg.gyro_lpf = 0xFF;
- st.chip_cfg.accel_lpf = 0xFF;
- st.chip_cfg.sample_rate = 0xFFFF;
- st.chip_cfg.fifo_enable = 0xFF;
- st.chip_cfg.bypass_mode = 0xFF;
-
- /* icm_set_sensors always preserves this setting. */
- st.chip_cfg.clk_src = INV_CLK_PLL;
- /* Handled in next call to icm_set_bypass. */
- st.chip_cfg.active_low_int = 1;
- st.chip_cfg.latched_int = 0;
- st.chip_cfg.int_motion_only = 0;
- st.chip_cfg.lp_accel_mode = 0;
- memset(&st.chip_cfg.cache, 0, sizeof(st.chip_cfg.cache));
- if (icm_set_gyro_fsr(2000))
- return -1;
- if (icm_set_accel_fsr(2))
- return -1;
- if (icm_set_gyro_lpf(50))
- return -1;
- if (icm_set_accel_lpf(50))
- return -1;
- if (icm_set_sample_rate(50))
- return -1;
- if (icm_configure_fifo(0))
- return -1;
- if (icm_set_bypass(0))
- return -1;
- icm_set_sensors(0);
- return 0;
- }
复制代码 全速运行起来,代码第一次运行到I2C写入的地方,也就是下面这句后,会进入到I2C_WriteReg函数,然后就停在
/* wait for transfer completed. */
while ((!nakFlag) && (!completionFlag))
{
}
这个死循环里。
- I2C_WriteReg(I2C_BASEADDR,st.hw->addr,st.reg->pwr_mgmt_1,data[0]
复制代码
各位大佬帮小弟看看,到底是哪里有问题呢。这个代码是以前K64上能成功读取MPU6050的。
最佳答案
楼主啊,你这洋洋洒洒代码会让其他小伙伴望而却步的,我的建议是你可以用示波器或逻辑分析仪捕捉一下KL26和K64与slave target通信时的波形,这样不是一目了然了吗?! ...
|
|