88#include < string.h>
99
1010#include " spi_nand_flash.h"
11+ #include " spi_nand_flash_test_helpers.h"
1112#include " nand_linux_mmap_emul.h"
1213#include " nand_private/nand_impl_wrap.h"
1314
1415#include < catch2/catch_test_macros.hpp>
1516
16- #define PATTERN_SEED 0x12345678
17-
18- static void fill_buffer (uint32_t seed, uint8_t *dst, size_t count)
19- {
20- srand (seed);
21- for (size_t i = 0 ; i < count; ++i) {
22- uint32_t val = rand ();
23- memcpy (dst + i * sizeof (uint32_t ), &val, sizeof (val));
24- }
25- }
26-
27- static void check_buffer (uint32_t seed, const uint8_t *src, size_t count)
28- {
29- srand (seed);
30- for (size_t i = 0 ; i < count; ++i) {
31- uint32_t val;
32- memcpy (&val, src + i * sizeof (uint32_t ), sizeof (val));
33- if (!(rand () == val)) {
34- printf (" Val not equal\n " );
35- }
36- }
37- }
38-
3917TEST_CASE (" verify mark_bad_block works" , " [spi_nand_flash]" )
4018{
4119 nand_file_mmap_emul_config_t conf = {" " , 50 * 1024 * 1024 , true };
4220 spi_nand_flash_config_t nand_flash_config = {&conf, 0 , SPI_NAND_IO_MODE_SIO, 0 };
4321 spi_nand_flash_device_t *device_handle;
4422 REQUIRE (spi_nand_flash_init_device (&nand_flash_config, &device_handle) == ESP_OK);
4523
46- uint32_t sector_num, sector_size;
47- REQUIRE (spi_nand_flash_get_capacity (device_handle, §or_num) == 0 );
48- REQUIRE (spi_nand_flash_get_sector_size (device_handle, §or_size) == 0 );
24+ uint32_t block_num;
25+ REQUIRE (spi_nand_flash_get_block_num (device_handle, &block_num) == 0 );
4926
5027 uint32_t test_block = 15 ;
51- if ( test_block < sector_num) {
52- bool is_bad_status = false ;
53- // Verify if test_block is not bad block
54- REQUIRE ( nand_wrap_is_bad (device_handle, test_block, &is_bad_status) == 0 );
55- REQUIRE (is_bad_status == false );
56- // mark test_block as a bad block
57- REQUIRE ( nand_wrap_mark_bad (device_handle, test_block) == 0 );
58- // Verify if test_block is marked as bad block
59- REQUIRE ( nand_wrap_is_bad (device_handle, test_block, &is_bad_status) == 0 );
60- REQUIRE (is_bad_status == true );
61- }
28+ REQUIRE (( test_block < block_num) == true );
29+
30+ bool is_bad_status = false ;
31+ // Verify if test_block is not bad block
32+ REQUIRE (nand_wrap_is_bad (device_handle, test_block, & is_bad_status) == 0 );
33+ REQUIRE (is_bad_status == false );
34+ // mark test_block as a bad block
35+ REQUIRE ( nand_wrap_mark_bad (device_handle, test_block) == 0 );
36+ // Verify if test_block is marked as bad block
37+ REQUIRE (nand_wrap_is_bad (device_handle, test_block, & is_bad_status) == 0 );
38+ REQUIRE (is_bad_status == true );
6239
6340 spi_nand_flash_deinit_device (device_handle);
6441}
@@ -80,30 +57,32 @@ TEST_CASE("verify nand_prog, nand_read, nand_copy, nand_is_free works", "[spi_na
8057 uint8_t *temp_buf = (uint8_t *)malloc (sector_size);
8158 REQUIRE (temp_buf != NULL );
8259
83- fill_buffer (PATTERN_SEED, pattern_buf, sector_size / sizeof (uint32_t ));
60+ spi_nand_flash_fill_buffer ( pattern_buf, sector_size / sizeof (uint32_t ));
8461
8562 bool is_page_free = true ;
8663 uint32_t test_block = 20 ;
8764 uint32_t test_page = test_block * (block_size / sector_size); // (block_num * pages_per_block)
8865 uint32_t dst_page = test_page + 1 ;
89- if (test_page < sector_num) {
90- // Verify if test_page is free
91- REQUIRE (nand_wrap_is_free (device_handle, test_page, &is_page_free) == 0 );
92- REQUIRE (is_page_free == true );
93- // Write/program test_page
94- REQUIRE (nand_wrap_prog (device_handle, test_page, pattern_buf) == 0 );
95- // Verify if test_page is used/programmed
96- REQUIRE (nand_wrap_is_free (device_handle, test_page, &is_page_free) == 0 );
97- REQUIRE (is_page_free == false );
98-
99- REQUIRE (nand_wrap_read (device_handle, test_page, 0 , sector_size, temp_buf) == 0 );
100- check_buffer (PATTERN_SEED, temp_buf, sector_size / sizeof (uint32_t ));
101-
102- REQUIRE (nand_wrap_copy (device_handle, test_page, dst_page) == 0 );
103-
104- REQUIRE (nand_wrap_read (device_handle, dst_page, 0 , sector_size, temp_buf) == 0 );
105- check_buffer (PATTERN_SEED, temp_buf, sector_size / sizeof (uint32_t ));
106- }
66+
67+ REQUIRE ((test_page < sector_num) == true );
68+
69+ // Verify if test_page is free
70+ REQUIRE (nand_wrap_is_free (device_handle, test_page, &is_page_free) == 0 );
71+ REQUIRE (is_page_free == true );
72+ // Write/program test_page
73+ REQUIRE (nand_wrap_prog (device_handle, test_page, pattern_buf) == 0 );
74+ // Verify if test_page is used/programmed
75+ REQUIRE (nand_wrap_is_free (device_handle, test_page, &is_page_free) == 0 );
76+ REQUIRE (is_page_free == false );
77+
78+ REQUIRE (nand_wrap_read (device_handle, test_page, 0 , sector_size, temp_buf) == 0 );
79+ REQUIRE (spi_nand_flash_check_buffer (temp_buf, sector_size / sizeof (uint32_t )) == 0 );
80+
81+ REQUIRE (nand_wrap_copy (device_handle, test_page, dst_page) == 0 );
82+
83+ REQUIRE (nand_wrap_read (device_handle, dst_page, 0 , sector_size, temp_buf) == 0 );
84+ REQUIRE (spi_nand_flash_check_buffer (temp_buf, sector_size / sizeof (uint32_t )) == 0 );
85+
10786 free (pattern_buf);
10887 free (temp_buf);
10988 spi_nand_flash_deinit_device (device_handle);
0 commit comments