-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesp32.c
More file actions
103 lines (82 loc) · 3 KB
/
esp32.c
File metadata and controls
103 lines (82 loc) · 3 KB
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
#include <crossi2c/esp32.h>
#include <string.h>
#define CROSSLOG_TAG "crossi2c"
#include <crosslog.h>
int crossi2c_esp32_create(struct crossi2c_bus *bus, i2c_port_t port) {
memset(bus, 0, sizeof(*bus));
bus->port = port;
return 0;
}
int crossi2c_destroy(struct crossi2c_bus *bus) {
memset(bus, 0, sizeof(*bus));
return 0;
}
int crossi2c_read(struct crossi2c_bus *bus, uint16_t addr, void *buf, size_t len) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, addr << 1 | I2C_MASTER_READ, true);
if (len > 1)
i2c_master_read(cmd, buf, len - 1, I2C_MASTER_ACK);
i2c_master_read_byte(cmd, buf + len - 1, I2C_MASTER_NACK);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(bus->port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret) {
CROSSLOGE("i2c_master_cmd_begin: %s(%d)", esp_err_to_name(ret), ret);
return -1;
}
return 0;
}
int crossi2c_write(struct crossi2c_bus *bus, uint16_t addr, const void *buf, size_t len) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write(cmd, (void*)buf, len, true);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(bus->port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret) {
CROSSLOGE("i2c_master_cmd_begin: %s(%d)", esp_err_to_name(ret), ret);
return -1;
}
return 0;
}
int crossi2c_write_read(struct crossi2c_bus *bus, uint16_t addr,
const void *writebuf, size_t writelen,
void *readbuf, size_t readlen)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write(cmd, (void*)writebuf, writelen, true);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_READ, true);
if (readlen > 1)
i2c_master_read(cmd, readbuf, readlen - 1, I2C_MASTER_ACK);
i2c_master_read_byte(cmd, readbuf + readlen - 1, I2C_MASTER_NACK);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(bus->port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret) {
CROSSLOGE("i2c_master_cmd_begin: %s(%d)", esp_err_to_name(ret), ret);
return -1;
}
return 0;
}
int crossi2c_burst_write(struct crossi2c_bus *bus, uint16_t addr,
uint8_t reg, const void *buf, size_t len)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write(cmd, ®, 1, true);
i2c_master_write(cmd, (void*)buf, len, true);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(bus->port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret) {
CROSSLOGE("i2c_master_cmd_begin: %s(%d)", esp_err_to_name(ret), ret);
return -1;
}
return 0;
}