1 /*
2  * Copyright (c) 2023, Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <zephyr/drivers/flash.h>
9 #include <zephyr/kernel.h>
10 #include <string.h>
11 #include <zephyr/sys/printk.h>
12 
13 #ifndef OFFSET_PAGE
14 #define OFFSET_PAGE    0x00
15 #endif
16 
17 #ifndef NAND_NUM_PAGES
18 #define NAND_NUM_PAGES 50
19 #endif
20 
21 #define NAND_DEV       DEVICE_DT_GET(DT_ALIAS(nand));
22 
main(void)23 int main(void)
24 {
25 	const struct device *nand_dev;
26 	struct flash_pages_info page;
27 	size_t flash_block_size;
28 	size_t total_pages;
29 	int ret;
30 	uint8_t *w_Page_buffer;
31 	uint8_t *r_Page_buffer;
32 	uint8_t page_data = 0;
33 
34 	printk("Nand flash driver test sample\n");
35 
36 	nand_dev = NAND_DEV;
37 
38 	if (!device_is_ready(nand_dev)) {
39 		printk("Nand flash driver is not ready\n");
40 		return -ENODEV;
41 	}
42 
43 	total_pages = flash_get_page_count(nand_dev);
44 
45 	flash_block_size = flash_get_write_block_size(nand_dev);
46 	printk("Nand flash driver block size %lx\n", flash_block_size);
47 
48 	ret = flash_get_page_info_by_offs(nand_dev, 0x00, &page);
49 
50 	if (ret < 0) {
51 		printk("Nand flash driver page info error\n");
52 		return ret;
53 	}
54 	printk("The Page size of %lx\n", page.size);
55 
56 	w_Page_buffer = (uint8_t *)k_malloc(page.size * NAND_NUM_PAGES);
57 
58 	r_Page_buffer = (uint8_t *)k_malloc(page.size * NAND_NUM_PAGES);
59 
60 	if (w_Page_buffer != NULL) {
61 
62 		for (int index = 0; index < page.size * NAND_NUM_PAGES; index++) {
63 			w_Page_buffer[index] = (page_data++ % 255);
64 		}
65 
66 	} else {
67 		printk("Write memory not allocated\n");
68 		return -ENOSR;
69 	}
70 
71 	if (r_Page_buffer != NULL) {
72 		memset(r_Page_buffer, 0x55, page.size * NAND_NUM_PAGES);
73 	} else {
74 		printk("Read memory not allocated\n");
75 		k_free(w_Page_buffer);
76 		return -ENOSR;
77 	}
78 
79 	ret = flash_erase(nand_dev, OFFSET_PAGE, flash_block_size);
80 
81 	if (ret < 0) {
82 		printk("Nand flash driver read Error\n");
83 		k_free(w_Page_buffer);
84 		k_free(r_Page_buffer);
85 		return ret;
86 	}
87 	printk("Nand flash driver data erase successful....\n");
88 
89 	ret = flash_write(nand_dev, OFFSET_PAGE, w_Page_buffer, page.size * NAND_NUM_PAGES);
90 	if (ret < 0) {
91 		printk("Nand flash driver write error\n");
92 		k_free(w_Page_buffer);
93 		k_free(r_Page_buffer);
94 		return ret;
95 	}
96 	printk("Nand flash driver write completed....\n");
97 
98 	ret = flash_read(nand_dev, OFFSET_PAGE, r_Page_buffer, page.size * NAND_NUM_PAGES);
99 	if (ret < 0) {
100 		printk("Nand flash driver read error\n");
101 		k_free(w_Page_buffer);
102 		k_free(r_Page_buffer);
103 		return ret;
104 	}
105 
106 	printk("Nand flash driver read completed....\n");
107 
108 	ret = memcmp(w_Page_buffer, r_Page_buffer, page.size * NAND_NUM_PAGES);
109 
110 	if (ret < 0) {
111 		printk("Nand flash driver read not match\n");
112 		k_free(w_Page_buffer);
113 		k_free(r_Page_buffer);
114 		return ret;
115 	}
116 	printk("Nand flash driver read verified\n");
117 
118 	ret = flash_erase(nand_dev, OFFSET_PAGE, flash_block_size);
119 
120 	if (ret < 0) {
121 		printk("Nand flash driver read Error\n");
122 		k_free(w_Page_buffer);
123 		k_free(r_Page_buffer);
124 		return ret;
125 	}
126 	printk("Nand flash driver data erase successful....\n");
127 
128 	ret = flash_read(nand_dev, OFFSET_PAGE, r_Page_buffer, page.size * NAND_NUM_PAGES);
129 
130 	if (ret != 0) {
131 		printk("Nand flash driver read error\n");
132 		k_free(w_Page_buffer);
133 		k_free(r_Page_buffer);
134 		return ret;
135 	}
136 	memset(w_Page_buffer, 0xFF, page.size * NAND_NUM_PAGES);
137 
138 	ret = memcmp(w_Page_buffer, r_Page_buffer, page.size * NAND_NUM_PAGES);
139 
140 	if (ret < 0) {
141 		printk("Nand flash driver read not match\n");
142 		k_free(w_Page_buffer);
143 		k_free(r_Page_buffer);
144 		return ret;
145 	}
146 	printk("Nand flash driver read verified after erase....\n");
147 	printk("Nand flash driver test sample completed....\n");
148 
149 	return 0;
150 }
151