static int cs2100_write_ratio(FAR const struct cs2100_config_s *config, uint32_t ratio) { struct i2c_msg_s msg; uint8_t buffer[5]; regdbg("%02x<-%04l\n", CS2100_RATIO0, (unsigned long)ratio); DEBUGASSERT(config->i2c->ops && config->i2c->ops->transfer); /* Construct the I2C message (write N+1 bytes with no restart) */ buffer[0] = CS2100_RATIO0; buffer[1] = (uint8_t)(ratio >> 24); buffer[2] = (uint8_t)((ratio >> 16) & 0xff); buffer[3] = (uint8_t)((ratio >> 8) & 0xff); buffer[4] = (uint8_t)(ratio & 0xff); msg.addr = config->i2caddr; msg.flags = 0; msg.buffer = buffer; msg.length = 5; /* Send the message */ return I2C_TRANSFER(config->i2c, &msg, 1); }
static int cs2100_write_reg(FAR const struct cs2100_config_s *config, uint8_t regaddr, uint8_t regval) { struct i2c_msg_s msgs[2]; regdbg("%02x<-%02x\n", regaddr, regval); DEBUGASSERT(config->i2c->ops && config->i2c->ops->transfer); /* Construct the I2C message (write N+1 bytes with no restart) */ msga[0].frequency = config->i2cfreq; msgs[0].addr = config->i2caddr; msgs[0].flags = 0; msgs[0].buffer = ®addr; msgs[0].length = 1; msga[1].frequency = config->i2cfreq; msgs[1].addr = config->i2caddr; msgs[1].flags = I2C_M_NORESTART; msgs[1].buffer = ®val; msgs[1].length = 1; /* Send the message */ return I2C_TRANSFER(config->i2c, msgs, 2); }
static int cs2100_read_ratio(FAR const struct cs2100_config_s *config, uint32_t *ratio) { struct i2c_msg_s msg; uint8_t buffer[4]; int ret; DEBUGASSERT(config->i2c->ops && config->i2c->ops->transfer); /* Construct the I2C message (write N+1 bytes with no restart) */ buffer[0] = CS2100_RATIO0; msg.frequency = config->i2cfreq; msg.addr = config->i2caddr; msg.flags = 0; msg.buffer = buffer; msg.length = 1; /* Send the address followed by a STOP */ ret = I2C_TRANSFER(config->i2c, &msg, 1); if (ret == OK) { msg.frequency = config->i2cfreq; msg.addr = config->i2caddr; msg.flags = I2C_M_READ; msg.buffer = buffer; msg.length = 4; /* Read the ratio registers beginning with another START */ ret = I2C_TRANSFER(config->i2c, &msg, 1); /* Return the ratio */ if (ret == OK) { *ratio = ((uint32_t)buffer[0] << 24) | ((uint32_t)buffer[1] << 16) | ((uint32_t)buffer[2] << 8) | (uint32_t)buffer[0]; regdbg("%02x->%04l\n", CS2100_RATIO0, (unsigned long)*ratio); } } return ret; }
static int cs2100_read_reg(FAR const struct cs2100_config_s *config, uint8_t regaddr, uint8_t *regval) { struct i2c_msg_s msg; int ret; DEBUGASSERT(config->i2c->ops && config->i2c->ops->transfer); /* Construct the I2C message (write 1 bytes, restart, read N bytes) */ msg.frequency = config->i2cfreq; msg.addr = config->i2caddr; msg.flags = 0; msg.buffer = ®addr; msg.length = 1; /* Send the address followed by a STOP */ ret = I2C_TRANSFER(config->i2c, &msg, 1); if (ret == OK) { msg.frequency = config->i2cfreq; msg.addr = config->i2caddr; msg.flags = I2C_M_READ; msg.buffer = regval; msg.length = 1; /* Read the register beginning with another START */ ret = I2C_TRANSFER(config->i2c, &msg, 1); if (ret == OK) { regdbg("%02x->%02x\n", regaddr, *regval); } } return ret; }