Example #1
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);
}
Example #2
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;
}
Example #3
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;
}
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;
}
Example #5
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;
}
Example #6
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;
}
Example #7
0
/******************************************************************************
* FunctionName : i2c_master_send_nack
* Description  : response nack
* Parameters   : NONE
* Returns      : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
i2c_master_send_nack(void)
{
    i2c_master_setAck(0x1);
}
Example #8
0
int platform_i2c_recv_byte( unsigned id, int ack ){
  uint8_t r = i2c_master_readByte();
  i2c_master_setAck( !ack );
  return r;
}
/******************************************************************************
* FunctionName : i2c_master_send_nack
* Description  : response nack
* Parameters   : NONE
* Returns	  : NONE
*******************************************************************************/
void i2c_master_send_nack(void)
{
	i2c_master_setAck(0x1);
}
Example #10
0
/******************************************************************************
* FunctionName : i2c_master_send_ack
* Description  : response ack
* Parameters   : NONE
* Returns	  : NONE
*******************************************************************************/
void i2c_master_send_ack(void)
{
	i2c_master_setAck(0x0);
}