OK. I have the whole i2c-dev.c converted to Julia. I am having problems with how to define the ccall (again). I’ve been to the Calling C and Fortran documentation and there are no examples of how to pass a struct. Can you help again? I’m sorry to be so ignorant on this stuff.
Here’s the C Code:
#define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
#define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
union i2c_smbus_data {
__u8 byte;
__u16 word;
__u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
/* and one more for PEC */
};
/* This is the structure as used in the I2C_SMBUS ioctl call */
struct i2c_smbus_ioctl_data {
__u8 read_write;
__u8 command;
__u32 size;
union i2c_smbus_data *data;
};
static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
int size, union i2c_smbus_data *data)
{
struct i2c_smbus_ioctl_data args;
args.read_write = read_write;
args.command = command;
args.size = size;
args.data = data;
return ioctl(file,I2C_SMBUS,&args);
}
Here’s the Julia code. I figured i2c_smbus_data can just become a data::Vector{UInt8} and I’ll put/get the values appropriately (something like data[1] becomes byte, data[1] and data[2] become a word [with some bit shifting], and data[1:n] becomes block).
# This is the structure as used in the I2C_SMBUS ioctl call
mutable struct i2c_smbus_ioctl_data
read_write::UInt8
command::UInt8
size::UInt32
data::Vector{UInt8}
end
function i2c_smbus_access(file::Int32, read_write::UInt8, command::UInt8,
size::Int32, data::Vector{UInt8})
args = i2c_smbus_ioctl_data(read_write, command, size, data)
#args.read_write = read_write
#args.command = command
#args.size = size
#args.data = data
ret::Int32 = ccall((:ioctl, "libc"), Int32, (Int32, UInt16, i2c_smbus_ioctl_data), file, I2C_SMBUS, args)
return ret
end