test_partition.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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(0x10000, 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. }
  28. TEST_CASE("Can write, read, mmap partition", "[partition][ignore]")
  29. {
  30. const esp_partition_t *p = get_test_data_partition();
  31. printf("Using partition %s at 0x%x, size 0x%x\n", p->label, p->address, p->size);
  32. TEST_ASSERT_NOT_NULL(p);
  33. const size_t max_size = 2 * SPI_FLASH_SEC_SIZE;
  34. uint8_t *data = (uint8_t *) malloc(max_size);
  35. TEST_ASSERT_NOT_NULL(data);
  36. TEST_ASSERT_EQUAL(ESP_OK, esp_partition_erase_range(p, 0, p->size));
  37. srand(0);
  38. size_t block_size;
  39. for (size_t offset = 0; offset < p->size; offset += block_size) {
  40. block_size = ((rand() + 4) % max_size) & (~0x3);
  41. size_t left = p->size - offset;
  42. if (block_size > left) {
  43. block_size = left;
  44. }
  45. for (size_t i = 0; i < block_size / 4; ++i) {
  46. ((uint32_t *) (data))[i] = rand();
  47. }
  48. TEST_ASSERT_EQUAL(ESP_OK, esp_partition_write(p, offset, data, block_size));
  49. }
  50. srand(0);
  51. for (size_t offset = 0; offset < p->size; offset += block_size) {
  52. block_size = ((rand() + 4) % max_size) & (~0x3);
  53. size_t left = p->size - offset;
  54. if (block_size > left) {
  55. block_size = left;
  56. }
  57. TEST_ASSERT_EQUAL(ESP_OK, esp_partition_read(p, offset, data, block_size));
  58. for (size_t i = 0; i < block_size / 4; ++i) {
  59. TEST_ASSERT_EQUAL(rand(), ((uint32_t *) data)[i]);
  60. }
  61. }
  62. free(data);
  63. const uint32_t *mmap_data;
  64. spi_flash_mmap_handle_t mmap_handle;
  65. size_t begin = 3000;
  66. size_t size = 64000; //chosen so size is smaller than 64K but the mmap straddles 2 MMU blocks
  67. TEST_ASSERT_EQUAL(ESP_OK, esp_partition_mmap(p, begin, size, SPI_FLASH_MMAP_DATA,
  68. (const void **)&mmap_data, &mmap_handle));
  69. srand(0);
  70. for (size_t offset = 0; offset < p->size; offset += block_size) {
  71. block_size = ((rand() + 4) % max_size) & (~0x3);
  72. size_t left = p->size - offset;
  73. if (block_size > left) {
  74. block_size = left;
  75. }
  76. for (size_t i = 0; i < block_size / 4; ++i) {
  77. size_t pos = offset + i * 4;
  78. uint32_t expected = rand();
  79. if (pos < begin || pos >= (begin + size)) {
  80. continue;
  81. }
  82. TEST_ASSERT_EQUAL(expected, mmap_data[(pos - begin) / 4]);
  83. }
  84. }
  85. spi_flash_munmap(mmap_handle);
  86. }