uint8 TwoWire::requestFrom(uint8 address, int num_bytes) { if (num_bytes > WIRE_BUFSIZ) num_bytes = WIRE_BUFSIZ; rx_buf_idx = 0; rx_buf_len = 0; i2c_start(port); i2c_shift_out(port, (address << 1) | I2C_READ); if (!i2c_get_ack(port)) { return 0; } while (rx_buf_len < num_bytes) { rx_buf[rx_buf_len++] = i2c_shift_in(port); if(rx_buf_len < num_bytes) { i2c_send_ack(port); } } i2c_send_nack(port); i2c_stop(port); return rx_buf_len; }
uint8 TwoWire::requestFrom(uint8 address, int num_bytes) { if (num_bytes > WIRE_BUFSIZ) num_bytes = WIRE_BUFSIZ; rx_buf_idx = 0; rx_buf_len = 0; i2c_start(port); i2c_shift_out(port, (address << 1) | I2C_READ); if (!i2c_get_ack(port)) { //Serial1.print("requestFrom failed at byte "); //Serial1.print(rx_buf_len,10); //Serial1.println(); return ENACKADDR; } while (rx_buf_len < num_bytes) { rx_buf[rx_buf_len++] = i2c_shift_in(port); if(rx_buf_len < num_bytes) { i2c_send_ack(port); } } i2c_send_nack(port); i2c_stop(port); return rx_buf_len; }
static int i2c_do_read(i2c_t *obj, char * data, int last) { if (last) i2c_send_nack(obj); else i2c_send_ack(obj); *data = (obj->i2c->D & 0xFF); // start rx transfer and wait the end of the transfer return i2c_wait_end_rx_transfer(obj); }
uint8 TwoWire::readOneByte(uint8 address, uint8 *byte) { i2c_start(port); i2c_shift_out(port, (address << 1) | I2C_READ); if (!i2c_get_ack(port)) return ENACKADDR; *byte = i2c_shift_in(port); i2c_send_nack(port); i2c_stop(port); return SUCCESS; // no real way of knowing, but be optimistic! }
uint8 TwoWire::process() { itc_msg.xferred = 0; uint8 sla_addr = (itc_msg.addr << 1); if (itc_msg.flags == I2C_MSG_READ) { sla_addr |= I2C_READ; } i2c_start(); // shift out the address we're transmitting to i2c_shift_out(sla_addr); if (!i2c_get_ack()) { i2c_stop();// Roger Clark. 20141110 added to set clock high again, as it will be left in a low state otherwise return ENACKADDR; } // Recieving if (itc_msg.flags == I2C_MSG_READ) { while (itc_msg.xferred < itc_msg.length) { itc_msg.data[itc_msg.xferred++] = i2c_shift_in(); if (itc_msg.xferred < itc_msg.length) { i2c_send_ack(); } else { i2c_send_nack(); } } } // Sending else { for (uint8 i = 0; i < itc_msg.length; i++) { i2c_shift_out(itc_msg.data[i]); if (!i2c_get_ack()) { i2c_stop();// Roger Clark. 20141110 added to set clock high again, as it will be left in a low state otherwise return ENACKTRNS; } itc_msg.xferred++; } } i2c_stop(); return SUCCESS; }
uint8 TwoWire::process() { itc_msg.xferred = 0; uint8 sla_addr = (itc_msg.addr << 1); if (itc_msg.flags == I2C_MSG_READ) { sla_addr |= I2C_READ; } i2c_start(); // shift out the address we're transmitting to i2c_shift_out(sla_addr); if (!i2c_get_ack()) { return ENACKADDR; } // Recieving if (itc_msg.flags == I2C_MSG_READ) { while (itc_msg.xferred < itc_msg.length) { itc_msg.data[itc_msg.xferred++] = i2c_shift_in(); if (itc_msg.xferred < itc_msg.length) { i2c_send_ack(); } else { i2c_send_nack(); } } } // Sending else { for (uint8 i = 0; i < itc_msg.length; i++) { i2c_shift_out(itc_msg.data[i]); if (!i2c_get_ack()) { return ENACKTRNS; } itc_msg.xferred++; } } i2c_stop(); return SUCCESS; }
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { unsigned int loop, rxdata; int sadr, ack, bytes_read; rxdata = 0; switch ((int)obj->i2c) { case I2C_0: sadr = TSC_I2C_ADDR; break; case I2C_1: sadr = AAIC_I2C_ADDR; break; case I2C_2: case I2C_3: sadr = address; //LM75_I2C_ADDR; or MMA7660_I2C_ADDR; break; } bytes_read = 0; // Start bit i2c_start(obj); switch ((int)obj->i2c) { case I2C_0: // Set serial and register address i2c_send_byte(obj, sadr); ack += i2c_receive_ack(obj); i2c_send_byte(obj, address); ack += i2c_receive_ack(obj); // Stop bit i2c_stop(obj); // Start bit i2c_start_tsc(obj); // Read from I2C address i2c_send_byte(obj, sadr | 1); ack += i2c_receive_ack(obj); rxdata = (i2c_receive_byte(obj) & 0xFF); data[((length - 1) - bytes_read)] = (char)rxdata; bytes_read++; // Read multiple bytes if ((length > 1) && (length < 5)) { for (loop = 1; loop <= (length - 1); loop++) { // Send ACK i2c_send_ack(obj); // Next byte //rxdata = ((rxdata << 8) & 0xFFFFFF00); //rxdata |= (i2c_receive_byte(obj) & 0xFF); rxdata = i2c_receive_byte(obj); data[(length - 1) - bytes_read] = (char)rxdata; bytes_read++; } } break; case I2C_1: // Set serial and register address i2c_send_byte(obj, sadr); ack += i2c_receive_ack(obj); i2c_send_byte(obj, address); ack += i2c_receive_ack(obj); // Stop bit i2c_stop(obj); // Start bit i2c_start_tsc(obj); // Fall through to read data case I2C_2: case I2C_3: // Read from preset register address pointer i2c_send_byte(obj, sadr | 1); ack += i2c_receive_ack(obj); rxdata = i2c_receive_byte(obj); data[bytes_read] = (char)rxdata; bytes_read++; // Read multiple bytes if ((length > 1) && (length < 5)) { for (loop = 1; loop <= (length - 1); loop++) { // Send ACK i2c_send_ack(obj); // Next byte rxdata = i2c_receive_byte(obj); data[loop] = (char)rxdata; bytes_read++; } } break; } i2c_send_nack(obj); i2c_stop(obj); // Actual stop bit return bytes_read; }