-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2c.cpp
152 lines (129 loc) · 3.94 KB
/
i2c.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Credit: https://github.com/NaejEL/flipperzero-i2ctools
#include "i2c.h"
#include "scd30_logging.h"
int I2C::count = 0;
I2C::I2C(uint8_t address_): address(address_) {
furi_assert(++count == 1);
clearBuffers();
}
I2C::~I2C() {
--count;
stopInterrupts();
}
void I2C::clearBuffers() {
furi_assert(this);
for (uint8_t i = 0; i < MAX_RECORDS; i++) {
for (uint8_t j = 0; j < MAX_MESSAGE_SIZE; j++) {
frames[i].ack[j] = false;
frames[i].data[j] = 0;
}
frames[i].bitIndex = 0;
frames[i].dataIndex = 0;
}
frameIndex = 0;
state = BusState::Free;
first = true;
}
void I2C::startInterrupts() {
furi_assert(this);
furi_hal_gpio_init(&SCL, GpioModeInterruptRise, GpioPullNo, GpioSpeedHigh);
furi_hal_gpio_add_int_callback(&SCL, +[](void *userdata) {
auto &i2c = *reinterpret_cast<I2C *>(userdata);
if (i2c.state == BusState::Free)
return;
uint8_t frame = i2c.frameIndex;
uint8_t bit = i2c.frames[frame].bitIndex;
uint8_t data_idx = i2c.frames[frame].dataIndex;
if (bit < 8) {
i2c.frames[frame].data[data_idx] <<= 1;
i2c.frames[frame].data[data_idx] |= static_cast<int>(furi_hal_gpio_read(&SDA));
i2c.frames[frame].bitIndex++;
} else {
i2c.frames[frame].ack[data_idx] = !furi_hal_gpio_read(&SDA);
i2c.frames[frame].dataIndex++;
i2c.frames[frame].bitIndex = 0;
}
}, this);
// Add Rise and Fall Interrupt on SDA pin
furi_hal_gpio_init(&SDA, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedHigh);
furi_hal_gpio_add_int_callback(&SDA, +[](void *userdata) {
auto &i2c = *reinterpret_cast<I2C *>(userdata);
// SCL is low, maybe clock stretching
if (!furi_hal_gpio_read(&SCL))
return;
if (i2c.state == BusState::Started && furi_hal_gpio_read(&SDA)) {
// Check for stop condition: SDA rising while SCL is High
i2c.state = BusState::Free;
} else if (!furi_hal_gpio_read(&SDA)) {
// Check for start condition: SDA falling while SCL is high
i2c.state = BusState::Started;
if (i2c.first) {
i2c.first = false;
return;
}
i2c.frameIndex++;
if (MAX_RECORDS <= i2c.frameIndex)
i2c.clearBuffers();
}
}, this);
}
void I2C::stopInterrupts() {
furi_hal_gpio_remove_int_callback(&SCL);
furi_hal_gpio_remove_int_callback(&SDA);
// Reset GPIO pins to default state
furi_hal_gpio_init(&SCL, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_init(&SDA, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
}
#define HACK_ADDR (address << 1)
bool I2C::write(const uint8_t *data, uint8_t size) {
acquire();
const bool success = furi_hal_i2c_tx(I2C_BUS, HACK_ADDR, data, size, I2C_TIMEOUT);
if (!success)
ERROR("write: tx failed");
release();
return success;
}
bool I2C::read(uint8_t *buffer, uint8_t size) {
acquire();
const bool success = furi_hal_i2c_rx(I2C_BUS, HACK_ADDR, buffer, size, I2C_TIMEOUT);
release();
return success;
}
bool I2C::writeThenRead(const uint8_t *tx_data, uint8_t tx_size, uint8_t *rx_data, uint8_t rx_size) {
acquire();
const bool success = furi_hal_i2c_trx(I2C_BUS, HACK_ADDR, tx_data, tx_size, rx_data, rx_size, I2C_TIMEOUT);
if (!success)
ERROR("wtr: trx failed");
release();
return success;
}
bool I2C::readReg8(uint8_t reg_addr, uint8_t &out) {
acquire();
const bool success = furi_hal_i2c_read_reg_8(I2C_BUS, HACK_ADDR, reg_addr, &out, I2C_TIMEOUT);
release();
return success;
}
bool I2C::readReg16(uint8_t reg_addr, uint16_t &out) {
acquire();
const bool success = furi_hal_i2c_read_reg_16(I2C_BUS, HACK_ADDR, reg_addr, &out, I2C_TIMEOUT);
release();
return success;
}
bool I2C::writeReg8(uint8_t reg_addr, uint8_t data) {
acquire();
const bool success = furi_hal_i2c_write_reg_8(I2C_BUS, HACK_ADDR, reg_addr, data, I2C_TIMEOUT);
release();
return success;
}
bool I2C::writeReg16(uint8_t reg_addr, uint16_t data) {
acquire();
const bool success = furi_hal_i2c_write_reg_16(I2C_BUS, HACK_ADDR, reg_addr, data, I2C_TIMEOUT);
release();
return success;
}
void I2C::acquire() {
furi_hal_i2c_acquire(I2C_BUS);
}
void I2C::release() {
furi_hal_i2c_release(I2C_BUS);
}