1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
///////////////////////////////////////////////////////////////////////////////////////
// File I2C.xs
// Description: XS module for HiPi::Device::I2C
// Copyright: Copyright (c) 2013-2017 Mark Dootson
// License: This is free software; you can redistribute it and/or modify it under
// the same terms as the Perl 5 programming language system itself.
///////////////////////////////////////////////////////////////////////////////////////
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "mylib/include/ppport.h"
#include <sys/ioctl.h>
#include <i2c/smbus.h>
#include <linux/swab.h>
#define SCAN_MODE_AUTO 0
#define SCAN_MODE_QUICK 1
#define SCAN_MODE_READ 2
/* this is for i2c-dev.c */
#define I2C_SLAVE 0x0703 /* Change slave address */
/* Attn.: Slave address is 7 or 10 bits */
#define I2C_SLAVE_FORCE 0x0706 /* Change slave address */
/* Attn.: Slave address is 7 or 10 bits */
/* This changes the address, even if it */
/* is already taken! */
#define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */
#define I2C_FUNCS 0x0705 /* Get the adapter functionality */
#define I2C_RDWR 0x0707 /* Combined R/W transfer (one stop only)*/
#define I2C_PEC 0x0708 /* != 0 for SMBus PEC */
#define I2C_SMBUS 0x0720 /* SMBus-level access */
/* This is the structure as used in the I2C_RDWR ioctl call */
struct i2c_rdwr_ioctl_data {
struct i2c_msg *msgs; /* pointers to i2c_msgs */
int nmsgs; /* number of i2c_msgs */
};
MODULE = HiPi::Device::I2C PACKAGE = HiPi::Device::I2C
__s32
i2c_smbus_write_quick(file, value)
int file
__u8 value
CODE:
RETVAL = i2c_smbus_write_quick(file, value);
OUTPUT: RETVAL
__s32
i2c_smbus_read_byte( file )
int file
CODE:
RETVAL = i2c_smbus_read_byte( file );
OUTPUT: RETVAL
__s32
i2c_smbus_write_byte(file, value)
int file
__u8 value
CODE:
RETVAL = i2c_smbus_write_byte( file, value );
OUTPUT: RETVAL
__s32
i2c_smbus_read_byte_data(file, command )
int file
__u8 command
CODE:
RETVAL = i2c_smbus_read_byte_data(file, command );
OUTPUT: RETVAL
__s32
i2c_smbus_write_byte_data( file, command, value)
int file
__u8 command
__u8 value
CODE:
RETVAL = i2c_smbus_write_byte_data(file, command, value );
OUTPUT: RETVAL
__s32
i2c_smbus_read_word_data(file, command)
int file
__u8 command
CODE:
RETVAL = i2c_smbus_read_word_data(file, command );
OUTPUT: RETVAL
__s32
i2c_smbus_write_word_data( file, command, value)
int file
__u8 command
__u16 value
CODE:
RETVAL = i2c_smbus_write_word_data(file, command, value );
OUTPUT: RETVAL
__s32
i2c_smbus_read_word_swapped(file, command)
int file
__u8 command
CODE:
__s32 rval = i2c_smbus_read_word_data(file, command );
RETVAL = (rval < 0) ? rval : __swab16(rval);
OUTPUT: RETVAL
__s32
i2c_smbus_write_word_swapped( file, command, value)
int file
__u8 command
__u16 value
CODE:
RETVAL = i2c_smbus_write_word_data(file, command, __swab16(value) );
OUTPUT: RETVAL
__s32
i2c_smbus_process_call( file, command, value )
int file
__u8 command
__u16 value
CODE:
RETVAL = i2c_smbus_process_call(file, command, value );
OUTPUT: RETVAL
void
i2c_smbus_read_block_data( file, command )
int file
__u8 command
PPCODE:
int i;
__u8 buf[32];
int result = i2c_smbus_read_block_data(file, command, buf);
if (result < 0) {
EXTEND( SP, 1 );
PUSHs( &PL_sv_undef );
} else {
EXTEND( SP, (IV)result );
for( i = 0; i < result; ++i )
{
SV* var = sv_newmortal();
sv_setuv( var, (UV)buf[i] );
PUSHs( var );
}
}
void
i2c_smbus_read_i2c_block_data( file, command, len )
int file
__u8 command
__u8 len
PPCODE:
int i;
__u8 *buffer;
buffer = malloc(len * sizeof(__u8));
int result = i2c_smbus_read_i2c_block_data(file, command, len, buffer);
if (result < 0) {
EXTEND( SP, 1 );
PUSHs( &PL_sv_undef );
} else {
EXTEND( SP, (IV)result );
for( i = 0; i < result; ++i )
{
SV* var = sv_newmortal();
sv_setuv( var, (UV)buffer[i] );
PUSHs( var );
}
}
free( buffer );
__s32
i2c_smbus_write_block_data( file, command, dataarray )
int file
__u8 command
SV* dataarray
CODE:
STRLEN len;
AV* av;
__u8 *buffer;
int i;
if( !SvROK( dataarray ) || ( SvTYPE( (SV*) ( av = (AV*) SvRV( dataarray ) ) ) != SVt_PVAV ) )
{
croak( "the data array is not an array reference" );
return;
}
len = av_len( av ) + 1;
buffer = malloc(len * sizeof(__u8));
for( i = 0; i < (int)len; ++i )
buffer[i] = (__u8)SvUV(*av_fetch( av, i, 0 ));
RETVAL = i2c_smbus_write_block_data(file, command, len, buffer);
free( buffer);
OUTPUT: RETVAL
__s32
i2c_smbus_write_i2c_block_data( file, command, dataarray )
int file
__u8 command
SV* dataarray
CODE:
STRLEN len;
AV* av;
__u8 *buffer;
int i;
if( !SvROK( dataarray ) || ( SvTYPE( (SV*) ( av = (AV*) SvRV( dataarray ) ) ) != SVt_PVAV ) )
{
croak( "the data array is not an array reference" );
return;
}
len = av_len( av ) + 1;
buffer = malloc(len * sizeof(__u8));
for( i = 0; i < (int)len; ++i )
buffer[i] = (__u8)SvUV(*av_fetch( av, i, 0 ));
RETVAL = i2c_smbus_write_i2c_block_data(file, command, len, buffer);
free( buffer );
OUTPUT: RETVAL
int
_i2c_write( int file, __u16 address, unsigned char *wbuf, __u16 wlen )
CODE:
int ret;
struct i2c_rdwr_ioctl_data i2c_data;
struct i2c_msg msg[1];
i2c_data.msgs = msg;
i2c_data.nmsgs = 1; // use one message structure
i2c_data.msgs[0].addr = address;
i2c_data.msgs[0].flags = 0; // don't need flags
i2c_data.msgs[0].len = wlen;
i2c_data.msgs[0].buf = (__u8 *)wbuf;
ret = ioctl(file, I2C_RDWR, &i2c_data);
if (ret < 0) {
RETVAL = ret;
} else {
RETVAL = 0;
}
OUTPUT: RETVAL
int
_i2c_read( int file, __u16 address, unsigned char *rbuf, __u16 rlen)
CODE:
int ret;
struct i2c_rdwr_ioctl_data i2c_data;
struct i2c_msg msg[1];
i2c_data.msgs = msg;
i2c_data.nmsgs = 1;
i2c_data.msgs[0].addr = address;
i2c_data.msgs[0].flags = I2C_M_RD;
i2c_data.msgs[0].len = rlen;
i2c_data.msgs[0].buf = (__u8 *)rbuf;
ret = ioctl(file, I2C_RDWR, &i2c_data);
if (ret < 0) {
RETVAL = ret;
} else {
RETVAL = 0;
}
OUTPUT: RETVAL
int
_i2c_read_register( int file, __u16 address, unsigned char *wbuf, unsigned char *rbuf, __u16 rlen)
CODE:
int ret;
struct i2c_rdwr_ioctl_data i2c_data;
struct i2c_msg msg[2];
i2c_data.msgs = msg;
i2c_data.nmsgs = 2;
i2c_data.msgs[0].addr = address;
i2c_data.msgs[0].flags = 0;
i2c_data.msgs[0].len = 1;
i2c_data.msgs[0].buf = (__u8 *)wbuf;
i2c_data.msgs[1].addr = address;
i2c_data.msgs[1].flags = I2C_M_RD;
i2c_data.msgs[1].len = rlen;
i2c_data.msgs[1].buf = (__u8 *)rbuf;
ret = ioctl(file, I2C_RDWR, &i2c_data);
if (ret < 0) {
RETVAL = ret;
} else {
RETVAL = 0;
}
OUTPUT: RETVAL
void
i2c_scan_bus( file, mode = SCAN_MODE_AUTO, first = 0x03, last = 0x77 )
int file
int mode
int first
int last
PPCODE:
int i;
int j;
int res;
for (i = 0; i < 128; i += 16) {
for(j = 0; j < 16; j++) {
res = -1;
/* Skip unwanted addresses */
if (i+j < first || i+j > last) {
continue;
}
/* Set slave address */
if (ioctl(file, I2C_SLAVE, i+j) < 0) {
continue;
}
/* Probe this address */
switch (mode) {
case SCAN_MODE_QUICK:
res = i2c_smbus_write_quick(file, I2C_SMBUS_WRITE);
break;
case SCAN_MODE_READ:
res = i2c_smbus_read_byte(file);
break;
default:
if ((i+j >= 0x30 && i+j <= 0x37) || (i+j >= 0x50 && i+j <= 0x5F)) {
res = i2c_smbus_read_byte(file);
} else {
res = i2c_smbus_write_quick(file, I2C_SMBUS_WRITE );
}
}
if (res >= 0) {
EXTEND( SP, 1 );
SV* var = sv_newmortal();
sv_setuv( var, (UV)(i + j) );
PUSHs( var );
}
}
}
|