Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STM32SD Speed Test Example #80

Open
ywf1 opened this issue Nov 23, 2024 · 2 comments
Open

STM32SD Speed Test Example #80

ywf1 opened this issue Nov 23, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@ywf1
Copy link

ywf1 commented Nov 23, 2024

If possible could an example be added or provided with a read / write speed figure - currently using a stm32f405 with sdio interface, I want to identify maximum sampling rate of sensors, so wanted a good idea of the read/write (mostly write) speed using the lib.

Thank you for the help!

@ywf1 ywf1 added the enhancement New feature or request label Nov 23, 2024
@fpistm
Copy link
Member

fpistm commented Nov 25, 2024

Hi @ywf1
That's a good idea but have no time soon. Any contribution are welcome.

@ywf1
Copy link
Author

ywf1 commented Nov 30, 2024

Dear Fredric,

Tried it out myself!

Hardware: STM32F405, SDIO Interface
SD Card: SanDisk Ultra 32GB MicroSD HC I

Script:

#include <STM32SD.h>

#define TEST_FILE_NAME "/speed_test.txt"
#define TEST_BLOCK_SIZE 4096  // Size of each data block in bytes
#define TEST_BLOCK_COUNT 400 // Number of blocks to write and read

File testFile;

void setup() {
  delay(2000);

  Serial.begin(115200);
  while (!Serial) {
    delay(10); // Wait for Serial to initialize
  }

  Serial.println("Initializing SD card...");
  if (!SD.begin()) {
    Serial.println("SD initialization failed!");
    while (1);
  }
  Serial.println("SD initialization successful.");

  // Perform write speed test
  writeSpeedTest();

  // Perform read speed test
  readSpeedTest();
}

void writeSpeedTest() {
  Serial.println("Starting write speed test...");

  // Prepare test data
  uint8_t buffer[TEST_BLOCK_SIZE];
  for (int i = 0; i < TEST_BLOCK_SIZE; i++) {
    buffer[i] = i % 256;
  }

  // Open file for writing
  testFile = SD.open(TEST_FILE_NAME, FILE_WRITE);
  if (!testFile) {
    Serial.println("Failed to open file for writing.");
    return;
  }

  unsigned long startTime = micros();
  for (int i = 0; i < TEST_BLOCK_COUNT; i++) {
    testFile.write(buffer, TEST_BLOCK_SIZE);
  }
  testFile.close();
  unsigned long elapsedTime = micros() - startTime;

  float speed = (TEST_BLOCK_SIZE * TEST_BLOCK_COUNT) / (elapsedTime / 1000000.0) / 1024.0;
  Serial.print("Write speed: ");
  Serial.print(speed, 2); // Display speed with 2 decimal places
  Serial.println(" KB/s");
}

void readSpeedTest() {
  Serial.println("Starting read speed test...");

  // Open file for reading
  testFile = SD.open(TEST_FILE_NAME, FILE_READ);
  if (!testFile) {
    Serial.println("Failed to open file for reading.");
    return;
  }

  uint8_t buffer[TEST_BLOCK_SIZE];

  unsigned long startTime = micros();
  for (int i = 0; i < TEST_BLOCK_COUNT; i++) {
    testFile.read(buffer, TEST_BLOCK_SIZE);
  }
  testFile.close();
  unsigned long elapsedTime = micros() - startTime;

  float speed = (TEST_BLOCK_SIZE * TEST_BLOCK_COUNT) / (elapsedTime / 1000000.0) / 1024.0;
  Serial.print("Read speed: ");
  Serial.print(speed, 2); // Display speed with 2 decimal places
  Serial.println(" KB/s");
}

void loop() {
  // Do nothing in loop
}

Results:
image

Do these seem reasonable?

Best,
Yahya

@fpistm fpistm self-assigned this Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants