I would like to do I2C BlockRead using nRF52.
devzone.nordicsemi.com/.../i2c-block-read
When I looked at it, I found that it was not possible to receive variable length data.
For that reason I modified nrfx_twim.c as follows.
=======================================================================================================================
diff --git a/drivers/i2c/i2c_nrfx_twim.c b/drivers/i2c/i2c_nrfx_twim.c
index 46275025d7..6842e788c1 100644
--- a/drivers/i2c/i2c_nrfx_twim.c
+++ b/drivers/i2c/i2c_nrfx_twim.c
@@ -58,9 +58,11 @@ static int i2c_nrfx_twim_transfer(struct device *dev, struct i2c_msg *msgs,
};
nrfx_err_t res = nrfx_twim_xfer(&get_dev_config(dev)->twim,
- &cur_xfer,
- (msgs[i].flags & I2C_MSG_STOP) ?
- 0 : NRFX_TWIM_FLAG_TX_NO_STOP);
+ &cur_xfer,
+ ((msgs[i].flags & I2C_MSG_STOP) ?
+ 0 : NRFX_TWIM_FLAG_TX_NO_STOP) |
+ ((msgs[i].flags & I2C_MSG_RECV_LEN) ?
+ NRFX_TWIM_FLAG_RX_RECV_LEN : 0));
if (res != NRFX_SUCCESS) {
if (res == NRFX_ERROR_BUSY) {
ret = -EBUSY;
diff --git a/ext/hal/nordic/nrfx/drivers/include/nrfx_twim.h b/ext/hal/nordic/nrfx/drivers/include/nrfx_twim.h
index 7d10ae568a..a9c36dc3ed 100644
--- a/ext/hal/nordic/nrfx/drivers/include/nrfx_twim.h
+++ b/ext/hal/nordic/nrfx/drivers/include/nrfx_twim.h
@@ -110,6 +110,7 @@ typedef struct
#define NRFX_TWIM_FLAG_HOLD_XFER (1UL << 3) /**< Set up the transfer but do not start it. */
#define NRFX_TWIM_FLAG_REPEATED_XFER (1UL << 4) /**< Flag indicating that the transfer will be executed multiple times. */
#define NRFX_TWIM_FLAG_TX_NO_STOP (1UL << 5) /**< Flag indicating that the TX transfer will not end with a stop condition. */
+#define NRFX_TWIM_FLAG_RX_RECV_LEN (1UL << 6) /**< Flag indicating that the RX transfer length will be first received byte. */
/**
* @brief TWI master driver event types.
diff --git a/ext/hal/nordic/nrfx/drivers/src/nrfx_twim.c b/ext/hal/nordic/nrfx/drivers/src/nrfx_twim.c
index 75da22a3b4..f00ca12612 100644
--- a/ext/hal/nordic/nrfx/drivers/src/nrfx_twim.c
+++ b/ext/hal/nordic/nrfx/drivers/src/nrfx_twim.c
@@ -415,7 +415,23 @@ __STATIC_INLINE nrfx_err_t twim_xfer(twim_control_block_t * p_cb,
nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_RESUME);
break;
case NRFX_TWIM_XFER_RX:
- nrf_twim_rx_buffer_set(p_twim, p_xfer_desc->p_primary_buf, p_xfer_desc->primary_length);
+ if (NRFX_TWIM_FLAG_RX_RECV_LEN & flags)
+ {
+ nrf_twim_rx_buffer_set(p_twim, p_xfer_desc->p_primary_buf, 1);
+ nrf_twim_shorts_set(p_twim, NRF_TWIM_SHORT_LASTRX_SUSPEND_MASK);
+ nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_SUSPENDED);
+ nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_RESUME);
+ nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_STARTRX);
+ while (!nrf_twim_event_check(p_twim, NRF_TWIM_EVENT_SUSPENDED))
+ {}
+ NRFX_LOG_DEBUG("TWIM: Event: %s.", EVT_TO_STR_TWIM(NRF_TWIM_EVENT_SUSPENDED));
+ nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_SUSPENDED);
+ nrf_twim_rx_buffer_set(p_twim, p_xfer_desc->p_primary_buf + 1, p_xfer_desc->p_primary_buf[0]);
+ }
+ else
+ {
+ nrf_twim_rx_buffer_set(p_twim, p_xfer_desc->p_primary_buf, p_xfer_desc->primary_length);
+ }
nrf_twim_shorts_set(p_twim, NRF_TWIM_SHORT_LASTRX_STOP_MASK);
p_cb->int_mask = NRF_TWIM_INT_STOPPED_MASK | NRF_TWIM_INT_ERROR_MASK;
start_task = NRF_TWIM_TASK_STARTRX;
diff --git a/ext/hal/nordic/nrfx/hal/nrf_twim.h b/ext/hal/nordic/nrfx/hal/nrf_twim.h
index f06db8e0af..9dbf0c0db6 100644
--- a/ext/hal/nordic/nrfx/hal/nrf_twim.h
+++ b/ext/hal/nordic/nrfx/hal/nrf_twim.h
@@ -84,6 +84,7 @@ typedef enum
NRF_TWIM_SHORT_LASTTX_SUSPEND_MASK = TWIM_SHORTS_LASTTX_SUSPEND_Msk, ///< Shortcut between LASTTX event and SUSPEND task.
NRF_TWIM_SHORT_LASTTX_STOP_MASK = TWIM_SHORTS_LASTTX_STOP_Msk, ///< Shortcut between LASTTX event and STOP task.
NRF_TWIM_SHORT_LASTRX_STARTTX_MASK = TWIM_SHORTS_LASTRX_STARTTX_Msk, ///< Shortcut between LASTRX event and STARTTX task.
+ NRF_TWIM_SHORT_LASTRX_SUSPEND_MASK = TWIM_SHORTS_LASTRX_SUSPEND_Msk, ///< Shortcut between LASTRX event and SUSPEND task.
NRF_TWIM_SHORT_LASTRX_STOP_MASK = TWIM_SHORTS_LASTRX_STOP_Msk, ///< Shortcut between LASTRX event and STOP task.
NRF_TWIM_ALL_SHORTS_MASK = TWIM_SHORTS_LASTTX_STARTRX_Msk |
TWIM_SHORTS_LASTTX_SUSPEND_Msk |
diff --git a/include/i2c.h b/include/i2c.h
index 90acd60f3c..f31393a20e 100644
--- a/include/i2c.h
+++ b/include/i2c.h
@@ -88,6 +88,9 @@ extern "C" {
/** Use 10-bit addressing for this message. */
#define I2C_MSG_ADDR_10_BITS (1 << 3)
+/** Length will be first recied byte. */
+#define I2C_MSG_RECV_LEN (1 << 4)
+
/**
* @brief One I2C Message.
*
=======================================================================================================================
However, after READ the SLAVE address, HOST returns NACK to SLAVE after receiving two 8-byte data.
Then, READ the SLAVE address again.

I do not know the reason why HOST returns NACK to SLAVE after receiving two data.
Is my correction method wrong?
Thank you in advance.