Skip to content

Commit 294e2df

Browse files
Laczennashif
authored andcommitted
flash_page_layout: refactor flash_page_get_info
refactor flash_page_get_info to simplify and to avoid using mixing the usage of an off_t (offs) and an uint32_t (page_index). Signed-off-by: Laczen JMS <[email protected]>
1 parent 9db0a18 commit 294e2df

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

drivers/flash/flash_page_layout.c

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,50 @@
77
#include <drivers/flash.h>
88

99
static int flash_get_page_info(const struct device *dev, off_t offs,
10-
bool use_addr, struct flash_pages_info *info)
10+
uint32_t index, struct flash_pages_info *info)
1111
{
1212
const struct flash_driver_api *api = dev->api;
1313
const struct flash_pages_layout *layout;
14-
size_t page_count = 0;
15-
off_t group_offs = 0;
16-
uint32_t num_in_group;
17-
off_t end = 0;
1814
size_t layout_size;
15+
uint32_t index_jmp;
16+
17+
info->start_offset = 0;
18+
info->index = 0U;
1919

2020
api->page_layout(dev, &layout, &layout_size);
2121

2222
while (layout_size--) {
23-
if (use_addr) {
24-
end += layout->pages_count * layout->pages_size;
23+
info->size = layout->pages_size;
24+
if (offs == 0) {
25+
index_jmp = index - info->index;
2526
} else {
26-
end += layout->pages_count;
27+
index_jmp = (offs - info->start_offset) / info->size;
2728
}
2829

29-
if (offs < end) {
30-
info->size = layout->pages_size;
31-
32-
if (use_addr) {
33-
num_in_group = (offs - group_offs) /
34-
layout->pages_size;
35-
} else {
36-
num_in_group = offs - page_count;
37-
}
38-
39-
info->start_offset = group_offs +
40-
num_in_group * layout->pages_size;
41-
info->index = page_count + num_in_group;
42-
30+
index_jmp = MIN(index_jmp, layout->pages_count);
31+
info->start_offset += (index_jmp * info->size);
32+
info->index += index_jmp;
33+
if (index_jmp < layout->pages_count) {
4334
return 0;
4435
}
4536

46-
group_offs += layout->pages_count * layout->pages_size;
47-
page_count += layout->pages_count;
48-
4937
layout++;
5038
}
5139

52-
return -EINVAL; /* page of the index doesn't exist */
40+
return -EINVAL; /* page at offs or idx doesn't exist */
5341
}
5442

5543
int z_impl_flash_get_page_info_by_offs(const struct device *dev, off_t offs,
5644
struct flash_pages_info *info)
5745
{
58-
return flash_get_page_info(dev, offs, true, info);
46+
return flash_get_page_info(dev, offs, 0U, info);
5947
}
6048

6149
int z_impl_flash_get_page_info_by_idx(const struct device *dev,
6250
uint32_t page_index,
6351
struct flash_pages_info *info)
6452
{
65-
return flash_get_page_info(dev, page_index, false, info);
53+
return flash_get_page_info(dev, 0, page_index, info);
6654
}
6755

6856
size_t z_impl_flash_get_page_count(const struct device *dev)

0 commit comments

Comments
 (0)