test_sd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2015-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. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "unity.h"
  18. #include "driver/gpio.h"
  19. #include "driver/sdmmc_host.h"
  20. #include "driver/sdmmc_defs.h"
  21. #include "sdmmc_cmd.h"
  22. #include "esp_log.h"
  23. #include "esp_heap_alloc_caps.h"
  24. #include <time.h>
  25. #include <sys/time.h>
  26. TEST_CASE("can probe SD", "[sd][ignore]")
  27. {
  28. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  29. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  30. sdmmc_host_init();
  31. sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config);
  32. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  33. TEST_ASSERT_NOT_NULL(card);
  34. TEST_ESP_OK(sdmmc_card_init(&config, card));
  35. sdmmc_card_print_info(stdout, card);
  36. sdmmc_host_deinit();
  37. free(card);
  38. }
  39. static void do_single_write_read_test(sdmmc_card_t* card,
  40. size_t start_block, size_t block_count)
  41. {
  42. size_t block_size = card->csd.sector_size;
  43. size_t total_size = block_size * block_count;
  44. printf(" %8d | %3d | %4.1f ", start_block, block_count, total_size / 1024.0f);
  45. uint32_t* buffer = pvPortMallocCaps(total_size, MALLOC_CAP_DMA);
  46. srand(start_block);
  47. for (size_t i = 0; i < total_size / sizeof(buffer[0]); ++i) {
  48. buffer[i] = rand();
  49. }
  50. struct timeval t_start_wr;
  51. gettimeofday(&t_start_wr, NULL);
  52. TEST_ESP_OK(sdmmc_write_sectors(card, buffer, start_block, block_count));
  53. struct timeval t_stop_wr;
  54. gettimeofday(&t_stop_wr, NULL);
  55. float time_wr = 1e3f * (t_stop_wr.tv_sec - t_start_wr.tv_sec) + 1e-3f * (t_stop_wr.tv_usec - t_start_wr.tv_usec);
  56. memset(buffer, 0xbb, total_size);
  57. struct timeval t_start_rd;
  58. gettimeofday(&t_start_rd, NULL);
  59. TEST_ESP_OK(sdmmc_read_sectors(card, buffer, start_block, block_count));
  60. struct timeval t_stop_rd;
  61. gettimeofday(&t_stop_rd, NULL);
  62. float time_rd = 1e3f * (t_stop_rd.tv_sec - t_start_rd.tv_sec) + 1e-3f * (t_stop_rd.tv_usec - t_start_rd.tv_usec);
  63. printf(" | %6.2f | %.2f | %.2fs | %.2f\n",
  64. time_wr, total_size / (time_wr / 1000) / (1024 * 1024),
  65. time_rd, total_size / (time_rd / 1000) / (1024 * 1024));
  66. srand(start_block);
  67. for (size_t i = 0; i < total_size / sizeof(buffer[0]); ++i) {
  68. TEST_ASSERT_EQUAL_HEX32(rand(), buffer[i]);
  69. }
  70. free(buffer);
  71. }
  72. TEST_CASE("can write and read back blocks", "[sd][ignore]")
  73. {
  74. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  75. config.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
  76. sdmmc_host_init();
  77. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  78. sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config);
  79. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  80. TEST_ASSERT_NOT_NULL(card);
  81. TEST_ESP_OK(sdmmc_card_init(&config, card));
  82. sdmmc_card_print_info(stdout, card);
  83. printf(" sector | count | size(kB) | wr_time(ms) | wr_speed(MB/s) | rd_time(ms) | rd_speed(MB/s)\n");
  84. do_single_write_read_test(card, 0, 1);
  85. do_single_write_read_test(card, 0, 4);
  86. do_single_write_read_test(card, 1, 16);
  87. do_single_write_read_test(card, 16, 32);
  88. do_single_write_read_test(card, 48, 64);
  89. do_single_write_read_test(card, 128, 128);
  90. do_single_write_read_test(card, card->csd.capacity - 64, 32);
  91. do_single_write_read_test(card, card->csd.capacity - 64, 64);
  92. do_single_write_read_test(card, card->csd.capacity - 8, 1);
  93. do_single_write_read_test(card, card->csd.capacity/2, 1);
  94. do_single_write_read_test(card, card->csd.capacity/2, 4);
  95. do_single_write_read_test(card, card->csd.capacity/2, 8);
  96. do_single_write_read_test(card, card->csd.capacity/2, 16);
  97. do_single_write_read_test(card, card->csd.capacity/2, 32);
  98. do_single_write_read_test(card, card->csd.capacity/2, 64);
  99. do_single_write_read_test(card, card->csd.capacity/2, 128);
  100. free(card);
  101. sdmmc_host_deinit();
  102. }