1.. zephyr:code-sample:: spi-flash-at45
2   :name: AT45 DataFlash driver
3   :relevant-api: flash_interface
4
5   Use the AT45 family DataFlash driver to interact with the flash memory over SPI.
6
7Overview
8********
9
10This sample shows how to use the AT45 family DataFlash driver and how to
11specify devicetree nodes that enable flash chip instances to be handled
12by the driver (an overlay with two sample nodes is provided).
13The sample writes a defined test region in the flash memory with bytes of
14increasing (and overflowing at the 8-bit range) values and then reads it back
15to check if the write was successful. The starting value is also increased
16in consecutive runs of the sample.
17
18The sample have two config overlays.
19The :zephyr_file:`samples/drivers/spi_flash_at45/overlay-pm.conf` enables the
20device power management API which allows set device into a low power state.
21The :zephyr_file:`samples/drivers/spi_flash_at45/overlay-page_layout.conf`
22enables the flash page layout API which allow show the flash information.
23
24In the default configuration, the AT45 flash driver is configured to use
25the Read-Modify-Write functionality of DataFlash chips and does not perform
26page erasing, as it is not needed in this case. This can be modified by
27simply disabling the :kconfig:option:`CONFIG_SPI_FLASH_AT45_USE_READ_MODIFY_WRITE` option.
28
29Requirements
30************
31
32This sample has been tested on the Nordic Semiconductor nRF9160 DK
33(nrf9160dk/nrf9160) board with the AT45DB321E chip connected.
34It can be easily adjusted to be usable on other boards and with other
35AT45 family chips by just providing a corresponding overlay file.
36
37Building and Running
38********************
39
40The code can be found in :zephyr_file:`samples/drivers/spi_flash_at45`.
41
42To build and flash the application:
43
44.. zephyr-app-commands::
45   :zephyr-app: samples/drivers/spi_flash_at45
46   :board: nrf9160dk/nrf9160
47   :goals: build flash
48   :compact:
49
50To build and flash with device power management enabled:
51
52.. zephyr-app-commands::
53   :zephyr-app: samples/drivers/spi_flash_at45
54   :board: nrf9160dk/nrf9160
55   :gen-args: -DEXTRA_CONF_FILE=overlay-pm.conf
56   :goals: build flash
57   :compact:
58
59To build and flash with flash page layout enabled:
60
61.. zephyr-app-commands::
62   :zephyr-app: samples/drivers/spi_flash_at45
63   :board: nrf9160dk/nrf9160
64   :gen-args: -DEXTRA_CONF_FILE=overlay-page_layout.conf
65   :goals: build flash
66   :compact:
67
68Finally, to build and flash with both device power management and flash page
69layout enabled:
70
71.. zephyr-app-commands::
72   :zephyr-app: samples/drivers/spi_flash_at45
73   :board: nrf9160dk/nrf9160
74   :gen-args: -DEXTRA_CONF_FILE="overlay-pm.conf overlay-page_layout.conf"
75   :goals: build flash
76   :compact:
77
78Sample Output
79=============
80
81This is a typical output when both device power management and flash page
82layout are enabled:
83
84.. code-block:: console
85
86   DataFlash sample app on nrf9160dk_nrf9160
87   Using DATAFLASH_1, chip size: 4194304 bytes (page: 512)
88   Reading the first byte of the test region ... OK
89   Preparing test content starting with 0x01.
90   Writing the first half of the test region... OK
91   Writing the second half of the test region... OK
92   Reading the whole test region... OK
93   Checking the read content... OK
94   Putting the flash device into low power state... OK
95
96The sample is supplied with the overlay file that specifies two instances
97of AT45 family chips but only the one labeled "DATAFLASH_1" is required
98for the sample to work. If the other chip is not connected, the following
99log message appears, but apart from that the behavior of the sample stays
100unaffected.
101
102.. code-block:: console
103
104   [00:00:00.000,000] <err> spi_flash_at45: Wrong JEDEC ID: ff ff ff, expected: 1f 24 00
105