You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@nuttx.apache.org by Martin Klang <ma...@pingdynasty.com> on 2022/09/20 09:30:09 UTC

ESP32 SPI Flash memory mapping

Hello! This is my first post here.

I have a question about memory mapping SPI flash on the ESP32.

The esp32 flash driver suggests "Consider using spi_flash_mmap()" [1]
Does anyone have an example or suggestion for how to do that?

When I try to use spi_flash_mmap() and spi_flash_munmap() as per the 
docs [2] I run into difficulties: the esp32-idf files that define these 
functions are not part of a standard NuttX build. In short, after 
including the relevant headers I end up with undefined symbol 
`spi_flash_munmap` (but spi_flash_mmap is found, surprisingly). So I 
guess I need to compile the spi-flash part of esp32-idf as a library and 
include in my NuttX build - right?

Meanwhile the NuttX codebase has its own implementation of esp32 memory 
mapping (used for reading encrypted partitions), in the functions 
esp32_mmap() and esp32_ummap() [3]. Unfortunately they are static in 
esp32_spiflash.c.

Is the NuttX memory mapping implementation compatible with 
spi_flash_mmap()? Would it not make sense to make available, in NuttX, 
an public interface to these functions?

Okay, that was more than one question! Any and all feedback or advice is 
welcome.
BTW my requirement for memory mapping is to implement XiP.

best regards,


Martin Klang


[1] 
https://github.com/apache/incubator-nuttx/blob/f49a5797213f2ec3fd348e1826db6e080e7be4d9/arch/risc-v/src/esp32c3/rom/esp32c3_spiflash.h#L934

[2] 
https://docs.espressif.com/projects/esp-idf/en/release-v3.2/api-reference/storage/spi_flash.html#memory-mapping-apis

[3] 
https://github.com/apache/incubator-nuttx/blob/master/arch/xtensa/src/esp32/esp32_spiflash.c#L1315


Re: ESP32 SPI Flash memory mapping

Posted by Martin Klang <ma...@pingdynasty.com>.
On 9/20/22 17:24, Martin Klang wrote:
> We're using the S2

umm, no, the S3!


Re: ESP32 SPI Flash memory mapping

Posted by Martin Klang <ma...@pingdynasty.com>.
Hello Gustavo, thank you for the quick reply!


On 9/20/22 16:39, Gustavo Henrique Nihei wrote:
> Welcome to the Apache NuttX mailing list!

thanks :)

>> The esp32 flash driver suggests "Consider using spi_flash_mmap()" [1]
> The "spi_flash_mmap()" and the referenced "spi_flash_read()" are ROM
> functions from the ESP32-C3 chip and their usage should be restricted to
> the chip implementation, so they should not be called from the userspace.
Okay, I see. Are there any alternative functions available in userspace?
>> BTW my requirement for memory mapping is to implement XiP.
> By default, the NuttX application firmware images for every supported chip
> of the ESP32 family runs from the External Flash, so XIP is already
> supported.

Okay yes, understood. What I'm trying to do is to store a WASM AoT image 
on flash, and then run it using XiP in WAMR. I've already got the 
physical flash address, and I need to map, and unmap, to virtual memory.

> If by supporting XIP you mean dynamically load executable code from
> External Flash, then ROMFS support may be what you are looking for.
> https://cwiki.apache.org/confluence/display/NUTTX/RAM+Disks+and+ROM+Disks

Yes that's pretty much it, but I need to both save to and load from 
flash, dynamically.

We considered using an existing file system but because we need to 
guarantee contiguous memory and (probably) 64kb alignment we discarded 
that idea. So instead we have implemented our own simple, slot-based 
flash driver. Storing to (encrypted) flash is working fine, reading back 
into a buffer is also no problem, but getting a memory mapped pointer to 
the data is proving a little challenging.

Since the reading back part already involves memory mapping inside 
esp32_spiflash.c it seems it shouldn't be impossible. I just can't see 
quite how.

> It seems you are working with an ESP32-C3-based platform, so you may take a
> look at these defconfigs that rely on the ROMFS support for achieving
> different purposes:
> https://github.com/apache/incubator-nuttx/tree/master/boards/risc-v/esp32c3/esp32c3-devkit/configs/elf
> https://github.com/apache/incubator-nuttx/tree/master/boards/risc-v/esp32c3/esp32c3-devkit/configs/module
> https://github.com/apache/incubator-nuttx/tree/master/boards/risc-v/esp32c3/esp32c3-devkit/configs/romfs

We're using the S2, I just included the wrong link in my email, sorry! 
The config should be quite similar though. I will have a close look at 
your references, thank you again.


best regards,


Martin


Re: ESP32 SPI Flash memory mapping

Posted by Gustavo Henrique Nihei <gh...@gmail.com>.
Hi Martin,

> Hello! This is my first post here.

Welcome to the Apache NuttX mailing list!

> The esp32 flash driver suggests "Consider using spi_flash_mmap()" [1]

The "spi_flash_mmap()" and the referenced "spi_flash_read()" are ROM
functions from the ESP32-C3 chip and their usage should be restricted to
the chip implementation, so they should not be called from the userspace.

> BTW my requirement for memory mapping is to implement XiP.

By default, the NuttX application firmware images for every supported chip
of the ESP32 family runs from the External Flash, so XIP is already
supported.
In fact, there is an option for running the entire firmware image from the
Internal SRAM, which is disabled by default:
https://github.com/apache/incubator-nuttx/blob/f49a5797213f2ec3fd348e1826db6e080e7be4d9/arch/xtensa/src/esp32/Kconfig#L257-L262


If by supporting XIP you mean dynamically load executable code from
External Flash, then ROMFS support may be what you are looking for.
https://cwiki.apache.org/confluence/display/NUTTX/RAM+Disks+and+ROM+Disks

It seems you are working with an ESP32-C3-based platform, so you may take a
look at these defconfigs that rely on the ROMFS support for achieving
different purposes:
https://github.com/apache/incubator-nuttx/tree/master/boards/risc-v/esp32c3/esp32c3-devkit/configs/elf
https://github.com/apache/incubator-nuttx/tree/master/boards/risc-v/esp32c3/esp32c3-devkit/configs/module
https://github.com/apache/incubator-nuttx/tree/master/boards/risc-v/esp32c3/esp32c3-devkit/configs/romfs

Best regards,
Gustavo.

On Tue, Sep 20, 2022 at 6:30 AM Martin Klang <ma...@pingdynasty.com> wrote:

> Hello! This is my first post here.
>
> I have a question about memory mapping SPI flash on the ESP32.
>
> The esp32 flash driver suggests "Consider using spi_flash_mmap()" [1]
> Does anyone have an example or suggestion for how to do that?
>
> When I try to use spi_flash_mmap() and spi_flash_munmap() as per the
> docs [2] I run into difficulties: the esp32-idf files that define these
> functions are not part of a standard NuttX build. In short, after
> including the relevant headers I end up with undefined symbol
> `spi_flash_munmap` (but spi_flash_mmap is found, surprisingly). So I
> guess I need to compile the spi-flash part of esp32-idf as a library and
> include in my NuttX build - right?
>
> Meanwhile the NuttX codebase has its own implementation of esp32 memory
> mapping (used for reading encrypted partitions), in the functions
> esp32_mmap() and esp32_ummap() [3]. Unfortunately they are static in
> esp32_spiflash.c.
>
> Is the NuttX memory mapping implementation compatible with
> spi_flash_mmap()? Would it not make sense to make available, in NuttX,
> an public interface to these functions?
>
> Okay, that was more than one question! Any and all feedback or advice is
> welcome.
> BTW my requirement for memory mapping is to implement XiP.
>
> best regards,
>
>
> Martin Klang
>
>
> [1]
>
> https://github.com/apache/incubator-nuttx/blob/f49a5797213f2ec3fd348e1826db6e080e7be4d9/arch/risc-v/src/esp32c3/rom/esp32c3_spiflash.h#L934
>
> [2]
>
> https://docs.espressif.com/projects/esp-idf/en/release-v3.2/api-reference/storage/spi_flash.html#memory-mapping-apis
>
> [3]
>
> https://github.com/apache/incubator-nuttx/blob/master/arch/xtensa/src/esp32/esp32_spiflash.c#L1315
>
>

-- 
Gustavo Henrique Nihei