Skip to content

Commit

Permalink
drivers: flash: w25qxxdv: Avoid locking when not threaded
Browse files Browse the repository at this point in the history
When using CONFIG_MULTITHREADING=n, the semaphore primitives are
non-functional and useless. Remove their usage when this option is
enabled.

Signed-off-by: Johannes Hutter <johannes@proglove.de>
  • Loading branch information
Johannes Hutter authored and nashif committed Jun 18, 2018
1 parent 6245d6c commit 392da5b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
34 changes: 22 additions & 12 deletions drivers/flash/spi_flash_w25qxxdv.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
#include "spi_flash_w25qxxdv.h"
#include "flash_priv.h"

#if defined(CONFIG_MULTITHREADING)
#define SYNC_INIT() k_sem_init( \
&((struct spi_flash_data *)dev->driver_data)->sem, 1, UINT_MAX)
#define SYNC_LOCK() k_sem_take(&driver_data->sem, K_FOREVER)
#define SYNC_UNLOCK() k_sem_give(&driver_data->sem)
#else
#define SYNC_INIT()
#define SYNC_LOCK()
#define SYNC_UNLOCK()
#endif

static int spi_flash_wb_access(struct spi_flash_data *ctx,
u8_t cmd, bool addressed, off_t offset,
void *data, size_t length, bool write)
Expand Down Expand Up @@ -123,14 +134,14 @@ static int spi_flash_wb_read(struct device *dev, off_t offset, void *data,
return -ENODEV;
}

k_sem_take(&driver_data->sem, K_FOREVER);
SYNC_LOCK();

wait_for_flash_idle(dev);

ret = spi_flash_wb_access(driver_data, W25QXXDV_CMD_READ,
true, offset, data, len, false);

k_sem_give(&driver_data->sem);
SYNC_UNLOCK();

return ret;
}
Expand All @@ -146,13 +157,13 @@ static int spi_flash_wb_write(struct device *dev, off_t offset,
return -ENOTSUP;
}

k_sem_take(&driver_data->sem, K_FOREVER);
SYNC_LOCK();

wait_for_flash_idle(dev);

reg = spi_flash_wb_reg_read(dev, W25QXXDV_CMD_RDSR);
if (!(reg & W25QXXDV_WEL_BIT)) {
k_sem_give(&driver_data->sem);
SYNC_UNLOCK();
return -EIO;
}

Expand All @@ -165,7 +176,7 @@ static int spi_flash_wb_write(struct device *dev, off_t offset,
ret = spi_flash_wb_access(driver_data, W25QXXDV_CMD_PP,
true, offset, (void *)data, len, true);

k_sem_give(&driver_data->sem);
SYNC_UNLOCK();

return ret;
}
Expand All @@ -176,7 +187,7 @@ static int spi_flash_wb_write_protection_set(struct device *dev, bool enable)
u8_t reg = 0;
int ret;

k_sem_take(&driver_data->sem, K_FOREVER);
SYNC_LOCK();

wait_for_flash_idle(dev);

Expand All @@ -188,7 +199,7 @@ static int spi_flash_wb_write_protection_set(struct device *dev, bool enable)

ret = spi_flash_wb_reg_write(dev, reg);

k_sem_give(&driver_data->sem);
SYNC_UNLOCK();

return ret;
}
Expand Down Expand Up @@ -252,12 +263,12 @@ static int spi_flash_wb_erase(struct device *dev, off_t offset, size_t size)
return -ENODEV;
}

k_sem_take(&driver_data->sem, K_FOREVER);
SYNC_LOCK();

reg = spi_flash_wb_reg_read(dev, W25QXXDV_CMD_RDSR);

if (!(reg & W25QXXDV_WEL_BIT)) {
k_sem_give(&driver_data->sem);
SYNC_UNLOCK();
return -EIO;
}

Expand Down Expand Up @@ -292,7 +303,7 @@ static int spi_flash_wb_erase(struct device *dev, off_t offset, size_t size)
}
}

k_sem_give(&driver_data->sem);
SYNC_UNLOCK();

return ret;
}
Expand Down Expand Up @@ -340,10 +351,9 @@ static int spi_flash_wb_configure(struct device *dev)

static int spi_flash_init(struct device *dev)
{
struct spi_flash_data *data = dev->driver_data;
int ret;

k_sem_init(&data->sem, 1, UINT_MAX);
SYNC_INIT();

ret = spi_flash_wb_configure(dev);
if (!ret) {
Expand Down
2 changes: 2 additions & 0 deletions drivers/flash/spi_flash_w25qxxdv.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ struct spi_flash_data {
struct spi_cs_control cs_ctrl;
#endif /* CONFIG_SPI_FLASH_W25QXXDV_GPIO_SPI_CS */
struct spi_config spi_cfg;
#if defined(CONFIG_MULTITHREADING)
struct k_sem sem;
#endif /* CONFIG_MULTITHREADING */
};


Expand Down

0 comments on commit 392da5b

Please sign in to comment.