Skip to content

Commit 191864f

Browse files
ananglcarlescufi
authored andcommitted
[nrf fromtree] drivers: i2c: nrfx: Fix i2c_recover_bus implementation for PINCTRL
When PINCTRL is enabled, the SCL and SDA pin numbers are not available in configuration structures used for nrfx drivers initialization. In this case, these pin numbers need to be obtained from peripheral registers. Signed-off-by: Andrzej Głąbek <[email protected]> (cherry picked from commit e530ba9)
1 parent 0afc8f6 commit 191864f

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

drivers/i2c/i2c_nrfx_twi.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ struct i2c_nrfx_twi_config {
3232
#endif
3333
};
3434

35+
static int i2c_nrfx_twi_recover_bus(const struct device *dev);
36+
3537
static int i2c_nrfx_twi_transfer(const struct device *dev,
3638
struct i2c_msg *msgs,
3739
uint8_t num_msgs, uint16_t addr)
@@ -117,14 +119,13 @@ static int i2c_nrfx_twi_transfer(const struct device *dev,
117119
* In many situation, a retry is sufficient.
118120
* However, some time the I2C device get stuck and need
119121
* help to recover.
120-
* Therefore we always call nrfx_twi_bus_recover() to
121-
* make sure everything has been done to restore the
122+
* Therefore we always call i2c_nrfx_twi_recover_bus()
123+
* to make sure everything has been done to restore the
122124
* bus from this error.
123125
*/
124126
LOG_ERR("Error on I2C line occurred for message %d", i);
125127
nrfx_twi_disable(&config->twi);
126-
nrfx_twi_bus_recover(config->config.scl,
127-
config->config.sda);
128+
(void)i2c_nrfx_twi_recover_bus(dev);
128129
ret = -EIO;
129130
break;
130131
}
@@ -195,10 +196,19 @@ static int i2c_nrfx_twi_configure(const struct device *dev,
195196
static int i2c_nrfx_twi_recover_bus(const struct device *dev)
196197
{
197198
const struct i2c_nrfx_twi_config *config = dev->config;
199+
uint32_t scl_pin;
200+
uint32_t sda_pin;
201+
nrfx_err_t err;
198202

199-
nrfx_err_t err = nrfx_twi_bus_recover(config->config.scl,
200-
config->config.sda);
203+
#ifdef CONFIG_PINCTRL
204+
scl_pin = nrf_twi_scl_pin_get(config->twi.p_twi);
205+
sda_pin = nrf_twi_sda_pin_get(config->twi.p_twi);
206+
#else
207+
scl_pin = config->config.scl;
208+
sda_pin = config->config.sda;
209+
#endif
201210

211+
err = nrfx_twi_bus_recover(scl_pin, sda_pin);
202212
return (err == NRFX_SUCCESS ? 0 : -EBUSY);
203213
}
204214

drivers/i2c/i2c_nrfx_twim.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ struct i2c_nrfx_twim_config {
3838

3939
static int init_twim(const struct device *dev);
4040

41+
static int i2c_nrfx_twim_recover_bus(const struct device *dev);
42+
4143
static int i2c_nrfx_twim_transfer(const struct device *dev,
4244
struct i2c_msg *msgs,
4345
uint8_t num_msgs, uint16_t addr)
@@ -170,14 +172,13 @@ static int i2c_nrfx_twim_transfer(const struct device *dev,
170172
* In many situation, a retry is sufficient.
171173
* However, some time the I2C device get stuck and need
172174
* help to recover.
173-
* Therefore we always call nrfx_twim_bus_recover() to
174-
* make sure everything has been done to restore the
175+
* Therefore we always call i2c_nrfx_twim_recover_bus()
176+
* to make sure everything has been done to restore the
175177
* bus from this error.
176178
*/
177179
LOG_ERR("Error on I2C line occurred for message %d", i);
178180
nrfx_twim_disable(&dev_config->twim);
179-
nrfx_twim_bus_recover(dev_data->twim_config.scl,
180-
dev_data->twim_config.sda);
181+
(void)i2c_nrfx_twim_recover_bus(dev);
181182
ret = -EIO;
182183
break;
183184
}
@@ -304,11 +305,23 @@ static int i2c_nrfx_twim_configure(const struct device *dev,
304305

305306
static int i2c_nrfx_twim_recover_bus(const struct device *dev)
306307
{
308+
uint32_t scl_pin;
309+
uint32_t sda_pin;
310+
nrfx_err_t err;
311+
312+
#ifdef CONFIG_PINCTRL
313+
const struct i2c_nrfx_twim_config *dev_config = dev->config;
314+
315+
scl_pin = nrf_twim_scl_pin_get(dev_config->twim.p_twim);
316+
sda_pin = nrf_twim_sda_pin_get(dev_config->twim.p_twim);
317+
#else
307318
struct i2c_nrfx_twim_data *dev_data = dev->data;
308319

309-
nrfx_err_t err = nrfx_twim_bus_recover(dev_data->twim_config.scl,
310-
dev_data->twim_config.sda);
320+
scl_pin = dev_data->twim_config.scl;
321+
sda_pin = dev_data->twim_config.sda;
322+
#endif
311323

324+
err = nrfx_twim_bus_recover(scl_pin, sda_pin);
312325
return (err == NRFX_SUCCESS ? 0 : -EBUSY);
313326
}
314327

0 commit comments

Comments
 (0)