test_large_flash_writes.c 3.1 KB

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