| 29 | namespace |
| 30 | { |
| 31 | void writeToFile(const String& filename, uint32_t totalBytes, uint32_t bytesPerRound) |
| 32 | { |
| 33 | char* buf = new char[totalBytes]; |
| 34 | if(buf == nullptr) { |
| 35 | Serial.println(_F("Failed to allocate heap")); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | Serial << _F("Write ") << totalBytes / 1024 << _F(" kBytes in ") << bytesPerRound << _F(" Bytes increment:") |
| 40 | << endl; |
| 41 | for(unsigned i = 0; i < totalBytes; i++) { |
| 42 | buf[i] = (i % 10) + '0'; |
| 43 | } |
| 44 | |
| 45 | OneShotFastUs timer; |
| 46 | |
| 47 | FIL file; |
| 48 | FRESULT fRes = f_open(&file, filename.c_str(), FA_WRITE | FA_CREATE_ALWAYS); |
| 49 | |
| 50 | if(fRes == FR_OK) { |
| 51 | unsigned i = 0; |
| 52 | do { |
| 53 | uint32_t remainingBytes = totalBytes - i > bytesPerRound ? bytesPerRound : totalBytes - i; |
| 54 | uint32_t bytesWritten = 0; |
| 55 | f_write(&file, buf + i, remainingBytes, &bytesWritten); |
| 56 | if(bytesWritten != remainingBytes) { |
| 57 | Serial << _F("Only written ") << i + bytesWritten << " bytes" << endl; |
| 58 | break; |
| 59 | } |
| 60 | i += bytesWritten; |
| 61 | } while(i < totalBytes); |
| 62 | |
| 63 | f_close(&file); |
| 64 | |
| 65 | //get the time at test end |
| 66 | unsigned elapsed = timer.elapsedTime(); |
| 67 | |
| 68 | Serial << (i / 1024.0f) * 1000000.0f / elapsed << _F(" kB/s") << endl; |
| 69 | } else { |
| 70 | Serial << _F("fopen FAIL: ") << fRes << endl; |
| 71 | } |
| 72 | |
| 73 | delete[] buf; |
| 74 | } |
| 75 | |
| 76 | void stat_file(char* fname) |
| 77 | { |