uchar i2c_read (uchar dev_addr, unsigned int offset, int alen, uchar * data, int len) { uchar status = 0; unsigned int i2cFreq = CONFIG_SYS_I2C_SPEED; DP (puts ("i2c_read\n")); i2c_init (i2cFreq, 0); /* set the i2c frequency */ status = i2c_start (); if (status) { #ifdef DEBUG_I2C printf ("Transaction start failed: 0x%02x\n", status); #endif return status; } status = i2c_set_dev_offset (dev_addr, offset, 0, alen); /* send the slave address + offset */ if (status) { #ifdef DEBUG_I2C printf ("Failed to set slave address & offset: 0x%02x\n", status); #endif return status; } i2c_init (i2cFreq, 0); /* set the i2c frequency again */ status = i2c_start (); if (status) { #ifdef DEBUG_I2C printf ("Transaction restart failed: 0x%02x\n", status); #endif return status; } status = i2c_select_device (dev_addr, 1, 0); /* send the slave address */ if (status) { #ifdef DEBUG_I2C printf ("Address not acknowledged: 0x%02x\n", status); #endif return status; } status = i2c_get_data (data, len); if (status) { #ifdef DEBUG_I2C printf ("Data not recieved: 0x%02x\n", status); #endif return status; } return 0; }
/*! * \brief Write to an I<sup>2</sup>C device. * * The i2c_write() primitive writes data to a device at the given address * on the given I<sup>2</sup>C Bus. * * \param [in,out] i2c Pointer to I<sup>2</sup>C Bus handle. * \param addr I<sup>2</sup>C device's 7/10-bit address. * \param buf Pointer to the buffer that will be written. * \param len Number of bytes to write. * * \return * On success, returns \h_ge 0 number of bytes written.\n * Else errno is set appropriately and -1 is returned. */ int i2c_write(i2c_t *i2c, i2c_addr_t addr, const byte_t *buf, uint_t len) { int rc; if( (rc = i2c_select_device(i2c , addr)) != -1 ) { rc = (int)write(i2c->fd , buf, (size_t)len); } return rc; }
static uchar i2c_set_dev_offset (uchar dev_addr, unsigned int offset, int ten_bit, int alen) { uchar status; unsigned int table[2]; /* initialize the table of address offset bytes */ /* utilized for 2 byte address offsets */ /* NOTE: the order is high byte first! */ table[1] = offset & 0xff; /* low byte */ table[0] = offset / 0x100; /* high byte */ DP (puts ("i2c_set_dev_offset\n")); status = i2c_select_device (dev_addr, 0, ten_bit); if (status) { #ifdef DEBUG_I2C printf ("Failed to select device setting offset: 0x%02x\n", status); #endif return status; } /* check the address offset length */ if (alen == 0) /* no address offset */ return (0); else if (alen == 1) { /* 1 byte address offset */ status = i2c_write_data (&offset, 1); if (status) { #ifdef DEBUG_I2C printf ("Failed to write data: 0x%02x\n", status); #endif return status; } } else if (alen == 2) { /* 2 bytes address offset */ status = i2c_write_data (table, 2); if (status) { #ifdef DEBUG_I2C printf ("Failed to write data: 0x%02x\n", status); #endif return status; } } else { /* address offset unknown or not supported */ printf ("Address length offset %d is not supported\n", alen); return 1; } return 0; /* sucessful completion */ }
/* 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; }
/* 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; }
/* Read data from the IO Expander */ int i2c_ioexp_read(uint8_t *msg, int size, uint8_t addr) { int ret; dbg_verbose("%s()\n", __func__); dbg_print_buf(DBG_VERBOSE, msg, size); i2c_select_device(addr); /* We need 2 messages (cf. datasheet) */ struct i2c_msg_s msgv[2] = { /* Write the command byte, no restart */ { .addr = addr, .flags = 0, .buffer = msg, .length = 1 }, /* Read the bytes */ { .addr = addr,
bool read_l3g4200d(int& fd,short& x, short& y, short& z) { if(!i2c_select_device(fd,L3G4200D,"L3G4200D")) { return false; } char buf[7]; buf[0] = 0x28; // This is the register we wish to read from if(!i2c_write(fd,buf,2)) { return false; } if (i2c_read(fd, buf, 6) != 6) { // Read back data into buf[] printf("Unable to read from slave\n"); return false; } else { x = (buf[1]<<8) | buf[0]; y = (buf[3]<<8) | buf[2]; z = (buf[5]<<8) | buf[4]; } return true; }