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); //Send the address i2c_shift_out(port, (address << 1) | I2C_READ); //Check the ack on the address if (!i2c_get_ack(port)) { i2c_stop(port); return ENACKADDR; } //Start reading data one byte at a time. while (rx_buf_len < (num_bytes-1)) { *(rx_buf + rx_buf_len) = i2c_shift_in(port); //Write_nac generates a propr ack response from the device (9th clock pulse) i2c_write_nack(port); rx_buf_len++; } *(rx_buf + rx_buf_len++) = i2c_shift_in(port); //STM32 bug rx_buf_len to rx_buf_len++ //get_nac checks for a propr ack response from the device (9th clock pulse) i2c_get_ack(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)) { 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; }
uint8 TwoWire::readOneByte(uint8 address, uint8 *byte) { //Shift in toggles the clock line 8 times leaving it low checking the //Date line on each clock cycle *byte = i2c_shift_in(port); //Write_nac generates a propr ack response from the device (9th clock pulse) i2c_write_nack(port); //i2c_stop(port); return SUCCESS; // no real way of knowing, but be optimistic! }
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; }