|
| 1 | +#include <STM32SD.h> |
| 2 | + |
| 3 | +#define TEST_FILE_NAME "/bench.txt" |
| 4 | +#define TEST_BLOCK_SIZE 4096 // Size of each data block in bytes |
| 5 | +#define TEST_BLOCK_COUNT 400 // Number of blocks to write and read |
| 6 | + |
| 7 | +File testFile; |
| 8 | + |
| 9 | +void setup() { |
| 10 | + delay(2000); |
| 11 | + |
| 12 | + Serial.begin(115200); |
| 13 | + while (!Serial) { |
| 14 | + delay(10); // Wait for Serial to initialize |
| 15 | + } |
| 16 | + |
| 17 | + Serial.println("Initializing SD card..."); |
| 18 | + if (!SD.begin()) { |
| 19 | + Serial.println("SD initialization failed!"); |
| 20 | + while (1) |
| 21 | + ; |
| 22 | + } |
| 23 | + Serial.println("SD initialization successful."); |
| 24 | + Serial.flush(); |
| 25 | + |
| 26 | + // Perform write speed test |
| 27 | + if (writeSpeedTest()) { |
| 28 | + // Perform read speed test |
| 29 | + readSpeedTest(); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +bool writeSpeedTest() { |
| 34 | + bool status = false; |
| 35 | + Serial.println("Starting write speed test..."); |
| 36 | + Serial.flush(); |
| 37 | + // Prepare test data |
| 38 | + uint8_t buffer[TEST_BLOCK_SIZE]; |
| 39 | + for (uint32_t i = 0; i < TEST_BLOCK_SIZE; i++) { |
| 40 | + buffer[i] = i % 256; |
| 41 | + } |
| 42 | + |
| 43 | + // Open file for writing |
| 44 | + testFile = SD.open(TEST_FILE_NAME, FILE_WRITE); |
| 45 | + if (!testFile) { |
| 46 | + Serial.println("Failed to open file for writing."); |
| 47 | + } else { |
| 48 | + |
| 49 | + unsigned long startTime = micros(); |
| 50 | + for (int i = 0; i < TEST_BLOCK_COUNT; i++) { |
| 51 | + testFile.write(buffer, TEST_BLOCK_SIZE); |
| 52 | + } |
| 53 | + testFile.close(); |
| 54 | + |
| 55 | + float speed = (TEST_BLOCK_SIZE * TEST_BLOCK_COUNT) / ((micros() - startTime) / 1000000.0) / 1024.0; |
| 56 | + Serial.print("Write speed: "); |
| 57 | + Serial.print(speed, 2); // Display speed with 2 decimal places |
| 58 | + Serial.println(" KB/s"); |
| 59 | + Serial.flush(); |
| 60 | + status = true; |
| 61 | + } |
| 62 | + return status; |
| 63 | +} |
| 64 | + |
| 65 | +void readSpeedTest() { |
| 66 | + Serial.println("Starting read speed test..."); |
| 67 | + Serial.flush(); |
| 68 | + // Open file for reading |
| 69 | + testFile = SD.open(TEST_FILE_NAME, FILE_READ); |
| 70 | + if (!testFile) { |
| 71 | + Serial.println("Failed to open file for reading."); |
| 72 | + } else { |
| 73 | + |
| 74 | + uint8_t buffer[TEST_BLOCK_SIZE]; |
| 75 | + unsigned long startTime = micros(); |
| 76 | + for (int i = 0; i < TEST_BLOCK_COUNT; i++) { |
| 77 | + testFile.read(buffer, TEST_BLOCK_SIZE); |
| 78 | + } |
| 79 | + testFile.close(); |
| 80 | + |
| 81 | + float speed = (TEST_BLOCK_SIZE * TEST_BLOCK_COUNT) / ((micros() - startTime) / 1000000.0) / 1024.0; |
| 82 | + Serial.print("Read speed: "); |
| 83 | + Serial.print(speed, 2); // Display speed with 2 decimal places |
| 84 | + Serial.println(" KB/s"); |
| 85 | + Serial.flush(); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +void loop() { |
| 90 | + // Do nothing in loop |
| 91 | +} |
0 commit comments