Skip to content

bus: Allow configuration change for a node #3419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hw/bus/drivers/i2c_common/include/bus/drivers/i2c_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ struct bus_i2c_dev_cfg {
struct bus_i2c_dev {
struct bus_dev bdev;
struct bus_i2c_dev_cfg cfg;
/** I2C address */
uint16_t addr;
/** I2C frequency in kHz */
uint16_t freq;

#if MYNEWT_VAL(BUS_DEBUG_OS_DEV)
uint32_t devmagic;
Expand Down
3 changes: 3 additions & 0 deletions hw/bus/drivers/i2c_da1469x/src/i2c_da1469x.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ i2c_da1469x_configure_controller(struct bus_i2c_dev *dev, uint8_t address, uint1

BUS_DEBUG_VERIFY_DEV(dev);

if (dev->addr == address && dev->freq == freq) {
goto end;
}
i2c_regs = da1469x_i2c[dev->cfg.i2c_num].regs;

if (i2c_regs->I2C_ENABLE_REG & I2C_I2C_ENABLE_REG_I2C_EN_Msk) {
Expand Down
5 changes: 3 additions & 2 deletions hw/bus/drivers/i2c_hal/src/i2c_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@ bus_i2c_configure(struct bus_dev *bdev, struct bus_node *bnode)
{
struct bus_i2c_dev *dev = (struct bus_i2c_dev *)bdev;
struct bus_i2c_node *node = (struct bus_i2c_node *)bnode;
struct bus_i2c_node *current_node = (struct bus_i2c_node *)bdev->configured_for;
struct hal_i2c_settings i2c_cfg;
int rc;

BUS_DEBUG_VERIFY_DEV(dev);
BUS_DEBUG_VERIFY_NODE(node);

if (current_node && (current_node->freq == node->freq)) {
if (dev->freq == node->freq) {
return 0;
}

Expand All @@ -104,6 +103,8 @@ bus_i2c_configure(struct bus_dev *bdev, struct bus_node *bnode)
if (rc) {
goto done;
}
dev->freq = node->freq;
dev->addr = node->addr;

rc = hal_i2c_enable(dev->cfg.i2c_num);

Expand Down
8 changes: 7 additions & 1 deletion hw/bus/drivers/i2c_nrf52_twim/src/i2c_nrf52_twim.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ bus_i2c_nrf52_twim_configure_controller(struct bus_i2c_dev *dev, uint8_t address

BUS_DEBUG_VERIFY_DEV(dev);

if (dev->freq == freq && dev->addr == address) {
goto end;
}

nrf_twim = twims[dev->cfg.i2c_num].nrf_twim;

switch (freq) {
Expand All @@ -427,8 +431,10 @@ bus_i2c_nrf52_twim_configure_controller(struct bus_i2c_dev *dev, uint8_t address

if (rc == 0) {
nrf_twim->ADDRESS = address;
dev->addr = address;
dev->freq = freq;
}

end:
return rc;
}

Expand Down
8 changes: 7 additions & 1 deletion hw/bus/drivers/i2c_nrf5340/src/i2c_nrf5340.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ bus_i2c_nrf5340_configure_controller(struct bus_i2c_dev *dev, uint8_t address, u
NRF_TWIM_Type *nrf_twim;
int rc = 0;

if (dev->freq == freq && dev->addr == address) {
goto end;
}

nrf_twim = twims[dev->cfg.i2c_num].nrf_twim;

switch (freq) {
Expand All @@ -228,8 +232,10 @@ bus_i2c_nrf5340_configure_controller(struct bus_i2c_dev *dev, uint8_t address, u

if (rc == 0) {
nrf_twim->ADDRESS = address;
dev->addr = address;
dev->freq = freq;
}

end:
return rc;
}

Expand Down
13 changes: 8 additions & 5 deletions hw/bus/drivers/i2c_nrf91_twim/src/i2c_nrf91_twim.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,21 +395,19 @@ bus_i2c_nrf91_twim_configure(struct bus_dev *bdev, struct bus_node *bnode)
{
struct bus_i2c_dev *dev = (struct bus_i2c_dev *)bdev;
struct bus_i2c_node *node = (struct bus_i2c_node *)bnode;
struct bus_i2c_node *current_node = (struct bus_i2c_node *)bdev->configured_for;
NRF_TWIM_Type *nrf_twim;
int rc;

BUS_DEBUG_VERIFY_DEV(dev);
BUS_DEBUG_VERIFY_NODE(node);

if (dev->addr == node->addr && dev->freq == node->freq) {
return 0;
}
nrf_twim = twims[dev->cfg.i2c_num].nrf_twim;

nrf_twim->ADDRESS = node->addr;

if (current_node && (current_node->freq == node->freq)) {
return 0;
}

rc = 0;

switch (node->freq) {
Expand All @@ -429,6 +427,11 @@ bus_i2c_nrf91_twim_configure(struct bus_dev *bdev, struct bus_node *bnode)
rc = SYS_EIO;
}

if (rc == 0) {
dev->freq = node->freq;
dev->addr = node->addr;
}

return rc;
}

Expand Down
10 changes: 6 additions & 4 deletions hw/bus/drivers/spi_apollo3/src/spi_apollo3.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,15 @@ bus_spi_configure(struct bus_dev *bdev, struct bus_node *bnode)
{
struct bus_spi_dev *spi_dev = (struct bus_spi_dev *)bdev;
struct bus_spi_node *node = (struct bus_spi_node *)bnode;
struct bus_spi_node *current_node = (struct bus_spi_node *)bdev->configured_for;
struct hal_spi_settings spi_cfg;
int rc;

BUS_DEBUG_VERIFY_DEV(spi_dev);
BUS_DEBUG_VERIFY_NODE(node);

/* No need to reconfigure if already configured with the same settings */
if (current_node && (current_node->mode == node->mode) &&
(current_node->data_order == node->data_order) &&
(current_node->freq == node->freq)) {
if ((spi_dev->mode == node->mode) && (spi_dev->data_order == node->data_order) &&
(spi_dev->freq == node->freq)) {
return 0;
}

Expand All @@ -105,6 +103,10 @@ bus_spi_configure(struct bus_dev *bdev, struct bus_node *bnode)
goto done;
}

spi_dev->freq = node->freq;
spi_dev->data_order = node->data_order;
spi_dev->mode = node->mode;

spi_cfg.data_mode = node->mode;
spi_cfg.data_order = node->data_order;
spi_cfg.baudrate = node->freq * 1000;
Expand Down
6 changes: 6 additions & 0 deletions hw/bus/drivers/spi_common/include/bus/drivers/spi_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ struct bus_spi_dev_cfg {
struct bus_spi_dev {
struct bus_dev bdev;
struct bus_spi_dev_cfg cfg;
/** Data mode, one of the BUS_SPI_MODE_x */
int mode;
/** Data order, one of the BUS_SPI_DATA_ORDER_LSB/MSB */
int data_order;
/** Current SPI frequency in kHz */
uint32_t freq;

#if MYNEWT_VAL(BUS_DEBUG_OS_DEV)
uint32_t devmagic;
Expand Down
12 changes: 8 additions & 4 deletions hw/bus/drivers/spi_da1469x/src/spi_da1469x.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ spi_da1469x_disable(struct bus_dev *bdev)
* Domain COM can be powered off, register content can be lost prepare
* to set it from scratch.
*/
bdev->configured_for = NULL;
dev->freq = 0;

da1469x_pd_release(MCU_PD_DOMAIN_COM);

Expand Down Expand Up @@ -437,7 +437,6 @@ spi_da1469x_configure(struct bus_dev *bdev, struct bus_node *bnode)
{
struct bus_spi_dev *dev = (struct bus_spi_dev *)bdev;
struct bus_spi_node *node = (struct bus_spi_node *)bnode;
struct bus_spi_node *current_node = (struct bus_spi_node *)bdev->configured_for;
struct spi_da1469x_driver_data *dd;
SPI_Type *regs;
uint32_t ctrl_reg;
Expand All @@ -453,11 +452,16 @@ spi_da1469x_configure(struct bus_dev *bdev, struct bus_node *bnode)
regs->SPI_CTRL_REG = ctrl_reg ^ (SPI_SPI_CTRL_REG_SPI_ON_Msk);
}

if (current_node && (current_node->freq == node->freq) && current_node->mode == node->mode) {
/* Same device, no changes required. */
if ((spi_dev->mode == node->mode) && (spi_dev->data_order == node->data_order) &&
(spi_dev->freq == node->freq)) {
/* Same configuration, no changes required. */
goto end;
}

spi_dev->freq = node->freq;
spi_dev->data_order = node->data_order;
spi_dev->mode = node->mode;

ctrl_reg &= ~(SPI_SPI_CTRL_REG_SPI_TX_FIFO_NOTFULL_MASK_Msk |
SPI_SPI_CTRL_REG_SPI_DMA_TXREQ_MODE_Msk |
SPI_SPI_CTRL_REG_SPI_9BIT_VAL_Msk |
Expand Down
10 changes: 6 additions & 4 deletions hw/bus/drivers/spi_hal/src/spi_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,15 @@ bus_spi_configure(struct bus_dev *bdev, struct bus_node *bnode)
{
struct bus_spi_dev *spi_dev = (struct bus_spi_dev *)bdev;
struct bus_spi_node *node = (struct bus_spi_node *)bnode;
struct bus_spi_node *current_node = (struct bus_spi_node *)bdev->configured_for;
struct hal_spi_settings spi_cfg;
int rc;

BUS_DEBUG_VERIFY_DEV(spi_dev);
BUS_DEBUG_VERIFY_NODE(node);

/* No need to reconfigure if already configured with the same settings */
if (current_node && (current_node->mode == node->mode) &&
(current_node->data_order == node->data_order) &&
(current_node->freq == node->freq)) {
if ((spi_dev->mode == node->mode) && (spi_dev->data_order == node->data_order) &&
(spi_dev->freq == node->freq)) {
return 0;
}

Expand All @@ -114,6 +112,10 @@ bus_spi_configure(struct bus_dev *bdev, struct bus_node *bnode)
goto done;
}

spi_dev->freq = node->freq;
spi_dev->data_order = node->data_order;
spi_dev->mode = node->mode;

spi_cfg.data_mode = node->mode;
spi_cfg.data_order = node->data_order;
spi_cfg.baudrate = node->freq;
Expand Down
10 changes: 5 additions & 5 deletions hw/bus/drivers/spi_stm32/src/spi_stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,6 @@ spi_stm32_configure(struct bus_dev *bdev, struct bus_node *bnode)
{
struct bus_spi_dev *dev = (struct bus_spi_dev *)bdev;
struct bus_spi_node *node = (struct bus_spi_node *)bnode;
struct bus_spi_node *current_node = (struct bus_spi_node *)bdev->configured_for;
struct spi_stm32_driver_data *dd;
const struct stm32_spi_hw *hw;
uint32_t pclk;
Expand All @@ -620,10 +619,8 @@ spi_stm32_configure(struct bus_dev *bdev, struct bus_node *bnode)

dd = driver_data(dev);

if (current_node &&
current_node->freq == node->freq &&
current_node->data_order == node->data_order &&
current_node->mode == node->mode) {
if (dev->freq == node->freq && dev->data_order == node->data_order &&
dev->mode == node->mode) {
goto end;
}

Expand All @@ -639,6 +636,9 @@ spi_stm32_configure(struct bus_dev *bdev, struct bus_node *bnode)
if (prescaler > 7) {
rc = SYS_EINVAL;
} else {
dev->freq = node->freq;
dev->data_order = node->data_order;
dev->mode = node->mode;
#if MYNEWT_VAL(MCU_STM32H7) || MYNEWT_VAL(MCU_STM32U5)
dd->hspi.Init.BaudRatePrescaler = prescaler << SPI_CFG1_MBR_Pos;
#else
Expand Down
5 changes: 0 additions & 5 deletions hw/bus/src/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,6 @@ bus_node_lock(struct os_dev *node, os_time_t timeout)
}
#endif

/* No need to configure if already configured for the same node */
if (bdev->configured_for == bnode) {
return 0;
}

/*
* Configuration is done on 1st lock so in case we need to configure device
* on nested lock it means that most likely bus device was locked for one
Expand Down
Loading