aboutsummaryrefslogtreecommitdiffstats
path: root/examples/test-fsmc.cpp
blob: d1c8567b0ecf9f776997c9ba064d68e09b8743a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stddef.h>

#include "wirish.h"
#include "fsmc.h"

#define LED       BOARD_LED_PIN

// Start of FSMC SRAM bank 1
static uint16 *const ptr_start = (uint16*)0x60000000;
// End of Maple Native SRAM chip address space (512K 16-bit words)
static uint16 *const ptr_end = (uint16*)0x60080000;
// For snprintf
static char buf[100];

void setup() {
    pinMode(LED, OUTPUT);
    digitalWrite(LED, HIGH);

    Serial1.begin(115200);
    Serial1.println("Hello World!");

    Serial1.print("Initializing RAM chip... ");
    fsmc_native_sram_init();
    Serial1.println("Done.");
}

void test_single_write() {
    uint16 *ptr = ptr_start;
    uint16 tmp;

    Serial1.print("Writing 0x1234... ");
    *ptr = 0x1234;
    Serial1.println("Done.");

    Serial1.print("Reading... ");
    tmp = *ptr;
    Serial1.print("Done: 0x");
    Serial1.println(tmp, HEX);

    if (tmp != 0x1234) {
        Serial1.println("Mismatch, abort.");
        ASSERT(0);
    }
}

void test_all_addresses() {
    uint32 start, end;
    uint16 count = 0;
    uint16 *ptr;
    Serial1.println("Now writing all memory addresses (unrolled loop)");
    SerialUSB.end();
    start = micros();
    for (ptr = ptr_start; ptr < ptr_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.begin();
    Serial1.print("Done. Elapsed time (us): ");
    Serial1.println(end - start);

    Serial1.println("Validating writes.");
    for (ptr = ptr_start, count = 0; ptr < ptr_end; ptr++, count++) {
        if (*ptr != count) {
            snprintf(buf, sizeof buf, "mismatch: %p = 0x%xu, should be 0x%xu.",
                     ptr, *ptr, count);
            Serial1.println(buf);
            ASSERT(0);
        }
    }
    ptrdiff_t nwrites = ptr_end - ptr_start;
    double us_per_write = double(end-start) / double(nwrites);
    Serial1.println("Done; all writes seem valid.");
    snprintf(buf, sizeof buf,
             "Number of writes = %d; avg. time per write = %g us (%g MHz)",
             nwrites, us_per_write, 1 / us_per_write);
    Serial1.println(buf);
}

__attribute__((constructor)) void premain() {
    init();
}

int main(void) {
    setup();

    test_single_write();
    test_all_addresses();

    Serial1.println("Tests pass, finished.");

    while (true)
        ;

    return 0;
}