Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
uint8_t TwoWire::writeOneByte(uint8_t byte) {
    i2c_shift_out(port, byte);
    if (!i2c_get_ack(port)) return ENACKTRNS;
//Serial.begin(9600);
//Serial.println("44444444444444444444444444444");
    return SUCCESS;
}
Exemplo n.º 4
0
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;
}
Exemplo n.º 5
0
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;
}
Exemplo n.º 6
0
uint8 TwoWire::writeOneByte(uint8 byte) {
    i2c_shift_out(port, byte);
	if (!i2c_get_ack(port)) 
	{
	i2c_stop(port);
	return ENACKTRNS;
	}
    //if (!i2c_get_ack(port)) return ENACKTRNS;

    return SUCCESS;
}
Exemplo n.º 7
0
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!
}
Exemplo n.º 8
0
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;
}
Exemplo n.º 9
0
uint8_t TwoWire::endTransmission(void) {
    if (tx_buf_overflow) return EDATA;
   // Serial.begin(9600);
    i2c_start(port);

    i2c_shift_out(port, (tx_addr << 1) | I2C_WRITE);
    if (!i2c_get_ack(port)) return ENACKADDR;

    // shift out the address we're transmitting to
    for (uint8_t i = 0; i < tx_buf_idx; i++) {
        uint8_t ret = writeOneByte(tx_buf[i]); 
        if (ret) return ret;    // SUCCESS is 0
    } 
    i2c_stop(port);

    tx_buf_idx = 0;
    tx_buf_overflow = false;
    return SUCCESS;
}