test_large_flash_writes.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // Test for spi_flash_write() with large buffers (in RAM or on flash)
  7. #include <assert.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <sys/param.h>
  13. #include <unity.h>
  14. #include <test_utils.h>
  15. #include <esp_spi_flash.h>
  16. #include <esp_log.h>
  17. #include "esp_rom_spiflash.h"
  18. #include "../cache_utils.h"
  19. #include "soc/timer_periph.h"
  20. static const uint8_t large_const_buffer[16400] = {
  21. 203, // first byte
  22. 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
  23. 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,
  24. [50 ... 99] = 2,
  25. [1600 ... 2000] = 3,
  26. [8000 ... 9000] = 77,
  27. [15000 ... 16398] = 8,
  28. 43 // last byte
  29. };
  30. static void test_write_large_buffer(const uint8_t *source, size_t length);
  31. TEST_CASE("Test spi_flash_write large const buffer", "[spi_flash][esp_flash]")
  32. {
  33. // buffer in flash
  34. test_write_large_buffer(large_const_buffer, sizeof(large_const_buffer));
  35. }
  36. TEST_CASE("Test spi_flash_write large RAM buffer", "[spi_flash][esp_flash]")
  37. {
  38. // buffer in RAM
  39. uint8_t *source_buf = malloc(sizeof(large_const_buffer));
  40. TEST_ASSERT_NOT_NULL(source_buf);
  41. memcpy(source_buf, large_const_buffer, sizeof(large_const_buffer));
  42. test_write_large_buffer(source_buf, sizeof(large_const_buffer));
  43. free(source_buf);
  44. }
  45. static void test_write_large_buffer(const uint8_t *source, size_t length)
  46. {
  47. const esp_partition_t *part = get_test_data_partition();
  48. TEST_ASSERT(part->size > length + 2 + SPI_FLASH_SEC_SIZE);
  49. printf("Writing %d bytes from source %p\n", length, source);
  50. uint8_t *buf = malloc(length);
  51. TEST_ASSERT_NOT_NULL(buf);
  52. ESP_ERROR_CHECK( spi_flash_erase_range(part->address, (length + SPI_FLASH_SEC_SIZE) & ~(SPI_FLASH_SEC_SIZE-1)) );
  53. // note writing to unaligned address
  54. ESP_ERROR_CHECK( spi_flash_write(part->address + 1, source, length) );
  55. ESP_ERROR_CHECK( spi_flash_read(part->address + 1, buf, length) );
  56. TEST_ASSERT_EQUAL_HEX8_ARRAY(source, buf, length);
  57. free(buf);
  58. // check nothing was written at beginning or end
  59. uint8_t ends[8];
  60. ESP_ERROR_CHECK( spi_flash_read(part->address, ends, sizeof(ends)) );
  61. TEST_ASSERT_EQUAL_HEX8(0xFF, ends[0]);
  62. TEST_ASSERT_EQUAL_HEX8(source[0] , ends[1]);
  63. ESP_ERROR_CHECK( spi_flash_read(part->address + length, ends, sizeof(ends)) );
  64. TEST_ASSERT_EQUAL_HEX8(source[length-1], ends[0]);
  65. TEST_ASSERT_EQUAL_HEX8(0xFF, ends[1]);
  66. TEST_ASSERT_EQUAL_HEX8(0xFF, ends[2]);
  67. TEST_ASSERT_EQUAL_HEX8(0xFF, ends[3]);
  68. }