我是 atmel / microchip studio 的新手,并试图通过 I2C 与从机通信。I2C 传输命令的 HAL 库或 ASF 框架如下。
int32_t i2c_m_sync_cmd_write(struct i2c_m_sync_desc *i2c, uint8_t reg, uint8_t *buffer, uint8_t length)
{
struct _i2c_m_msg msg;
int32_t ret;
msg.addr = i2c->slave_addr;
msg.len = 1;
msg.flags = 0;
msg.buffer = ®
ret = _i2c_m_sync_transfer(&i2c->device, &msg);
if (ret != 0) {
/* error occurred */
return ret;
}
****msg.flags = I2C_M_STOP;
msg.buffer = buffer;
msg.len = length;
ret = _i2c_m_sync_transfer(&i2c->device, &msg);
if (ret != 0) {
/* error occurred */
return ret;
}****
return ERR_NONE;
}
问题是四颗星(****)之间的代码传输了一个不需要的帧,因此,从站行为异常。所需帧在代码星号部分上方的代码部分中正确传输。如果注释星号部分,则代码无法正常工作。有什么解决办法吗?