|
| 1 | +#include "Storage.h" |
| 2 | +#include "GPIO.h" |
| 3 | +#include "SPI.h" |
| 4 | +#include "Driver/MC23LC1024.h" |
| 5 | + |
| 6 | +// Configure usage of software or hardware spi bus manager |
| 7 | +// #define USE_SOFTWARE_SPI |
| 8 | +#define USE_HARDWARE_SPI |
| 9 | + |
| 10 | +#if defined(USE_SOFTWARE_SPI) |
| 11 | +#include "Software/SPI.h" |
| 12 | +Software::SPI<BOARD::D11, BOARD::D12, BOARD::D13> spi; |
| 13 | +#elif defined(USE_HARDWARE_SPI) |
| 14 | +#include "Hardware/SPI.h" |
| 15 | +Hardware::SPI spi; |
| 16 | +#endif |
| 17 | + |
| 18 | +// External memory storage (128 Kbyte) |
| 19 | +MC23LC1024<BOARD::D10> sram(spi); |
| 20 | + |
| 21 | +// Sample with timestamp and value (analog) |
| 22 | +struct sample_t { |
| 23 | + uint32_t timestamp; |
| 24 | + uint16_t value; |
| 25 | +}; |
| 26 | + |
| 27 | +// Sample element, number of elements and external storage (60 Kbyte) |
| 28 | +// Single sample in memory and 10000 vector members on storage |
| 29 | +sample_t sample; |
| 30 | +const size_t NMEMB = 10000; |
| 31 | +Storage::Block vector(sram, &sample, sizeof(sample), NMEMB); |
| 32 | + |
| 33 | +void setup() |
| 34 | +{ |
| 35 | + Serial.begin(57600); |
| 36 | + while (!Serial); |
| 37 | + |
| 38 | + // Print size of storage, vector, sample and number of members |
| 39 | + Serial.print(F("sram.SIZE = ")); |
| 40 | + Serial.println(sram.SIZE); |
| 41 | + Serial.print(F("vector.size = ")); |
| 42 | + Serial.println(vector.size()); |
| 43 | + Serial.print(F("vector.SIZE = ")); |
| 44 | + Serial.println(vector.SIZE); |
| 45 | + Serial.print(F("vector.NMEMB = ")); |
| 46 | + Serial.println(vector.NMEMB); |
| 47 | +} |
| 48 | + |
| 49 | +void loop() |
| 50 | +{ |
| 51 | + // Sample analog values, timestamp and write to storage |
| 52 | + for (size_t i = 0; i < vector.NMEMB; i++) { |
| 53 | + sample.timestamp = micros(); |
| 54 | + sample.value = analogRead(A0); |
| 55 | + vector.write(i); |
| 56 | + } |
| 57 | + |
| 58 | + // Read back samples and calculate min, max and sum |
| 59 | + uint16_t min = UINT16_MAX; |
| 60 | + uint16_t max = 0; |
| 61 | + uint64_t sum = 0; |
| 62 | + for (size_t i = 0; i < vector.NMEMB; i++) { |
| 63 | + vector.read(i); |
| 64 | + if (sample.value < min) min = sample.value; |
| 65 | + if (sample.value > max) max = sample.value; |
| 66 | + sum += sample.value; |
| 67 | + } |
| 68 | + |
| 69 | + // Read back first and last sample timestamp to calculate |
| 70 | + // average microseconds per sample and write to storage |
| 71 | + uint32_t us = 0; |
| 72 | + vector.read(vector.NMEMB - 1); |
| 73 | + us = sample.timestamp; |
| 74 | + vector.read(0); |
| 75 | + us -= sample.timestamp; |
| 76 | + us /= vector.NMEMB; |
| 77 | + |
| 78 | + // Print the results; samples per second, min, max and average values |
| 79 | + Serial.println(); |
| 80 | + Serial.print(F("sps = ")); |
| 81 | + Serial.println(1000000.0 / us); |
| 82 | + Serial.print(F("min = ")); |
| 83 | + Serial.println(min); |
| 84 | + Serial.print(F("max = ")); |
| 85 | + Serial.println(max); |
| 86 | + Serial.print(F("sum = ")); |
| 87 | + Serial.println((float) sum); |
| 88 | + Serial.print(F("avg = ")); |
| 89 | + Serial.println((float) sum / vector.NMEMB); |
| 90 | + |
| 91 | + delay(5000); |
| 92 | +} |
0 commit comments