Skip to content

Commit db120a1

Browse files
anhnhancaopeterharperuk
authored andcommitted
Adding I2C Burst Reading/Writing feature
1 parent efe2103 commit db120a1

File tree

1 file changed

+18
-10
lines changed
  • src/rp2_common/hardware_i2c

1 file changed

+18
-10
lines changed

src/rp2_common/hardware_i2c/i2c.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void i2c_set_slave_mode(i2c_inst_t *i2c, bool slave, uint8_t addr) {
131131
}
132132

133133
static int i2c_write_blocking_internal(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop,
134-
check_timeout_fn timeout_check, struct timeout_state *ts) {
134+
check_timeout_fn timeout_check, struct timeout_state *ts, bool burst) {
135135
invalid_params_if(HARDWARE_I2C, addr >= 0x80); // 7-bit addresses
136136
invalid_params_if(HARDWARE_I2C, i2c_reserved_addr(addr));
137137
// Synopsys hw accepts start/stop flags alongside data items in the same
@@ -238,29 +238,33 @@ static int i2c_write_blocking_internal(i2c_inst_t *i2c, uint8_t addr, const uint
238238
}
239239

240240
// nostop means we are now at the end of a *message* but not the end of a *transfer*
241-
i2c->restart_on_next = nostop;
241+
i2c->restart_on_next = burst ? false : nostop;
242242
return rval;
243243
}
244244

245245
int i2c_write_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop) {
246-
return i2c_write_blocking_internal(i2c, addr, src, len, nostop, NULL, NULL);
246+
return i2c_write_blocking_internal(i2c, addr, src, len, nostop, NULL, NULL, false);
247247
}
248248

249249
int i2c_write_blocking_until(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop,
250250
absolute_time_t until) {
251251
timeout_state_t ts;
252-
return i2c_write_blocking_internal(i2c, addr, src, len, nostop, init_single_timeout_until(&ts, until), &ts);
252+
return i2c_write_blocking_internal(i2c, addr, src, len, nostop, init_single_timeout_until(&ts, until), &ts, false);
253253
}
254254

255255
int i2c_write_timeout_per_char_us(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop,
256256
uint timeout_per_char_us) {
257257
timeout_state_t ts;
258258
return i2c_write_blocking_internal(i2c, addr, src, len, nostop,
259-
init_per_iteration_timeout_us(&ts, timeout_per_char_us), &ts);
259+
init_per_iteration_timeout_us(&ts, timeout_per_char_us), &ts, false);
260+
}
261+
262+
int i2c_write_blocking_burst_mode(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len) {
263+
return i2c_write_blocking_internal(i2c, addr, src, len, true, NULL, NULL, true);
260264
}
261265

262266
static int i2c_read_blocking_internal(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop,
263-
check_timeout_fn timeout_check, timeout_state_t *ts) {
267+
check_timeout_fn timeout_check, timeout_state_t *ts, bool burst) {
264268
invalid_params_if(HARDWARE_I2C, addr >= 0x80); // 7-bit addresses
265269
invalid_params_if(HARDWARE_I2C, i2c_reserved_addr(addr));
266270
invalid_params_if(HARDWARE_I2C, len == 0);
@@ -322,22 +326,26 @@ static int i2c_read_blocking_internal(i2c_inst_t *i2c, uint8_t addr, uint8_t *ds
322326
rval = byte_ctr;
323327
}
324328

325-
i2c->restart_on_next = nostop;
329+
i2c->restart_on_next = burst ? false : nostop;
326330
return rval;
327331
}
328332

329333
int i2c_read_blocking(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop) {
330-
return i2c_read_blocking_internal(i2c, addr, dst, len, nostop, NULL, NULL);
334+
return i2c_read_blocking_internal(i2c, addr, dst, len, nostop, NULL, NULL, false);
331335
}
332336

333337
int i2c_read_blocking_until(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop, absolute_time_t until) {
334338
timeout_state_t ts;
335-
return i2c_read_blocking_internal(i2c, addr, dst, len, nostop, init_single_timeout_until(&ts, until), &ts);
339+
return i2c_read_blocking_internal(i2c, addr, dst, len, nostop, init_single_timeout_until(&ts, until), &ts, false);
336340
}
337341

338342
int i2c_read_timeout_per_char_us(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop,
339343
uint timeout_per_char_us) {
340344
timeout_state_t ts;
341345
return i2c_read_blocking_internal(i2c, addr, dst, len, nostop,
342-
init_per_iteration_timeout_us(&ts, timeout_per_char_us), &ts);
346+
init_per_iteration_timeout_us(&ts, timeout_per_char_us), &ts, false);
347+
}
348+
349+
int i2c_read_blocking_burst_mode(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len) {
350+
return i2c_read_blocking_internal(i2c, addr, dst, len, true, NULL, NULL, true);
343351
}

0 commit comments

Comments
 (0)