示例#1
0
// read a number of bytes from the rtc over i2c
// returns true to indicate success
static bool ICACHE_FLASH_ATTR ds1307_recv(uint8 *data, uint8 len) {
	
	int loop;

	// signal i2c start
	i2c_master_start();

	// write address & direction
	i2c_master_writeByte((uint8)((DS1307_ADDR << 1) | 1));
	if (!i2c_master_checkAck()) {
		//uart0_send("i2c error3\r\n");
		i2c_master_stop();
		return false;
	}

	// read bytes
	for (loop = 0; loop < len; loop++) {
		data[loop] = i2c_master_readByte();
		// send ack (except after last byte, then we send nack)
		if (loop < (len - 1)) i2c_master_send_ack(); else i2c_master_send_nack();
	}

	// signal i2c stop
	i2c_master_stop();

	return true;

}
示例#2
0
LOCAL i2c_status ICACHE_FLASH_ATTR io2_acp_execute(uint8 address, uint8 command, uint16 *data) {
	uint8 d[2];
	
	i2c_master_start();

	/* Send address */
	i2c_master_writeByte(address << 1 | 0);
	if (i2c_master_getAck()) {
		i2c_master_stop();
		return I2C_ADDRESS_NACK;
	}

	/* Send command */
	i2c_master_writeByte(command);
	if (i2c_master_getAck()) {
		goto error;
	}
	
	i2c_master_stop();
	i2c_master_start();

	/* Send address */
	i2c_master_writeByte(address << 1 | 1);
	if (i2c_master_getAck()) {
		i2c_master_stop();
		return I2C_ADDRESS_NACK;
	}
	
	/* Read data */
	d[0] = i2c_master_readByte();
	i2c_master_send_ack();
	d[1] = i2c_master_readByte();
	i2c_master_send_nack();
	
	i2c_master_stop();
	
	*data = 256 * d[1] + d[0];
	return I2C_OK;
	
	error: i2c_master_stop();
	return I2C_DATA_NACK;
}
示例#3
0
bool rv3029_read(uint8_t regaddr, uint8_t bytes, uint8_t *val) {
    i2c_master_start();

	// Write I²C Address for writing register
    i2c_master_writeByte(RV3029_ADDR_W);
    if (i2c_master_getAck()) {
		os_printf("RV3029: no ACK for write addr\r\n");
		i2c_master_stop();
		return false;
	}

	// Write register address
    i2c_master_writeByte(regaddr);
    if(i2c_master_getAck()) {
		os_printf("RV3029: no ACK for register\r\n");
		i2c_master_stop();
		return false;
	}

	i2c_master_stop();
	i2c_master_start();

	// Write I²C Address for reading
    i2c_master_writeByte(RV3029_ADDR_R);
    if (i2c_master_getAck()) {
		os_printf("RV3029: no ACK for read addr\r\n");
		i2c_master_stop();
		return false;
	}

	uint8_t i;
	for (i = 0; i < bytes; ++i) {
		val[i] =  i2c_master_readByte();
		i2c_master_send_ack();
	}

	i2c_master_stop();

	return true;
}
static bool ICACHE_FLASH_ATTR byteReadNoReg(uint8_t adr, uint8_t data[], uint8_t length){
	uint8_t i;

	i2c_master_start();

	i2c_master_writeByte((uint8_t)((adr << 1) | 1));
	if (!i2c_master_checkAck()) {
		i2c_master_stop();
		return false;
	}

	for(i = 0; i < length-1; i++){
		data[i] = i2c_master_readByte();
		i2c_master_send_ack();	
	}

	data[i] = i2c_master_readByte();
	
	i2c_master_send_nack();
	i2c_master_stop();

	return true;
}
示例#5
0
文件: robko.c 项目: modSwap/ESP8266
LOCAL i2c_status ICACHE_FLASH_ATTR robko_read(uint8 address, uint8 reg, uint8 *data, uint8 len) {
	uint8 retry = 0;
	i2c_status status;
	do {
#if ROBKO_DEBUG
#if ROBKO_VERBOSE_OUTPUT
		debug("ROBKO: 0x%02X 0x%02X reading %d bytes...\n", address, reg, len);
#endif
#endif
		status = I2C_OK;
		i2c_master_start();
		
		/* Send address for write */
		i2c_master_writeByte(address << 1 | 0);
		if (i2c_master_getAck()) {
#if ROBKO_DEBUG
#if ROBKO_VERBOSE_OUTPUT
			debug("ROBKO: Address ACK failed [0x%02X] [set READ command] \n", address);
#endif
#endif
			status = I2C_ADDRESS_NACK;
			goto done;
		}
		
		/* Send register */
		i2c_master_writeByte(reg);
		if (i2c_master_getAck()) {
#if ROBKO_DEBUG
#if ROBKO_VERBOSE_OUTPUT
			debug("ROBKO: Register ACK failed [0x%02X] [set READ command] \n", reg);
#endif
#endif
			status = I2C_DATA_NACK;
			goto done;
		}
		
		i2c_master_stop();
		i2c_master_start();

		/* Send address for read */
		i2c_master_writeByte(address << 1 | 1);
		if (i2c_master_getAck()) {
#if ROBKO_DEBUG
#if ROBKO_VERBOSE_OUTPUT
			debug("ROBKO: Address ACK failed [0x%02X] [execute READ command] \n", address);
#endif
#endif
			status = I2C_ADDRESS_NACK;
			goto done;
		}
		
#if ROBKO_DEBUG
#if ROBKO_VERBOSE_OUTPUT
			debug("READ: ");
#endif
#endif
		/* Read data */
		uint8 i = 0;
		while (true) {
			data[i] = i2c_master_readByte();
#if ROBKO_DEBUG
#if ROBKO_VERBOSE_OUTPUT
			debug("0x%02X ", data[i]);
#endif
#endif
			i++;
			if (i < len) {
				i2c_master_send_ack();
			} else {
				i2c_master_send_nack();
				break;
			}
		}
		
done: 
		i2c_master_stop();
		retry++;
	} while (
		retry < ROBKO_I2C_RETRY && 
		status != I2C_OK
	);
	
	
#if ROBKO_DEBUG
#if ROBKO_VERBOSE_OUTPUT
	debug("\nROBKO: Done.\n\n");
#endif
#endif
	return status;
}