aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test-fsmc.cpp
diff options
context:
space:
mode:
authorbryan newbold <bnewbold@robocracy.org>2014-10-16 20:20:20 -0700
committerbryan newbold <bnewbold@robocracy.org>2014-10-16 20:20:20 -0700
commitdba5a9fe68ebb996eeee69fa9b573fe5a1561ce2 (patch)
tree6d9c249686b81b2b70da9b9a3dd1e246c221b4b3 /tests/test-fsmc.cpp
parent160d861ba3fe50c30891d1abcb2c520be84aaa85 (diff)
downloadlibrambutan-dba5a9fe68ebb996eeee69fa9b573fe5a1561ce2.tar.gz
librambutan-dba5a9fe68ebb996eeee69fa9b573fe5a1561ce2.zip
refactor: move test-style examples to ./tests
Diffstat (limited to 'tests/test-fsmc.cpp')
-rw-r--r--tests/test-fsmc.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/tests/test-fsmc.cpp b/tests/test-fsmc.cpp
new file mode 100644
index 0000000..1621317
--- /dev/null
+++ b/tests/test-fsmc.cpp
@@ -0,0 +1,122 @@
+#include <stddef.h> // for ptrdiff_t
+
+#include <wirish/wirish.h>
+#include <libmaple/fsmc.h>
+
+#ifndef BOARD_maple_native
+#error "Sorry, this example only works on Maple Native."
+#endif
+
+// Start of FSMC SRAM bank 1
+static uint16 *const sram_start = (uint16*)0x60000000;
+// End of Maple Native SRAM chip address space (512K 16-bit words)
+static uint16 *const sram_end = (uint16*)0x60100000;
+
+void test_single_write(void);
+void test_all_addresses(void);
+
+void setup() {
+ pinMode(BOARD_LED_PIN, OUTPUT);
+ digitalWrite(BOARD_LED_PIN, HIGH);
+
+ SerialUSB.read();
+ SerialUSB.println("*** Beginning RAM chip test");
+
+ test_single_write();
+ test_all_addresses();
+
+ SerialUSB.println("Tests pass, finished.");
+ SerialUSB.println("***\n");
+}
+
+void loop() {
+}
+
+void test_single_write() {
+ uint16 *ptr = sram_start;
+ uint16 tmp;
+
+ SerialUSB.print("Writing 0x1234... ");
+ *ptr = 0x1234;
+ SerialUSB.println("Done.");
+
+ SerialUSB.print("Reading... ");
+ tmp = *ptr;
+ SerialUSB.print("Done: 0x");
+ SerialUSB.println(tmp, HEX);
+
+ if (tmp != 0x1234) {
+ SerialUSB.println("Mismatch; abort.");
+ ASSERT(0);
+ }
+}
+
+void test_all_addresses() {
+ uint32 start, end;
+ uint16 count = 0;
+ uint16 *ptr;
+
+ SerialUSB.println("Now writing all memory addresses (unrolled loop)");
+ start = micros();
+ for (ptr = sram_start; ptr < sram_end;) {
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ *ptr++ = count++;
+ }
+ end = micros();
+ SerialUSB.print("Done. Elapsed time (us): ");
+ SerialUSB.println(end - start);
+
+ SerialUSB.println("Validating writes.");
+ for (ptr = sram_start, count = 0; ptr < sram_end; ptr++, count++) {
+ uint16 value = *ptr;
+ if (value != count) {
+ SerialUSB.print("mismatch: 0x");
+ SerialUSB.print((uint32)ptr);
+ SerialUSB.print(" = 0x");
+ SerialUSB.print(value, HEX);
+ SerialUSB.print(", should be 0x");
+ SerialUSB.print(count, HEX);
+ SerialUSB.println(".");
+ ASSERT(0);
+ }
+ }
+ SerialUSB.println("Done; all writes seem valid.");
+
+ ptrdiff_t nwrites = sram_end - sram_start;
+ double us_per_write = double(end-start) / double(nwrites);
+ SerialUSB.print("Number of writes = ");
+ SerialUSB.print(nwrites);
+ SerialUSB.print("; avg. time per write = ");
+ SerialUSB.print(us_per_write);
+ SerialUSB.print(" us (");
+ SerialUSB.print(1 / us_per_write);
+ SerialUSB.println(" MHz)");
+}
+
+__attribute__((constructor)) void premain() {
+ init();
+}
+
+int main(void) {
+ setup();
+
+ while (true) {
+ loop();
+ }
+
+ return 0;
+}