1 #include <stdio.h> 2 #include <stdlib.h> 3 #include "unity.h" 4 #include "test_utils.h" 5 #include "esp_partition.h" 6 7 8 TEST_CASE("Can read partition table", "[partition]") 9 { 10 11 const esp_partition_t *p = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, NULL); 12 TEST_ASSERT_NOT_NULL(p); 13 TEST_ASSERT_EQUAL(0x20000, p->address); 14 TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, p->subtype); 15 16 esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, NULL); 17 TEST_ASSERT_NOT_NULL(it); 18 int count = 0; 19 const esp_partition_t* prev = NULL; 20 for (; it != NULL; it = esp_partition_next(it)) { 21 const esp_partition_t *p = esp_partition_get(it); 22 TEST_ASSERT_NOT_NULL(p); 23 if (prev) { 24 TEST_ASSERT_TRUE_MESSAGE(prev->address < p->address, "incorrect partition order"); 25 } 26 prev = p; 27 ++count; 28 } 29 esp_partition_iterator_release(it); 30 TEST_ASSERT_EQUAL(5, count); 31 32 it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL); 33 TEST_ASSERT_NOT_NULL(it); 34 count = 0; 35 for (; it != NULL; it = esp_partition_next(it)) { 36 ++count; 37 } 38 esp_partition_iterator_release(it); 39 TEST_ASSERT_EQUAL(8, count); 40 } 41 42 TEST_CASE("Can write, read, mmap partition", "[partition][ignore]") 43 { 44 const esp_partition_t *p = get_test_data_partition(); 45 printf("Using partition %s at 0x%x, size 0x%x\n", p->label, p->address, p->size); 46 TEST_ASSERT_NOT_NULL(p); 47 const size_t max_size = 2 * SPI_FLASH_SEC_SIZE; 48 uint8_t *data = (uint8_t *) malloc(max_size); 49 TEST_ASSERT_NOT_NULL(data); 50 51 TEST_ASSERT_EQUAL(ESP_OK, esp_partition_erase_range(p, 0, p->size)); 52 53 srand(0); 54 size_t block_size; 55 for (size_t offset = 0; offset < p->size; offset += block_size) { 56 block_size = ((rand() + 4) % max_size) & (~0x3); 57 size_t left = p->size - offset; 58 if (block_size > left) { 59 block_size = left; 60 } 61 for (size_t i = 0; i < block_size / 4; ++i) { 62 ((uint32_t *) (data))[i] = rand(); 63 } 64 TEST_ASSERT_EQUAL(ESP_OK, esp_partition_write(p, offset, data, block_size)); 65 } 66 67 srand(0); 68 for (size_t offset = 0; offset < p->size; offset += block_size) { 69 block_size = ((rand() + 4) % max_size) & (~0x3); 70 size_t left = p->size - offset; 71 if (block_size > left) { 72 block_size = left; 73 } 74 TEST_ASSERT_EQUAL(ESP_OK, esp_partition_read(p, offset, data, block_size)); 75 for (size_t i = 0; i < block_size / 4; ++i) { 76 TEST_ASSERT_EQUAL(rand(), ((uint32_t *) data)[i]); 77 } 78 } 79 80 free(data); 81 82 const uint32_t *mmap_data; 83 spi_flash_mmap_handle_t mmap_handle; 84 size_t begin = 3000; 85 size_t size = 64000; //chosen so size is smaller than 64K but the mmap straddles 2 MMU blocks 86 TEST_ASSERT_EQUAL(ESP_OK, esp_partition_mmap(p, begin, size, SPI_FLASH_MMAP_DATA, 87 (const void **)&mmap_data, &mmap_handle)); 88 srand(0); 89 for (size_t offset = 0; offset < p->size; offset += block_size) { 90 block_size = ((rand() + 4) % max_size) & (~0x3); 91 size_t left = p->size - offset; 92 if (block_size > left) { 93 block_size = left; 94 } 95 for (size_t i = 0; i < block_size / 4; ++i) { 96 size_t pos = offset + i * 4; 97 uint32_t expected = rand(); 98 if (pos < begin || pos >= (begin + size)) { 99 continue; 100 } 101 TEST_ASSERT_EQUAL(expected, mmap_data[(pos - begin) / 4]); 102 } 103 } 104 105 spi_flash_munmap(mmap_handle); 106 } 107