1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/drivers/flash.h>
9 #include <zephyr/device.h>
10 #include <zephyr/ztest.h>
11 
12 #define MAX_NUM_OF_SECTORS		1024
13 #define NUM_OF_SECTORS_TO_TEST		4
14 #define FLASH_SECTOR_SIZE		65536
15 #define TEST_DATA_LEN			4
16 
ZTEST(nios2_qspi,test_qspi_flash)17 ZTEST(nios2_qspi, test_qspi_flash)
18 {
19 	const struct device *flash_dev;
20 	uint32_t i, offset, rd_val, wr_val;
21 	uint8_t wr_buf[4] = {0xAA, 0xBB, 0xCC, 0xDD};
22 	uint8_t rd_buf[2];
23 
24 	flash_dev = DEVICE_DT_GET(DT_NODELABEL(n25q512ax3));
25 	zassert_true(!device_is_ready(flash_dev), TC_PASS, "Flash device is not ready!");
26 
27 	for (i = 0U; i < NUM_OF_SECTORS_TO_TEST; i++) {
28 		TC_PRINT("\nTesting: Flash Sector-%d\n", i);
29 		offset = FLASH_SECTOR_SIZE * i;
30 
31 		/* Flash Erase Test */
32 		TC_PRINT("	Flash Erase Test...");
33 		zassert_equal(flash_erase(flash_dev,
34 				offset, FLASH_SECTOR_SIZE),
35 				TC_PASS, "Flash erase call failed!");
36 		zassert_equal(flash_read(flash_dev, offset,
37 				&rd_val, TEST_DATA_LEN),
38 				TC_PASS, "Flash read call failed!");
39 		/* In case of erase all bits will be set to 1 */
40 		wr_val = 0xffffffff;
41 		zassert_equal(rd_val != wr_val,	TC_PASS,
42 					"Flash Erase Test failed!!");
43 		TC_PRINT("PASS\n");
44 
45 
46 		/* Flash Write & Read Test */
47 		TC_PRINT("	Flash Write & Read Test...");
48 		wr_val = 0xAABBCCDD;
49 		zassert_equal(flash_write(flash_dev, offset,
50 				&wr_val, TEST_DATA_LEN),
51 				TC_PASS, "Flash write call failed!");
52 		zassert_equal(flash_read(flash_dev, offset,
53 				&rd_val, TEST_DATA_LEN),
54 				TC_PASS, "Flash read call failed!");
55 		zassert_equal(rd_val != wr_val,	TC_PASS,
56 					"Flash Write & Read Test failed!!");
57 		TC_PRINT("PASS\n");
58 
59 
60 		/* Flash Unaligned Read Test */
61 		TC_PRINT("	Flash Unaligned Read Test...");
62 		zassert_equal(flash_write(flash_dev, offset + sizeof(wr_val),
63 				&wr_buf, sizeof(wr_buf)),
64 				TC_PASS, "Flash write call failed!");
65 		zassert_equal(flash_read(flash_dev, offset + sizeof(wr_val) + 1,
66 				&rd_buf, sizeof(rd_buf)),
67 				TC_PASS, "Flash read call failed!");
68 		zassert_equal(memcmp(wr_buf + 1, rd_buf, sizeof(rd_buf)),
69 				TC_PASS, "Flash Write & Read Test failed!!");
70 		TC_PRINT("PASS\n");
71 	}
72 }
73 
74 ZTEST_SUITE(nios2_qspi, NULL, NULL, NULL, NULL, NULL);
75