Skip to content
Permalink
Browse files

drivers: nrf: Add power management to nrf TWI driver.

Add power management to nrf TWI driver.

Signed-off-by: Mieszko Mierunski <mieszko.mierunski@nordicsemi.no>
  • Loading branch information...
Mierunski authored and carlescufi committed Jun 6, 2019
1 parent 05cdd28 commit 5867c36950d986c007b1ceff15b9a8252f142914
Showing with 62 additions and 20 deletions.
  1. +62 −20 drivers/i2c/i2c_nrfx_twi.c
@@ -17,10 +17,12 @@ struct i2c_nrfx_twi_data {
struct k_sem transfer_sync;
struct k_sem completion_sync;
volatile nrfx_err_t res;
u32_t dev_config;
};

struct i2c_nrfx_twi_config {
nrfx_twi_t twi;
nrfx_twi_config_t config;
};

static inline struct i2c_nrfx_twi_data *get_dev_data(struct device *dev)
@@ -127,6 +129,7 @@ static int i2c_nrfx_twi_configure(struct device *dev, u32_t dev_config)
LOG_ERR("unsupported speed");
return -EINVAL;
}
get_dev_data(dev)->dev_config = dev_config;

return 0;
}
@@ -136,9 +139,10 @@ static const struct i2c_driver_api i2c_nrfx_twi_driver_api = {
.transfer = i2c_nrfx_twi_transfer,
};

static int init_twi(struct device *dev, const nrfx_twi_config_t *config)
static int init_twi(struct device *dev)
{
nrfx_err_t result = nrfx_twi_init(&get_dev_config(dev)->twi, config,
nrfx_err_t result = nrfx_twi_init(&get_dev_config(dev)->twi,
&get_dev_config(dev)->config,
event_handler, dev);
if (result != NRFX_SUCCESS) {
LOG_ERR("Failed to initialize device: %s",
@@ -148,6 +152,43 @@ static int init_twi(struct device *dev, const nrfx_twi_config_t *config)

return 0;
}

#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
static int twi_nrfx_pm_control(struct device *dev, u32_t ctrl_command,
void *context, device_pm_cb cb, void *arg)
{
static u32_t current_state = DEVICE_PM_ACTIVE_STATE;

if (ctrl_command == DEVICE_PM_SET_POWER_STATE) {
u32_t new_state = *((const u32_t *)context);

if (new_state != current_state) {
switch (new_state) {
case DEVICE_PM_ACTIVE_STATE:
init_twi(dev);
i2c_nrfx_twi_configure(
dev, get_dev_data(dev)->dev_config);
break;

default:
nrfx_twi_uninit(&get_dev_config(dev)->twi);
break;
}
current_state = new_state;
}
} else {
assert(ctrl_command == DEVICE_PM_GET_POWER_STATE);
*((u32_t *)context) = current_state;
}

if (cb) {
cb(dev, 0, context, arg);
}

return 0;
}
#endif /* CONFIG_DEVICE_POWER_MANAGEMENT */

#define I2C_NRFX_TWI_INVALID_FREQUENCY ((nrf_twi_frequency_t)-1)
#define I2C_NRFX_TWI_FREQUENCY(bitrate) \
(bitrate == I2C_BITRATE_STANDARD ? NRF_TWI_FREQ_100K \
@@ -166,31 +207,32 @@ static int init_twi(struct device *dev, const nrfx_twi_config_t *config)
IRQ_CONNECT(DT_NORDIC_NRF_I2C_I2C_##idx##_IRQ_0, \
DT_NORDIC_NRF_I2C_I2C_##idx##_IRQ_0_PRIORITY, \
nrfx_isr, nrfx_twi_##idx##_irq_handler, 0); \
const nrfx_twi_config_t config = { \
.scl = DT_NORDIC_NRF_I2C_I2C_##idx##_SCL_PIN, \
.sda = DT_NORDIC_NRF_I2C_I2C_##idx##_SDA_PIN, \
.frequency = I2C_NRFX_TWI_FREQUENCY( \
DT_NORDIC_NRF_I2C_I2C_##idx##_CLOCK_FREQUENCY) \
}; \
return init_twi(dev, &config); \
return init_twi(dev); \
} \
static struct i2c_nrfx_twi_data twi_##idx##_data = { \
.transfer_sync = Z_SEM_INITIALIZER( \
.transfer_sync = Z_SEM_INITIALIZER( \
twi_##idx##_data.transfer_sync, 1, 1), \
.completion_sync = Z_SEM_INITIALIZER( \
.completion_sync = Z_SEM_INITIALIZER( \
twi_##idx##_data.completion_sync, 0, 1) \
}; \
static const struct i2c_nrfx_twi_config twi_##idx##z_config = { \
.twi = NRFX_TWI_INSTANCE(idx) \
.twi = NRFX_TWI_INSTANCE(idx), \
.config = { \
.scl = DT_NORDIC_NRF_I2C_I2C_##idx##_SCL_PIN, \
.sda = DT_NORDIC_NRF_I2C_I2C_##idx##_SDA_PIN, \
.frequency = I2C_NRFX_TWI_FREQUENCY( \
DT_NORDIC_NRF_I2C_I2C_##idx##_CLOCK_FREQUENCY) \
} \
}; \
DEVICE_AND_API_INIT(twi_##idx, \
DT_NORDIC_NRF_I2C_I2C_##idx##_LABEL, \
twi_##idx##_init, \
&twi_##idx##_data, \
&twi_##idx##z_config, \
POST_KERNEL, \
CONFIG_I2C_INIT_PRIORITY, \
&i2c_nrfx_twi_driver_api)
DEVICE_DEFINE(twi_##idx, \
DT_NORDIC_NRF_I2C_I2C_##idx##_LABEL, \
twi_##idx##_init, \
twi_nrfx_pm_control, \
&twi_##idx##_data, \
&twi_##idx##z_config, \
POST_KERNEL, \
CONFIG_I2C_INIT_PRIORITY, \
&i2c_nrfx_twi_driver_api)

#ifdef CONFIG_I2C_0_NRF_TWI
I2C_NRFX_TWI_DEVICE(0);

0 comments on commit 5867c36

Please sign in to comment.
You can’t perform that action at this time.