1.. zephyr:code-sample:: soc-flash-nrf
2   :name: nRF SoC Internal Storage
3   :relevant-api: flash_interface flash_area_api
4
5   Use the flash API to interact with the SoC flash.
6
7Overview
8********
9
10This sample demonstrates using the :ref:`Flash API <flash_api>` on an SoC internal storage.
11The sample uses :ref:`Flash map API <flash_map_api>` to obtain a device that has one
12partition defined with the label ``storage_partition``, then uses :ref:`Flash API <flash_api>`
13to directly access and modify the contents of a device within the area defined for said
14partition.
15
16Within the sample, user may observe how read/write/erase operations
17are performed on a device, and how to first check whether device is
18ready for operation.
19
20Building and Running
21********************
22
23The sample will be built for any SoC with internal storage, as long as
24there is a fixed-partition named ``storage_partition`` defined
25on that internal storage.
26
27.. zephyr-app-commands::
28   :zephyr-app: samples/drivers/soc_flash_nrf
29   :board: nrf52840dk/nrf52840
30   :goals: build flash
31   :compact:
32
33Sample Output
34=============
35
36.. code-block:: console
37
38   *** Booting Zephyr OS build v2.7.99-17621-g54832687bcbb ***
39
40   Nordic nRF5 Internal Storage Sample
41   ===================================
42
43   Test 1: Flash erase page at 0x82000
44      Flash erase succeeded!
45
46   Test 2: Flash write (word array 1)
47      Attempted to write 1122 at 0x82000
48      Attempted to read 0x82000
49      Data read: 1122
50      Data read matches data written. Good!
51      Attempted to write aabb at 0x82004
52      Attempted to read 0x82004
53      Data read: aabb
54      Data read matches data written. Good!
55      Attempted to write abcd at 0x82008
56      Attempted to read 0x82008
57      Data read: abcd
58      Data read matches data written. Good!
59      Attempted to write 1234 at 0x8200c
60      Attempted to read 0x8200c
61      Data read: 1234
62      Data read matches data written. Good!
63
64   Test 3: Flash erase (2 pages at 0x80000)
65      Flash erase succeeded!
66
67   Test 4: Flash write (word array 2)
68      Attempted to write 1234 at 0x82000
69      Attempted to read 0x82000
70      Data read: 1234
71      Data read matches data written. Good!
72      Attempted to write aabb at 0x82004
73      Attempted to read 0x82004
74      Data read: aabb
75      Data read matches data written. Good!
76      Attempted to write abcd at 0x82008
77      Attempted to read 0x82008
78      Data read: abcd
79      Data read matches data written. Good!
80      Attempted to write 1122 at 0x8200c
81      Attempted to read 0x8200c
82      Data read: 1122
83      Data read matches data written. Good!
84
85   Test 5: Flash erase page at 0x82000
86      Flash erase succeeded!
87
88   Test 6: Non-word aligned write (word array 3)
89      Attempted to write 1122 at 0x82001
90      Attempted to read 0x82001
91      Data read: 1122
92      Data read matches data written. Good!
93      Attempted to write aabb at 0x82005
94      Attempted to read 0x82005
95      Data read: aabb
96      Data read matches data written. Good!
97      Attempted to write abcd at 0x82009
98      Attempted to read 0x82009
99      Data read: abcd
100      Data read matches data written. Good!
101      Attempted to write 1234 at 0x8200d
102      Attempted to read 0x8200d
103      Data read: 1234
104      Data read matches data written. Good!
105      Attempted to write 1122 at 0x82011
106      Attempted to read 0x82011
107      Data read: 1122
108      Data read matches data written. Good!
109      Attempted to write aabb at 0x82015
110      Attempted to read 0x82015
111      Data read: aabb
112      Data read matches data written. Good!
113      Attempted to write abcd at 0x82019
114      Attempted to read 0x82019
115      Data read: abcd
116      Data read matches data written. Good!
117      Attempted to write 1234 at 0x8201d
118      Attempted to read 0x8201d
119      Data read: 1234
120      Data read matches data written. Good!
121
122   Test 7: Page layout API
123      Offset  0x00041234:
124        belongs to the page 65 of start offset 0x00041000
125        and the size of 0x00001000 B.
126      Page of number 37 has start offset 0x00025000
127        and size of 0x00001000 B.
128        Page index resolved properly
129      SoC flash consists of 256 pages.
130
131   Test 8: Write block size API
132      write-block-size = 1
133
134   Finished!
135