1.. zephyr:code-sample:: esp32-flash-memory-mapped
2   :name: Memory-Mapped Flash
3   :relevant-api: flash_interface
4
5   Write data into scratch area and read it using flash API and memory-mapped pointer.
6
7Overview
8********
9
10ESP32 features memory hardware which allows regions of flash memory to be mapped into instruction
11and data address spaces. This mapping works only for read operations. It is not possible to modify
12contents of flash memory by writing to a mapped memory region.
13
14Mapping happens in 64 KB pages. Memory mapping hardware can map flash into the data address space
15and the instruction address space. See the technical reference manual for more details and
16limitations about memory mapping hardware. For more information, check `ESP32 Flash Memory-Mapping`_.
17
18Supported SoCs
19**************
20
21All ESP32 SoCs support flash memory-mapped feature.
22
23Building and Running
24********************
25
26Make sure you have your board connected over USB port.
27
28.. code-block:: console
29
30   west build -b esp32s3_devkitm samples/boards/espressif/flash_memory_mapped
31   west flash
32
33Sample Output
34=============
35
36To check the output of this sample, run ``west espressif monitor`` or any other serial
37console program (e.g., minicom, putty, screen, etc).
38This example uses ``west espressif monitor``, which automatically detects the serial
39port at ``/dev/ttyUSB0``:
40
41.. code-block:: console
42
43   $ west espressif monitor
44
45The sample code erases the scratch area defined in DTS file and writes a 32-bytes data buffer in it.
46Next, it prints that region content using flash API read and also using memory-mapped pointer.
47Both readings should return the same value. Important to notice that writing using memory-mapped pointer
48is not allowed.
49
50
51.. code-block:: console
52
53  *** Booting Zephyr OS build v3.5.0-rc3-10-g3118724fa268 ***
54  [00:00:00.255,000] <inf> flash_memory_mapped: memory-mapped pointer address: 0x3c040000
55  [00:00:01.122,000] <inf> flash_memory_mapped: flash read using memory-mapped pointer
56                                                ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff |........ ........
57                                                ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff |........ ........
58  [00:00:01.122,000] <inf> flash_memory_mapped: writing 32-bytes data using flash API
59  [00:00:01.122,000] <inf> flash_memory_mapped: flash read using flash API
60                                                00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f |........ ........
61                                                10 11 12 13 14 15 16 17  18 19 1a 1b 1c 1d 1e 1f |........ ........
62  [00:00:01.122,000] <inf> flash_memory_mapped: flash read using memory-mapped pointer
63                                                00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f |........ ........
64                                                10 11 12 13 14 15 16 17  18 19 1a 1b 1c 1d 1e 1f |........ ........
65
66.. _ESP32 Flash Memory-Mapping:
67   https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/spi_flash/index.html#memory-mapping-api
68