|
-
- #include <SPI.h>
- #include "SdFat.h"
- #include "FreeStack.h"
-
-
- const uint8_t chipSelect = SS;
-
-
- const uint32_t BLOCK_COUNT = 10000UL;
-
-
- const uint32_t MICROS_PER_BLOCK = 10000;
-
-
- SdFat sd;
-
-
- SdFile file;
-
-
- uint32_t bgnBlock, endBlock;
-
-
- ArduinoOutStream cout(Serial);
-
-
- #define error(s) sd.errorHalt(F(s))
-
-
- #define OVER_DIM 20
- struct {
- uint32_t block;
- uint32_t micros;
- } over[OVER_DIM];
-
- void setup(void) {
- Serial.begin(9600);
-
-
- while (!Serial) {
- SysCall::yield();
- }
- }
-
- void loop(void) {
- do {
- delay(10);
- } while (Serial.read() >= 0);
-
- cout << F("Type any character to start\n");
- while (Serial.read() <= 0) {
- SysCall::yield();
- }
-
- cout << F("FreeStack: ") << FreeStack() << endl;
-
-
-
- if (!sd.begin(chipSelect, SPI_FULL_SPEED)) {
- sd.initErrorHalt();
- }
-
-
- sd.remove("RawWrite.txt");
-
-
- if (!file.createContiguous(sd.vwd(), "RawWrite.txt", 512UL*BLOCK_COUNT)) {
- error("createContiguous failed");
- }
-
- if (!file.contiguousRange(&bgnBlock, &endBlock)) {
- error("contiguousRange failed");
- }
-
-
-
-
-
- uint8_t* pCache = (uint8_t*)sd.vol()->cacheClear();
-
-
- memset(pCache, ' ', 512);
- for (uint16_t i = 0; i < 512; i += 64) {
-
- pCache[i + 61] = '0' + (i/64);
- pCache[i + 62] = '\r';
- pCache[i + 63] = '\n';
- }
-
- cout << F("Start raw write of ") << file.fileSize() << F(" bytes at\n");
- cout << 512000000UL/MICROS_PER_BLOCK << F(" bytes per second\n");
- cout << F("Please wait ") << (BLOCK_COUNT*MICROS_PER_BLOCK)/1000000UL;
- cout << F(" seconds\n");
-
-
- if (!sd.card()->writeStart(bgnBlock, BLOCK_COUNT)) {
- error("writeStart failed");
- }
-
- uint16_t overruns = 0;
- uint32_t maxWriteTime = 0;
- uint32_t t = micros();
- uint32_t tNext = t;
-
-
- for (uint32_t b = 0; b < BLOCK_COUNT; b++) {
-
- tNext += MICROS_PER_BLOCK;
-
-
- uint32_t n = b;
- for (int8_t d = 5; d >= 0; d--) {
- pCache[d] = n || d == 5 ? n % 10 + '0' : ' ';
- n /= 10;
- }
-
- uint32_t tw = micros();
- if (!sd.card()->writeData(pCache)) {
- error("writeData failed");
- }
- tw = micros() - tw;
-
-
- if (tw > maxWriteTime) {
- maxWriteTime = tw;
- }
-
- if (micros() > tNext) {
- if (overruns < OVER_DIM) {
- over[overruns].block = b;
- over[overruns].micros = tw;
- }
- overruns++;
-
- tNext = micros();
- } else {
-
- while(micros() < tNext);
- }
- }
-
- t = micros() - t;
-
-
- if (!sd.card()->writeStop()) {
- error("writeStop failed");
- }
-
- cout << F("Done\n");
- cout << F("Elapsed time: ") << setprecision(3)<< 1.e-6*t;
- cout << F(" seconds\n");
- cout << F("Max write time: ") << maxWriteTime << F(" micros\n");
- cout << F("Overruns: ") << overruns << endl;
- if (overruns) {
- uint8_t n = overruns > OVER_DIM ? OVER_DIM : overruns;
- cout << F("fileBlock,micros") << endl;
- for (uint8_t i = 0; i < n; i++) {
- cout << over[i].block << ',' << over[i].micros << endl;
- }
- }
-
- file.close();
- Serial.println();
- }
|