drivers/periph_common/flashpage: add offset and length to API#22248
drivers/periph_common/flashpage: add offset and length to API#22248fabian18 wants to merge 4 commits into
Conversation
crasbe
left a comment
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
I don't try to be fancy :)
This should catch overflow.
There was a problem hiding this comment.
Well yes, but the only case where the condition wouldn't be met, is when len is zero.
There was a problem hiding this comment.
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.
| * @return FLASHPAGE_OK if data in the RWWEE page is identical to @p data | ||
| * @return FLASHPAGE_NOMATCH if data and RWWEE page content diverge |
There was a problem hiding this comment.
| * @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 👀
There was a problem hiding this comment.
| od_hex_dump(addr, len, LINE_LEN); |
Unrelated static-test warning fix.
There was a problem hiding this comment.
| /* Make each page slightly different by changing starting char for easier comparison by eye */ | |
| fill += (page % ('z' - 'a')); |
Unrelated comment style fix.
There was a problem hiding this comment.
Now that we have offset and length, wouldn't it make sense to also test that specifically?
The test still reads a whole page 🤔
| * @return FLASHPAGE_OK if data in the page is identical to @p data | ||
| * @return FLASHPAGE_NOMATCH if data and page content diverge |
There was a problem hiding this comment.
| * @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 |
There was a problem hiding this comment.
| * @retval FLASHPAGE_OK on success | |
| * @retval FLASHPAGE_NOMATCH if data and page content diverge |
There was a problem hiding this comment.
| * @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. |
There was a problem hiding this comment.
| * @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.
kfessel
left a comment
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
why keep the name if it no longer reads the entire flash page?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 introducingflashpage_offset_read()requires noone to adapt, but for me counts as API overengineering
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
| * @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 |
Contribution description
Adds
offsetandlengthparameters toflashpage_read()andflashpage_verify()API.Testing procedure
BOARD=same54-xpro make -C tests/periph/flashpage flash termIssues/PRs references
Declaration of AI-Tools / LLMs usage:
AI-Tools / LLMs that were used are: