Skip to content

Commit 3bc8663

Browse files
AnhNhan2803anhnhancaopeterharperuk
authored
Adding I2C Burst Reading/Writing feature (#1495)
* Adding I2C Burst Reading/Writing feature * Add functions to header file. Fixing: #1495 * Some missing changes Rename the functions. Lose the "mode" and "blocking" needs to be at the end. Just set restart_on_next in the caller rather than adding a parameter. --------- Co-authored-by: anhnhancao <[email protected]> Co-authored-by: Peter Harper <[email protected]>
1 parent 3cb21c8 commit 3bc8663

File tree

2 files changed

+41
-0
lines changed
  • src/rp2_common/hardware_i2c

2 files changed

+41
-0
lines changed

src/rp2_common/hardware_i2c/i2c.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ int i2c_write_timeout_per_char_us(i2c_inst_t *i2c, uint8_t addr, const uint8_t *
259259
init_per_iteration_timeout_us(&ts, timeout_per_char_us), &ts);
260260
}
261261

262+
int i2c_write_burst_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len) {
263+
int rc = i2c_write_blocking_internal(i2c, addr, src, len, true, NULL, NULL);
264+
i2c->restart_on_next = false;
265+
return rc;
266+
}
267+
262268
static int i2c_read_blocking_internal(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop,
263269
check_timeout_fn timeout_check, timeout_state_t *ts) {
264270
invalid_params_if(HARDWARE_I2C, addr >= 0x80); // 7-bit addresses
@@ -341,3 +347,9 @@ int i2c_read_timeout_per_char_us(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, si
341347
return i2c_read_blocking_internal(i2c, addr, dst, len, nostop,
342348
init_per_iteration_timeout_us(&ts, timeout_per_char_us), &ts);
343349
}
350+
351+
int i2c_read_burst_blocking(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len) {
352+
int rc = i2c_read_blocking_internal(i2c, addr, dst, len, true, NULL, NULL);
353+
i2c->restart_on_next = false;
354+
return rc;
355+
}

src/rp2_common/hardware_i2c/include/hardware/i2c.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,21 @@ int i2c_read_timeout_per_char_us(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, si
316316
*/
317317
int i2c_write_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop);
318318

319+
/*! \brief Attempt to write specified number of bytes to address, blocking in burst mode
320+
* \ingroup hardware_i2c
321+
*
322+
* This version of the function will not issue a stop and will not restart on the next write.
323+
* This allows you to write consecutive bytes of data without having to resend a stop bit and
324+
* (for example) without having to send address byte(s) repeatedly
325+
*
326+
* \param i2c Either \ref i2c0 or \ref i2c1
327+
* \param addr 7-bit address of device to read from
328+
* \param dst Pointer to buffer to receive data
329+
* \param len Length of data in bytes to receive
330+
* \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged or no device present.
331+
*/
332+
int i2c_write_burst_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len);
333+
319334
/*! \brief Attempt to read specified number of bytes from address, blocking
320335
* \ingroup hardware_i2c
321336
*
@@ -329,6 +344,20 @@ int i2c_write_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t
329344
*/
330345
int i2c_read_blocking(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop);
331346

347+
/*! \brief Attempt to read specified number of bytes from address, blocking in burst mode
348+
* \ingroup hardware_i2c
349+
*
350+
* This version of the function will not issue a stop and will not restart on the next read.
351+
* This allows you to read consecutive bytes of data without having to resend a stop bit and
352+
* (for example) without having to send address byte(s) repeatedly
353+
*
354+
* \param i2c Either \ref i2c0 or \ref i2c1
355+
* \param addr 7-bit address of device to read from
356+
* \param dst Pointer to buffer to receive data
357+
* \param len Length of data in bytes to receive
358+
* \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged or no device present.
359+
*/
360+
int i2c_read_burst_blocking(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len);
332361

333362
/*! \brief Determine non-blocking write space available
334363
* \ingroup hardware_i2c

0 commit comments

Comments
 (0)