2014-04-11 15:27:43 +01:00
|
|
|
/*
|
2014-07-14 17:49:31 +01:00
|
|
|
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
|
2014-04-22 15:51:28 +01:00
|
|
|
* Copyright (c) 2014 Intel Corporation.
|
2014-04-11 15:27:43 +01:00
|
|
|
*
|
2014-11-18 16:09:08 +00:00
|
|
|
* Code is modified from the RoadNarrows-robotics i2c library (distributed under
|
|
|
|
|
* the MIT license, license is included verbatim under src/i2c/LICENSE)
|
|
|
|
|
*
|
2014-04-11 15:27:43 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
* the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-04-27 21:17:54 +01:00
|
|
|
#include "i2c.h"
|
2014-06-24 17:24:54 +01:00
|
|
|
#include "mraa_internal.h"
|
2014-04-11 15:27:43 +01:00
|
|
|
|
2014-11-18 16:09:08 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/errno.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include "linux/i2c-dev.h"
|
2016-03-29 21:51:47 -10:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
2015-05-27 15:36:46 -07:00
|
|
|
|
2015-03-23 14:39:12 +00:00
|
|
|
typedef union i2c_smbus_data_union {
|
|
|
|
|
uint8_t byte; ///< data byte
|
2014-11-18 16:09:08 +00:00
|
|
|
unsigned short word; ///< data short word
|
|
|
|
|
uint8_t block[I2C_SMBUS_BLOCK_MAX + 2];
|
|
|
|
|
///< block[0] is used for length and one more for PEC
|
|
|
|
|
} i2c_smbus_data_t;
|
|
|
|
|
|
2015-03-23 14:39:12 +00:00
|
|
|
typedef struct i2c_smbus_ioctl_data_struct {
|
|
|
|
|
uint8_t read_write; ///< operation direction
|
|
|
|
|
uint8_t command; ///< ioctl command
|
|
|
|
|
int size; ///< data size
|
|
|
|
|
i2c_smbus_data_t* data; ///< data
|
2014-11-18 16:09:08 +00:00
|
|
|
} i2c_smbus_ioctl_data_t;
|
|
|
|
|
|
2015-05-27 15:36:46 -07:00
|
|
|
|
2015-07-27 09:38:06 -07:00
|
|
|
// static mraa_adv_func_t* func_table;
|
2015-05-27 15:36:46 -07:00
|
|
|
|
2014-11-18 16:09:08 +00:00
|
|
|
int
|
2015-03-23 14:39:12 +00:00
|
|
|
mraa_i2c_smbus_access(int fh, uint8_t read_write, uint8_t command, int size, i2c_smbus_data_t* data)
|
2014-11-18 16:09:08 +00:00
|
|
|
{
|
2015-03-23 14:39:12 +00:00
|
|
|
i2c_smbus_ioctl_data_t args;
|
2014-11-18 16:09:08 +00:00
|
|
|
|
2015-03-23 14:39:12 +00:00
|
|
|
args.read_write = read_write;
|
|
|
|
|
args.command = command;
|
|
|
|
|
args.size = size;
|
|
|
|
|
args.data = data;
|
2014-11-18 16:09:08 +00:00
|
|
|
|
2015-03-23 14:39:12 +00:00
|
|
|
return ioctl(fh, I2C_SMBUS, &args);
|
2014-11-18 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
2015-09-03 15:28:36 +01:00
|
|
|
static mraa_i2c_context
|
2015-07-27 09:38:06 -07:00
|
|
|
mraa_i2c_init_internal(mraa_adv_func_t* advance_func, unsigned int bus)
|
|
|
|
|
{
|
|
|
|
|
mraa_result_t status = MRAA_SUCCESS;
|
|
|
|
|
|
|
|
|
|
if (advance_func == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
mraa: Prefer calloc over malloc
Switch to using calloc on all calls to malloc where the memory isn't
initialized. For things like the mraa_board_t, not allocating all to zero
causes issues such as with the sub_platform member, where if that's not zero
mraa_get_platform_type will try to dereference a random memory location for the
sub_platform->platform_name, which can result in segmentation faults and other
issues.
Note that in some places where immediately after the malloc call is a copy
operation, there is no need for calloc, as all the memory gets overwritten
anyways, but in cases where there may or may not be memory written to (such as
in mraa_file_contains, with reading from a file), even though in most cases the
memory is overwritten, it could be the case that the read operation does
nothing, but the memory still has non-zero values, by virtue of the fact it
wasn't overwritten.
Signed-off-by: Ian Johnson <ijohnson@wolfram.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-01-17 09:39:45 -06:00
|
|
|
mraa_i2c_context dev = (mraa_i2c_context) calloc(1, sizeof(struct _i2c));
|
2015-07-27 09:38:06 -07:00
|
|
|
if (dev == NULL) {
|
2016-03-29 22:11:47 -10:00
|
|
|
syslog(LOG_CRIT, "i2c%i_init: Failed to allocate memory for context", bus);
|
2015-07-27 09:38:06 -07:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2015-09-03 15:28:36 +01:00
|
|
|
|
2015-07-27 09:38:06 -07:00
|
|
|
dev->advance_func = advance_func;
|
|
|
|
|
dev->busnum = bus;
|
|
|
|
|
|
|
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_init_pre)) {
|
|
|
|
|
status = advance_func->i2c_init_pre(bus);
|
|
|
|
|
if (status != MRAA_SUCCESS)
|
|
|
|
|
goto init_internal_cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_init_bus_replace)) {
|
|
|
|
|
status = dev->advance_func->i2c_init_bus_replace(dev);
|
|
|
|
|
if (status != MRAA_SUCCESS)
|
|
|
|
|
goto init_internal_cleanup;
|
|
|
|
|
} else {
|
|
|
|
|
char filepath[32];
|
|
|
|
|
snprintf(filepath, 32, "/dev/i2c-%u", bus);
|
|
|
|
|
if ((dev->fh = open(filepath, O_RDWR)) < 1) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i_init: Failed to open requested i2c port %s: %s", bus, filepath, strerror(errno));
|
|
|
|
|
status = MRAA_ERROR_INVALID_RESOURCE;
|
2015-07-27 09:38:06 -07:00
|
|
|
goto init_internal_cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ioctl(dev->fh, I2C_FUNCS, &dev->funcs) < 0) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_CRIT, "i2c%i_init: Failed to get I2C_FUNC map from device: %s", bus, strerror(errno));
|
2015-07-27 09:38:06 -07:00
|
|
|
dev->funcs = 0;
|
|
|
|
|
}
|
2015-09-03 15:28:36 +01:00
|
|
|
}
|
2015-07-27 09:38:06 -07:00
|
|
|
|
|
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_init_post)) {
|
|
|
|
|
status = dev->advance_func->i2c_init_post(dev);
|
|
|
|
|
if (status != MRAA_SUCCESS)
|
|
|
|
|
goto init_internal_cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
init_internal_cleanup:
|
|
|
|
|
if (status == MRAA_SUCCESS) {
|
|
|
|
|
return dev;
|
2015-12-01 11:48:36 +00:00
|
|
|
} else {
|
2015-07-27 09:38:06 -07:00
|
|
|
if (dev != NULL)
|
|
|
|
|
free(dev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_i2c_context
|
|
|
|
|
mraa_i2c_init(int bus)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2015-05-27 15:36:46 -07:00
|
|
|
mraa_board_t* board = plat;
|
|
|
|
|
if (board == NULL) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i_init: Platform Not Initialised", bus);
|
2014-11-03 15:44:40 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2015-05-27 15:36:46 -07:00
|
|
|
|
2015-08-10 17:16:00 -07:00
|
|
|
if (mraa_is_sub_platform_id(bus)) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_NOTICE, "i2c%i_init: Using sub platform", bus);
|
2015-05-27 15:36:46 -07:00
|
|
|
board = board->sub_platform;
|
|
|
|
|
if (board == NULL) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i_init: Sub platform Not Initialised", bus);
|
2015-05-27 15:36:46 -07:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2015-06-26 14:22:09 -07:00
|
|
|
bus = mraa_get_sub_platform_index(bus);
|
2015-05-27 15:36:46 -07:00
|
|
|
}
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_NOTICE, "i2c_init: Selected bus %d", bus);
|
2015-05-27 15:36:46 -07:00
|
|
|
|
|
|
|
|
if (board->i2c_bus_count == 0) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c_init: No i2c buses defined in platform");
|
2014-11-03 15:44:40 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2015-05-27 15:36:46 -07:00
|
|
|
if (bus >= board->i2c_bus_count) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c_init: i2c%i over i2c bus count", bus);
|
2014-11-03 15:44:40 +00:00
|
|
|
return NULL;
|
2014-05-02 16:07:18 +01:00
|
|
|
}
|
2014-11-03 15:44:40 +00:00
|
|
|
|
2015-05-27 15:36:46 -07:00
|
|
|
if (board->i2c_bus[bus].bus_id == -1) {
|
2016-04-26 10:59:26 +01:00
|
|
|
syslog(LOG_ERR, "Invalid i2c bus %i, moving to default i2c bus %i", bus, board->def_i2c_bus);
|
|
|
|
|
bus = board->def_i2c_bus;
|
2014-11-03 15:44:40 +00:00
|
|
|
}
|
2015-12-11 16:26:16 +00:00
|
|
|
if (!board->no_bus_mux) {
|
|
|
|
|
int pos = board->i2c_bus[bus].sda;
|
|
|
|
|
if (board->pins[pos].i2c.mux_total > 0) {
|
|
|
|
|
if (mraa_setup_mux_mapped(board->pins[pos].i2c) != MRAA_SUCCESS) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i_init: Failed to set-up i2c sda multiplexer", bus);
|
2015-12-11 16:26:16 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-11-03 15:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-11 16:26:16 +00:00
|
|
|
pos = board->i2c_bus[bus].scl;
|
|
|
|
|
if (board->pins[pos].i2c.mux_total > 0) {
|
|
|
|
|
if (mraa_setup_mux_mapped(board->pins[pos].i2c) != MRAA_SUCCESS) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i_init: Failed to set-up i2c scl multiplexer", bus);
|
2015-12-11 16:26:16 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-11-03 15:44:40 +00:00
|
|
|
}
|
2014-12-29 14:41:22 +00:00
|
|
|
}
|
2014-11-03 15:44:40 +00:00
|
|
|
|
2015-07-27 09:38:06 -07:00
|
|
|
return mraa_i2c_init_internal(board->adv_func, (unsigned int) board->i2c_bus[bus].bus_id);
|
2014-05-02 16:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-27 09:38:06 -07:00
|
|
|
|
2015-09-03 15:28:36 +01:00
|
|
|
mraa_i2c_context
|
|
|
|
|
mraa_i2c_init_raw(unsigned int bus)
|
2014-05-02 16:07:18 +01:00
|
|
|
{
|
2015-08-04 12:27:57 -07:00
|
|
|
return mraa_i2c_init_internal(plat == NULL ? NULL : plat->adv_func, bus);
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-27 09:38:06 -07:00
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_result_t
|
2014-12-10 00:32:17 +00:00
|
|
|
mraa_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2015-05-27 15:36:46 -07:00
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_set_frequency_replace)) {
|
2015-07-27 09:38:06 -07:00
|
|
|
return dev->advance_func->i2c_set_frequency_replace(dev, mode);
|
2014-12-10 00:27:04 +00:00
|
|
|
}
|
2014-10-20 10:01:53 +01:00
|
|
|
return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-11 10:47:13 +01:00
|
|
|
int
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2015-05-27 15:36:46 -07:00
|
|
|
int bytes_read = 0;
|
2015-12-01 11:48:36 +00:00
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_read_replace)) {
|
2015-07-27 09:38:06 -07:00
|
|
|
bytes_read = dev->advance_func->i2c_read_replace(dev, data, length);
|
2015-12-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2015-05-27 15:36:46 -07:00
|
|
|
bytes_read = read(dev->fh, data, length);
|
2015-12-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
if (bytes_read == length) {
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-29 21:51:47 -10:00
|
|
|
return -1;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-29 21:51:47 -10:00
|
|
|
int
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_i2c_read_byte(mraa_i2c_context dev)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2015-12-01 11:47:57 +00:00
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_read_byte_replace))
|
2015-10-08 13:19:08 -07:00
|
|
|
return dev->advance_func->i2c_read_byte_replace(dev);
|
2014-11-18 16:09:08 +00:00
|
|
|
i2c_smbus_data_t d;
|
2015-03-23 14:39:12 +00:00
|
|
|
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, I2C_NOCMD, I2C_SMBUS_BYTE, &d) < 0) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i: read_byte: Access error: %s", dev->busnum, strerror(errno));
|
|
|
|
|
return -1;
|
2014-11-18 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
return 0x0FF & d.byte;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-29 21:51:47 -10:00
|
|
|
int
|
2014-10-30 23:25:10 +00:00
|
|
|
mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
|
|
|
|
|
{
|
2015-08-04 12:27:57 -07:00
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_read_byte_data_replace))
|
|
|
|
|
return dev->advance_func->i2c_read_byte_data_replace(dev, command);
|
2014-11-18 16:09:08 +00:00
|
|
|
i2c_smbus_data_t d;
|
2015-03-23 14:39:12 +00:00
|
|
|
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command, I2C_SMBUS_BYTE_DATA, &d) < 0) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i: read_byte_data: Access error: %s", dev->busnum, strerror(errno));
|
|
|
|
|
return -1;
|
2014-11-18 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
return 0x0FF & d.byte;
|
2014-11-17 16:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-29 21:51:47 -10:00
|
|
|
int
|
2014-11-17 16:23:49 +00:00
|
|
|
mraa_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
|
|
|
|
|
{
|
2015-10-08 13:19:08 -07:00
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_read_word_data_replace))
|
|
|
|
|
return dev->advance_func->i2c_read_word_data_replace(dev, command);
|
2014-11-18 16:09:08 +00:00
|
|
|
i2c_smbus_data_t d;
|
2015-03-23 14:39:12 +00:00
|
|
|
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command, I2C_SMBUS_WORD_DATA, &d) < 0) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i: read_word_data: Access error: %s", dev->busnum, strerror(errno));
|
|
|
|
|
return -1;
|
2014-11-18 16:09:08 +00:00
|
|
|
}
|
2014-11-26 07:52:40 +00:00
|
|
|
return 0xFFFF & d.word;
|
2014-10-30 23:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-03 16:26:19 +00:00
|
|
|
int
|
|
|
|
|
mraa_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
|
|
|
|
|
{
|
2015-10-08 13:19:08 -07:00
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_read_bytes_data_replace))
|
|
|
|
|
return dev->advance_func->i2c_read_bytes_data_replace(dev, command, data, length);
|
2015-03-03 16:26:19 +00:00
|
|
|
struct i2c_rdwr_ioctl_data d;
|
|
|
|
|
struct i2c_msg m[2];
|
|
|
|
|
|
|
|
|
|
m[0].addr = dev->addr;
|
2015-03-13 16:01:59 +00:00
|
|
|
m[0].flags = 0x00;
|
2015-03-03 16:26:19 +00:00
|
|
|
m[0].len = 1;
|
2016-03-29 21:51:47 -10:00
|
|
|
m[0].buf = (char*) &command;
|
2015-03-03 16:26:19 +00:00
|
|
|
m[1].addr = dev->addr;
|
2015-03-13 16:01:59 +00:00
|
|
|
m[1].flags = I2C_M_RD;
|
2015-03-03 16:26:19 +00:00
|
|
|
m[1].len = length;
|
2016-03-29 21:51:47 -10:00
|
|
|
m[1].buf = (char*) data;
|
2015-03-03 16:26:19 +00:00
|
|
|
|
|
|
|
|
d.msgs = m;
|
|
|
|
|
d.nmsgs = 2;
|
|
|
|
|
|
2016-03-29 21:51:47 -10:00
|
|
|
int ret = ioctl(dev->fh, I2C_RDWR, &d);
|
|
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
syslog(LOG_ERR, "i2c%i: read_bytes_data: Access error: %s", dev->busnum, strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return length;
|
2015-03-03 16:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_result_t
|
|
|
|
|
mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2015-10-08 13:19:08 -07:00
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_write_replace))
|
|
|
|
|
return dev->advance_func->i2c_write_replace(dev, data, length);
|
2014-11-18 16:09:08 +00:00
|
|
|
i2c_smbus_data_t d;
|
|
|
|
|
int i;
|
|
|
|
|
uint8_t command = data[0];
|
|
|
|
|
|
|
|
|
|
data = &data[1];
|
2015-03-23 14:39:12 +00:00
|
|
|
length = length - 1;
|
2014-11-18 16:09:08 +00:00
|
|
|
if (length > I2C_SMBUS_I2C_BLOCK_MAX) {
|
|
|
|
|
length = I2C_SMBUS_I2C_BLOCK_MAX;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
2014-11-18 16:09:08 +00:00
|
|
|
|
2015-03-23 14:39:12 +00:00
|
|
|
for (i = 1; i <= length; i++) {
|
|
|
|
|
d.block[i] = data[i - 1];
|
2014-11-18 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
d.block[0] = length;
|
|
|
|
|
|
2016-03-28 04:16:39 +01:00
|
|
|
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, I2C_SMBUS_I2C_BLOCK_DATA, &d) < 0) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i: write: Access error: %s", dev->busnum, strerror(errno));
|
|
|
|
|
return MRAA_ERROR_UNSPECIFIED;
|
2016-03-28 04:16:39 +01:00
|
|
|
}
|
|
|
|
|
return MRAA_SUCCESS;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_result_t
|
|
|
|
|
mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2015-07-27 09:38:06 -07:00
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_write_byte_replace)) {
|
|
|
|
|
return dev->advance_func->i2c_write_byte_replace(dev, data);
|
|
|
|
|
} else {
|
2015-05-27 15:36:46 -07:00
|
|
|
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) < 0) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i: write_byte: Access error: %s", dev->busnum, strerror(errno));
|
|
|
|
|
return MRAA_ERROR_UNSPECIFIED;
|
2015-05-27 15:36:46 -07:00
|
|
|
}
|
|
|
|
|
return MRAA_SUCCESS;
|
2015-07-27 09:38:06 -07:00
|
|
|
}
|
2014-11-17 16:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mraa_result_t
|
2015-03-23 14:39:12 +00:00
|
|
|
mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
|
2014-11-17 16:23:49 +00:00
|
|
|
{
|
2015-08-04 12:27:57 -07:00
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_write_byte_data_replace))
|
|
|
|
|
return dev->advance_func->i2c_write_byte_data_replace(dev, data, command);
|
2014-11-18 16:09:08 +00:00
|
|
|
i2c_smbus_data_t d;
|
|
|
|
|
d.byte = data;
|
2015-03-23 14:39:12 +00:00
|
|
|
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, I2C_SMBUS_BYTE_DATA, &d) < 0) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i: write_byte_data: Access error: %s", dev->busnum, strerror(errno));
|
|
|
|
|
return MRAA_ERROR_UNSPECIFIED;
|
2014-11-17 16:23:49 +00:00
|
|
|
}
|
|
|
|
|
return MRAA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mraa_result_t
|
2015-03-23 14:39:12 +00:00
|
|
|
mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
|
2014-11-17 16:23:49 +00:00
|
|
|
{
|
2015-08-04 12:27:57 -07:00
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_write_word_data_replace))
|
|
|
|
|
return dev->advance_func->i2c_write_word_data_replace(dev, data, command);
|
2014-11-18 16:09:08 +00:00
|
|
|
i2c_smbus_data_t d;
|
|
|
|
|
d.word = data;
|
2015-03-23 14:39:12 +00:00
|
|
|
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, I2C_SMBUS_WORD_DATA, &d) < 0) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i: write_word_data: Access error: %s", dev->busnum, strerror(errno));
|
|
|
|
|
return MRAA_ERROR_UNSPECIFIED;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
2014-06-24 17:24:54 +01:00
|
|
|
return MRAA_SUCCESS;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_result_t
|
2014-10-31 15:10:15 +00:00
|
|
|
mraa_i2c_address(mraa_i2c_context dev, uint8_t addr)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2015-09-16 15:19:20 -07:00
|
|
|
if (dev == NULL) {
|
|
|
|
|
return MRAA_ERROR_INVALID_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 15:10:15 +00:00
|
|
|
dev->addr = (int) addr;
|
2015-07-27 09:38:06 -07:00
|
|
|
if (IS_FUNC_DEFINED(dev, i2c_address_replace)) {
|
|
|
|
|
return dev->advance_func->i2c_address_replace(dev, addr);
|
|
|
|
|
} else {
|
2015-05-27 15:36:46 -07:00
|
|
|
if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) {
|
2016-03-29 21:51:47 -10:00
|
|
|
syslog(LOG_ERR, "i2c%i: address: Failed to set slave address %d: %s", dev->busnum, addr, strerror(errno));
|
|
|
|
|
return MRAA_ERROR_UNSPECIFIED;
|
2015-05-27 15:36:46 -07:00
|
|
|
}
|
|
|
|
|
return MRAA_SUCCESS;
|
2015-07-27 09:38:06 -07:00
|
|
|
}
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-27 09:38:06 -07:00
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_result_t
|
|
|
|
|
mraa_i2c_stop(mraa_i2c_context dev)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2014-04-27 21:17:54 +01:00
|
|
|
free(dev);
|
2014-06-24 17:24:54 +01:00
|
|
|
return MRAA_SUCCESS;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
2015-07-27 09:38:06 -07:00
|
|
|
|