|
10 | 10 | #include "spi_nand_flash.h" |
11 | 11 | #include "nand_linux_mmap_emul.h" |
12 | 12 | #include "nand_private/nand_impl_wrap.h" |
| 13 | +#include "esp_blockdev.h" |
| 14 | +#include "esp_nand_blockdev.h" |
13 | 15 |
|
14 | 16 | #include <catch2/catch_test_macros.hpp> |
15 | 17 |
|
16 | 18 | #define PATTERN_SEED 0x12345678 |
17 | 19 |
|
| 20 | +static void fill_buffer(uint32_t seed, uint8_t *dst, size_t count) |
| 21 | +{ |
| 22 | + srand(seed); |
| 23 | + for (size_t i = 0; i < count; ++i) { |
| 24 | + uint32_t val = rand(); |
| 25 | + memcpy(dst + i * sizeof(uint32_t), &val, sizeof(val)); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +static void check_buffer(uint32_t seed, const uint8_t *src, size_t count) |
| 30 | +{ |
| 31 | + srand(seed); |
| 32 | + for (size_t i = 0; i < count; ++i) { |
| 33 | + uint32_t val; |
| 34 | + memcpy(&val, src + i * sizeof(uint32_t), sizeof(val)); |
| 35 | + if (!(rand() == val)) { |
| 36 | + printf("Val not equal\n"); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
18 | 41 | TEST_CASE("verify mark_bad_block works", "[spi_nand_flash]") |
19 | 42 | { |
20 | 43 | nand_file_mmap_emul_config_t conf = {"", 50 * 1024 * 1024, true}; |
@@ -42,15 +65,6 @@ TEST_CASE("verify mark_bad_block works", "[spi_nand_flash]") |
42 | 65 | spi_nand_flash_deinit_device(device_handle); |
43 | 66 | } |
44 | 67 |
|
45 | | -static void fill_buffer(uint32_t seed, uint8_t *dst, size_t count) |
46 | | -{ |
47 | | - srand(seed); |
48 | | - for (size_t i = 0; i < count; ++i) { |
49 | | - uint32_t val = rand(); |
50 | | - memcpy(dst + i * sizeof(uint32_t), &val, sizeof(val)); |
51 | | - } |
52 | | -} |
53 | | - |
54 | 68 | TEST_CASE("verify nand_prog, nand_read, nand_copy, nand_is_free works", "[spi_nand_flash]") |
55 | 69 | { |
56 | 70 | nand_file_mmap_emul_config_t conf = {"", 50 * 1024 * 1024, false}; |
@@ -85,10 +99,83 @@ TEST_CASE("verify nand_prog, nand_read, nand_copy, nand_is_free works", "[spi_na |
85 | 99 | REQUIRE(is_page_free == false); |
86 | 100 |
|
87 | 101 | REQUIRE(nand_wrap_read(device_handle, test_page, 0, sector_size, temp_buf) == 0); |
| 102 | + check_buffer(PATTERN_SEED, temp_buf, sector_size / sizeof(uint32_t)); |
| 103 | + |
88 | 104 | REQUIRE(nand_wrap_copy(device_handle, test_page, dst_page) == 0); |
| 105 | + |
89 | 106 | REQUIRE(nand_wrap_read(device_handle, dst_page, 0, sector_size, temp_buf) == 0); |
| 107 | + check_buffer(PATTERN_SEED, temp_buf, sector_size / sizeof(uint32_t)); |
90 | 108 | } |
91 | 109 | free(pattern_buf); |
92 | 110 | free(temp_buf); |
93 | 111 | spi_nand_flash_deinit_device(device_handle); |
94 | 112 | } |
| 113 | + |
| 114 | +TEST_CASE("verify mark_bad_block works with bdl interface", "[spi_nand_flash]") |
| 115 | +{ |
| 116 | + nand_file_mmap_emul_config_t conf = {"", 50 * 1024 * 1024, true}; |
| 117 | + spi_nand_flash_config_t nand_flash_config = {&conf, 0, SPI_NAND_IO_MODE_SIO, 0}; |
| 118 | + spi_nand_flash_device_t *device_handle; |
| 119 | + esp_blockdev_handle_t nand_bdl; |
| 120 | + REQUIRE(nand_flash_get_blockdev(&nand_flash_config, &device_handle, &nand_bdl) == 0); |
| 121 | + |
| 122 | + uint32_t block_size = nand_bdl->geometry.erase_size; |
| 123 | + uint32_t block_num = nand_bdl->geometry.disk_size / block_size; |
| 124 | + |
| 125 | + uint32_t test_block = 15; |
| 126 | + if (test_block < block_num) { |
| 127 | + REQUIRE(nand_bdl->ops->erase(nand_bdl, test_block * block_size, block_size) == 0); |
| 128 | + // Verify if test_block is not bad block |
| 129 | + esp_blockdev_cmd_arg_is_bad_block_t bad_block_status = {test_block, false}; |
| 130 | + REQUIRE(nand_bdl->ops->ioctl(nand_bdl, ESP_BLOCKDEV_CMD_IS_BAD_BLOCK, &bad_block_status) == 0); |
| 131 | + REQUIRE(bad_block_status.status == false); |
| 132 | + // mark test_block as a bad block |
| 133 | + uint32_t block = test_block; |
| 134 | + REQUIRE(nand_bdl->ops->ioctl(nand_bdl, ESP_BLOCKDEV_CMD_MARK_BAD_BLOCK, &block) == 0); |
| 135 | + // Verify if test_block is marked as bad block |
| 136 | + REQUIRE(nand_bdl->ops->ioctl(nand_bdl, ESP_BLOCKDEV_CMD_IS_BAD_BLOCK, &bad_block_status) == 0); |
| 137 | + REQUIRE(bad_block_status.status == true); |
| 138 | + } |
| 139 | + |
| 140 | + nand_bdl->ops->release(nand_bdl); |
| 141 | +} |
| 142 | + |
| 143 | +TEST_CASE("verify nand_prog, nand_read, nand_copy, nand_is_free works with bdl interface", "[spi_nand_flash]") |
| 144 | +{ |
| 145 | + nand_file_mmap_emul_config_t conf = {"", 50 * 1024 * 1024, false}; |
| 146 | + spi_nand_flash_config_t nand_flash_config = {&conf, 0, SPI_NAND_IO_MODE_SIO, 0}; |
| 147 | + spi_nand_flash_device_t *device_handle; |
| 148 | + esp_blockdev_handle_t nand_bdl; |
| 149 | + REQUIRE(nand_flash_get_blockdev(&nand_flash_config, &device_handle, &nand_bdl) == 0); |
| 150 | + |
| 151 | + uint32_t block_size = nand_bdl->geometry.erase_size; |
| 152 | + uint32_t sector_size = nand_bdl->geometry.write_size; |
| 153 | + uint32_t sector_num = nand_bdl->geometry.disk_size / sector_size; |
| 154 | + |
| 155 | + uint8_t *pattern_buf = (uint8_t *)malloc(sector_size); |
| 156 | + REQUIRE(pattern_buf != NULL); |
| 157 | + uint8_t *temp_buf = (uint8_t *)malloc(sector_size); |
| 158 | + REQUIRE(temp_buf != NULL); |
| 159 | + |
| 160 | + fill_buffer(PATTERN_SEED, pattern_buf, sector_size / sizeof(uint32_t)); |
| 161 | + |
| 162 | + uint32_t test_block = 20; |
| 163 | + uint32_t test_page = test_block * (block_size / sector_size); //(block_num * pages_per_block) |
| 164 | + if (test_page < sector_num) { |
| 165 | + // Verify if test_page is free |
| 166 | + esp_blockdev_cmd_arg_is_free_page_t page_free_status = {test_page, true}; |
| 167 | + REQUIRE(nand_bdl->ops->ioctl(nand_bdl, ESP_BLOCKDEV_CMD_IS_FREE_PAGE, &page_free_status) == 0); |
| 168 | + REQUIRE(page_free_status.status == true); |
| 169 | + // Write/program test_page |
| 170 | + REQUIRE(nand_bdl->ops->write(nand_bdl, pattern_buf, test_page * sector_size, sector_size) == 0); |
| 171 | + // Verify if test_page is used/programmed |
| 172 | + REQUIRE(nand_bdl->ops->ioctl(nand_bdl, ESP_BLOCKDEV_CMD_IS_FREE_PAGE, &page_free_status) == 0); |
| 173 | + REQUIRE(page_free_status.status == false); |
| 174 | + |
| 175 | + REQUIRE(nand_bdl->ops->read(nand_bdl, temp_buf, sector_size, test_page * sector_size, sector_size) == 0); |
| 176 | + check_buffer(PATTERN_SEED, temp_buf, sector_size / sizeof(uint32_t)); |
| 177 | + } |
| 178 | + free(pattern_buf); |
| 179 | + free(temp_buf); |
| 180 | + nand_bdl->ops->release(nand_bdl); |
| 181 | +} |
0 commit comments