|
27 | 27 | #include "BufferedBlockDevice.h"
|
28 | 28 | #include "BlockDevice.h"
|
29 | 29 | #include <algorithm>
|
| 30 | +#include "MbedCRC.h" |
30 | 31 |
|
31 | 32 | #if COMPONENT_SPIF
|
32 | 33 | #include "SPIFBlockDevice.h"
|
@@ -690,6 +691,43 @@ void test_deinit_bd()
|
690 | 691 | block_device = NULL;
|
691 | 692 | }
|
692 | 693 |
|
| 694 | +void test_write_deinit_init() |
| 695 | +{ |
| 696 | + TEST_SKIP_UNLESS_MESSAGE(block_device != NULL, "no block device found."); |
| 697 | + MbedCRC<POLY_32BIT_ANSI, 32> ct; |
| 698 | + // Determine start_address & stop_address, choose random block somewhere |
| 699 | + uint8_t sector_num = rand() % (num_of_sectors - 2); |
| 700 | + bd_addr_t addr = sectors_addr[sector_num]; |
| 701 | + bd_addr_t end = sectors_addr[sector_num + 1]; |
| 702 | + bd_size_t size = end - addr; |
| 703 | + uint8_t *buf = (uint8_t *) malloc(size); |
| 704 | + TEST_ASSERT_NOT_EQUAL(buf, 0); |
| 705 | + uint32_t crc[2]; |
| 706 | + |
| 707 | + for (int i = 0; i < 10; i++) { |
| 708 | + // Generate test pattern |
| 709 | + for (int j = 0; j < size; j++) { |
| 710 | + buf[j] = (uint8_t)'0' + i + j; |
| 711 | + } |
| 712 | + ct.compute((void *)buf, size, &crc[0]); |
| 713 | + |
| 714 | + int err; |
| 715 | + err = block_device->erase(addr, size); |
| 716 | + TEST_ASSERT_EQUAL(err, 0); |
| 717 | + err = block_device->program(buf, addr, size); |
| 718 | + TEST_ASSERT_EQUAL(err, 0); |
| 719 | + memset(buf, 0, size); |
| 720 | + err = block_device->deinit(); |
| 721 | + TEST_ASSERT_EQUAL(0, err); |
| 722 | + err = block_device->init(); |
| 723 | + TEST_ASSERT_EQUAL(0, err); |
| 724 | + err = block_device->read(buf, addr, size); |
| 725 | + ct.compute((void *)buf, size, &crc[1]); |
| 726 | + TEST_ASSERT_EQUAL(crc[0], crc[1]); |
| 727 | + } |
| 728 | + free(buf); |
| 729 | +} |
| 730 | + |
693 | 731 | void test_get_type_functionality()
|
694 | 732 | {
|
695 | 733 | utest_printf("\nTest get blockdevice type..\n");
|
@@ -727,6 +765,7 @@ typedef struct {
|
727 | 765 |
|
728 | 766 | template_case_t template_cases[] = {
|
729 | 767 | {"Testing Init block device", test_init_bd, greentea_failure_handler},
|
| 768 | + {"Testing write -> deinit -> init -> read", test_write_deinit_init, greentea_failure_handler}, |
730 | 769 | {"Testing read write random blocks", test_random_program_read_erase, greentea_failure_handler},
|
731 | 770 | #if defined(MBED_CONF_RTOS_PRESENT)
|
732 | 771 | {"Testing multi threads erase program read", test_multi_threads, greentea_failure_handler},
|
|
0 commit comments