typedef struct _peripheral_i2c_s *peripheral_i2c_h;
-peripheral_i2c_h peripheral_i2c_init(int bus);
+int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c);
int peripheral_i2c_stop(peripheral_i2c_h i2c);
#define I2C_NAME "i2c"
int I2C_Addr = 0;
-peripheral_i2c_h peripheral_i2c_init(int bus)
+int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c)
{
- peripheral_i2c_h i2c;
+ peripheral_i2c_h handle;
int ret = PERIPHERAL_ERROR_NONE;
- assert(bus >= 0);
+ if (bus < 0)
+ return PERIPHERAL_ERROR_INVALID_PARAMETER;
/* Initialize peripheral_i2c_h */
- i2c = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s));
+ handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s));
- if (i2c == NULL) {
+ if (handle == NULL) {
_E("Failed to allocate peripheral_i2c_h");
- return NULL;
+ return PERIPHERAL_ERROR_OUT_OF_MEMORY;
}
- if (!get_dbus_connection())
- set_dbus_connection();
+ if (!get_dbus_connection()) {
+ ret = set_dbus_connection();
+ if (ret != PERIPHERAL_ERROR_NONE)
+ goto exit;
+ }
- ret = peripheral_dbus_i2c(i2c, I2C_NAME, "INIT", bus, 0, I2C_Addr);
+ ret = peripheral_dbus_i2c(handle, I2C_NAME, "INIT", bus, 0, I2C_Addr);
+exit:
if (ret != PERIPHERAL_ERROR_NONE) {
- free(i2c);
_E("[PERIPHERAL] I2C init error\n");
- i2c = NULL;
+ free(handle);
+ handle = NULL;
}
+ *i2c = handle;
- return i2c;
+ return ret;
}
int peripheral_i2c_stop(peripheral_i2c_h i2c)
int cnt = 0;
int bus_num;
unsigned char buf[10];
- peripheral_i2c_h dev;
+ peripheral_i2c_h i2c;
printf(">> I2C bus number : ");
if (scanf("%d", &bus_num) < 0)
return 0;
- if ((dev = peripheral_i2c_init(bus_num)) == NULL) {
+ if ((peripheral_i2c_init(bus_num, &i2c)) != 0) {
printf("Failed to initialize I2C device\n");
return 0;
}
- if (peripheral_i2c_set_address(dev, GY30_ADDR) != 0) {
+ if (peripheral_i2c_set_address(i2c, GY30_ADDR) != 0) {
printf("Failed to set address\n");
goto error;
}
buf[0] = GY30_CONT_HIGH_RES_MODE;
- if (peripheral_i2c_write(dev, buf, 1) != 0) {
+ if (peripheral_i2c_write(i2c, buf, 1) != 0) {
printf("Failed to write\n");
goto error;
}
while (cnt++ < 15) {
int result;
sleep(1);
- peripheral_i2c_read(dev, buf, 2);
+ peripheral_i2c_read(i2c, buf, 2);
result = GY30_READ_INTENSITY(buf);
printf("Result [%d]\n", result);
}
- peripheral_i2c_stop(dev);
+ peripheral_i2c_stop(i2c);
return 1;
error:
- peripheral_i2c_stop(dev);
+ peripheral_i2c_stop(i2c);
return 0;
}