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
|
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include "FlexiForce.h"
int fd;
unsigned char addr;
unsigned char buf;
// fonction de lecture des bits
/* lire un octet sur le bus i2c
we want to access and then by reading this register
@param fd: file descriptor of the device
@param reg: register to access
@param buf: buffer used to store the result
@return : -1 in case of error otherwise 0
*/
// write the Byte Control
/*int write_ByteControl(int fd, unsigned char addr, unsigned char reg, unsigned char value)
{
unsigned char buf[]
}*/
int read_byte (int fd, unsigned char addr, unsigned char reg, unsigned char *buf)
{
// choix de notre module PCF8591P qui jouera le role de l'esclave
struct i2c_msg msg = {addr, 0, 1, buf};
// creation d'une requete IOCTL
struct i2c_rdwr_ioctl_data rdwr = { &msg, 1};
buf[0]= reg; // selection du registre
// write the desired register address
if (ioctl (fd, I2C_RDWR, &rdwr) < 0)
{
printf ("write error\n");
return -1;
}
msg.flags = I2C_M_RD; // read
//read the result and write it in buf[0]
if ( ioctl (fd, I2C_RDWR, &rdwr< 0))
{
printf ("Erreur de lecture\n");
return -1;
}
return 0;
}
/* Programme principal*/
int main (int argc, char **argv)
{
unsigned char buf;
unsigned char addr;
unsigned char subaddr;
unsigned char value;
if (argc != 4)
{
printf("wrong args numbers\n syntaxe (hexa): \ni2cread i2c-devfile addr subaddr\n");
return -1;
}
fd = open (argv[1], O_RDWR);
if ( fd< 0)
{
printf ("can't open %s\n", argv[1]);
return -1;
}
addr = (unsigned char) strtol (argv[2]), (char **) NULL, 16);
subaddr = (unsigned char ) strtol (argv[3]), (char **)NULL, 16);
read_byte (fd, addr, subaddr, &buf);
printf("Read component %x at subaddress %x->%x\n", addr, subaddr, buf);
close (fd);
return 0;
} |
Partager