uint8 HalI2CWriteSingle(uint8 addr, uint8 data) { uint8 ret = 0; if (i2cMstStrt(0) != mstAddrAckW) { I2C_STOP(); return ret; } I2C_WRITE(addr); if (I2CSTAT != mstDataAckW) { I2C_STOP(); return ret; } I2C_WRITE(data); if (I2CSTAT == mstDataAckW) { ret = 1; } I2C_STOP(); return ret; }
void motoi2c_attach_common(device_t self, struct motoi2c_softc *sc, const struct motoi2c_settings *i2c) { struct i2cbus_attach_args iba; mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE); if (i2c == NULL) i2c = &motoi2c_default_settings; sc->sc_i2c = motoi2c; sc->sc_i2c.ic_cookie = sc; if (sc->sc_iord == NULL) sc->sc_iord = motoi2c_iord1; if (sc->sc_iowr == NULL) sc->sc_iowr = motoi2c_iowr1; memset(&iba, 0, sizeof(iba)); iba.iba_tag = &sc->sc_i2c; I2C_WRITE(I2CCR, 0); /* reset before changing anything */ I2C_WRITE(I2CDFSRR, i2c->i2c_dfsrr); /* sampling units */ I2C_WRITE(I2CFDR, i2c->i2c_fdr); /* divider 3072 (0x31) */ I2C_WRITE(I2CADR, i2c->i2c_adr); /* our slave address is 0x7f */ I2C_WRITE(I2CSR, 0); /* clear status flags */ config_found_ia(self, "i2cbus", &iba, iicbus_print); }
static void headset_on_work(struct work_struct *work) { unsigned char reg_value; // I2C_WRITE(MAX9879_INPUTMODE_CTRL,0x70); // dINA = 1(mono), dINB = 1(mono) I2C_READ(MAX9879_OUTPUTMODE_CTRL,®_value); if(reg_value & 0x80) { reg_value &= 0x7F; I2C_WRITE(MAX9879_OUTPUTMODE_CTRL, reg_value); I2C_WRITE(MAX9879_SPKVOL_CTRL,0x0); // MAX 0x1F : 0dB I2C_WRITE(MAX9879_LEFT_HPHVOL_CTRL,0x0); // MAX 0x1F : 0dB I2C_WRITE(MAX9879_RIGHT_HPHVOL_CTRL,0x0); // MAX 0x1F : 0dB } // msleep(1000); // I2C_WRITE(MAX9879_LEFT_HPHVOL_CTRL,0x17); // MAX 0x1F : 0dB // I2C_WRITE(MAX9879_RIGHT_HPHVOL_CTRL,0x17); // MAX 0x1F : 0dB #if 0 if(is_incall == 0) { msleep(6000); } else { msleep(300); } #else msleep(600); #endif max9879_i2c_headset_onoff(true); }
static Status tml_Tx(uint8_t *pBuff, uint16_t buffLen) { if (I2C_WRITE(pBuff, buffLen) != kStatus_Success) { Sleep(10); if(I2C_WRITE(pBuff, buffLen) != kStatus_Success) { return ERROR; } } return SUCCESS; }
int sleepMode(void){ unsigned char ret = -1; ret = I2C_START(TemperatureSensorAddress); if(ret != 0) return ret; I2C_WRITE(0x01); // write on confiuration register I2C_WRITE(0xC0); // Put SuhtDown mode, start one shot convertion I2C_STOP(); return 0; }
int WakeUpMode(void){ unsigned char ret = -1; ret = I2C_START(TemperatureSensorAddress); if(ret != 0) return ret; I2C_WRITE(0x01); // CONTROLS/STATUS REGISTER I2C_WRITE(0x40); //WakeUP I2C_STOP(); return 0; }
static int bq2425x_getreg8(FAR struct bq2425x_dev_s *priv, uint8_t regaddr, FAR uint8_t *regval) { uint8_t val; int ret; /* Set the I2C address and address size */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); /* Write the register address */ ret = I2C_WRITE(priv->i2c, ®addr, 1); if (ret < 0) { batdbg("I2C_WRITE failed: %d\n", ret); return ret; } /* Restart and read 8-bits from the register */ ret = I2C_READ(priv->i2c, &val, 1); if (ret < 0) { batdbg("I2C_READ failed: %d\n", ret); return ret; } /* Copy 8-bit value to be returned */ *regval = val; return OK; }
static int ov2640_putreg(FAR struct i2c_dev_s *i2c, uint8_t regaddr, uint8_t regval) { uint8_t buffer[2]; int ret; #ifdef CONFIG_OV2640_REGDEBUG dbg("%02x <- %02x\n", regaddr, regval); #endif /* Set up for the transfer */ buffer[0] = regaddr; /* Register address */ buffer[1] = regval; /* New register value */ /* And do it */ ret = I2C_WRITE(i2c, buffer, 2); if (ret < 0) { gdbg("ERROR: I2C_WRITE failed: %d\n", ret); return ret; } return OK; }
/************************************************************************************************** * @fn HalI2CWrite * * @brief Write to the I2C bus as a Master. * * input parameters * * @param len - Number of bytes to write. * @param pBuf - Pointer to the data buffer to write. * * output parameters * * None. * * @return The number of bytes successfully written. */ uint8 HalI2CWrite(uint8 len, uint8 *pBuf) { if (i2cMstStrt(0) != mstAddrAckW) { len = 0; } for (uint8 cnt = 0; cnt < len; cnt++) { I2C_WRITE(*pBuf++); if (I2CSTAT != mstDataAckW) { if (I2CSTAT == mstDataNackW) { len = cnt + 1; } else { len = cnt; } break; } } I2C_STOP(); return len; }
static int lm75_readb16(FAR struct lm75_dev_s *priv, uint8_t regaddr, FAR b16_t *regvalue) { uint8_t buffer[2]; int ret; /* Write the register address */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); ret = I2C_WRITE(priv->i2c, ®addr, 1); if (ret < 0) { sndbg("I2C_WRITE failed: %d\n", ret); return ret; } /* Restart and read 16-bits from the register (discarding 7) */ ret = I2C_READ(priv->i2c, buffer, 2); if (ret < 0) { sndbg("I2C_READ failed: %d\n", ret); return ret; } /* Data format is: TTTTTTTT Txxxxxxx where TTTTTTTTT is a nine-bit, * signed temperature value with LSB = 0.5 degrees centigrade. So the * raw data is b8_t */ *regvalue = b8tob16((b8_t)buffer[0] << 8 | (b8_t)buffer[1]); sndbg("addr: %02x value: %08x ret: %d\n", regaddr, *regvalue, ret); return OK; }
static int mb7040_changeaddr(FAR struct mb7040_dev_s *priv, uint8_t addr) { uint8_t buffer[3]; int ret; sndbg("new addr: %02x\n", addr); /* Sanity check */ DEBUGASSERT((addr & 1) == 0); DEBUGASSERT(addr != 0x00 && addr != 0x50 && addr != 0xa4 && addr != 0xaa); /* Set up a 3-byte message to send */ buffer[0] = MB7040_ADDRUNLOCK1_REG; buffer[1] = MB7040_ADDRUNLOCK2_REG; buffer[2] = addr; /* Write the register address followed by the data (no RESTART) */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); ret = I2C_WRITE(priv->i2c, buffer, sizeof(buffer)); if (ret < 0) { sndbg("I2C_WRITE failed: %d\n", ret); return ret; } priv->addr = addr; return ret; }
uint8 HalI2CReadSingle(uint8 addr) { uint8 ret = 0xFF; if (i2cMstStrt(0) != mstAddrAckW) { I2C_STOP(); return ret; } I2C_WRITE(addr); if (I2CSTAT != mstDataAckW) { I2C_STOP(); return ret; } if (i2cMstRepeatedStrt(I2C_MST_RD_BIT) != mstAddrAckR) { I2C_STOP(); return ret; } I2C_SET_NACK(); // read a byte from the I2C interface I2C_READ(ret); if (I2CSTAT != mstDataNackR) { ret = 0xFF; } I2C_STOP(); return ret; }
static uint8_t ov2640_getreg(FAR struct i2c_dev_s *i2c, uint8_t regaddr) { uint8_t regval; int ret; /* Write the register address */ ret = I2C_WRITE(i2c, ®addr, 1); if (ret < 0) { gdbg("ERROR: I2C_WRITE failed: %d\n", ret); return 0; } /* Restart and read 8-bits from the register */ ret = I2C_READ(i2c, ®val, 1); if (ret < 0) { gdbg("ERROR: I2C_READ failed: %d\n", ret); return 0; } #ifdef CONFIG_OV2640_REGDEBUG else { dbg("%02x -> %02x\n", regaddr, regval); } #endif return regval; }
/** * prcmu_abb_write() - Write register value(s) to the ABB. * @slave: The I2C slave address. * @reg: The (start) register address. * @value: The value(s) to write. * @size: The number of registers to write. * * Reads register value(s) from the ABB. * @size has to be 1 for the current firmware version. */ int prcmu_abb_write(u8 slave, u8 reg, u8 *value, u8 size) { int r; if (size != 1) return -EINVAL; r = mutex_lock_interruptible(&mb5_transfer.lock); if (r) return r; while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5)) cpu_relax(); writeb(I2C_WRITE(slave), REQ_MB5_I2C_SLAVE_OP); writeb(I2C_STOP_EN, REQ_MB5_I2C_HW_BITS); writeb(reg, REQ_MB5_I2C_REG); writeb(*value, REQ_MB5_I2C_VAL); writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET); if (!wait_for_completion_timeout(&mb5_transfer.work, msecs_to_jiffies(500))) { pr_err("prcmu: prcmu_abb_write timed out.\n"); r = -EIO; goto unlock_and_return; } r = ((mb5_transfer.ack.status == I2C_WR_OK) ? 0 : -EIO); unlock_and_return: mutex_unlock(&mb5_transfer.lock); return r; }
/* busy waiting for byte data transfer completion */ static int motoi2c_busy_wait(struct motoi2c_softc *sc, uint8_t cr) { uint8_t sr; u_int timo; int error = 0; timo = 1000; while (((sr = I2C_READ(I2CSR)) & SR_MIF) == 0 && --timo) DELAY(10); if (timo == 0) { DPRINTF(("%s: timeout (sr=%#x, cr=%#x)\n", __func__, sr, I2C_READ(I2CCR))); error = ETIMEDOUT; } /* * RXAK is only valid when transmitting. */ if ((cr & CR_MTX) && (sr & SR_RXAK)) { DPRINTF(("%s: missing rx ack (%#x): spin=%u\n", __func__, sr, 1000 - timo)); error = EIO; } I2C_WRITE(I2CSR, 0); return error; }
static int max1704x_getreg16(FAR struct max1704x_dev_s *priv, uint8_t regaddr, FAR uint16_t *regval) { uint8_t buffer[2]; int ret; /* Set the I2C address and address size */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); /* Write the register address */ ret = I2C_WRITE(priv->i2c, ®addr, 1); if (ret < 0) { batdbg("I2C_WRITE failed: %d\n", ret); return ret; } /* Restart and read 16-bits from the register */ ret = I2C_READ(priv->i2c, buffer, 2); if (ret < 0) { batdbg("I2C_READ failed: %d\n", ret); return ret; } /* Return the 16-bit value */ return (uint16_t)buffer[0] << 8 | (uint16_t)buffer[1]; return OK; }
static void motoi2c_release_bus(void *v, int flags) { struct motoi2c_softc * const sc = v; I2C_WRITE(I2CCR, 0); /* reset before changing anything */ mutex_exit(&sc->sc_buslock); }
int configureTemperature(void){ int t; I2C_INIT(); t=I2C_START(TemperatureSensorAddress+I2C_WRITE_FLAG); if(t!=0) return t; I2C_WRITE(0x04); // CONTROLS/STATUS REGISTER I2C_WRITE(0xE0); // 14bits resolution & Timeout Off0xE0 I2C_WRITE(0x01); // CONTROLS/STATUS REGISTER I2C_WRITE(0x40); I2C_STOP(); return 0; }
static uint8 i2cMstRepeatedStrt(uint8 RD_WRn) { I2C_STRT(); if (I2CSTAT == mstRepStart) { I2C_WRITE(i2cAddr | RD_WRn); } return I2CSTAT; }
static int motoi2c_acquire_bus(void *v, int flags) { struct motoi2c_softc * const sc = v; mutex_enter(&sc->sc_buslock); I2C_WRITE(I2CCR, CR_MEN); /* enable the I2C module */ return 0; }
/************************************************************************************************** * @fn i2cMstStrt * * @brief Attempt to send an I2C bus START and Slave Address as an I2C bus Master. * * input parameters * * @param RD_WRn - The LSB of the Slave Address as Read/~Write. * * @return The I2C status of the START request or of the Slave Address Ack. */ static uint8 i2cMstStrt(uint8 RD_WRn) { I2C_STRT(); if (I2CSTAT == mstStarted) /* A start condition has been transmitted */ { I2C_WRITE(i2cAddr | RD_WRn); } return I2CSTAT; }
unsigned char readAdc1(void){ unsigned char readValue = 0; I2C_START_WAIT(LightSensorAddress+I2C_WRITE_FLAG); I2C_WRITE(0x83); I2C_REP_START(LightSensorAddress+I2C_READ_FLAG); readValue = I2C_READ_ACK(); return readValue; }
int getTemperature(void){ int temperature=0; unsigned char temp; WakeUpMode(); /* Wait data temperature available */ do { I2C_START_WAIT(TemperatureSensorAddress+I2C_WRITE_FLAG); I2C_WRITE(0x04); // CONTROLS/STATUS REGISTER I2C_REP_START(TemperatureSensorAddress+I2C_READ_FLAG); // Send a responce request temp = I2C_READ_NACK(); // Read the response I2C_STOP(); } while(!(temp & 0x01)); // While Data Available Flag is not set /* Read the TEMPERATURE DATA REGISTER */ I2C_START_WAIT(TemperatureSensorAddress+I2C_WRITE_FLAG); I2C_WRITE(0x00); // Write the temperature data register 0x00 I2C_REP_START(TemperatureSensorAddress+I2C_READ_FLAG); // Send a responce request temperature = I2C_READ_NACK()<<8; // Read the response I2C_WRITE(0x10); // Write the temperature hight byte data register 0x00 I2C_REP_START(TemperatureSensorAddress+I2C_READ_FLAG); // Send a responce request temperature = I2C_READ_NACK()<<8; // Read the response I2C_WRITE(0x11); // Write the temperature low byte data register 0x00 I2C_REP_START(TemperatureSensorAddress+I2C_READ_FLAG); // Send a responce request temperature |= I2C_READ_NACK(); // Read the response I2C_STOP(); /* Go to sleep mode */ sleepMode(); return temperature/128; }
static int kxtj9_reg_write8(uint8_t reg, uint8_t val) { uint8_t buf[2]; int ret = 0; int index = ACCEL_NUM_RETRIES; buf[0] = reg; buf[1] = val; do { ret = I2C_WRITE(g_data->i2c, buf, 2); } while (ret != 0 && index--); return ret; }
static int lm75_writeconf(FAR struct lm75_dev_s *priv, uint8_t conf) { uint8_t buffer[2]; sndbg("conf: %02x\n", conf); /* Set up a 2 byte message to send */ buffer[0] = LM75_CONF_REG; buffer[1] = conf; /* Write the register address followed by the data (no RESTART) */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); return I2C_WRITE(priv->i2c, buffer, 2); }
int tfa9890_reg_write(FAR struct tfa9890_dev_s *priv, uint8_t reg, uint16_t val) { uint8_t buf[3]; I2C_SETADDRESS(priv->i2c, priv->i2c_addr, 7); buf[0] = reg; #ifdef CONFIG_ENDIAN_BIG buf[1] = val & 0x00ff; buf[2] = (val & 0xff00) >> 8; #else buf[2] = val & 0x00ff; buf[1] = (val & 0xff00) >> 8; #endif return I2C_WRITE(priv->i2c, (uint8_t *)buf, 4); }
/* Write data to the IO Expander */ int i2c_ioexp_write(uint8_t *msg, int size, uint8_t addr) { int ret; i2c_select_device(addr); ret = I2C_WRITE(sw_exp_dev, msg, size); if (ret) { dbg_error("%s(): Error %d\n", __func__, ret); return ERROR; } dbg_verbose("%s()\n", __func__); dbg_print_buf(DBG_VERBOSE, msg, size); return ret; }
/* Send data from SVC to switch */ inline int i2c_switch_send_msg(uint8_t *buf, unsigned int size) { int ret; dbg_verbose("%s()\n", __func__); dbg_print_buf(DBG_VERBOSE, buf, size); i2c_select_device(I2C_ADDR_SWITCH); ret = I2C_WRITE(sw_exp_dev, buf, size); if (ret) { dbg_error("%s(): Error %d\n", __func__, ret); return ERROR; } return 0; }
static int mb7040_measurerange(FAR struct mb7040_dev_s *priv) { uint8_t regaddr; int ret; regaddr = MB7040_RANGE_REG; sndbg("addr: %02x\n", regaddr); /* Write the register address */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); ret = I2C_WRITE(priv->i2c, ®addr, sizeof(regaddr)); if (ret < 0) { sndbg("I2C_WRITE failed: %d\n", ret); } return ret; }
static int bq2425x_putreg8(FAR struct bq2425x_dev_s *priv, uint8_t regaddr, uint8_t regval) { uint8_t buffer[2]; batdbg("addr: %02x regval: %08x\n", regaddr, regval); /* Set up a 3 byte message to send */ buffer[0] = regaddr; buffer[1] = regval; /* Set the I2C address and address size */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); /* Write the register address followed by the data (no RESTART) */ return I2C_WRITE(priv->i2c, buffer, 2); }