YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
i2c_BBB.cpp
1#include <fcntl.h> // O_RDWR
2#include <unistd.h> // close
3#include <sys/ioctl.h> // ioctl
4#include <linux/i2c-dev.h>
5#include <linux/i2c.h>
6
7#include "i2c_BBB.h"
8#include "yahal_assert.h"
9
10namespace I2C {
11enum i2c_mode { READ = 0x01, WRITE = 0x00 };
12}
13
14i2c_BBB::i2c_BBB(const char * dev) {
15 int ret;
16 _fd = open (dev, O_RDWR);
17// assert ((_fd != -1), "I2C open failed");
18 ret = ioctl (_fd, I2C_TIMEOUT, 300); // in 10mS units ...
19// assert ((ret != -1), "I2C_TIMEOUT failed");
20}
21
22i2c_BBB::~i2c_BBB() {
23 int ret = close( _fd );
24 yahal_assert(ret != -1);
25}
26
27
28int16_t i2c_BBB::i2cRead (uint16_t addr, uint8_t *rxbuf, uint16_t len, bool sendStop) {
29 yahal_assert(sendStop);
30 yahal_assert(ioctl(_fd, I2C_SLAVE, addr) != -1);
31 int n = ::read(_fd, rxbuf, len);
32 yahal_assert(n == len);
33 return n;
34}
35
36
37int16_t i2c_BBB::i2cWrite(uint16_t addr, uint8_t *txbuf, uint16_t len, bool sendStop) {
38 yahal_assert(sendStop);
39
40 i2c_rdwr_ioctl_data data;
41 i2c_msg messages[1];
42
43 data.msgs = messages;
44 data.nmsgs = 1;
45
46 messages[0].addr = addr;
47 messages[0].flags = I2C::WRITE;
48 messages[0].len = len;
49 messages[0].buf = txbuf;
50
51 int ret = ioctl(_fd, I2C_RDWR, &data);
52 yahal_assert(ret != -1);
53 return ret;
54
55
56// assert((ioctl(_fd, I2C_SLAVE, addr) != -1), "I2C_SLAVE failed");
57// int n = ::write(_fd, txbuf, len);
58// assert((n == len), "I2C write failed");
59// return n;
60}
61
62void i2c_BBB::setSpeed(uint32_t Hz) {
63}
64
65