| 881 |
theseven |
1 |
#include "global.h"
|
|
|
2 |
#include "protocol/i2c/i2c.h"
|
|
|
3 |
|
|
|
4 |
enum i2c_result i2c_init(const struct i2c_driver_instance* instance)
|
|
|
5 |
{
|
|
|
6 |
if (!instance) return I2C_RESULT_BAD_OBJECT;
|
|
|
7 |
return instance->driver->init(instance);
|
|
|
8 |
}
|
|
|
9 |
|
|
|
10 |
enum i2c_result i2c_txn(const struct i2c_driver_instance* instance, const struct i2c_transaction* transaction)
|
|
|
11 |
{
|
|
|
12 |
if (!instance) return I2C_RESULT_BAD_OBJECT;
|
|
|
13 |
return instance->driver->txn(instance, transaction);
|
|
|
14 |
}
|
|
|
15 |
|
|
|
16 |
enum i2c_result i2c_read_regs(const struct i2c_driver_instance* instance, int address, int reg, void* buf, int len)
|
|
|
17 |
{
|
|
|
18 |
if (!instance) return I2C_RESULT_BAD_OBJECT;
|
|
|
19 |
if (reg < 0 || reg > 255) return I2C_RESULT_INVALID_ARGUMENT;
|
|
|
20 |
struct __attribute__((packed,aligned(4)))
|
|
|
21 |
{
|
|
|
22 |
struct i2c_transaction info;
|
|
|
23 |
struct i2c_transfer transfers[2];
|
|
|
24 |
} txn =
|
|
|
25 |
{
|
|
|
26 |
.info.address = address,
|
|
|
27 |
.info.transfercount = 2,
|
|
|
28 |
.transfers =
|
|
|
29 |
{
|
|
|
30 |
{ .type = I2C_TRANSFER_TYPE_TX, .len = 1, .txbuf = ® },
|
|
|
31 |
{ .type = I2C_TRANSFER_TYPE_RX, .len = len, .txbuf = buf },
|
|
|
32 |
},
|
|
|
33 |
};
|
|
|
34 |
return i2c_txn(instance, &txn.info);
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
enum i2c_result i2c_write_regs(const struct i2c_driver_instance* instance, int address, int reg, const void* buf, int len)
|
|
|
38 |
{
|
|
|
39 |
if (!instance) return I2C_RESULT_BAD_OBJECT;
|
|
|
40 |
if (reg < 0 || reg > 255) return I2C_RESULT_INVALID_ARGUMENT;
|
|
|
41 |
struct __attribute__((packed,aligned(4)))
|
|
|
42 |
{
|
|
|
43 |
struct i2c_transaction info;
|
|
|
44 |
struct i2c_transfer transfers[2];
|
|
|
45 |
} txn =
|
|
|
46 |
{
|
|
|
47 |
.info.address = address,
|
|
|
48 |
.info.transfercount = 2,
|
|
|
49 |
.transfers =
|
|
|
50 |
{
|
|
|
51 |
{ .type = I2C_TRANSFER_TYPE_TX, .len = 1, .txbuf = ® },
|
|
|
52 |
{ .type = I2C_TRANSFER_TYPE_CONT, .len = len, .txbuf = buf },
|
|
|
53 |
},
|
|
|
54 |
};
|
|
|
55 |
return i2c_txn(instance, &txn.info);
|
|
|
56 |
}
|