test_partition.c 3.6 KB

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