1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | // created: 2014/08/28
|
---|
6 | // filename: Unix_I2cPort.cpp
|
---|
7 | //
|
---|
8 | // author: Guillaume Sanahuja
|
---|
9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
10 | //
|
---|
11 | // version: $Id: $
|
---|
12 | //
|
---|
13 | // purpose: Class for unix i2c port
|
---|
14 | //
|
---|
15 | //
|
---|
16 | /*********************************************************************/
|
---|
17 |
|
---|
18 | #include "Unix_I2cPort.h"
|
---|
19 | #include <errno.h>
|
---|
20 | #include <fcntl.h> /* File control definitions */
|
---|
21 | #include <unistd.h>
|
---|
22 | #include <sys/ioctl.h>
|
---|
23 | #include <linux/i2c-dev.h>
|
---|
24 |
|
---|
25 |
|
---|
26 | using std::string;
|
---|
27 |
|
---|
28 | namespace flair
|
---|
29 | {
|
---|
30 | namespace core
|
---|
31 | {
|
---|
32 |
|
---|
33 | Unix_I2cPort::Unix_I2cPort(const Object* parent,string name,string device) : I2cPort(parent,name)
|
---|
34 | {
|
---|
35 | //open port
|
---|
36 | fd = open( device.c_str(), O_RDWR );
|
---|
37 | if (fd == -1)
|
---|
38 | {
|
---|
39 | Err("open_port: Unable to open %s\n",device.c_str());
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | Unix_I2cPort::~Unix_I2cPort()
|
---|
44 | {
|
---|
45 | close(fd);
|
---|
46 | }
|
---|
47 |
|
---|
48 | void Unix_I2cPort::SetRxTimeout(core::Time timeout_ns)
|
---|
49 | {
|
---|
50 | Warn("not implemented\n");
|
---|
51 | }
|
---|
52 |
|
---|
53 | void Unix_I2cPort::SetTxTimeout(core::Time timeout_ns)
|
---|
54 | {
|
---|
55 | Warn("not implemented\n");
|
---|
56 | }
|
---|
57 |
|
---|
58 | int Unix_I2cPort::SetSlave(uint16_t address)
|
---|
59 | {
|
---|
60 | int err=ioctl( fd, I2C_SLAVE_FORCE, address);
|
---|
61 | if( err < 0 )
|
---|
62 | {
|
---|
63 | Err("Failed to set slave address\n");
|
---|
64 | }
|
---|
65 |
|
---|
66 | return err;
|
---|
67 | }
|
---|
68 |
|
---|
69 | ssize_t Unix_I2cPort::Write(const void *buf,size_t nbyte)
|
---|
70 | {
|
---|
71 | return write(fd,buf, nbyte);
|
---|
72 | }
|
---|
73 |
|
---|
74 | ssize_t Unix_I2cPort::Read(void *buf,size_t nbyte)
|
---|
75 | {
|
---|
76 | return read(fd,buf, nbyte);
|
---|
77 | }
|
---|
78 |
|
---|
79 | } // end namespace core
|
---|
80 | } // end namespace flair
|
---|
81 |
|
---|