bool iotjs_i2c_open(iotjs_i2c_t* i2c) {
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_i2c_t, i2c)
  iotjs_i2c_platform_data_t* platform_data = _this->platform_data;
  IOTJS_ASSERT(platform_data);

  // Init i2c context
  platform_data->i2c_context = iotbus_i2c_init(platform_data->bus);
  if (!platform_data->i2c_context) {
    DLOG("%s: cannot open I2C", __func__);
    return false;
  }

  // Set i2c frequency
  int ret =
      iotbus_i2c_set_frequency(platform_data->i2c_context, IOTBUS_I2C_STD);
  if (ret < 0) {
    DLOG("%s: cannot set frequency", __func__);
    return false;
  }

  if (iotbus_i2c_set_address(platform_data->i2c_context, _this->address) < 0) {
    DLOG("%s: cannot set address", __func__);
    return false;
  }

  return true;
}
Exemple #2
0
/******************************
     I2C TEST
 ******************************/
int systemio_test_i2c(char *failstr)
{
	// i2c read write test
	int result = SYSIO_RESULT_FAIL;
	int fail_flag = 0;
	int ret = 0;
	iotbus_i2c_context_h hnd;
	int bus_num = 1;

	// changing addr and cmd depending on board
	uint8_t addr = 0x08;
	uint8_t cmd[2] = { 0x01, 0x02 };

	hnd = iotbus_i2c_init(bus_num);
	if (hnd == NULL) {
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_init() fail \n");
		REGISTER_FAIL_REASON("iotbus_i2c_init() fail", &fail_flag);
		goto done;
	} else {
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_init() success \n");
	}

	ret = iotbus_i2c_set_frequency(hnd, IOTBUS_I2C_STD);
	if (ret < 0) {
		REGISTER_FAIL_REASON("iotbus_i2c_set_frequency() fail", &fail_flag);
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_set_frequency() fail \n");
		//return ;
	} else {
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_set_frequency() success \n");
	}

	ret = iotbus_i2c_set_address(hnd, addr);
	if (ret < 0) {
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_set_address() fail \n");
		REGISTER_FAIL_REASON("iotbus_i2c_set_address() fail", &fail_flag);
		//return ;
	} else {
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_set_address() success \n");
	}

	ret = iotbus_i2c_write(hnd, cmd, 2);
	if (ret < 0) {
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_write() fail \n");
		//      REGISTER_FAIL_REASON("iotbus_i2c_write() fail", &fail_flag);
		//return ;
	} else {
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_write() success \n");
	}

	uint8_t buf[10];
	ret = iotbus_i2c_read(hnd, buf, 10);
	if (ret < 0) {
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_read() fail \n");
		//      REGISTER_FAIL_REASON("iotbus_i2c_read() fail", &fail_flag);
		//return ;
	} else {
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_read() success \n");
	}

	ret = iotbus_i2c_stop(hnd);
	if (ret < 0) {
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_stop() fail \n");
		REGISTER_FAIL_REASON("iotbus_i2c_stop() fail", &fail_flag);
		//return ;
	} else {
		SYSIO_DEBUG("[IOTAPI] iotbus_i2c_stop() success \n");
	}

done:
	/* check if succeed */
	if (fail_flag == 0) {
		result = SYSIO_RESULT_SUCCESS;
	}

	return result;

}