int wiringPiI2CReadReg8 (int fd, int reg) { union i2c_smbus_data data; if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data)) return -1 ; else return data.byte & 0xFF ; }
int wiringPiI2CRead (int fd) { union i2c_smbus_data data ; if (i2c_smbus_access (fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data)) return -1 ; else return data.byte & 0xFF ; }
static int32_t i2c_smbus_write_word_data(int file, uint8_t cmd, uint16_t value) { union i2c_smbus_data data; data.word = value; return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd, I2C_SMBUS_WORD_DATA, &data); }
int modread16 (int fd, int reg) { union i2c_smbus_data data; if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data)) return -1 ; else return data.word & 0xFFFF ; }
static int32_t i2c_smbus_write_byte_data(int file, uint8_t cmd, uint8_t value) { union i2c_smbus_data data; data.byte = value; return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd, I2C_SMBUS_BYTE_DATA, &data); }
/*! * \brief Read a data 2-byte word from the SMBus. * * \param fd File descriptor to opened SMBus device. * \param command Command to SMBus device. * * \return * Returns read 2-byte word on on success. * Else errno is set appropriately and -1 is returned. */ int i2c_smbus_read_word_data(int fd, uint8_t command) { i2c_smbus_data_t data; int rc; rc = i2c_smbus_access(fd, I2C_SMBUS_READ, command, I2C_SMBUS_WORD_DATA, &data); return rc>=0? 0x0FFFF & data.word: -1; }
/*! * \brief Read a data byte from the SMBus. * * \param fd File descriptor to opened SMBus device. * \param command Command to SMBus device. * * \return * Returns read byte on on success. * Else errno is set appropriately and -1 is returned. */ int i2c_smbus_read_byte_data(int fd, uint8_t command) { i2c_smbus_data_t data; int rc; rc = i2c_smbus_access(fd, I2C_SMBUS_READ, command, I2C_SMBUS_BYTE_DATA, &data); return rc>=0? 0x0FF & data.byte: -1; }
static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value) { union i2c_smbus_data data; data.word = value; if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command, I2C_SMBUS_PROC_CALL,&data)) return -1; else return 0x0FFFF & data.word; }
__s32 i2c_smbus_read_byte(int file) { union i2c_smbus_data data; int err; err = i2c_smbus_access(file, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data); if (err < 0) return err; return 0x0FF & data.byte; }
// add int wiringPiI2CWriteBlock (int fd, int size, int reg, uint8_t *value) { union i2c_smbus_data data ; data.block[0] = size; for(int i=0; i < size; i++) { data.block[i+1] = value[i]; } return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BLOCK_DATA, &data) ; }
static int32_t i2c_smbus_read_byte(int fd) { union i2c_smbus_data data; int err; err = i2c_smbus_access(fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data); if (err < 0) return err; return data.byte; }
static int32_t i2c_smbus_read_word_data(int fd, uint8_t cmd) { union i2c_smbus_data data; int err; err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd, I2C_SMBUS_WORD_DATA, &data); if (err < 0) return err; return data.word; }
static int32_t i2c_smbus_read_byte_data(int fd, uint8_t cmd) { union i2c_smbus_data data; int err; err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd, I2C_SMBUS_BYTE_DATA, &data); if (err < 0) return err; return data.byte; }
/*! * \brief Issue a 2-byte word process call (write/read) to the SMBus. * * \param fd File descriptor to opened SMBus device. * \param command Command to SMBus device. * \param value Word value to write. * * \return * Returns read 2-byte word on on success. * Else errno is set appropriately and -1 is returned. */ int i2c_smbus_process_call(int fd, uint8_t command, unsigned short value) { i2c_smbus_data_t data; int rc; data.word = value; rc = i2c_smbus_access(fd, I2C_SMBUS_WRITE, command, I2C_SMBUS_PROC_CALL, &data); return rc>=0? 0x0FFFF & data.word: -1; }
__s32 i2c_smbus_read_word_data(int file, __u8 command) { union i2c_smbus_data data; int err; err = i2c_smbus_access(file, I2C_SMBUS_READ, command, I2C_SMBUS_WORD_DATA, &data); if (err < 0) return err; return 0x0FFFF & data.word; }
__s32 i2c_smbus_read_byte_data(int file, __u8 command) { union i2c_smbus_data data; int err; err = i2c_smbus_access(file, I2C_SMBUS_READ, command, I2C_SMBUS_BYTE_DATA, &data); if (err < 0) return err; return 0x0FF & data.byte; }
__s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, __u8 length, const __u8 *values) { union i2c_smbus_data data; int i; if (length > I2C_SMBUS_BLOCK_MAX) length = I2C_SMBUS_BLOCK_MAX; for (i = 1; i <= length; i++) data.block[i] = values[i-1]; data.block[0] = length; return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, I2C_SMBUS_I2C_BLOCK_BROKEN, &data); }
static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, __u8 length, __u8 *values) { union i2c_smbus_data data; int i; if (length > 32) length = 32; for (i = 1; i <= length; i++) data.block[i] = values[i-1]; data.block[0] = length; return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, I2C_SMBUS_I2C_BLOCK_DATA, &data); }
__s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values) { union i2c_smbus_data data; int i, err; err = i2c_smbus_access(file, I2C_SMBUS_READ, command, I2C_SMBUS_BLOCK_DATA, &data); if (err < 0) return err; for (i = 1; i <= data.block[0]; i++) values[i-1] = data.block[i]; return data.block[0]; }
/* Returns the number of read bytes */ static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, __u8 *values) { union i2c_smbus_data data; int i; if (i2c_smbus_access(file,I2C_SMBUS_READ,command, I2C_SMBUS_I2C_BLOCK_DATA,&data)) return -1; else { for (i = 1; i <= data.block[0]; i++) values[i-1] = data.block[i]; return data.block[0]; } }
/* * Returns the number of bytes read, vals must hold at * least I2C_SMBUS_BLOCK_MAX bytes. */ static int32_t i2c_smbus_read_block_data(int fd, uint8_t cmd, uint8_t *vals) { union i2c_smbus_data data; int i, err; err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd, I2C_SMBUS_BLOCK_DATA, &data); if (err < 0) return err; for (i = 1; i <= data.block[0]; i++) *vals++ = data.block[i]; return data.block[0]; }
static int32_t i2c_smbus_write_i2c_block_data(int file, uint8_t cmd, uint8_t length, const uint8_t *values) { union i2c_smbus_data data; if (length > I2C_SMBUS_BLOCK_MAX) length = I2C_SMBUS_BLOCK_MAX; memcpy(data.block+1, values, length); data.block[0] = length; return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd, I2C_SMBUS_I2C_BLOCK_BROKEN, &data); }
/*! * \brief Read a block of data from the SMBus via low-level I<sup>2</sup>C. * * \param fd File descriptor to opened SMBus device. * \param command Command to SMBus device. * \param [out] values Buffer to hold the block of read byte values.\n * Must be large enough to receive the data. * * \return * On success, returns \h_ge 0 the number of bytes read, excluding any header * fields. Else errno is set appropriately and -1 is returned. */ int i2c_smbus_read_i2c_block_data(int fd, uint8_t command, uint8_t *values) { i2c_smbus_data_t data; int i; int rc; rc = i2c_smbus_access(fd, I2C_SMBUS_READ, command, I2C_SMBUS_I2C_BLOCK_DATA, &data); if( rc >= 0 ) { for(i=1; i<=data.block[0]; i++) { values[i-1] = data.block[i]; } rc = data.block[0]; } return rc; }
/* Returns the number of read bytes */ static inline __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length, __u8 *values) { union i2c_smbus_data data; int i; if (length > 32) length = 32; for (i = 1; i <= length; i++) data.block[i] = values[i-1]; data.block[0] = length; if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command, I2C_SMBUS_BLOCK_PROC_CALL,&data)) return -1; else { for (i = 1; i <= data.block[0]; i++) values[i-1] = data.block[i]; return data.block[0]; } }
int I2C::i2c_write(const uint8_t *data, int length) { if (m_fd == -1) return EBADF; if (length > I2C_SMBUS_BLOCK_MAX + 1) return EFBIG; union i2c_smbus_data msg; const uint8_t command = data[0]; --length; msg.block[0] = length; memcpy(msg.block + 1, data + 1, length); const int res = i2c_smbus_access(m_fd, I2C_SMBUS_WRITE, command, I2C_SMBUS_I2C_BLOCK_DATA, &msg); return res ? errno : 0; }
static int32_t i2c_smbus_read_i2c_block_data(int fd, uint8_t cmd, uint8_t len, uint8_t *vals) { union i2c_smbus_data data; int i, err; if (len > I2C_SMBUS_BLOCK_MAX) len = I2C_SMBUS_BLOCK_MAX; data.block[0] = len; err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd, len == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN : I2C_SMBUS_I2C_BLOCK_DATA, &data); if (err < 0) return err; for (i = 1; i <= data.block[0]; i++) *vals++ = data.block[i]; return data.block[0]; }
/*! * \brief Write a block of data to the SMBus via low-level I<sup>2</sup>C. * * \param fd File descriptor to opened SMBus device. * \param command Command to SMBus device. * \param length Length of buffer (bytes) to write. * \param [in] values Buffer of data to write. * * \return * Returns \h_ge 0 on success. * Else errno is set appropriately and -1 is returned. */ int i2c_smbus_write_i2c_block_data(int fd, uint8_t command, uint8_t length, const uint8_t *values) { i2c_smbus_data_t data; int i; if( length > I2C_SMBUS_I2C_BLOCK_MAX ) { length = I2C_SMBUS_I2C_BLOCK_MAX; } for(i=1; i<=length; i++) { data.block[i] = values[i-1]; } data.block[0] = length; return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command, I2C_SMBUS_I2C_BLOCK_DATA, &data); }
/* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you ask for less than 32 bytes, your code will only work with kernels 2.6.23 and later. */ __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, __u8 length, __u8 *values) { union i2c_smbus_data data; int i, err; if (length > I2C_SMBUS_BLOCK_MAX) length = I2C_SMBUS_BLOCK_MAX; data.block[0] = length; err = i2c_smbus_access(file, I2C_SMBUS_READ, command, length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN : I2C_SMBUS_I2C_BLOCK_DATA, &data); if (err < 0) return err; for (i = 1; i <= data.block[0]; i++) values[i-1] = data.block[i]; return data.block[0]; }
static void i2c_drv_ctl_write_block_data(i2c_drv_portdata* d, char* buf0, ErlDrvSizeT len) { __s32 result; union i2c_smbus_data data; data.block[0] = 2; data.block[1] = buf0[1]; data.block[2] = buf0[2]; result = i2c_smbus_access( d->fd, I2C_SMBUS_WRITE, (__u8)buf0[0], I2C_SMBUS_I2C_BLOCK_BROKEN, &data); if (result || d->verbose) fprintf(stderr, "i2c_smbus_write_block_data reg=0x%02X: [%i, %i] result=%i\r\n", buf0[0], buf0[1], buf0[2], result); }
/* Returns the number of read bytes */ __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length, __u8 *values) { union i2c_smbus_data data; int i, err; if (length > I2C_SMBUS_BLOCK_MAX) length = I2C_SMBUS_BLOCK_MAX; for (i = 1; i <= length; i++) data.block[i] = values[i-1]; data.block[0] = length; err = i2c_smbus_access(file, I2C_SMBUS_WRITE, command, I2C_SMBUS_BLOCK_PROC_CALL, &data); if (err < 0) return err; for (i = 1; i <= data.block[0]; i++) values[i-1] = data.block[i]; return data.block[0]; }