Skip to content

Conversation

@yonsch
Copy link
Contributor

@yonsch yonsch commented Jul 14, 2022

Add support for flash read/write/erase on rpi_pico. Tested with the flash_shell sample.
Note: Writes are currently inefficient, as they are performed on a page basis. This is because pico-sdk does not currently provide an API for writing smaller ranges.

@Laczen
Copy link
Contributor

Laczen commented Jul 14, 2022

@yonsch, why don't you use a write block size of 256 byte. This will limit the possibilities of using the flash but it will be correct and avoid unnecessary erases when trying to emulate 1 byte writing support. If you do wish to support 1 byte writing it should be required to read whatever is on the flash (instead of setting it to the erase value), apply the changes, and write to flash.

@Laczen
Copy link
Contributor

Laczen commented Jul 14, 2022

@yonsch, is it safe to write to flash when executing from the same flash?

Copy link
Contributor

Choose a reason for hiding this comment

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

remove label, its no longer needed.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

make the precedence explicit:

(offset + size) > FLASH_SIZE;

Copy link
Member

Choose a reason for hiding this comment

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

MISRA C:2004, 13.2 - Tests of a value against zero should be made explicit, unless the operand is effectively Boolean.

Copy link
Member

Choose a reason for hiding this comment

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

remove

Copy link
Member

Choose a reason for hiding this comment

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

can be made into one line, the col limit is 100

Copy link
Member

Choose a reason for hiding this comment

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

might be better to test if size > 0 as well:
MISRA C:2004, 13.2 - Tests of a value against zero should be made explicit, unless the operand is effectively Boolean.

@yonsch
Copy link
Contributor Author

yonsch commented Aug 21, 2022

@Laczen

why don't you use a write block size of 256 byte

I wanted this to be future compatible for when this feature will be available, but it turns out that the flash works with page writes, so there is no way around a write size of 256.

is it safe to write to flash when executing from the same flash?

Yes, because the relevant functions are executed from RAM. Disabling interrupts was also added to prevent hard faults when ISRs are called during flash operations.

@yonsch yonsch force-pushed the pico_flash branch 3 times, most recently from 85f9239 to f36b2a9 Compare August 21, 2022 20:30
Copy link
Contributor

Choose a reason for hiding this comment

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

depends on DT_HAS_RASPBERRYPI_PICO_FLASH_CONTROLLER_ENABLED should replace the 'depends on FLASH && SOC_SERIES_RP2XXXX`

@yonsch yonsch force-pushed the pico_flash branch 3 times, most recently from 098a647 to d888f3c Compare September 5, 2022 22:07
@yonsch
Copy link
Contributor Author

yonsch commented Sep 6, 2022

Added "real" byte-aligned writes. The low level functions were taken from the SDK and bootrom, and adapted to Zephyr (functionally and stylistically). I suppose this is fine in terms of licensing (the sdk is BSD-3 Clause), but would like someone to verify this.

@Laczen
Copy link
Contributor

Laczen commented Sep 6, 2022

Added "real" byte-aligned writes. The low level functions were taken from the SDK and bootrom, and adapted to Zephyr (functionally and stylistically). I suppose this is fine in terms of licensing (the sdk is BSD-3 Clause), but would like someone to verify this.

@yonsch, regarding the "real" byte-aligned writes: it is strange that the write-block-size is set to the page size while the routines allow smaller writes, is this intended ? I didn't find the _write_partial routines, but do they work by reading, modifying and writing back ? If they do it might be a good idea to set the write-block-size to 16, this will enable support for most flash usage, but still keep the amount of rewrites reduced.

Copy link
Contributor

Choose a reason for hiding this comment

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

Write block size is given as 1 in the devicetree but as 256 here?

@yonsch
Copy link
Contributor Author

yonsch commented Nov 6, 2022

@soburi Thank you for your review. I replaced XIP_BASE with FLASH_BASE, which is based on devicetree. However, I did not touch XIP_SSI_BASE, since I'm not sure it should be exposed to the devicetree. The user shouldn't really play with the SSI, and it's only used through this driver (this particular snippet is from the HAL, so it should be okay). I don't think we should create a compatible for the SSI just for this instance.

@yonsch yonsch requested review from de-nordic and soburi November 6, 2022 21:20
@yonsch yonsch force-pushed the pico_flash branch 2 times, most recently from c298b34 to ff5aec5 Compare November 13, 2022 18:47
@carlescufi
Copy link
Member

@soburi please approve if happy

@carlescufi carlescufi requested a review from lgehreke November 14, 2022 12:29
@yonsch yonsch requested review from de-nordic and lgehreke and removed request for lgehreke November 14, 2022 15:51
de-nordic
de-nordic previously approved these changes Nov 14, 2022
Copy link
Contributor

@de-nordic de-nordic left a comment

Choose a reason for hiding this comment

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

Look OK. Thank you for the contribution.

Note on the PAGE_WRITE_SIZE and SECTOR_ERASE_SIZE: although I prefer the verb WRITE/ERASE within the name of the identifier, to clarify what the identifier is supposed to do rather than magic interpretation of sector as erase unit and page as write unit, I understand that this is this is how the device specification names them. If it will be desired by you or other reviewers to revert the names to original, I will approve the code anyway.

@Laczen
Copy link
Contributor

Laczen commented Nov 14, 2022

@yonsch, the usage of PAGE_WRITE_SIZE and SECTOR_ERASE_SIZE introduce yet another terminology that only adds to the confusion. If possible I would like you to revert this to PAGE_SIZE and SECTOR_SIZE that can be easily found in the device specifications.

Add a flash driver for the rpi_pico

Signed-off-by: Yonatan Schachter <[email protected]>
Add flash controller support for Raspberry Pi's RP2040 SoC

Signed-off-by: Yonatan Schachter <[email protected]>
Add flash support for the rpi_pico board

Signed-off-by: Yonatan Schachter <[email protected]>
Copy link
Contributor

@Laczen Laczen left a comment

Choose a reason for hiding this comment

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

Thanks

@fabiobaltieri fabiobaltieri merged commit df3eff5 into zephyrproject-rtos:main Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Devicetree Binding PR modifies or adds a Device Tree binding area: Devicetree area: Flash platform: Raspberry Pi Pico Raspberry Pi Pico (RPi Pico)

Projects

None yet

Development

Successfully merging this pull request may close these issues.