test_large_flash_writes.c 3.1 KB

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