|
-
- #include <SdFat.h>
- #include <SdFatUtil.h>
- #include <StdioStream.h>
- #include "AnalogBinLogger.h"
-
-
-
- const uint8_t PIN_LIST[] = {0, 1, 2, 3, 4};
-
-
- const float SAMPLE_RATE = 5000;
-
-
-
-
-
- const float SAMPLE_INTERVAL = 1.0/SAMPLE_RATE;
-
-
-
-
- #define ROUND_SAMPLE_INTERVAL 1
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- uint8_t const ADC_REF = (1 << REFS0);
-
-
-
-
-
-
-
-
-
- const uint32_t FILE_BLOCK_COUNT = 256000;
-
-
- #define FILE_BASE_NAME "ANALOG"
-
-
- #define RECORD_EIGHT_BITS 0
-
-
-
-
-
-
- const int8_t ERROR_LED_PIN = 3;
-
-
- const uint8_t SD_CS_PIN = SS;
-
-
-
-
-
-
-
- #if RAMEND < 0X8FF
- #error Too little SRAM
-
- #elif RAMEND < 0X10FF
-
- const uint8_t BUFFER_BLOCK_COUNT = 1;
-
- const uint8_t QUEUE_DIM = 4;
-
- #elif RAMEND < 0X20FF
-
- const uint8_t BUFFER_BLOCK_COUNT = 4;
-
- const uint8_t QUEUE_DIM = 8;
-
- #elif RAMEND < 0X40FF
-
- const uint8_t BUFFER_BLOCK_COUNT = 12;
-
- const uint8_t QUEUE_DIM = 16;
-
- #else
-
- const uint8_t BUFFER_BLOCK_COUNT = 28;
-
- const uint8_t QUEUE_DIM = 32;
- #endif
-
-
-
-
- #define TMP_FILE_NAME "TMP_LOG.BIN"
-
-
- const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
-
-
- const uint8_t PIN_COUNT = sizeof(PIN_LIST)/sizeof(PIN_LIST[0]);
-
-
- const uint16_t MIN_ADC_CYCLES = 15;
-
-
- const uint16_t ISR_SETUP_ADC = 100;
-
-
- const uint16_t ISR_TIMER0 = 160;
-
- SdFat sd;
-
- SdBaseFile binFile;
-
- char binName[13] = FILE_BASE_NAME "00.BIN";
-
- #if RECORD_EIGHT_BITS
- const size_t SAMPLES_PER_BLOCK = DATA_DIM8/PIN_COUNT;
- typedef block8_t block_t;
- #else
- const size_t SAMPLES_PER_BLOCK = DATA_DIM16/PIN_COUNT;
- typedef block16_t block_t;
- #endif
-
- block_t* emptyQueue[QUEUE_DIM];
- uint8_t emptyHead;
- uint8_t emptyTail;
-
- block_t* fullQueue[QUEUE_DIM];
- volatile uint8_t fullHead;
- uint8_t fullTail;
-
-
- inline uint8_t queueNext(uint8_t ht) {return (ht + 1) & (QUEUE_DIM -1);}
-
-
-
-
- block_t* isrBuf;
-
-
- bool isrBufNeeded = true;
-
-
- uint16_t isrOver = 0;
-
-
- uint8_t adcmux[PIN_COUNT];
- uint8_t adcsra[PIN_COUNT];
- uint8_t adcsrb[PIN_COUNT];
- uint8_t adcindex = 1;
-
-
- volatile bool timerError = false;
- volatile bool timerFlag = false;
-
-
- ISR(ADC_vect) {
-
- #if RECORD_EIGHT_BITS
- uint8_t d = ADCH;
- #else
-
- uint16_t d = ADC;
- #endif
-
- if (isrBufNeeded && emptyHead == emptyTail) {
-
- if (isrOver < 0XFFFF) isrOver++;
-
-
- timerFlag = false;
- return;
- }
-
- if (PIN_COUNT > 1) {
- ADMUX = adcmux[adcindex];
- ADCSRB = adcsrb[adcindex];
- ADCSRA = adcsra[adcindex];
- if (adcindex == 0) timerFlag = false;
- adcindex = adcindex < (PIN_COUNT - 1) ? adcindex + 1 : 0;
- } else {
- timerFlag = false;
- }
-
- if (isrBufNeeded) {
-
- isrBuf = emptyQueue[emptyTail];
- emptyTail = queueNext(emptyTail);
- isrBuf->count = 0;
- isrBuf->overrun = isrOver;
- isrBufNeeded = false;
- }
-
- isrBuf->data[isrBuf->count++] = d;
-
-
- if (isrBuf->count >= PIN_COUNT*SAMPLES_PER_BLOCK) {
-
- uint8_t tmp = fullHead;
- fullQueue[tmp] = (block_t*)isrBuf;
- fullHead = queueNext(tmp);
-
-
- isrBufNeeded = true;
- isrOver = 0;
- }
- }
-
-
- ISR(TIMER1_COMPB_vect) {
-
- if (timerFlag) timerError = true;
- timerFlag = true;
- }
-
-
- #define error(msg) error_P(PSTR(msg))
-
- void error_P(const char* msg) {
- sd.errorPrint_P(msg);
- fatalBlink();
- }
-
-
- void fatalBlink() {
- while (true) {
- if (ERROR_LED_PIN >= 0) {
- digitalWrite(ERROR_LED_PIN, HIGH);
- delay(200);
- digitalWrite(ERROR_LED_PIN, LOW);
- delay(200);
- }
- }
- }
-
- #if ADPS0 != 0 || ADPS1 != 1 || ADPS2 != 2
- #error unexpected ADC prescaler bits
- #endif
-
-
- void adcInit(metadata_t* meta) {
- uint8_t adps;
- uint32_t ticks = F_CPU*SAMPLE_INTERVAL + 0.5;
-
- if (ADC_REF & ~((1 << REFS0) | (1 << REFS1))) {
- error("Invalid ADC reference");
- }
- #ifdef ADC_PRESCALER
- if (ADC_PRESCALER > 7 || ADC_PRESCALER < 2) {
- error("Invalid ADC prescaler");
- }
- adps = ADC_PRESCALER;
- #else
-
- int32_t adcCycles = (ticks - ISR_TIMER0)/PIN_COUNT;
- - (PIN_COUNT > 1 ? ISR_SETUP_ADC : 0);
-
- for (adps = 7; adps > 0; adps--) {
- if (adcCycles >= (MIN_ADC_CYCLES << adps)) break;
- }
- #endif
- meta->adcFrequency = F_CPU >> adps;
- if (meta->adcFrequency > (RECORD_EIGHT_BITS ? 2000000 : 1000000)) {
- error("Sample Rate Too High");
- }
- #if ROUND_SAMPLE_INTERVAL
-
- ticks += 1 << (adps - 1);
- ticks >>= adps;
- ticks <<= adps;
- #endif
-
- if (PIN_COUNT > sizeof(meta->pinNumber)/sizeof(meta->pinNumber[0])) {
- error("Too many pins");
- }
- meta->pinCount = PIN_COUNT;
- meta->recordEightBits = RECORD_EIGHT_BITS;
-
- for (int i = 0; i < PIN_COUNT; i++) {
- uint8_t pin = PIN_LIST[i];
- if (pin >= NUM_ANALOG_INPUTS) error("Invalid Analog pin number");
- meta->pinNumber[i] = pin;
-
-
- adcmux[i] = (pin & 7) | ADC_REF;
- if (RECORD_EIGHT_BITS) adcmux[i] |= 1 << ADLAR;
-
-
- adcsrb[i] = i == 0 ? (1 << ADTS2) | (1 << ADTS0) : 0;
- #ifdef MUX5
- if (pin > 7) adcsrb[i] |= (1 << MUX5);
- #endif
- adcsra[i] = (1 << ADEN) | (1 << ADIE) | adps;
- adcsra[i] |= i == 0 ? 1 << ADATE : 1 << ADSC;
- }
-
-
- TCCR1A = 0;
- uint8_t tshift;
- if (ticks < 0X10000) {
-
- TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10);
- tshift = 0;
- } else if (ticks < 0X10000*8) {
-
- TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
- tshift = 3;
- } else if (ticks < 0X10000*64) {
-
- TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11) | (1 << CS10);
- tshift = 6;
- } else if (ticks < 0X10000*256) {
-
- TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS12);
- tshift = 8;
- } else if (ticks < 0X10000*1024) {
-
- TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS12) | (1 << CS10);
- tshift = 10;
- } else {
- error("Sample Rate Too Slow");
- }
-
- ticks >>= tshift;
-
- ICR1 = ticks - 1;
-
- OCR1B = 0;
-
-
- ticks <<= tshift;
-
-
- meta->sampleInterval = ticks;
- meta->cpuFrequency = F_CPU;
- float sampleRate = (float)meta->cpuFrequency/meta->sampleInterval;
- Serial.print(F("Sample pins:"));
- for (int i = 0; i < meta->pinCount; i++) {
- Serial.print(' ');
- Serial.print(meta->pinNumber[i], DEC);
- }
- Serial.println();
- Serial.print(F("ADC bits: "));
- Serial.println(meta->recordEightBits ? 8 : 10);
- Serial.print(F("ADC clock kHz: "));
- Serial.println(meta->adcFrequency/1000);
- Serial.print(F("Sample Rate: "));
- Serial.println(sampleRate);
- Serial.print(F("Sample interval usec: "));
- Serial.println(1000000.0/sampleRate, 4);
- }
-
-
- void adcStart() {
-
- isrBufNeeded = true;
- isrOver = 0;
- adcindex = 1;
-
-
- ADCSRA |= 1 << ADIF;
-
-
- ADMUX = adcmux[0];
- ADCSRB = adcsrb[0];
- ADCSRA = adcsra[0];
-
-
- timerError = false;
- timerFlag = false;
- TCNT1 = 0;
- TIFR1 = 1 << OCF1B;
- TIMSK1 = 1 << OCIE1B;
- }
-
- void adcStop() {
- TIMSK1 = 0;
- ADCSRA = 0;
- }
-
-
- void binaryToCsv() {
- uint8_t lastPct = 0;
- block_t buf;
- metadata_t* pm;
- uint32_t t0 = millis();
- char csvName[13];
- StdioStream csvStream;
-
- if (!binFile.isOpen()) {
- Serial.println(F("No current binary file"));
- return;
- }
- binFile.rewind();
- if (!binFile.read(&buf , 512) == 512) error("Read metadata failed");
-
- strcpy(csvName, binName);
- strcpy_P(&csvName[BASE_NAME_SIZE + 3], PSTR("CSV"));
-
- if (!csvStream.fopen(csvName, "w")) {
- error("open csvStream failed");
- }
- Serial.println();
- Serial.print(F("Writing: "));
- Serial.print(csvName);
- Serial.println(F(" - type any character to stop"));
- pm = (metadata_t*)&buf;
- csvStream.print(F("Interval,"));
- float intervalMicros = 1.0e6*pm->sampleInterval/(float)pm->cpuFrequency;
- csvStream.print(intervalMicros, 4);
- csvStream.println(F(",usec"));
- for (uint8_t i = 0; i < pm->pinCount; i++) {
- if (i) csvStream.putc(',');
- csvStream.print(F("pin"));
- csvStream.print(pm->pinNumber[i]);
- }
- csvStream.println();
- uint32_t tPct = millis();
- while (!Serial.available() && binFile.read(&buf, 512) == 512) {
- uint16_t i;
- if (buf.count == 0) break;
- if (buf.overrun) {
- csvStream.print(F("OVERRUN,"));
- csvStream.println(buf.overrun);
- }
- for (uint16_t j = 0; j < buf.count; j += PIN_COUNT) {
- for (uint16_t i = 0; i < PIN_COUNT; i++) {
- if (i) csvStream.putc(',');
- csvStream.print(buf.data[i + j]);
- }
- csvStream.println();
- }
- if ((millis() - tPct) > 1000) {
- uint8_t pct = binFile.curPosition()/(binFile.fileSize()/100);
- if (pct != lastPct) {
- tPct = millis();
- lastPct = pct;
- Serial.print(pct, DEC);
- Serial.println('%');
- }
- }
- if (Serial.available()) break;
- }
- csvStream.fclose();
- Serial.print(F("Done: "));
- Serial.print(0.001*(millis() - t0));
- Serial.println(F(" Seconds"));
- }
-
-
- void checkOverrun() {
- bool headerPrinted = false;
- block_t buf;
- uint32_t bgnBlock, endBlock;
- uint32_t bn = 0;
-
- if (!binFile.isOpen()) {
- Serial.println(F("No current binary file"));
- return;
- }
- if (!binFile.contiguousRange(&bgnBlock, &endBlock)) {
- error("contiguousRange failed");
- }
- binFile.rewind();
- Serial.println();
- Serial.println(F("Checking overrun errors - type any character to stop"));
- if (!binFile.read(&buf , 512) == 512) {
- error("Read metadata failed");
- }
- bn++;
- while (binFile.read(&buf, 512) == 512) {
- if (buf.count == 0) break;
- if (buf.overrun) {
- if (!headerPrinted) {
- Serial.println();
- Serial.println(F("Overruns:"));
- Serial.println(F("fileBlockNumber,sdBlockNumber,overrunCount"));
- headerPrinted = true;
- }
- Serial.print(bn);
- Serial.print(',');
- Serial.print(bgnBlock + bn);
- Serial.print(',');
- Serial.println(buf.overrun);
- }
- bn++;
- }
- if (!headerPrinted) {
- Serial.println(F("No errors found"));
- } else {
- Serial.println(F("Done"));
- }
- }
-
-
- void dumpData() {
- block_t buf;
- if (!binFile.isOpen()) {
- Serial.println(F("No current binary file"));
- return;
- }
- binFile.rewind();
- if (binFile.read(&buf , 512) != 512) {
- error("Read metadata failed");
- }
- Serial.println();
- Serial.println(F("Type any character to stop"));
- delay(1000);
- while (!Serial.available() && binFile.read(&buf , 512) == 512) {
- if (buf.count == 0) break;
- if (buf.overrun) {
- Serial.print(F("OVERRUN,"));
- Serial.println(buf.overrun);
- }
- for (uint16_t i = 0; i < buf.count; i++) {
- Serial.print(buf.data[i], DEC);
- if ((i+1)%PIN_COUNT) {
- Serial.print(',');
- } else {
- Serial.println();
- }
- }
- }
- Serial.println(F("Done"));
- }
-
-
-
- uint32_t const ERASE_SIZE = 262144L;
- void logData() {
- uint32_t bgnBlock, endBlock;
-
-
- block_t block[BUFFER_BLOCK_COUNT];
-
- Serial.println();
-
-
- adcInit((metadata_t*) &block[0]);
-
-
- if (BASE_NAME_SIZE > 6) {
- error("FILE_BASE_NAME too long");
- }
- while (sd.exists(binName)) {
- if (binName[BASE_NAME_SIZE + 1] != '9') {
- binName[BASE_NAME_SIZE + 1]++;
- } else {
- binName[BASE_NAME_SIZE + 1] = '0';
- if (binName[BASE_NAME_SIZE] == '9') {
- error("Can't create file name");
- }
- binName[BASE_NAME_SIZE]++;
- }
- }
-
- if (sd.exists(TMP_FILE_NAME)) {
- Serial.println(F("Deleting tmp file"));
- if (!sd.remove(TMP_FILE_NAME)) {
- error("Can't remove tmp file");
- }
- }
-
- Serial.println(F("Creating new file"));
- binFile.close();
- if (!binFile.createContiguous(sd.vwd(),
- TMP_FILE_NAME, 512 * FILE_BLOCK_COUNT)) {
- error("createContiguous failed");
- }
-
- if (!binFile.contiguousRange(&bgnBlock, &endBlock)) {
- error("contiguousRange failed");
- }
-
- uint8_t* cache = (uint8_t*)sd.vol()->cacheClear();
- if (cache == 0) error("cacheClear failed");
-
-
- Serial.println(F("Erasing all data"));
- uint32_t bgnErase = bgnBlock;
- uint32_t endErase;
- while (bgnErase < endBlock) {
- endErase = bgnErase + ERASE_SIZE;
- if (endErase > endBlock) endErase = endBlock;
- if (!sd.card()->erase(bgnErase, endErase)) {
- error("erase failed");
- }
- bgnErase = endErase + 1;
- }
-
- if (!sd.card()->writeStart(bgnBlock, FILE_BLOCK_COUNT)) {
- error("writeBegin failed");
- }
-
- if (!sd.card()->writeData((uint8_t*)&block[0])) {
- error("Write metadata failed");
- }
-
- emptyHead = emptyTail = 0;
- fullHead = fullTail = 0;
-
-
- emptyQueue[emptyHead] = (block_t*)cache;
- emptyHead = queueNext(emptyHead);
-
-
- for (uint8_t i = 0; i < BUFFER_BLOCK_COUNT; i++) {
- emptyQueue[emptyHead] = &block[i];
- emptyHead = queueNext(emptyHead);
- }
-
- delay(1000);
- Serial.println(F("Logging - type any character to stop"));
-
- Serial.flush();
- delay(10);
- uint32_t bn = 1;
- uint32_t t0 = millis();
- uint32_t t1 = t0;
- uint32_t overruns = 0;
- uint32_t count = 0;
- uint32_t maxLatency = 0;
-
-
- adcStart();
- while (1) {
- if (fullHead != fullTail) {
-
- block_t* pBlock = fullQueue[fullTail];
-
-
- uint32_t usec = micros();
- if (!sd.card()->writeData((uint8_t*)pBlock)) {
- error("write data failed");
- }
- usec = micros() - usec;
- t1 = millis();
- if (usec > maxLatency) maxLatency = usec;
- count += pBlock->count;
-
-
- if (pBlock->overrun) {
- overruns += pBlock->overrun;
- if (ERROR_LED_PIN >= 0) {
- digitalWrite(ERROR_LED_PIN, HIGH);
- }
- }
-
- emptyQueue[emptyHead] = pBlock;
- emptyHead = queueNext(emptyHead);
- fullTail = queueNext(fullTail);
- bn++;
- if (bn == FILE_BLOCK_COUNT) {
-
- adcStop();
- break;
- }
- }
- if (timerError) {
- error("Missed timer event - rate too high");
- }
- if (Serial.available()) {
-
- adcStop();
- if (isrBuf != 0 && isrBuf->count >= PIN_COUNT) {
-
- isrBuf->count = PIN_COUNT*(isrBuf->count/PIN_COUNT);
-
- fullQueue[fullHead] = isrBuf;
- fullHead = queueNext(fullHead);
- isrBuf = 0;
- }
- if (fullHead == fullTail) break;
- }
- }
- if (!sd.card()->writeStop()) {
- error("writeStop failed");
- }
-
- if (bn != FILE_BLOCK_COUNT) {
- Serial.println(F("Truncating file"));
- if (!binFile.truncate(512L * bn)) {
- error("Can't truncate file");
- }
- }
- if (!binFile.rename(sd.vwd(), binName)) {
- error("Can't rename file");
- }
- Serial.print(F("File renamed: "));
- Serial.println(binName);
- Serial.print(F("Max block write usec: "));
- Serial.println(maxLatency);
- Serial.print(F("Record time sec: "));
- Serial.println(0.001*(t1 - t0), 3);
- Serial.print(F("Sample count: "));
- Serial.println(count/PIN_COUNT);
- Serial.print(F("Samples/sec: "));
- Serial.println((1000.0/PIN_COUNT)*count/(t1-t0));
- Serial.print(F("Overruns: "));
- Serial.println(overruns);
- Serial.println(F("Done"));
- }
-
- void setup(void) {
- if (ERROR_LED_PIN >= 0) {
- pinMode(ERROR_LED_PIN, OUTPUT);
- }
- Serial.begin(9600);
-
-
- analogRead(PIN_LIST[0]);
-
- Serial.print(F("FreeRam: "));
- Serial.println(FreeRam());
-
-
- if (!sd.begin(SD_CS_PIN, SPI_FULL_SPEED)) {
- sd.initErrorPrint();
- fatalBlink();
- }
- }
-
- void loop(void) {
-
- while (Serial.read() >= 0) {}
- Serial.println();
- Serial.println(F("type:"));
- Serial.println(F("c - convert file to CSV"));
- Serial.println(F("d - dump data to Serial"));
- Serial.println(F("e - overrun error details"));
- Serial.println(F("r - record ADC data"));
-
- while(!Serial.available()) {}
- char c = tolower(Serial.read());
- if (ERROR_LED_PIN >= 0) {
- digitalWrite(ERROR_LED_PIN, LOW);
- }
-
- do {
- delay(10);
- } while (Serial.read() >= 0);
-
- if (c == 'c') {
- binaryToCsv();
- } else if (c == 'd') {
- dumpData();
- } else if (c == 'e') {
- checkOverrun();
- } else if (c == 'r') {
- logData();
- } else {
- Serial.println(F("Invalid entry"));
- }
- }
|