Skip to content

drivers/periph_common/flashpage: add offset and length to API#22248

Open
fabian18 wants to merge 4 commits into
RIOT-OS:masterfrom
fabian18:pr/drivers_flashpage_offset_length
Open

drivers/periph_common/flashpage: add offset and length to API#22248
fabian18 wants to merge 4 commits into
RIOT-OS:masterfrom
fabian18:pr/drivers_flashpage_offset_length

Conversation

@fabian18
Copy link
Copy Markdown
Contributor

@fabian18 fabian18 commented May 6, 2026

Contribution description

Adds offset and length parameters to flashpage_read() and flashpage_verify() API.

Testing procedure

BOARD=same54-xpro make -C tests/periph/flashpage flash term

Type '/exit' to exit.
help
2026-05-06 15:59:08,235 # help
2026-05-06 15:59:08,237 # Command              Description
2026-05-06 15:59:08,241 # ---------------------------------------
2026-05-06 15:59:08,245 # info                 Show information about pages
2026-05-06 15:59:08,250 # dump                 Dump the selected page to STDOUT
2026-05-06 15:59:08,255 # dump_local           Dump the local page buffer to STDOUT
2026-05-06 15:59:08,263 # read                 Copy the given page to the local page buffer and dump to STDOUT
2026-05-06 15:59:08,268 # write                Write the local page buffer to the given page
2026-05-06 15:59:08,274 # write_raw            Write raw bytes (max 64B) to the given address
2026-05-06 15:59:08,279 # erase                Erase the given page buffer
2026-05-06 15:59:08,284 # edit                 Write bytes to the local page buffer
2026-05-06 15:59:08,288 # test                 Write and verify test pattern
2026-05-06 15:59:08,295 # test_last_pagewise   Write and verify test pattern on last page available
2026-05-06 15:59:08,301 # test_reserved_pagewise Write and verify short write on reserved page
2026-05-06 15:59:08,307 # test_last_raw        Write and verify raw short write on last page available
2026-05-06 15:59:08,313 # dump_config_page     Dump the content of the MCU configuration page
2026-05-06 15:59:08,319 # test_config_page     Test writing config page. (!DANGER ZONE!)
> test
2026-05-06 15:59:18,311 # test
2026-05-06 15:59:18,313 # usage: test <page>
> test 22
2026-05-06 15:59:24,975 # test 22
2026-05-06 15:59:25,031 # wrote local page buffer to flash page 22 at addr 0x0002c000
> test_last_pagewise
2026-05-06 15:59:40,386 # test_last_pagewise
2026-05-06 15:59:40,456 # wrote local page buffer to last flash page
> test_reserved_pagewise
2026-05-06 15:59:54,033 # test_reserved_pagewise
2026-05-06 15:59:54,035 # Reserved page num: 4 
2026-05-06 15:59:54,041 # Since the last firmware update this test has been run 2 times 
2026-05-06 15:59:54,109 # wrote local page buffer to reserved flash page
2026-05-06 15:59:54,109 # 
2026-05-06 15:59:54,120 # When running on a bootloader, as an extra check, try restarting the board and check whether this application still comes up.
> test_last_raw
2026-05-06 16:00:13,445 # test_last_raw
2026-05-06 16:00:13,491 # wrote raw short buffer to last flash page

Issues/PRs references

Declaration of AI-Tools / LLMs usage:

AI-Tools / LLMs that were used are:

  • none

@github-actions github-actions Bot added Area: tests Area: tests and testing framework Area: drivers Area: Device drivers labels May 6, 2026
@crasbe crasbe added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. labels May 6, 2026
@riot-ci
Copy link
Copy Markdown

riot-ci commented May 6, 2026

Murdock results

✔️ PASSED

42f817e tests/periph/flashpage: test page offset read

Success Failures Total Runtime
11108 0 11108 12m:34s

Artifacts

Copy link
Copy Markdown
Contributor

@crasbe crasbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for highjacking your PR for unrelated style fixes 😅

void flashpage_read(unsigned page, void *data, size_t offset, size_t len)
{
assert(page < FLASHPAGE_NUMOF);
assert(offset + len <= flashpage_size(page) && offset + len > offset);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't offset + len > offset just a fancy way of writing len > 0 or len != 0, considering size_t is an unsigned integer under the hood?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't try to be fancy :)
This should catch overflow.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well yes, but the only case where the condition wouldn't be met, is when len is zero.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am not mistaken, than for example (10 + SIZE_MAX) overflows to 9. 9 is less than page size (8K for same54-xpro), but the second check 9 > 10 catches the overflow.

Comment thread drivers/include/periph/flashpage.h Outdated
Comment on lines 412 to 413
* @return FLASHPAGE_OK if data in the RWWEE page is identical to @p data
* @return FLASHPAGE_NOMATCH if data and RWWEE page content diverge
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return FLASHPAGE_OK if data in the RWWEE page is identical to @p data
* @return FLASHPAGE_NOMATCH if data and RWWEE page content diverge
* @retval FLASHPAGE_OK if data in the RWWEE page is identical to @p data
* @retval FLASHPAGE_NOMATCH if data and RWWEE page content diverge

Unrelated to your changes, but while we're here 👀

Comment thread tests/periph/flashpage/main.c Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
od_hex_dump(addr, len, LINE_LEN);

Unrelated static-test warning fix.

Comment thread tests/periph/flashpage/main.c Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Make each page slightly different by changing starting char for easier comparison by eye */
fill += (page % ('z' - 'a'));

Unrelated comment style fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have offset and length, wouldn't it make sense to also test that specifically?
The test still reads a whole page 🤔

Comment thread drivers/include/periph/flashpage.h Outdated
Comment on lines 291 to 292
* @return FLASHPAGE_OK if data in the page is identical to @p data
* @return FLASHPAGE_NOMATCH if data and page content diverge
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return FLASHPAGE_OK if data in the page is identical to @p data
* @return FLASHPAGE_NOMATCH if data and page content diverge
* @retval FLASHPAGE_OK if data in the page is identical to @p data
* @retval FLASHPAGE_NOMATCH if data and page content diverge

Comment thread drivers/include/periph/flashpage.h Outdated
Comment on lines 305 to 306
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @retval FLASHPAGE_OK on success
* @retval FLASHPAGE_NOMATCH if data and page content diverge

Comment thread drivers/include/periph/flashpage.h Outdated
Comment on lines 427 to 428
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @retval FLASHPAGE_OK on success
* @retval FLASHPAGE_NOMATCH if data and RWWEE page content diverge

* @param[out] data memory to write the page to, MUST be FLASHPAGE_SIZE
* byte
* @param[in] offset offset in the page to start reading from
* @param[in] len length of the data to be read.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param[in] len length of the data to be read.
* @param[in] len length of the data to be read (must be bigger than 0).

If my assumption in the previous comment is correct, this would be good to add IMO.

Copy link
Copy Markdown
Contributor

@kfessel kfessel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why the functions keep their name if they are no longer page bound

* @pre @p offset + @p len MUST be less than or equal to FLASHPAGE_SIZE.
*/
void flashpage_read(unsigned page, void *data);
void flashpage_read(unsigned page, void *data, size_t offset, size_t len);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why keep the name if it no longer reads the entire flash page?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is not fundamentally wrong. Neither does flashpage_write necessarily write a full page.
It depends on whether we want to offer 2 APIs to get around API change and people not having to adapt to that.
Or have 1 API which is more compact. I don't like to read large headers and figure out which function to use.
We have some functions which are called _read_byte, when there is a general read, which I find is a bit overengineered. The nightmare of that coap_. So on personal preference I simply chose to extend the existing API, instead of introducing a new one.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flashpage_write seems to be to the lower level API point - it does not even take a page as input

if write and read would share a naming scheme the original read should have been named flashpage_read_page (see flashpage_write_page for comparison) but in that case this function should be void flashpage_read(void *target_addr, const void *data, size_t len);

similar for _read_byte functions these are often the result of a lower level that provides byte based access and than a higher level is put on top that basically loops for how many bytes should be read.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I would call overengineering.

uint8_t eeprom_read_byte(uint32_t pos)
{
    uint8_t byte;
    eeprom_read(pos, &byte, 1);
    return byte;
}

But _read_byte functions are not the main topic here. There is still nothing fundamentally wrong.
What do you want?

  • Having flashpage_read_page() to read a full page requires people to adapt and having this PR's API change also requires people to adapt.
  • Keeping flashpage_read() as is, and introducing flashpage_offset_read() requires noone to adapt, but for me counts as API overengineering

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flashpage_offset_read would be my preference to cause less api break downstream

* @retval FLASHPAGE_NOMATCH if data and page content diverge
*/
int flashpage_verify(unsigned page, const void *data);
int flashpage_verify(unsigned page, const void *data, size_t offset, size_t len);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why keep the name if it no longer ensures the entire flash page matches

void flashpage_read(unsigned page, void *data, size_t offset, size_t len);

/**
* @brief Verify the given page against the given data
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @brief Verify the given page against the given data
* @brief Verify the given page or half a page or two pages or ... against the given data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: drivers Area: Device drivers Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants