Пример #1
0
bool ICACHE_FLASH_ATTR write(uint8_t addr, uint8_t *buf, uint8_t length)
{
	i2c_master_start();
	i2c_master_writeByte(PCA9685_ADDR | I2C_WRITE);
	if (i2c_master_getAck())
	{
//		os_printf("addr not ack when tx write cmd \n");
		i2c_master_stop();
		return false;
	}

	i2c_master_writeByte(addr);
	if (i2c_master_getAck())
	{
//		os_printf("data not ack when tx write byte1 \n");
		i2c_master_stop();
		return false;
	}

	uint8_t i;
	for(i=0; i<length; i++)
	{
		i2c_master_writeByte(buf[i]);
		if (i2c_master_getAck())
		{
//			os_printf("data not ack when tx write byte %d\n", i);
			i2c_master_stop();
			return false;
		}
	}
	i2c_master_stop();
	return true;
}
Пример #2
0
bool rv3029_write(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;
	}

	uint8_t i;
	for (i = 0; i < bytes; ++i) {
		i2c_master_writeByte(val[i]);
	    if(i2c_master_getAck()) {
			os_printf("RV3029: no ACK for datawrite\r\n");
			i2c_master_stop();
			return false;
		}
	}

	i2c_master_stop();

	return true;
}
static bool ICACHE_FLASH_ATTR singleByteWrite(uint8_t adr, uint8_t reg, uint8_t data){
	i2c_master_start();

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

	i2c_master_writeByte(reg);
	if (!i2c_master_checkAck()) {
		i2c_master_stop();
		return false;
	}
	
	i2c_master_writeByte(data);
	if (!i2c_master_checkAck()) {
		i2c_master_stop();
		return false;
	}

	i2c_master_stop();

	return true;
}
Пример #4
0
// set the current data address, this is the start of the write command
// next either send the data to be written, or start a read instead
// returns true to indicate success
static bool ICACHE_FLASH_ATTR at24c_setAddr(uint16 addr) {

	uint8 loop;
	uint8 data[2];

	// signal i2c start
	i2c_master_start();

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

	// write data address
	data[0] = (uint8)(((unsigned)addr) >> 8);
	data[1] = (uint8)(((unsigned)addr) & 0xff);
	for (loop = 0; loop < 2; loop++) {
		i2c_master_writeByte(data[loop]);
		if (!i2c_master_checkAck()) {
			//uart0_send("i2c error\r\n");
			i2c_master_stop();
			return false;
		}
	}

	return true;
}
Пример #5
0
LOCAL void ICACHE_FLASH_ATTR
read_cb(void)
{
	uint8_t ack, low, high;
	wdt_feed();
	os_printf("Time=%ld\n", system_get_time());
	i2c_master_start();
	i2c_master_writeByte(BH1750_ADDR);
	ack = i2c_master_getAck();
	if (ack)
	{
		os_printf("I2C:No ack after sending address\n");
		i2c_master_stop();
		return;
	}
	i2c_master_stop();
    i2c_master_wait(40000); // why?

    i2c_master_start();
    i2c_master_writeByte(BH1750_ADDR + 1);
    ack = i2c_master_getAck();
	if (ack)
	{
		os_printf("I2C:No ack after write command\n");
		i2c_master_stop();
		return;
	}
	low = i2c_master_readByte();
	high = i2c_master_readByte();
	i2c_master_setAck(1);
    i2c_master_stop();
	os_printf("I2C:read(L/H)=(0x%02x/0x%02x)\n", low, high);
}
Пример #6
0
LOCAL i2c_status ICACHE_FLASH_ATTR io2_set_execute(uint8 address, uint8 command, uint8 data) {
	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;
	}

	/* Send data */
	i2c_master_writeByte(data);
	if (i2c_master_getAck()) {
		goto error;
	}
	
	i2c_master_stop();
	return I2C_OK;
	
	error: i2c_master_stop();
	return I2C_DATA_NACK;
}
Пример #7
0
bool i2c_master_writeBytes(uint8 address, uint8 *values, uint8 length)
{
	i2c_master_start();

	i2c_master_writeByte(address);
	if (!i2c_master_checkAck())
	{
		i2c_master_stop();
#ifdef CONFIG_I2C_MASTER_DEBUG
		console_printf( "Device not ACKed on address\n" );
#endif
		return false;
	}

	for(uint8 i = 0; i < length; i++){

		i2c_master_writeByte(values[i]);
		if (!i2c_master_checkAck())
		{
#ifdef CONFIG_I2C_MASTER_DEBUG
			console_printf( "Device not ACKed on write\n" );
#endif
			i2c_master_stop();
			return false;
		}
	}

	i2c_master_stop();
	return true;
}
Пример #8
0
// send a number of bytes to the rtc over i2c
// returns true to indicate success
static bool ICACHE_FLASH_ATTR ds1307_send(uint8 *data, uint8 len) {

	int loop;

	// signal i2c start
	i2c_master_start();

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

	// send the data
	for (loop = 0; loop < len; loop++) {
		i2c_master_writeByte(data[loop]);
		if (!i2c_master_checkAck()) {
			//uart0_send("i2c error2\r\n");
			i2c_master_stop();
			return false;
		}
	}

	// signal i2c stop
	i2c_master_stop();

	return true;

}
Пример #9
0
i2c_status ICACHE_FLASH_ATTR rgb_set(i2c_config *config) {
    rgb_config_data *config_data = (rgb_config_data *)config->data;
    i2c_master_start();

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

    /* Send command */
    i2c_master_writeByte(0x03);
    if (i2c_master_getAck())
        goto error;

    i2c_master_writeByte(config_data->red);
    if (i2c_master_getAck())
        goto error;

    i2c_master_writeByte(config_data->green);
    if (i2c_master_getAck())
        goto error;

    i2c_master_writeByte(config_data->blue);
    if (i2c_master_getAck())
        goto error;

    i2c_master_stop();
    return I2C_OK;

error:
    i2c_master_stop();
    return I2C_DATA_NACK;
}
Пример #10
0
/************ low level data pushing commands **********/
void LCD_expanderWrite(uint8 _data){                                        
    i2c_master_start();
    i2c_master_writeByte(LCD_ADDRESS << 1);
    if (!i2c_master_checkAck()) 
    {
        i2c_master_stop();
        return;
    }
    //i2c_master_writeByte((uint8)(_data) | _backlightval);
    i2c_master_writeByte((uint8)(_data) | LCD_BACKLIGHT);
    i2c_master_checkAck();
    i2c_master_stop();
}
Пример #11
0
void LSM303_write_byte(char device, char addr,char byte)
{
	i2c_master_start();
	i2c_master_writeByte(device | WRITE);
	i2c_master_readNAck();
	
	i2c_master_writeByte(addr);
	i2c_master_readNAck();
	
	i2c_master_writeByte(byte);
	i2c_master_readNAck();
	
	i2c_master_stop();
}
Пример #12
0
i2c_status ICACHE_FLASH_ATTR tc_read(i2c_config *config) {
	tc_config_data *config_data = (tc_config_data *)config->data;
	i2c_master_start();

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

	/* Send command */
	i2c_master_writeByte(0x21);
	if (i2c_master_getAck()) {
		i2c_master_stop();
		return I2C_DATA_NACK;	
	}

	i2c_master_stop();
	i2c_master_start();
	
	i2c_master_writeByte(config->address << 1 | 1);
	if (i2c_master_getAck()) {
		i2c_master_stop();
		return I2C_ADDRESS_NACK;
	}
	
	uint8 i;
	uint8 data[4];
	for (i=0; i < 4; i++) {
		data[i] = i2c_master_readByte();
		i2c_master_setAck(i == 3);
	}
	i2c_master_stop();
	
	if ((data[2] & 0x01) != 0) {
		return I2C_COMMUNICATION_FAILED;
	}
	
	sint16 d = data[3] * 256 + (data[2] & 0xFC);
	float tf = 0.0625 * d;
	int ti = tf;
	uint16 td = (tf - ti) * 100;
	
	config_data->temperature = d / 4;
	os_sprintf(config_data->temperature_str, "%d.%02d", ti, td);
	
	return I2C_OK;
}
Пример #13
0
// wait for a write operation to complete
// by 'acknowledge polling'
void ICACHE_FLASH_ATTR at24c_writeWait() {
	do {
		i2c_master_start();
		i2c_master_writeByte((uint8)((AT24C_ADDR << 1) | 1));
	} while (!i2c_master_checkAck());
	i2c_master_stop();
}
Пример #14
0
// write within a page
// note if you try to write past a page boundary the write will
// wrap back to the start of the same page, so you need to know
// how much you're writing and where you're writing it to
// you don't need to start writing at the start of a page, but if you
// start in the middle you'll be able to write less before wrapping
// optionally wait for the eeprom to complete the write
// returns true to indicate success
bool ICACHE_FLASH_ATTR at24c_writeInPage(uint16 addr, uint8* data, uint8 len, bool wait) {

	int loop;

	// set data address (includes i2c setup,
	// so no need to call i2c_master_start here)
	if (!at24c_setAddr(addr)) return false;

	// send the data
	for (loop = 0; loop < len; loop++) {
		i2c_master_writeByte(data[loop]);
		if (!i2c_master_checkAck()) {
			//uart0_send("i2c error\r\n");
			i2c_master_stop();
			return false;
		}
	}

	// signal i2c stop
	i2c_master_stop();

	// optionally, wait until the eeprom signals the write is finished
	if (wait) at24c_writeWait();

	return true;
}
Пример #15
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;

}
static bool ICACHE_FLASH_ATTR commandOnlyWrite(uint8_t adr, uint8_t reg){
	i2c_master_start();

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

	i2c_master_writeByte(reg);
	if (!i2c_master_checkAck()) {
		i2c_master_stop();
		return false;
	}

	i2c_master_stop();

	return true;
}
Пример #17
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;
}
Пример #18
0
LOCAL i2c_status ICACHE_FLASH_ATTR rgb_on(uint8 address) {
    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(0x01);
    if (i2c_master_getAck()) {
        i2c_master_stop();
        return I2C_DATA_NACK;
    }

    i2c_master_stop();
    return I2C_OK;
}
Пример #19
0
int platform_i2c_send_address( unsigned id, uint16_t address, int direction ){
  // Convert enum codes to R/w bit value.
  // If TX == 0 and RX == 1, this test will be removed by the compiler
  if ( ! ( PLATFORM_I2C_DIRECTION_TRANSMITTER == 0 &&
           PLATFORM_I2C_DIRECTION_RECEIVER == 1 ) ) {
    direction = ( direction == PLATFORM_I2C_DIRECTION_TRANSMITTER ) ? 0 : 1;
  }

  i2c_master_writeByte( (uint8_t) ((address << 1) | direction ));
  // Low-level returns nack (0=acked); we return ack (1=acked).
  return ! i2c_master_getAck();
}
Пример #20
0
/******************************************************************************
 * FunctionName : user_mvh3004_burst_read
 * Description  : burst read mvh3004's internal data
 * Parameters   : uint8 addr - mvh3004's address
 *                uint8 *pData - data point to put read data
 *                uint16 len - read length
 * Returns      : bool - true or false
*******************************************************************************/
LOCAL bool ICACHE_FLASH_ATTR
user_mvh3004_burst_read(uint8 addr, uint8 *pData, uint16 len)
{
    uint8 ack;
    uint16 i;

    i2c_master_start();
    i2c_master_writeByte(addr);
    ack = i2c_master_getAck();

    if (ack) {
        os_printf("addr not ack when tx write cmd \n");
        i2c_master_stop();
        return false;
    }

    i2c_master_stop();
    i2c_master_wait(40000);

    i2c_master_start();
    i2c_master_writeByte(addr + 1);
    ack = i2c_master_getAck();

    if (ack) {
        os_printf("addr not ack when tx write cmd \n");
        i2c_master_stop();
        return false;
    }

    for (i = 0; i < len; i++) {
        pData[i] = i2c_master_readByte();

        i2c_master_setAck((i == (len - 1)) ? 1 : 0);
    }

    i2c_master_stop();

    return true;
}
Пример #21
0
bool ICACHE_FLASH_ATTR read(uint8_t addr, uint8_t *buf, uint8_t length)
{
	i2c_master_start();
	i2c_master_writeByte(PCA9685_ADDR | I2C_WRITE);
	if (i2c_master_getAck())
	{
//		os_printf("addr not ack when tx write cmd \n");
		i2c_master_stop();
		return false;
	}

	i2c_master_writeByte(addr);
	if (i2c_master_getAck())
	{
//		os_printf("data not ack when tx write byte1 \n");
		i2c_master_stop();
		return false;
	}

	i2c_master_start();
	i2c_master_writeByte(PCA9685_ADDR | I2C_READ);
	if (i2c_master_getAck())
	{
//		os_printf("addr not ack when tx read cmd \n");
		i2c_master_stop();
		return false;
	}

	uint8_t i = 0;
	for(i=0; i<length; i++)
	{
		buf[i] = i2c_master_readByte();
		i2c_master_setAck(0);//(i == (length - 1)) ? 1 : 0);
	}

	i2c_master_stop();
	return true;
}
Пример #22
0
void LSM303_read_bytes(char device, char addr, char bytes[], char count)
{
	i2c_master_start();
	i2c_master_writeByte(device | WRITE); 
	i2c_master_readNAck();
	i2c_master_writeByte(addr);
	i2c_master_readNAck();
	
	i2c_master_start();
	i2c_master_writeByte(device | READ); 
	i2c_master_readNAck();
	
	int i;
	for (i = 0; i < count-1; ++i)
	{
		bytes[i] = i2c_master_readByte();
		i2c_master_writeAck();
	}
	bytes[count-1] = i2c_master_readByte();
	i2c_master_writeNack();
	
	i2c_master_stop();
}
static bool ICACHE_FLASH_ATTR byteRead(uint8_t adr, uint8_t reg, uint8_t data[], uint8_t length){
	uint8_t i;

	i2c_master_start();

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

	i2c_master_writeByte(reg);
	if (!i2c_master_checkAck()) {
		i2c_master_stop();
		return false;
	}

	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;
}
Пример #24
0
bool i2c_master_readBytes(uint8 address, uint8 *values, uint8 length)
{
	if(values[0] > 0){
		if(!i2c_master_writeBytes(address, values, 1)){
			return false;
		}
	}

	uint8 timeout = 100;
	do{
		i2c_master_start();
		i2c_master_writeByte(address+1);
		if(!i2c_master_checkAck()){
			i2c_master_stop();
			i2c_master_wait(1000);
			continue;
		}
		break;
	}while(--timeout>0);

	if(timeout == 0){
		return false;
	}

#ifdef CONFIG_I2C_MASTER_DEBUG
	console_printf("Read: ");
#endif
	uint8 readed = 0;
	while((readed < length) && (--timeout>0)){
		uint8 byte = i2c_master_readByte();
		values[readed++] = byte;
		i2c_master_setAck((readed == length));	// send the ACK or NAK as applicable
		i2c_master_setDC(1, 0); // release SDA
#ifdef CONFIG_I2C_MASTER_DEBUG
		console_printf("%d ", byte);
#endif
	}
#ifdef CONFIG_I2C_MASTER_DEBUG
	console_printf("\n");
#endif
	i2c_master_stop();
	return true;
}
Пример #25
0
/* Wrote a simple I2C bus scanner to find connected i2C devices */
void i2c_bus_scan()
{
    int index = 0;
    uint8_t result;

    os_printf("Scanning I2C bus for devices\r\n");
    for (index=1; index <=127; index ++)
    {
        i2c_master_start();                     // Start I2C request

        i2c_master_writeByte(index<<1| 0b00000001);     //DS1307 address + W
        result = i2c_master_checkAck();

        if (result)
        {
            os_printf("\tI2C device present at 0x%x\r\n", index);
        }

        i2c_master_stop();
    }

}
Пример #26
0
bool ICACHE_FLASH_ATTR
mcp3221_read(uint8 addr, uint16 *pData)
{
  uint8 ack;

  i2c_master_start();
  i2c_master_writeByte(addr);

  // reading ack, should be 0
  ack = i2c_master_getAck();
  if (ack != 0) {
    DEBUG("addr not ack when tx write cmd, line %d\n", __LINE__);
    i2c_master_stop();
    return false;
  }

  // reading high byte, first 4 bit should be zero
  uint8 temp = i2c_master_readByte();
  if ((temp | 0x0f) != 0x0f) {
    DEBUG("wrong reading\n");
    return false;
  }
  *pData = temp*256;

  // return ack 0 for high byte
  i2c_master_setAck(0);

  // reading low byte
  temp = i2c_master_readByte();
  *pData += temp;

  // return ack 1 for lower byte
  i2c_master_setAck(1);

  // stop, we dont need continuous reading
  i2c_master_stop();

  return true;
}
Пример #27
0
int platform_i2c_send_byte( unsigned id, uint8_t data ){
  i2c_master_writeByte(data);
  // Low-level returns nack (0=acked); we return ack (1=acked).
  return ! i2c_master_getAck();
}
Пример #28
0
uint8 LCD_init(){

    i2c_master_start();
    i2c_master_writeByte(LCD_ADDRESS << 1);
    if (!i2c_master_checkAck()) {
        i2c_master_stop();
        return 0;
    }
    i2c_master_stop();

    _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;

    if (LCD_ROW > 1) {
        _displayfunction |= LCD_2LINE;
    }
    _numlines = LCD_ROW;

    // for some 1 line displays you can select a 10 pixel high font
    if ((LCD_DOTSIZE != 0) && (LCD_ROW == 1)) {
        _displayfunction |= LCD_5x10DOTS;
    }

    // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
    // according to datasheet, we need at least 40ms after power rises above 2.7V
    // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
    os_delay_us(50000); 
  
    // Now we pull both RS and R/W low to begin commands
    LCD_expanderWrite(0);   // reset expanderand turn backlight off
    os_delay_us(1000000);

    //put the LCD into 4 bit mode
    // this is according to the hitachi HD44780 datasheet
    // figure 24, pg 46
    
    // we start in 8bit mode, try to set 4 bit mode
    LCD_write4bits(0x30);
    os_delay_us(4500); // wait min 4.1ms
    
    // second try
    LCD_write4bits(0x30);
    os_delay_us(4500); // wait min 4.1ms
    
    // third go!
    LCD_write4bits(0x30); 
    os_delay_us(150);
    
    // finally, set to 4-bit interface
    LCD_write4bits(0x20); 


    // set # lines, font size, etc.
    LCD_command(LCD_FUNCTIONSET | _displayfunction);  
    
    // turn the display on with no cursor or blinking default
    _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
    LCD_display();
    
    // clear it off
    LCD_clear();
    
    // Initialize to default text direction (for roman languages)
    _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
    
    // set the entry mode
    LCD_command(LCD_ENTRYMODESET | _displaymode);
    
    LCD_home();

    return 1;
}
Пример #29
0
LOCAL i2c_status ICACHE_FLASH_ATTR robko_set(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 set %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 SET 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 SET command] \n", reg);
#endif
#endif
			status = I2C_DATA_NACK;
			goto done;
		}
		
		/* Write data */
		uint8 i = 0;
		while (i < len) {
			i2c_master_writeByte(data[i]);
			if (i2c_master_getAck()) {
#if ROBKO_DEBUG
#if ROBKO_VERBOSE_OUTPUT
				debug("ROBKO: Data ACK failed [execute SET command] \n");
#endif
#endif
				status = I2C_DATA_NACK;
				break;
			}
#if ROBKO_DEBUG
#if ROBKO_VERBOSE_OUTPUT
			debug("0x%02X ", data[i]);
#endif
#endif
			i++;
		}
		
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;
}
Пример #30
0
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;
}