// Read a char from servo ic uint8_t i2c_slave_read(uint8_t slave_address, uint8_t address) { uint8_t read_byte; // Read one byte (i.e. servo pos of one servo) i2c_Start(); // send Start i2c_Address(slave_address, I2C_WRITE); // Send slave address with write operation i2c_Write(address); // Set register for servo 0 i2c_Restart(); // Restart i2c_Address(slave_address, I2C_READ); // Send slave address with read operation read_byte = i2c_Read(0); // Read one byte // If more than one byte to be read, (0) should // be on last byte only // e.g.3 bytes= i2c_Read(1); i2c_Read(1); i2c_Read(0); i2c_Stop(); // send Stop return read_byte; // return byte. If reading more than one byte // store in an array }
unsigned int mem_Read(unsigned int addr,unsigned char *data, unsigned int data_len) { unsigned int i=0; unsigned char b; i2c_Start(); i2c_Write(MEM_ADDRESS); if (i2c_Timeout()) goto Timeout; b = addr>>8; i2c_Write(b); if (i2c_Timeout()) goto Timeout; b = addr&0xFF; i2c_Write(b); if (i2c_Timeout()) goto Timeout; //i2c_Stop(); i2c_Start(); i2c_Write(MEM_ADDRESS | 1); if (i2c_Timeout()) goto Timeout; for (i=0; i<data_len; i++) { data[i] = i2c_Read(); clear_CPU_operation_detection(); if (i<data_len-1) i2c_Ack(); else i2c_Nack(); } i2c_Stop(); return i; Timeout: return i; }
/************************************************************************* Read one byte from the I2C device, read is followed by a stop condition Return: byte read from I2C device *************************************************************************/ unsigned char i2c_readNak(void) { return i2c_Read(0); }/* i2c_readNak */
/************************************************************************* Read one byte from the I2C device, request more data from device Return: byte read from I2C device *************************************************************************/ unsigned char i2c_readAck(void) { return i2c_Read(1); }/* i2c_readAck */
/** * Perform continuous reading accesses on I2C buses. * @param[in] threadData data needed for I2C processing. */ static void tatl06_03I2C_Process(void *threadData) { DTH_I2C_PROCESS_DATA *i2cData ; int vl_error = 0; int fd = 0; int count = 1; char buf[1024]; i2cData = (DTH_I2C_PROCESS_DATA *) threadData ; memset(buf, 0, 1024); if (strcmp(i2cData->deviceName, I2C3_DEVICE_NAME) == 0) { /* on 8520 no I2C3. Need to check board type */ fd = open("/sys/devices/platform/ab8505-i2c.0/ab8500-usb.0/boot_time_device",O_RDWR); if (fd<0){ /* board 8500 */ count = 6; } else { /* board 8520 : just need to return OK*/ close (fd); while (i2cData->order == START) { i2cData->state = ACTIVATED ; i2cData->error = NO_ERROR ; usleep(1000); } i2cData->state = DEACTIVATED ; pthread_exit(NULL); } } buf[0] = i2cData->registerAddress ; /* register address to be read. */ i2cData->state = ACTIVATED ; fd = i2c_Open(i2cData->deviceName, O_RDWR); if (fd < 0) { i2cData->error = I2cOpenError ; i2cData->state = DEACTIVATED ; pthread_exit(NULL); } if (i2cData->order == START) printf("dev=%s slave_addr=%x reg_addr=%x count=%d\n", i2cData->deviceName, i2cData->deviceAddress, i2cData->registerAddress, count); while (i2cData->order == START) { vl_error = i2c_Read(fd, i2cData->deviceAddress, buf, count); if (vl_error < 0) { i2c_Close(fd); i2cData->error = I2cReadError ; i2cData->state = DEACTIVATED ; pthread_exit(NULL); } i2cData->error = NO_ERROR ; usleep(1000); } i2c_Close(fd); i2cData->error = NO_ERROR ; i2cData->state = DEACTIVATED ; pthread_exit(NULL); }