test_partitions.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Test for spi_flash_{read,write}.
  15. #include <assert.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/param.h>
  20. #include <unity.h>
  21. #include <test_utils.h>
  22. #include <esp_partition.h>
  23. #include <esp_attr.h>
  24. TEST_CASE("Test erase partition", "[spi_flash]")
  25. {
  26. const esp_partition_t *part = get_test_data_partition();
  27. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  28. spi_flash_reset_counters();
  29. #endif
  30. // erase whole partition
  31. ESP_ERROR_CHECK( esp_partition_erase_range(part, 0, part->size) );
  32. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  33. spi_flash_dump_counters();
  34. #endif
  35. // put some dummy data on sector boundaries
  36. const static DRAM_ATTR char some_data[] = "abcdefghijklmn";
  37. for (int i = 0; i < part->size; i+= 4096) {
  38. ESP_ERROR_CHECK( esp_partition_write(part, i, some_data, strlen(some_data)) );
  39. }
  40. // check it's there!
  41. char buf[strlen(some_data)];
  42. for (int i = 0; i < part->size; i+= 4096) {
  43. memset(buf, 0x00, sizeof(buf));
  44. ESP_ERROR_CHECK( esp_partition_read(part, i, buf, sizeof(buf)) );
  45. TEST_ASSERT_EQUAL_INT(0, strncmp(buf, some_data, sizeof(buf)));
  46. }
  47. // erase the whole thing again
  48. ESP_ERROR_CHECK( esp_partition_erase_range(part, 0, part->size) );
  49. // check it's gone
  50. for (int i = 0; i < part->size; i+= 4096) {
  51. memset(buf, 0x00, sizeof(buf));
  52. ESP_ERROR_CHECK( esp_partition_read(part, i, buf, sizeof(buf)) );
  53. for (int i = 0; i < sizeof(buf); i++) {
  54. TEST_ASSERT_EQUAL_HEX8(0xFF, buf[i]);
  55. }
  56. }
  57. }