Skip to content

Commit

Permalink
fix: Flash PM: correct code style
Browse files Browse the repository at this point in the history
* fix code style according to Zephyr's styleguide
  • Loading branch information
Alexander Preißner committed Jun 25, 2018
1 parent 533f517 commit 32a0a52
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 85 deletions.
106 changes: 62 additions & 44 deletions drivers/flash/spi_flash_w25qxxxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ static int spi_flash_wb_read(struct device *dev, off_t offset, void *data,
struct spi_flash_data * const driver_data = dev->driver_data;
u8_t *buf = driver_data->buf;

// Exit if address range out of memory bounds
if (offset < 0 || (offset + len) > CONFIG_SPI_FLASH_W25QXXXX_FLASH_SIZE) {
/* Exit if address range out of memory bounds */
if (offset < 0
|| (offset + len)
> CONFIG_SPI_FLASH_W25QXXXX_FLASH_SIZE) {
return -EFAULT;
}

Expand All @@ -159,10 +161,12 @@ static int spi_flash_wb_read(struct device *dev, off_t offset, void *data,
return -EIO;
}

// If requested data is longer than
// W25QXXXX_PAGE_SIZE, split read access into
// multiple SPI transactions until requested length is satisfied.
/* If requested data is longer than
* W25QXXXX_PAGE_SIZE, split read access into
* multiple SPI transactions until requested length is satisfied.
*/
size_t transaction_len = 0;

while (len > 0) {
if (len > W25QXXXX_PAGE_SIZE) {
transaction_len = W25QXXXX_PAGE_SIZE;
Expand All @@ -181,14 +185,15 @@ static int spi_flash_wb_read(struct device *dev, off_t offset, void *data,

if (spi_transceive(driver_data->spi, buf,
transaction_len + W25QXXXX_LEN_CMD_ADDRESS, buf,
transaction_len + W25QXXXX_LEN_CMD_ADDRESS) != 0) {
transaction_len + W25QXXXX_LEN_CMD_ADDRESS)
!= 0) {
k_sem_give(&driver_data->sem);
return -EIO;
}

memcpy(data, buf + W25QXXXX_LEN_CMD_ADDRESS, transaction_len);

// Update indices and pointers
/* Update indices and pointers */
offset += transaction_len;
len -= transaction_len;
data += transaction_len;
Expand Down Expand Up @@ -221,10 +226,10 @@ static int spi_flash_wb_write_within_page(struct device *dev, off_t offset,
struct spi_flash_data * const driver_data = dev->driver_data;
u8_t *buf = driver_data->buf;

// Exit if address range out of page bounds
if (offset
< 0||
((offset & (W25QXXXX_PAGE_SIZE - 1)) + len) > W25QXXXX_PAGE_SIZE) {
/* Exit if address range out of page bounds */
if (offset < 0 ||
((offset & (W25QXXXX_PAGE_SIZE - 1)) + len)
> W25QXXXX_PAGE_SIZE) {
return -EFAULT;
}

Expand Down Expand Up @@ -258,7 +263,8 @@ static int spi_flash_wb_write_within_page(struct device *dev, off_t offset,
* flash automatically turns on write protection at the completion
* of each write or erase transaction.
*/
if (spi_write(driver_data->spi, buf, len + W25QXXXX_LEN_CMD_ADDRESS) != 0) {
if (spi_write(driver_data->spi, buf, len + W25QXXXX_LEN_CMD_ADDRESS)
!= 0) {
k_sem_give(&driver_data->sem);
return -EIO;
}
Expand Down Expand Up @@ -318,24 +324,31 @@ static int spi_flash_wb_write(struct device *dev, off_t offset,
{
int rc;

// Exit if address range out of memory bounds
if (offset < 0 || (offset + len) > CONFIG_SPI_FLASH_W25QXXXX_FLASH_SIZE) {
/* Exit if address range out of memory bounds */
if (offset < 0
|| (offset + len)
> CONFIG_SPI_FLASH_W25QXXXX_FLASH_SIZE) {
return -EFAULT;
}

// If requested data is longer than W25QXXXX_PAGE_SIZE or
// W25QXXXX_PAGE_SIZE, split write
// access into multiple SPI transactions until requested length is
// satisfied.
/* If requested data is longer than W25QXXXX_PAGE_SIZE or
* W25QXXXX_PAGE_SIZE, split write
* access into multiple SPI transactions until requested length is
* satisfied.
*/
size_t transaction_len = 0;
// The given data pointer is declared constant by the API. Therefore, we
// use a local incrementing offset variable to hand the reference to the
// correct data chunk over to `spi_flash_wb_write_within_page`.

/* The given data pointer is declared constant by the API. Therefore, we
* use a local incrementing offset variable to hand the reference to the
* correct data chunk over to `spi_flash_wb_write_within_page`.
*/
off_t data_offset = 0;

while (len > 0) {

// Align first write access to W25Q page boundaries
if (((offset & (W25QXXXX_PAGE_SIZE - 1)) + len) > W25QXXXX_PAGE_SIZE) {
/* Align first write access to W25Q page boundaries */
if (((offset & (W25QXXXX_PAGE_SIZE - 1)) + len)
> W25QXXXX_PAGE_SIZE) {
transaction_len = W25QXXXX_PAGE_SIZE
- (offset & (W25QXXXX_PAGE_SIZE - 1));
} else if (len > W25QXXXX_PAGE_SIZE) {
Expand All @@ -344,20 +357,22 @@ static int spi_flash_wb_write(struct device *dev, off_t offset,
transaction_len = len;
}

// Disable write protection every time before data is written.
/*
* Disable write protection every time before data is written.
*/
rc = spi_flash_wb_write_protection_set(dev, false);
if (rc) {
return rc;
}

// Write transaction
rc = spi_flash_wb_write_within_page(dev, offset, (data + data_offset),
transaction_len);
/* Write transaction */
rc = spi_flash_wb_write_within_page(dev, offset,
(data + data_offset), transaction_len);
if (rc) {
return rc;
}

// Update indices and pointers
/* Update indices and pointers */
offset += transaction_len;
len -= transaction_len;
data_offset += transaction_len;
Expand Down Expand Up @@ -504,8 +519,6 @@ void spi_flash_wb_page_layout(struct device *dev,
{
*layout = &flash_w25qxxxx_pages_layout;
*layout_size = 1;

return;
}
#endif

Expand All @@ -529,9 +542,8 @@ void spi_flash_wb_page_layout(struct device *dev,
* @return 0 on success, <0 otherwise
* @retval -EIO SPI transaction failed or Flash device is not in expected
* power state
* @retval -ENOTSUP if an unknown power state is requested
* @retval -ENOSYS if command is neither `DEVICE_PM_GET_POWER_STATE` nor
* `DEVICE_PM_SET_POWER_STATE`
* @retval -ENOTSUP if an unknown power state is requested or if command is
* neither `DEVICE_PM_GET_POWER_STATE` nor `DEVICE_PM_SET_POWER_STATE`
*/
static int spi_flash_wb_power_mode_control(struct device *dev, u32_t command,
void *context)
Expand All @@ -558,7 +570,9 @@ static int spi_flash_wb_power_mode_control(struct device *dev, u32_t command,
return rc;
}
}
/* request device ID to check whether device is in active state */
/* request device ID to check whether device is in
* active state
*/
rc = spi_flash_wb_id(dev);
if (rc) {
return rc;
Expand All @@ -569,7 +583,8 @@ static int spi_flash_wb_power_mode_control(struct device *dev, u32_t command,
case DEVICE_PM_SUSPEND_STATE:
case DEVICE_PM_OFF_STATE:
/*
* covers all low-power states that the API offers with the Flash
* covers all low-power states that the API offers
* with the Flash
* device's power-down mode.
*/
if ((pm_state != DEVICE_PM_LOW_POWER_STATE)
Expand All @@ -582,8 +597,9 @@ static int spi_flash_wb_power_mode_control(struct device *dev, u32_t command,
}
}
/*
* request device ID to check whether device is in power-down
* state. ID request MUST FAIL in power-down state.
* request device ID to check whether device is in
* power-down state.
* ID request MUST FAIL in power-down state.
*/
rc = spi_flash_wb_id(dev);
if (rc == 0) {
Expand All @@ -595,7 +611,7 @@ static int spi_flash_wb_power_mode_control(struct device *dev, u32_t command,
return -ENOTSUP;
}
} else {
return -ENOSYS;
return -ENOTSUP;
}

return 0;
Expand Down Expand Up @@ -627,14 +643,15 @@ static int spi_flash_init(struct device *dev)

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

// Send resume command to start in defined state
/* Send resume command to start in defined state */
u8_t cmd = W25QXXXX_CMD_RDP;

ret = spi_flash_wb_reg_write(dev, &cmd);
if (ret){
return ret;
if (ret) {
return ret;
}

// Device ID fetch will also validate device power state.
/* Device ID fetch will also validate device power state. */
ret = spi_flash_wb_config(dev);
if (!ret) {
dev->driver_api = &spi_flash_api;
Expand All @@ -646,5 +663,6 @@ static int spi_flash_init(struct device *dev)
static struct spi_flash_data spi_flash_memory_data;

DEVICE_DEFINE(spi_flash_memory, CONFIG_SPI_FLASH_W25QXXXX_DRV_NAME,
spi_flash_init, spi_flash_wb_power_mode_control, &spi_flash_memory_data,
NULL, POST_KERNEL, CONFIG_SPI_FLASH_W25QXXXX_INIT_PRIORITY, NULL);
spi_flash_init, spi_flash_wb_power_mode_control,
&spi_flash_memory_data, NULL, POST_KERNEL,
CONFIG_SPI_FLASH_W25QXXXX_INIT_PRIORITY, NULL);
82 changes: 41 additions & 41 deletions samples/drivers/spi_flash/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ void main(void)
printf("\nTest 1: Flash erase\n");
flash_write_protection_set(flash_dev, false);
if (flash_erase(flash_dev,
FLASH_TEST_REGION_OFFSET,
FLASH_SECTOR_SIZE) != 0) {
FLASH_TEST_REGION_OFFSET,
FLASH_SECTOR_SIZE) != 0) {
printf(" Flash erase failed!\n");
} else {
printf(" Flash erase succeeded!\n");
Expand All @@ -52,13 +52,13 @@ void main(void)
buf[1] = TEST_DATA_BYTE_1;
printf(" Attempted to write %x %x\n", buf[0], buf[1]);
if (flash_write(flash_dev, FLASH_TEST_REGION_OFFSET, buf,
TEST_DATA_LEN) != 0) {
TEST_DATA_LEN) != 0) {
printf(" Flash write failed!\n");
return;
}

if (flash_read(flash_dev, FLASH_TEST_REGION_OFFSET, buf,
TEST_DATA_LEN) != 0) {
TEST_DATA_LEN) != 0) {
printf(" Flash read failed!\n");
return;
}
Expand All @@ -70,52 +70,52 @@ void main(void)
printf(" Data read does not match with data written!!\n");
}


/*
* Power mode test
*/
printf("\nTest 3: Flash power modes\n");
int ret;
uint32_t pm_state;
u32_t pm_state;

printf("Query Flash power mode\n");
ret = device_get_power_state(flash_dev, &pm_state);
if (ret || pm_state != DEVICE_PM_ACTIVE_STATE){
printf("ERROR: Power mode should be DEVICE_PM_ACTIVE_STATE\n");
}
else{
printf("GOOD: Power mode is DEVICE_PM_ACTIVE_STATE\n");
if (ret || pm_state != DEVICE_PM_ACTIVE_STATE) {
printf("ERROR: Power mode should be DEVICE_PM_ACTIVE_STATE\n");
} else {
printf("GOOD: Power mode is DEVICE_PM_ACTIVE_STATE\n");
}

while (1){
printf("15 seconds active state\n");
k_sleep(15000);
printf("Switch device to power-down mode\n");
ret = device_set_power_state(flash_dev, DEVICE_PM_LOW_POWER_STATE);
if (ret){
printf("ERROR: Couldn't set power-down mode\n");
}
ret = device_get_power_state(flash_dev, &pm_state);
if (ret || pm_state != DEVICE_PM_LOW_POWER_STATE){
printf("ERROR: Power mode should be DEVICE_PM_LOW_POWER_STATE\n");
}
else{
printf("GOOD: Power mode is DEVICE_PM_LOW_POWER_STATE\n");
}

printf("15 seconds power-down state\n");
k_sleep(15000);
printf("Switch device to active state\n");
ret = device_set_power_state(flash_dev, DEVICE_PM_ACTIVE_STATE);
if (ret){
printf("ERROR: Couldn't set active state\n");
}
ret = device_get_power_state(flash_dev, &pm_state);
if (ret || pm_state != DEVICE_PM_ACTIVE_STATE){
printf("ERROR: Power mode should be DEVICE_PM_ACTIVE_STATE\n");
}
else{
printf("GOOD: Power mode is DEVICE_PM_ACTIVE_STATE\n");
}
while (1) {
printf("15 seconds active state\n");
k_sleep(15000);
printf("Switch device to power-down mode\n");
ret = device_set_power_state(flash_dev,
DEVICE_PM_LOW_POWER_STATE);
if (ret) {
printf("ERROR: Couldn't set power-down mode\n");
}
ret = device_get_power_state(flash_dev, &pm_state);
if (ret || pm_state != DEVICE_PM_LOW_POWER_STATE) {
printf(
"ERROR: Power mode should be DEVICE_PM_LOW_POWER_STATE\n");
} else {
printf(
"GOOD: Power mode is DEVICE_PM_LOW_POWER_STATE\n");
}

printf("15 seconds power-down state\n");
k_sleep(15000);
printf("Switch device to active state\n");
ret = device_set_power_state(flash_dev, DEVICE_PM_ACTIVE_STATE);
if (ret) {
printf("ERROR: Couldn't set active state\n");
}
ret = device_get_power_state(flash_dev, &pm_state);
if (ret || pm_state != DEVICE_PM_ACTIVE_STATE) {
printf(
"ERROR: Power mode should be DEVICE_PM_ACTIVE_STATE\n");
} else {
printf("GOOD: Power mode is DEVICE_PM_ACTIVE_STATE\n");
}
}
}

0 comments on commit 32a0a52

Please sign in to comment.