From 3bc866395cc755f344d341a7f0b13b99d641cb0f Mon Sep 17 00:00:00 2001 From: Nhan Cao <93050378+AnhNhan2803@users.noreply.github.com> Date: Fri, 30 Aug 2024 11:55:25 -0600 Subject: [PATCH] Adding I2C Burst Reading/Writing feature (#1495) * Adding I2C Burst Reading/Writing feature * Add functions to header file. Fixing: https://github.com/raspberrypi/pico-sdk/pull/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 Co-authored-by: Peter Harper --- src/rp2_common/hardware_i2c/i2c.c | 12 ++++++++ .../hardware_i2c/include/hardware/i2c.h | 29 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/rp2_common/hardware_i2c/i2c.c b/src/rp2_common/hardware_i2c/i2c.c index 865c16f3..0bf88eb5 100644 --- a/src/rp2_common/hardware_i2c/i2c.c +++ b/src/rp2_common/hardware_i2c/i2c.c @@ -259,6 +259,12 @@ int i2c_write_timeout_per_char_us(i2c_inst_t *i2c, uint8_t addr, const uint8_t * init_per_iteration_timeout_us(&ts, timeout_per_char_us), &ts); } +int i2c_write_burst_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len) { + int rc = i2c_write_blocking_internal(i2c, addr, src, len, true, NULL, NULL); + i2c->restart_on_next = false; + return rc; +} + static int i2c_read_blocking_internal(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop, check_timeout_fn timeout_check, timeout_state_t *ts) { 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 return i2c_read_blocking_internal(i2c, addr, dst, len, nostop, init_per_iteration_timeout_us(&ts, timeout_per_char_us), &ts); } + +int i2c_read_burst_blocking(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len) { + int rc = i2c_read_blocking_internal(i2c, addr, dst, len, true, NULL, NULL); + i2c->restart_on_next = false; + return rc; +} diff --git a/src/rp2_common/hardware_i2c/include/hardware/i2c.h b/src/rp2_common/hardware_i2c/include/hardware/i2c.h index 13bafcc2..9a9cdb08 100644 --- a/src/rp2_common/hardware_i2c/include/hardware/i2c.h +++ b/src/rp2_common/hardware_i2c/include/hardware/i2c.h @@ -316,6 +316,21 @@ int i2c_read_timeout_per_char_us(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, si */ int i2c_write_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop); +/*! \brief Attempt to write specified number of bytes to address, blocking in burst mode + * \ingroup hardware_i2c + * + * This version of the function will not issue a stop and will not restart on the next write. + * This allows you to write consecutive bytes of data without having to resend a stop bit and + * (for example) without having to send address byte(s) repeatedly + * + * \param i2c Either \ref i2c0 or \ref i2c1 + * \param addr 7-bit address of device to read from + * \param dst Pointer to buffer to receive data + * \param len Length of data in bytes to receive + * \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged or no device present. + */ +int i2c_write_burst_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len); + /*! \brief Attempt to read specified number of bytes from address, blocking * \ingroup hardware_i2c * @@ -329,6 +344,20 @@ int i2c_write_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t */ int i2c_read_blocking(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop); +/*! \brief Attempt to read specified number of bytes from address, blocking in burst mode + * \ingroup hardware_i2c + * + * This version of the function will not issue a stop and will not restart on the next read. + * This allows you to read consecutive bytes of data without having to resend a stop bit and + * (for example) without having to send address byte(s) repeatedly + * + * \param i2c Either \ref i2c0 or \ref i2c1 + * \param addr 7-bit address of device to read from + * \param dst Pointer to buffer to receive data + * \param len Length of data in bytes to receive + * \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged or no device present. + */ +int i2c_read_burst_blocking(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len); /*! \brief Determine non-blocking write space available * \ingroup hardware_i2c