test_large_flash_writes.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <spi_flash_mmap.h>
  16. #include <esp_log.h>
  17. #include "esp_rom_spiflash.h"
  18. #include "esp_private/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 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 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. TEST_ESP_OK( esp_flash_erase_region(NULL, part->address, (length + SPI_FLASH_SEC_SIZE) & ~(SPI_FLASH_SEC_SIZE-1)) );
  53. // note writing to unaligned address
  54. TEST_ESP_OK( esp_flash_write(NULL, source, part->address + 1, length) );
  55. TEST_ESP_OK( esp_flash_read(NULL, buf, part->address + 1, 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. TEST_ESP_OK( esp_flash_read(NULL, ends, part->address, sizeof(ends)) );
  61. TEST_ASSERT_EQUAL_HEX8(0xFF, ends[0]);
  62. TEST_ASSERT_EQUAL_HEX8(source[0] , ends[1]);
  63. TEST_ESP_OK( esp_flash_read(NULL, ends, part->address + length, 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. }