Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stm32 with external NOR ospi flash in MemoryMapped mode #61082

Closed
wants to merge 7 commits into from

Conversation

FRASTM
Copy link
Collaborator

@FRASTM FRASTM commented Aug 2, 2023

This PR is enabling the MemoryMapped to access the octo NOR flash at the end of the initialization.
With this configuration, the NOR flash access is done with memcopy in read or write
Note that the erase is not supported but writing is possible in MemoryMapped mode.
(It is not recommended to fill the memory with 0xFFFFFFFF)

The drivers detects the CONFIG_STM32_MEMMAP flag to enable the MemoryMapped mode.

@FRASTM
Copy link
Collaborator Author

FRASTM commented Aug 2, 2023

reading flash of the b_u585i_iot02a in memory mapped mode (@ 0x70000000) :

Data read : 
00000000 read 3d
00000001 read b8
00000002 read f3
00000003 read 96
[00:00:00.140,000] <inf> flash_stm32_ospi: OSPI flash config is OPI / DTR
[00:00:00.140,000] <inf> flash_stm32_ospi: Read SFDP from octoFlash
[00:00:00.140,000] <inf> flash_stm32_ospi: Read SFDP from octoFlash
NOR in MemoryMapped mode 

@FRASTM FRASTM force-pushed the stm32u5_ospi_memmap branch 6 times, most recently from eb0bd14 to 317d714 Compare August 2, 2023 16:23
@FRASTM FRASTM force-pushed the stm32u5_ospi_memmap branch 3 times, most recently from 79e0958 to 93b0493 Compare August 7, 2023 13:11
@zephyrbot zephyrbot added manifest manifest-hal_stm32 DNM This PR should not be merged (Do Not Merge) labels Aug 7, 2023
@zephyrbot
Copy link
Collaborator

zephyrbot commented Aug 7, 2023

The following west manifest projects have been modified in this Pull Request:

Name Old Revision New Revision Diff

Note: This message is automatically posted and updated by the Manifest GitHub Action.

@FRASTM
Copy link
Collaborator Author

FRASTM commented Aug 7, 2023

rebase on 4d304a2
take the ../modules/hal/stm32/stm32cube/ PR 178

@FRASTM FRASTM force-pushed the stm32u5_ospi_memmap branch 2 times, most recently from 974a6a0 to b8a77b6 Compare August 11, 2023 14:35
@FRASTM FRASTM added the platform: STM32 ST Micro STM32 label Aug 11, 2023
@FRASTM
Copy link
Collaborator Author

FRASTM commented Aug 11, 2023

rebase on e9dd938

@FRASTM FRASTM force-pushed the stm32u5_ospi_memmap branch from b8a77b6 to 9dae609 Compare September 6, 2023 09:26
@FRASTM
Copy link
Collaborator Author

FRASTM commented Sep 6, 2023

New version where the HAL is not changed thanks to direct access to the hospi instance in the flash_stm32_ospi driver
The flash base addresse and size are directly placed in the flash-controller node of the Device Tree : reg = <0x70000000 DT_SIZE_M(64)>; (no more size property required)

Using a sample to demonstrate (adapted from samples/drivers/spi_flash) to read operation in memorymapped mode (no erase sector).
Requires the CONFIG_BOOT_DIRECT_XIP=y when compiling the mcuboot

Tested with the b_u585i_iot02a disco board ;

*** Booting Zephyr OS build zephyr-v3.4.0-3121-g4df7ebf34d86 ***
I: Starting Direct-XIP bootloader
I: Image 0 Primary slot: Image not found
I: Secondary slot: version=0.0.0+0
I: Image 0 loaded from the secondary slot
I: Bootloader chainload address offset: 0x0
�*** Booting Zephyr OS build zephyr-v3.4.0-3121-g4df7ebf34d86 ***
Hello World! from external flash  b_u585i_iot02a

@FRASTM FRASTM changed the title drivers: stm32u5 ospi flash driver enables the MemoryMapped mode drivers: stm32 ospi flash driver enables the MemoryMapped mode Sep 6, 2023
@FRASTM FRASTM force-pushed the stm32u5_ospi_memmap branch 3 times, most recently from 5b11367 to 66e6a1b Compare September 6, 2023 13:55
@FRASTM FRASTM changed the title drivers: stm32 ospi flash driver enables the MemoryMapped mode stm32u5 with external NOR ospi flash in MemoryMapped mode Sep 6, 2023
@de-nordic
Copy link
Collaborator

This PR is enabling the MemoryMapped to access the octo NOR flash at the end of the initialization. With this configuration, the NOR flash access is done with memcopy in read or write Note that the erase is not supported but writing is possible in MemoryMapped mode. (It is not recommended to fill the memory with 0xFFFFFFFF)

Maybe this could be useful here #62228.

@Laczen
Copy link
Collaborator

Laczen commented Sep 12, 2023

@FRASTM, thanks for the PR.

Is my conclusion correct that when using XIP and memory mapped mode there is no part of the flash device that can be used by a fs (that always need an erase) ?

@Laczen
Copy link
Collaborator

Laczen commented Dec 12, 2023

When the CONFIG_STM32_MEMMAP is set this version adds an Abort function to resume the MemoryMapped mode, so that erasing is possible and restore the MemoryMapped mode after completion Note that flash read and write are using memcpy.

Nice, if write works with memcpy that is OK. Is it not needed to disable irq's when memory mapped mode is aborted (would there be irq code that is not available during the erase) ?

@FRASTM FRASTM force-pushed the stm32u5_ospi_memmap branch 3 times, most recently from c39d778 to 4397fa5 Compare January 23, 2024 11:34
@FRASTM FRASTM force-pushed the stm32u5_ospi_memmap branch 2 times, most recently from e877694 to bea3951 Compare January 23, 2024 11:43
@FRASTM
Copy link
Collaborator Author

FRASTM commented Jan 23, 2024

Rebase on d7873e2

Requires MCUboot PR mcu-tools/mcuboot#1891 where the mcuboot is built/linked/stored at the boot_partition in internal mcu flash ( 0x8000000)

Add an example samples/subsys/fs/littlefs/ where the application is running in external NOR memory (XiP) and where the lfs1 partition is in internal MCU memory.

The slot0 where to execute the application is located in external flash memory and chosen as zephyr,code-partition = &slot0_partition; (slot0 partition is located in the external flash, offset 0)

  1. building/linking The sample application with

west build -p always -b b_u585i_iot02a samples/subsys/fs/littlefs/ --sysbuild --
-DCONFIG_FLASH_BASE_ADDRESS=0x70000000
-DSB_CONFIG_BOOTLOADER_MCUBOOT=y

  1. sign The sample application with

west sign -t imgtool -- --key ../bootloader/mcuboot/root-rsa-2048.pem

  1. load sample application with STM32CubeProgrammer at address of the external flash slot1

~/STMicroelectronics/STM32CubeProgrammer/bin/STM32_Programmer_CLI -c port=swd -el ~/STMicroelectronics/STM32CubeProgrammer/bin/ExternalLoader/MX25LM51245G_STM32U585I-IOT02A.stldr -d build/littlefs/zephyr/zephyr.signed.bin 0x70000000 -rst

  • Note that 0x70000000 is the address of the external flash where image is stored (slo0at offset 0, or offset of slot1)
	chosen {
		zephyr,flash = &mt25ql512ab1;
		zephyr,code-partition = &slot0_partition;
	};
  • Note that the mcuboot is built/linked/flashed for (internal MCU) address 0x8000000 (no image-0/1 required)

@FRASTM FRASTM force-pushed the stm32u5_ospi_memmap branch from bea3951 to fb86e36 Compare January 23, 2024 12:33
@FRASTM
Copy link
Collaborator Author

FRASTM commented Jan 23, 2024

*** Booting Zephyr OS build zephyr-v3.5.0-3956-gbea3951f1683 ***
I: Starting Direct-XIP bootloader
I: Image 0 Primary slot: Image not found
I: Secondary slot: version=0.0.0+0
I: Image 0 loaded from the secondary slot
I: Bootloader chainload address offset: 0x100000
I: Jumping to the image slot
I: NOR init'd in MemMapped mode

I: littlefs partition at /lfs1
I: LittleFS version 2.5, disk version 2.0
I: FS at flash-controller@40022000:0xd0000 is 8 0x2000-byte blocks with 512 cyce
I: sizes: rd 256 ; pr 256 ; ca 4096 ; la 256
I: Automount /lfs1 succeeded
*** Booting Zephyr OS build zephyr-v3.5.0-3956-gbea3951f1683 ***
Sample program to r/w files on littlefs
Area 0 at 0xd0000 on flash-controller@40022000 for 65536 bytes
/lfs1 automounted
/lfs1: bsize = 256 ; frsize = 8192 ; blocks = 8 ; bfree = 6
                                                                                
Listing dir /lfs1 ...                                                           
[FILE] boot_count (size = 1)                                                    
[FILE] pattern.bin (size = 547)                                                 
/lfs1/boot_count read count:4 (bytes: 1)                                        
/lfs1/boot_count write new boot count 5: [wr:1]                                 
------ FILE: /lfs1/pattern.bin ------                                           
05 aa aa aa aa aa aa aa 06 aa aa aa aa aa aa aa                                 
...                             
                                                                
I: /lfs1 unmounted                                                              
/lfs1 unmount: 0  

@FRASTM
Copy link
Collaborator Author

FRASTM commented Jan 23, 2024

When the CONFIG_STM32_MEMMAP is set this version adds an Abort function to resume the MemoryMapped mode, so that erasing is possible and restore the MemoryMapped mode after completion Note that flash read and write are using memcpy.

Nice, if write works with memcpy that is OK. Is it not needed to disable irq's when memory mapped mode is aborted (would there be irq code that is not available during the erase) ?

Write is not really guaranteed. in memorymapped mode : with stm32u585, the data written (with memcopy) and read back (in memcopy) are correct but do not seem persistent in flash !?

@Laczen
Copy link
Collaborator

Laczen commented Jan 23, 2024

@FRASTM, your commit comments are still showing that erase and write is not supported, is this still the case ? If erase works, can't you use the same methodology (go out of memmap, do erase, return to memmap) for the write ?

@de-nordic
Copy link
Collaborator

de-nordic commented Jan 23, 2024

stm32u585

https://www.st.com/resource/en/reference_manual/rm0456-stm32u5-series-armbased-32bit-mcus-stmicroelectronics.pdf

RM0456 Rev 5 1061/3637
RM0456 Octo-SPI interface (OCTOSPI)
1095
28.4.11 OCTOSPI memory-mapped mode:

"It is not recommended to program the flash memory using the memory-mapped writes:
the indirect-write mode fulfills this operation."

Page 1064 explains more.

Reads/writes to OSPI connected, memory-mapped, device will be probably cached, so unless you invalidate cache and force OQSPI to re-read the area then you will not know whether data has been written correctly, as it seems that there is no auto-erase on writes, writes are probably not verified.

The reservations are only for flash, so I guess that SRAM connected device will work fine, but that kind device changes bits in any to any transition, while flash in any to 0 only.

@FRASTM FRASTM force-pushed the stm32u5_ospi_memmap branch from fb86e36 to 5e5ed7d Compare January 23, 2024 14:56
@FRASTM
Copy link
Collaborator Author

FRASTM commented Jan 24, 2024

@FRASTM, your commit comments are still showing that erase and write is not supported, is this still the case ? If erase works, can't you use the same methodology (go out of memmap, do erase, return to memmap) for the write ?

Yes it works : erasing/writing the NOR octo-flash after aborting the mem map mode.

@FRASTM FRASTM force-pushed the stm32u5_ospi_memmap branch from 5e5ed7d to a25327c Compare January 24, 2024 11:58
@FRASTM
Copy link
Collaborator Author

FRASTM commented Jan 24, 2024

adding a commit to abort the memorymapped mode before erasing or writing to the external NOR octoflash

For the flash driver, the base address is the MCU internal flash
address (usualyy 0x8000000). This PR gets the that address
from the device tree node "st,stm32-nv-flash"
instead of relying on the CONFIG_FLASH_BASE_ADDRESS
which might differ when building for another flash memory.

Signed-off-by: Francois Ramu <francois.ramu@st.com>
This CONFIG_STM32_MEMMAP is for enabling the MemoryMapped mode
on external octo or quad spi memory.
In this case, the flash_stm32_read is done in mem map mode
the flash_stm32_erase is not available.

Signed-off-by: Francois Ramu <francois.ramu@st.com>
Enable the MemoryMapped Mode for the stm32 octoFlash driver
Configure the Flash in MemoryMapped to use in XiP mode.
With this mode the erase and write are not supported.
Address and size are given by the DTS register property.

Signed-off-by: Francois Ramu <francois.ramu@st.com>
This change is aborting the memoryMapped mode of the octo-flash
before erasing or writing the NOR. Operations are performed in
command mode.
Reading is always performed in MemoryMapped mode (memcopy)

Signed-off-by: Francois Ramu <francois.ramu@st.com>
Enable the DCACHE1 in INCR burt mode to allow writing to the external
NOR octoFlash when in MemoryMapped mode

Signed-off-by: Francois Ramu <francois.ramu@st.com>
Define the Device tree of the b_u585i_iot02a disco kit
to access the external NOR octo-flash in MemoryMapped mode
for XiP

Signed-off-by: Francois Ramu <francois.ramu@st.com>
Gives a sample to execute the little fs on external memory map
(XiP) where the lfs1 partition is in internal mcu flash
The application is built/linked/stored in the external NOR
flash on slot1 partition.

Signed-off-by: Francois Ramu <francois.ramu@st.com>
@FRASTM
Copy link
Collaborator Author

FRASTM commented Feb 20, 2024

The PR #68597 is now replacing this PR

@FRASTM FRASTM closed this Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform: STM32 ST Micro STM32
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants