int vfs_i2c_close(file_t *fp) { int ret = -1; /* return value */ i2c_dev_t *i2c_dev = NULL; /* device pointer */ /* check empty pointer. */ if ((fp != NULL) && (fp->node != NULL)) { /* close device if the device is last closed. */ if (fp->node->refs == 1) { /* get the device pointer. */ i2c_dev = (i2c_dev_t *)(fp->node->i_arg); /* lock the device. */ ret = aos_mutex_lock(&fp->node->mutex, AOS_WAIT_FOREVER); if (ret == 0) { /* turns off hardware. */ ret = hal_i2c_finalize(i2c_dev); } /* unlock the device. */ aos_mutex_unlock(&fp->node->mutex); } else { ret = VFS_SUCCESS; } } else { ret = -EINVAL; } return ret; }
static be_jse_symbol_t *i2c_close(void) { int8_t ret = -1; int8_t result = -1; item_handle_t i2c_handle; be_jse_symbol_t *arg0 = NULL; i2c_dev_t *i2c_device = NULL; be_jse_handle_function(0, &arg0, NULL, NULL, NULL); if (!arg0 || !symbol_is_int(arg0)) { goto out; } i2c_handle.handle = get_symbol_value_int(arg0); i2c_device = board_get_node_by_handle(MODULE_I2C, &i2c_handle); if (NULL == i2c_device) { be_error("i2c", "board_get_node_by_handle fail!\n"); goto out; } ret = hal_i2c_finalize(i2c_device); if (0 != ret) { be_error("i2c", "hal_i2c_finalize fail!\n"); goto out; } board_disattach_item(MODULE_I2C, &i2c_handle); result = 0; out: symbol_unlock(arg0); return new_int_symbol(result); }
result_t i2c_read(i2c_device_t* dev, void* buffer, uint16_t length) { uint16_t busId = dev->busId; uint16_t ret; if((ret = hal_i2c_set_slave_addr(busId, dev->devAddress)) != RV_OK) return ret; if((ret = hal_i2c_setup_interface(busId, dev->baudrate)) != RV_OK) return ret; if((ret = hal_i2c_read(busId, buffer, length)) != RV_OK); return ret; if((ret = hal_i2c_finalize(busId)) != RV_OK) return ret; return RV_OK; }